function teaser() 
{
	if($("#lstCountry").val()=="Spain"){	
		window.location.href="http://www.tanqueray.es?g=1"
		return false;	
	}
	
	var refresh;
	refresh = window.location.href;
	//window.location.href=refresh;
	window.location.reload();
}

function content() {
	$("#loading").fadeOut();
	$("#content").fadeIn(3000);
}

function setCookies(){
	var ua = navigator.userAgent.toLowerCase();
	var isAndroid = ua.indexOf("android")>-1;
	if(isAndroid){
			var refreshURL = window.location.href;
			var NAME = "passGateway";
			var OPTIONS = { path: gate.url, expires:null, path:'/'};
			var cookieContent = 'yes';
			$.ajax({
				url: "savecookie.php",
				method:"POST",
				//async:false,
				data:"refreshURL="+refreshURL,
				context:document.body,
				success:function(){
					//alert("success");
					teaser();
				}
			});
	//	  window.location = 'http://android.davidwalsh.name';
	}else{
			var NAME = "passGateway";
			var OPTIONS = {path:gate.url,expires:null,path:'/'};
			var cookieContent = 'yes';
			
			$.cookie(NAME,cookieContent,OPTIONS);
			
			var NAME = "language";
			var OPTIONS = {path:gate.url,expires:null,path:'/'};
			var cookieContent = gate.globals.language;
			
			$.cookie(NAME, cookieContent, OPTIONS);	
			dur = 99;
			if ($('#remember').is(':checked')) {	
			NAME =  "rememberGateway";
			cookieContent = 'yes';
				$.cookie(NAME, cookieContent, OPTIONS);
			}
			
			teaser();	
	}
}

//Main namespace
var gate = {
	utils: {},
	globals: {
		'month': 1,
		'date': 1,
		'year': 2001,
		'redirectURL':''
	},
	allowed: false,
	url: 'teaser/'
};

gate.utils.renderFonts = function() {
	$.each($('.din'), function(i, obj){
		$(obj).html($(obj).html().toUpperCase());
	});
	Cufon.replace($('.tanqueray'), {fontFamily: 'Tanqueray'});
	Cufon.replace($('.din'), {fontFamily: 'DIN 1451 Std'});
}


gate.loadForms = function() {
	
	$.each(countries, function(i, obj){
		//el if controla si i es 1 ha puesto united states y united kingdon y pone una rayita blanca sino es un país normal
		if(i===1){
			var div = $('<option class="option" value="'+obj[0]+'" >'+obj[0]+'</option><option class="hr-option" disabled="disabled" style="height:10px;"  >------------------------------------------</option>');	
		}else{
				var div = $('<option class="option" value="'+obj[0]+'">'+obj[0]+'</option>');				
			}
		$('#lstCountry').append(div);						
	});
	
	if(gate.globals.restyled == true) {		
	}else
  {
		$('#lstCountry').change(function(){
			var country = $(this).val();					
			gate.checkCountry(country);
			
		});
	}
	
	$.each(months, function(i, obj){
		var div = $('<option class="option" value="'+(i+1)+'">'+obj[0]+'</option>');
		$('#lstMonth').append(div);
	});
	
	for(var i=0; i<31; i++) {
		var div = $('<option class="option" value="'+(i+1)+'">'+(i+1)+'</option>');
		$('#lstDay').append(div);
	}
	
	var date =  new Date();
	var year = date.getFullYear();
	for(var i=year; i>(year-100); i--) {
		var div = $('<option class="option" value="'+(i)+'">'+(i)+'</option>');
		$('#lstYear').append(div);
	}
	
}

gate.checkCountry = function(country){
	//console.log('country' +country);
	$.each(countries, function(i, obj){
				
		if(obj[0] == country){
			if(obj[1] == 0){
				gate.globals.redirectURL = obj[3];
				gate.deny();							
			}
      else
      {
				gate.globals.minAge = obj[1];
				gate.globals.redirectURL = obj[3];
				gate.globals.language = obj[4];
				//console.log('redirect URL ' +gate.globals.redirectURL);
				gate.loadAge();										
			}
		}
	});
}

gate.loadAge = function() {
	$('#countryForm').fadeOut(500, function(){
		$('#ageForm').fadeIn(500);
	});
	if(gate.globals.restyled == true){
		
	}else{		
		$('#lstYear').bind('change', function(e){
			gate.checkAge();
		});
	}
}

gate.checkAge = function(){		
	uDate = new Date($('#lstYear').val(), $('#lstMonth').val(), $('#lstDay').val());
	
	var curDate = new Date();
	var curYear = curDate.getFullYear();
	var legalDate = new Date();
	legalDate.setFullYear(curYear - gate.globals.minAge);
	if(uDate < legalDate){
		gate.allow();
	}else{
		gate.deny();
	}
}


gate.allow = function() {

	gate.allowed = true;
	
}

gate.deny = function() {

	gate.allowed = false;
	
}

/**
 * Checks for the existence of the global console object and the log method.
 * Should be used instead of console.log so that IE doesn't throw errors when
 * it doesn't find console.
 * @param {Object} message - Message to be sent to console.log
 */
gate.utils.log = function(message){
	if( console && console.log)console.log(message);
	else return false;
}

function init() {

	gate.loadForms();

	gate.utils.renderFonts();

}

function parseString(str,v){
	if(str.indexOf("?")>-1){str = str.split("?")[1]}
	
	for(i=0;i<str.toString().split("&").length;i++){
		if(str.split("&")[i].split("=")[0] == v){
			var val = str.split("&")[i].split("=")[1];
			break;
		}
	}
	return val;
}

$('#checkAccess').bind('click', function(e){
		  
  if($("#lstCountry").val()=='')
  {
      alert("Please, select your country!");
      $("lstCountry").focus();
      return false;
  }
    
  if( $("#lstMonth").val() < 1 || $("#lstMonth").val() > 12 || isNaN( $("#lstMonth").val() ) )
  {
      alert('Please, select your date of birth!');
      $("#lstMonth").focus();
      return false;
  }
  
  if( $("#lstDay").val() < 1 || $("#lstDay").val() > 31 || isNaN( $("#lstDay").val() ) )
  {
      alert('Please, select your date of birth!');
      $("#lstDay").focus();
      return false;
  }    
      
  if( $("#lstYear").val() < 1900 || $("#lstYear").val() > 2012 || isNaN( $("#lstYear").val() ) )
  {
      alert('Please, select your date of birth !');
      $("#lstYear").focus();
      return false;
  }
	
	gate.checkCountry();
	gate.checkAge();
			
	if (gate.allowed) 
  { 			
	  setCookies(true); 
		//teaser();//lo comento
	} 
  else 
  { 
		if(gate.globals.redirectURL != undefined && gate.globals.redirectURL != 'undefined' && gate.globals.redirectURL != '')
    {	
			window.location = 'http://'+gate.globals.redirectURL;
			
		}else
    {				
			window.location = 'http://www.centurycouncil.org/landing';			
		}
	}
			
});


$(document).ready(function(){

	if(jQuery.browser.msie == true && jQuery.browser.version == "6.0"){
		$.each($(".png"), function(i, obj){
			$(obj).pngfix();
		});
		$.each($("label"), function(i, obj){
			$(obj).removeClass("tanqueray");
		});
	}
	
	$('.terms_dialog').live("click",function() {
		var urlPopup = $(this).attr("href");
		window.open(urlPopup,'legal','height=450,width=550,scrollbars=yes,status=no,toolbar=no,menubar=no,location=no');
		return false;
	});
	
	$('#accesibility-link').live("click",function() {
		var urlPopup = $(this).attr("href");
		window.open(urlPopup,'legal','height=450,width=550,scrollbars=yes,status=no,toolbar=no,menubar=no,location=no');
		return false;
	});
	
	
	
	$('.privacy_dialog').live("click",function() {
		var urlPopup = $(this).attr("href");
		window.open(urlPopup,'legal','height=450,width=550,scrollbars=yes,status=no,toolbar=no,menubar=no,location=no');
		return false;	
		
	});
	
	content();
	init();
			
});
