jQuery(document).ready(function(){
	carrefour.init();
});

jQuery(window).bind("resize", function(){
	carrefour.resize();
});

var carrefour = {
	init: function(){
		this.resize();
		this.navigation();
		this.handlePrint();
		if( jQuery("#home").size() == 1 ) this.flashHome();
		if( jQuery("#flash-spotstv").size() == 1 ) this.flashSpotsTV();
		if( jQuery("#flash-products").size() == 1 && jQuery("#home").size() == 0 ) this.flashProductsInside();
		
		this.fixHeight(".list-ingredients .left, .list-ingredients .right");
		
		if( jQuery(".quiz-questions form").size() == 1 ) errors.quiz(".quiz-questions form");
		if( jQuery(".form-concours form").size() == 1 ) errors.concours(".form-concours form");
		if( jQuery(".form-quiz-container form").size() == 1 ) errors.concours(".form-quiz-container form");
	},
	flashHome: function(){
		swfobject.embedSWF("swf/header.swf", "flash-header", "946", "114", "9.0.0");
		swfobject.embedSWF("swf/products.swf", "flash-products", "712", "260", "9.0.0", "", {}, { wmode: "transparent" });
	},
	flashSpotsTV: function(){
		swfobject.embedSWF("swf/spotstv/player_elements.swf", "flash-spotstv", "794", "367", "9.0.0", "", { XmlUrl: "swf/spotstv/xml/data-videoplayer.xml" }, { wmode: "transparent" });
	},
	flashProductsInside: function(){
		if( jQuery(".products-1").size() > 0 )
			flash = "rayon_epicerie.swf";
		else if( jQuery(".products-2").size() > 0 )
			flash = "rayon_boissons.swf";
		else if( jQuery(".products-3").size() > 0 )
			flash = "rayon_surgeles.swf";
		else if( jQuery(".products-4").size() > 0 )
			flash = "rayon_entretien.swf";
		
		swfobject.embedSWF("swf/" + flash, "flash-products", "946", "194", "9.0.0", "", {}, { wmode: "transparent" });
	},
	resize: function(){
		if(document.documentElement.clientWidth){ // bug background position
			width = Math.floor((document.documentElement.clientWidth - jQuery("#header")[0].offsetWidth)/2);
			jQuery("#header, #content-inner, #footer-inner").css("marginLeft", width + "px");
		}
	},
	navigation: function(){
		var currentImg = "";
		var imgHover = "";
		var active = true;
		
		jQuery("#navigation > li").hover(function(){
				active = ( jQuery("img", this).attr("src").indexOf("-on") != -1 ) ? false : true;
				
				jQuery("ul", this).css({ left: "0" });
				currentImg = jQuery("img", this).attr("src");
				if(currentImg != ""){
					if(active) jQuery("img:eq(0)", this).attr("src",currentImg.replace("-off","-on"));
					imgHover = currentImg.replace("-off","-on");
				}
			},
			function(){
				jQuery("ul", this).css({ left: "-9999px" });
				if(currentImg != ""){
					if(active) jQuery("img:eq(0)", this).attr("src",currentImg.replace("-on","-off"));
				}
		});
		
		if(active){
			jQuery("#navigation > li > ul").hover(function(){
					jQuery(this).parents("li").children("a").children("img").attr("src",imgHover);
				},
				function(){
					jQuery(this).parents("li").children("a").children("img").attr("src",imgHover);
			});
		}
		
		jQuery("#navigation > li > ul li").hover(function(){
				imgOff = jQuery("img", this).attr("src");
				jQuery("img", this).attr("src",imgOff.replace("-off","-on"));
			},
			function(){
				imgOff = jQuery("img", this).attr("src");
				jQuery("img", this).attr("src",imgOff.replace("-on","-off"));
		});
	},
	handlePrint: function(){
		jQuery(".print-recipe a").click(function(){
			window.print();
			return false;
		});
	},
	fixHeight: function(elt) {
		if( jQuery(elt).size() < 1 ) { return false; }
		var heightBlockMax=0;
		jQuery(elt).each(function(){ if( jQuery(this).height() > heightBlockMax ) heightBlockMax = jQuery(this).height(); }); // get max height
		jQuery(elt).each(function(){ jQuery(this).css("height",heightBlockMax); }); // assign max height
		heightBlockMax=0;
	}
};

var errors = {
	error: false,
	showErrorAll: function(elt){
		jQuery(elt + " label").addClass("error");
		this.error = true;
	},
	showError: function(elt){
		jQuery(elt).prev().addClass("error");
		this.error = true;
	},
	showErrorRadio: function(elt){
		jQuery(elt + " label").addClass("error");
		jQuery(elt).prev().addClass("error");
		this.error = true;
	},
	resetError: function(elt){
		jQuery(elt + " label, " + elt + " .label").removeClass("error");
	},
	checkEmail: function(elt){
		var reg = new RegExp("^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]{2,}[.][a-zA-Z]{2,3}$");
		if( !reg.test(jQuery(elt).val()) ){ this.showError(elt); }
	},
	checkNum: function(elt){
		var reg = new RegExp("[0-9]");
		if( reg.test(jQuery(elt).val()) ){ this.showError(elt); }
	},
	quiz: function(elt){
		current = this;
		
		jQuery(elt).submit(function(){
			current.error = false;
			current.resetError(elt);
			
			if( jQuery(elt + " input:checked").size() == 0 ) current.showErrorAll(elt);
			
			jQuery("#nom").val() != "" ? current.checkNum("#nom") : "";
			jQuery("#prenom").val() != "" ? current.checkNum("#prenom") : "";
			
			if(current.error) window.scroll(0,0);
			
			return current.error ? false : true;
		});
	},
	concours: function(elt){
		current = this;
		
		jQuery(elt).submit(function(){
			current.error = false;
			current.resetError(elt);
			
			jQuery(elt + " input.required, " + elt + " textarea.required").each(function(){
				val = jQuery(this).val();
				if( val == "" || val.length < 2 ) current.showError(this);
			});
			
			if( jQuery(elt + " .required-radio:checked").size() == 0 ) current.showErrorRadio(elt + " span.radio");
			
			jQuery("#email").val() != "" ? current.checkEmail("#email") : "";
			
			jQuery("#nom").val() != "" ? current.checkNum("#nom") : "";
			jQuery("#prenom").val() != "" ? current.checkNum("#prenom") : "";
			
			if(current.error) window.scroll(0,0);
			
			return current.error ? false : true;
		});
	}
};