$.query = function(s){ var q = s.substring(s.indexOf('=') + 1); return q };

var dURL= $.query(location.search);
var minAge = 17;
var xDate= 7;
var ageGate,ageCheck,y,m,d,bday,curAge;

$(function(){
  $('a').focus(function(){ this.blur() });
  $('#flags').show();
  $('#flags img').css({cursor:'pointer'}).bind('click', function(){ 
    if($(this).is('.de') && dURL == ""){ dURL = "http://community-de.ageofconan.com" } 
    else if($(this).is('.fr') && dURL == ""){ dURL = "http://community-fr.ageofconan.com" }
    else if($(this).is('.es') && dURL == ""){ dURL = "http://community-es.ageofconan.com" }
    else if($(this).is('.ru')){ dURL = "http://ageofconan.1c.ru/community/" }
    else if($(this).is('.pl')){ dURL = "http://www.aoc2009.pl/community/" }
    else if(dURL == ''){ dURL == "http://community.ageofconan.com" }
    if($(this).is('.ageCheck')){ ageCheck = true } else{ ageCheck = false } $('#flags').fadeOut('normal', $.checkCookie)
  });
  $('#form').hide();
});

$.checkCookie = function(){
  bday = $.cookie('bday');
  if(bday){ curAge = $.getAge(bday); curAge >= minAge ? $.redirect(true) : $.underAge() }
  else{ ageCheck ? $('#form').fadeIn('normal') : $.redirect(false) }
};

$.checkAge = function(){
  y = document.ageForm.birthyear[document.ageForm.birthyear.selectedIndex].value;
  m = document.ageForm.birthmonth[document.ageForm.birthmonth.selectedIndex].value;
  d = document.ageForm.birthday[document.ageForm.birthday.selectedIndex].value;
  bday = y+m+d;
  if(bday.length < 8){ return }
  $.cookie('bday',bday);
  $.checkCookie();
};

$.getAge = function(dateString){
  var now = new Date();
  var today = new Date(now.getFullYear(),now.getMonth(),now.getDate());
  var yearNow = now.getFullYear();
  var monthNow = now.getMonth();
  var dateNow = now.getDate();
  var dob = new Date(dateString.substring(0,4), dateString.substring(4,6)-1, dateString.substring(6,8));
  var yearDob = dob.getFullYear();
  var monthDob = dob.getMonth();
  var dateDob = dob.getDate();
  yearAge = yearNow - yearDob;
  if(monthNow >= monthDob){ var monthAge = monthNow - monthDob }
  else{ yearAge--; var monthAge = 12 + monthNow -monthDob }
  if(dateNow >= dateDob){ var dateAge = dateNow - dateDob }
  else{
    monthAge--;
    var dateAge = 31 + dateNow - dateDob;
    if(monthAge < 0){
      monthAge = 11;
      yearAge--; 
    }
  }
  return yearAge;
};

$.underAge = function(){
  $('#form').css('display') == 'none' ? $('#underage').fadeIn('normal') : $('#form').fadeOut('normal', function(){ $('#underage').fadeIn('normal') });
};

$.redirect = function(esrb){
  $.cookie('ageGate', 'false', {expires: 365, path: '/', domain: 'ageofconan.com'});
  esrb ? $.cookie('esrb', 'true', {expires: 365, path: '/', domain: 'ageofconan.com'}) : $.cookie('esrb', 'false', {expires: 365, path: '/', domain: 'ageofconan.com'});
  $('#form').css('display') == 'none' ? window.location.href=dURL : $('#form').fadeOut('normal', function(){ window.location.href=dURL });
};

