/**
 * EUVS
 * Main javascript file (included in every page)
 */

var rollTimeout;

// Main navigation
var activateitem = function(){
	
    if (!$(this).hasClass('initial')) {
/*        var image = $(this).children('a').children('img');
        image.attr('src', image.attr('src').replace('-inactive', '-active'));
*/
		var self = this;
		rollTimeout = setTimeout(function() { $(self).children("div").css("display","block"); }, 300);
        
        if ($(this).hasClass('bg-prev')) {
            $(this).removeClass('bg-prev');
            $(this).addClass('active-prev');
        }
        else if ($(this).hasClass('bg-next')) {
            $(this).removeClass('bg-next');
            $(this).addClass('active-next');
        }
        else {
            $(this).addClass('active');
        }
        
        if ($(this).next().length == 1) $(this).next().addClass('bg-next');
        if ($(this).prev().length == 1) $(this).prev().addClass('bg-prev');
    }

	
};

var deactivateitem = function() {
	
	clearTimeout(rollTimeout);
    if (!$(this).hasClass('initial')) {
/*        var image = $(this).children('a').children('img');
        image.attr('src', image.attr('src').replace('-active', '-inactive'));
*/		
		$(this).children("div").css("display","none");
        
        if ($(this).hasClass('active-prev')) {
            $(this).removeClass('active-prev');
            $(this).addClass('bg-prev');
        }
        else if ($(this).hasClass('active-next')) {
            $(this).removeClass('active-next');
            $(this).addClass('bg-next');
        }
        else {
            $(this).removeClass('active');
        }
        
        $(this).prev().removeClass('bg-prev');
        $(this).next().removeClass('bg-next');
    }else deactivateitem;
	
};

var checkDisclaimerAge = function(e) {
	var year = $('#disclaimer-overlay .birth_date_year').val();
	var month = $('#disclaimer-overlay .birth_date_month').val();
	var day = $('#disclaimer-overlay .birth_date_day').val();
	var country = $('#disclaimer-overlay select').val();

	if( !year.match(/^\d{4}$/) || !month.match(/^\d{1,2}$/) || !day.match(/^\d{1,2}$/) || country.match(/^\s*$/))
		alert($('body').hasClass('en') ? 'Please make sure that the information you are trying to submit is correct' : 'Veuillez vous assurer de la validité des informations soumises');
	else
		$.post('/en/age/check',
			{
				birth_date_year : year,
				birth_date_month : month,
				birth_date_day : day,
				country : country
			},
			function(data) {
				if(data.success) window.location.reload();
				else window.location = '/' + ($('body').hasClass('en') ? 'en' : 'fr' ) + '/?'+data.param;
			}, 'json'
		);
		return false;
}

$(document).ready(function() {
	
    // Search field
    var search_field_value = $("#search input").val();
    $("#search input").focus(function(){if ($(this).val() == search_field_value) $(this).val('').addClass('active');});
    $("#search input").blur(function(){if ($(this).val() == '') $(this).removeClass('active').val(search_field_value);});
    
    // Main navigation
	$("#nav li:has(div)").hover(activateitem,deactivateitem);

	// Disclaimer
	if($("#disclaimer-overlay").length) {
		$("#disclaimer-overlay").overlay({
			speed:'slow',
			close : false,
			closeOnClick:false,
			fadeInSpeed: 'normal',
			onBeforeLoad : function() {
				var isIE6 = ($.browser.msie && $.browser.version == '6.0');
				$('body').prepend(
					$(document.createElement('div')).css({
						width : Math.floor($(window).width()),
						height : isIE6 ? Math.floor($(document).height()) : Math.floor($(window).height()),
						position : isIE6 ? 'absolute' : 'fixed',
						background : '#222',
						opacity : '0.8',
						top : '0',
						left : '0',
						'z-index' : '8000'
					}).attr('id','bodyOverlay')
				).css('overflow','hidden');	
			},
			onLoad : function() {
				$('#disclaimer-overlay form').attr('action','javascript:void(0)');
				$('#disclaimer-overlay input.submit').click( checkDisclaimerAge );
				$("#disclaimer-overlay input[nameay=birth_date_day]").focus();
				$("#disclaimer-overlay").find("input[type=text]").focus(function(){
					$(this).val('');
				}).keyup(function(){
						if($(this).val().length == $(this).attr('maxlength')) {
							$(this).next('input').focus().select();
						}
					});
				$(window).keydown(function(event){
					if(event.keyCode == '27') return false;
				});
				$(window).resize(function(){
					$('#bodyOverlay').width($(window).width()).height($(window).height())
				});
				setTimeout(function() { $(window).scrollTop(0) }, 500);
			},
			onClose : function() {
				$('#bodyOverlay').fadeOut(600);
				$('body').css('overflow','auto');
			}
		});
		$("#disclaimer-overlay").overlay().load();
	}
	
	$.ifixpng('/img/pixel.gif');
	$('img[src$=.png]').not('.frieze img').ifixpng();
});
