/* Budweiser.com Age Gate JavaScript file */

$(document).ready(function(){
	$('#txtMonth').val('');
	$('#txtDay').val('');
	$('#txtYear').val('');
	var listNodeStandard = $('#age-list-container ul.standard li');
	var listYear = $('#age-list-years');
	
	$('a.external').bind('click', function (e) {
		$(this).attr('target', '_blank');
	});
	
	var submitOnComplete = function(){
		if($('#txtMonth').val()!='' && $('#txtDay').val()!='' && $('#txtYear').val()!=''){
			$('#age-current-year').unbind();
			$('#age-current-year').hide(function(){
				$('#loader').show();
				$('#submit').trigger('click');
			});
		}
	}
	
	$(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'){
			$('#txtMonth').val($(listNodeStandard).index(this)+1);	
		}
		else {
			$('#txtDay').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');
	
	$(listYear).bind('mouseleave', function(e){$(listNodeCurrentYear).hide();});
	
	$(listYear).bind('click', function(e){
		$(listNodeSelectedYear).show();
		$(listNodeSelectedYear).css("left", $(listNodeCurrentYear).css('left'));
		$(listNodeSelectedYear).html($(listNodeCurrentYear).html());
		$('#txtYear').val($(listNodeCurrentYear).html());
		submitOnComplete();
	});
	
	$(listYear).bind('mousemove', function(e){
		var xVal = Math.floor(e.pageX - (this.offsetLeft+$('#content').offset().left));
		var yearVal = Math.floor(xVal/($('ul#age-list-years li').width()+2)*10+1900);
		if(yearVal < 1900 || yearVal > 2010) {
			$(listNodeCurrentYear).hide();
		}
		else {
			$(listNodeCurrentYear).show();
			$(listNodeCurrentYear).html(yearVal);
			$(listNodeCurrentYear).css('left', xVal-($(listNodeCurrentYear).width()/12));
		}
	});
});
