/* Age Gate JavaScript file */

$(document).ready(function(){
	$('#txtCountry').val('');
	$('#txtLang').val('');
	$('#txtProvince').val($('#province_options').val());
	$('#dobm').val('');
	$('#dobd').val('');
	$('#doby').val('');
	
	var listNodeStandard = $('#dob_select ul.standard li');
	var listYear = $('#age-list-years');
	var listDay = $('#age-list-days');
	
	
	
	$('#province_select select').bind('change', function() {
		
		setTimeout( function() {
			$('#txtProvince').val($('#province_options').val())
			submitOnComplete();	
		}, 100);
			
	});
	
	$('#dob_year').bind('change', function() {
		
		$('#doby').val($(this).attr('value'));
		
		submitOnComplete();
	});
	
	$('#dob_day').bind('change', function() {
		
		$('#dobd').val($(this).attr('value'));
		
		submitOnComplete();
	});
	
	$('#lang_select ul a').bind('click', function() {
		$('#lang_select ul a').removeClass('active');
		$(this).addClass('active');
		
		$('#txtLang').val($(this).attr('value'));
		$('#txtLocale').val($(this).attr('value'));
		
		$.cookie("locale", $('#txtLocale').attr('value'));
		submitOnComplete();
	});
	
	$('a.external').bind('click', function (e) {
		$(this).attr('target', '_blank');
	});
	
	// Submit Handler
	var submitOnComplete = function(){
		if($('#txtCountry').val()=='ca') {
			if($('#txtProvince').val()!='Choose...' && $('#txtCountry').val()!='' && $('#txtLang').val()!='' && $('#txtProvince').val()!='' && $('#dobm').val()!='' && $('#dobd').val()!='' && $('#doby').val()!=''){
				$('#age-current-year').unbind();
				$('#age-current-year').hide(function(){
					$('#loader').show();
					$('#submit').addClass('active');
				});
				$('#submit').addClass('active');
				//$('#submit').trigger('click');
			}
		} else {
			if($('#txtCountry').val()!='' && $('#dobm').val()!='' && $('#dobd').val()!='' && $('#doby').val()!=''){
				$('#age-current-year').unbind();
				$('#age-current-year').hide(function(){
					$('#loader').show();
					$('#submit').addClass('active');
				});
				$('#submit').addClass('active');
				//$('#submit').trigger('click');
			}
		}
	}

	// Age Selector
	$(listNodeStandard).bind('mouseover', function(e){
		$(this).addClass('active');
	});
	$(listNodeStandard).bind('mouseout', function(e){
		$(this).removeClass('active');
	});
	$(listNodeStandard).bind('click', function(e){
		$(this).parent().find('li').removeClass('selected');
		if(!$(this).hasClass('selected')) {
			$(this).addClass('selected');
		}

		if($(this).parent().attr('id')=='age-list-months'){
			$('#dobm').val($(listNodeStandard).index(this)+1);	
		}
		else {
			$('#dobd').val($(this).text());
		}
		submitOnComplete();
	});
	
	$(listYear).append('<li id="age-selected-year" class="age-year"></li>');
	$(listYear).append('<li id="age-current-year" class="age-year"></li>');
	var listNodeCurrentYear = $('#age-current-year');
	var listNodeSelectedYear = $('#age-selected-year');
	
	$(listDay).append('<li id="age-selected-day" class="age-day"></li>');
	$(listDay).append('<li id="age-current-day" class="age-day"></li>');
	var listNodeCurrentDay = $('#age-current-day');
	var listNodeSelectedDay = $('#age-selected-day');
	
	$(listYear).bind('mouseleave', function(e){$(listNodeCurrentYear).hide();});
	$(listDay).bind('mouseleave', function(e){$(listNodeCurrentDay).hide();});
	
	$(listYear).bind('click', function(e){
		$(listNodeSelectedYear).show();
		$(listNodeSelectedYear).css("left", $(listNodeCurrentYear).css('left'));
		$(listNodeSelectedYear).html($(listNodeCurrentYear).html());
		$('#doby').val($(listNodeCurrentYear).html());
		submitOnComplete();
	});
	
	$(listDay).bind('click', function(e){
		$(listNodeSelectedDay).show();
		$(listNodeSelectedDay).css("left", $(listNodeCurrentDay).css('left'));
		$(listNodeSelectedDay).html($(listNodeCurrentDay).html());
		$('#dobd').val($(listNodeCurrentDay).html());
		submitOnComplete();
	});
	
	$(listYear).bind('mousemove', function(e){
		var xVal = Math.floor(e.pageX - (this.offsetLeft+$('body').offset().left));
		var yearVal = Math.floor(xVal/($('ul#age-list-years li').width()+2)*10+1900);
		if(yearVal < 1900 || yearVal > 2004) {
			$(listNodeCurrentYear).hide();
		}
		else {
			$(listNodeCurrentYear).show();
			$(listNodeCurrentYear).html(yearVal);
			$(listNodeCurrentYear).css('left', xVal-($(listNodeCurrentYear).width()/12));
		}
	});
	
	$(listDay).bind('mousemove', function(e){
		var xVal = Math.floor(e.pageX - (this.offsetLeft+$('body').offset().left));
		var dayVal = Math.floor(xVal/($('ul#age-list-days li').width()+115)*10+1);
		if(dayVal < 1 || dayVal > 31) {
			$(listNodeCurrentDay).hide();
		}
		else {
			$(listNodeCurrentDay).show();
			$(listNodeCurrentDay).html(dayVal);
			$(listNodeCurrentDay).css('left', xVal-($(listNodeCurrentDay).width()/12));
		}
	});
});
