/******************************************************************************
* gifiFonctions.js
*******************************************************************************

*******************************************************************************
*                                                                             *
* Copyright 2010									                          *
*                                                                             *
******************************************************************************/

var Timer;
var Pas = 15;

function moveProduct(Sens){
	
	Objet=document.getElementById("gifi-btListScrollDiv2");
	
	if(parseInt(Objet.style.left) + (Pas*Sens)>0){
		clearTimeout(Timer);
	}
	else if(parseInt(Objet.style.left) + (Pas*Sens)<-(Objet.offsetWidth-document.getElementById("gifi-btListScrollDiv1").offsetWidth)) {
		clearTimeout(Timer);
	}
		else {
				Objet.style.left = (parseInt(Objet.style.left) + (Pas*Sens)) + "px";
	}
	Timer = setTimeout("moveProduct(" + Sens + ");", 30);
}

(function ($) {
	
	 jQuery.extend(jQuery.fn,
		{
			vAlign: function() {
				return this.each(function(i){
				var ah = $(this).height();
				var ph = $(this).parent().height();
				var mh = Math.ceil((ph-ah) / 2);
				$(this).css('margin-top', mh);
				});
			},
			selectValue: function (options) {

					/*  Plug-in jQuery ""
					
					Exemple : 
						
					Utilisation :  Mes le champ en question à vide si il contient la valeur par défaut spécifiée
						
					Pamarètres possibles :
						defaultTxt : Texte par défaut (Exemple : Tapez votre recherche ici (String)
						
					*/

					// Paramètres par défaut
					var defaults =
					{
							'defaultTxt': ''
					};

					// Fusion de defaults et options
					var params = $.extend(defaults, options);
					
					var defaultTxt = params.defaultTxt;
					var cInput = $(this);
				
					cInput.focus(function(){
						if(cInput.val() == defaultTxt) cInput.val("");
					}).blur(function(){
						if(cInput.val() == "") cInput.val(defaultTxt);
					});
			}
		});
		
	$(document).ready(function(){
		$(".gifi-on:not(.gifi-rubs-lvl2,.gifi-rubs-lvl3)").next().addClass("gifi-on-next");
		
		$(".gifi-rubs-lvl1").hoverIntent({
			over : openMainNav,
			out : closeMainNav,
			timeout : 500
		}).first().addClass("gifi-firstRub-lvl1");
		function openMainNav(){
			$(this).addClass("gifi-over").children(".gifi-globalRubs-border").fadeIn('slow').parent().next().addClass("gifi-over-next");
		}
		function closeMainNav(){
			$(this).removeClass("gifi-over").children(".gifi-globalRubs-border").hide().parent().next().removeClass("gifi-over-next");
		}
		
		$(".gifi-links-lvl1").vAlign().click(function(){
				$(this).parent().addClass("gifi-over").children(".gifi-globalRubs-border").fadeIn('slow').parent().next().addClass("gifi-over-next");
				return false;
		});
		
		$(".gifi-globalRubs-lvl1 a[href='#']").css("cursor","default");
	
		$(".gifi-rubs-lvl1").each(function(){
			$(this).children(".gifi-globalRubs-lvl2").wrapAll("<div class='gifi-globalRubs-border'/>").parent().children(".gifi-possibleSht").last().append(function(){
				return $(".shtInMainNav").html();
			});
		});
		
		$(".gifi-globalRubs-lvl2").show().height(function(){
			return $(this).parent().height();
		});
		
		$(".formSearch form").submit(function(e) {

			var cInput = $(this).children("#gifi-fulltextField");
			var cVal = cInput.val() == "" ? $(this).children("#submitSearchBt").attr("alt") : cInput.val();
			
			function preg_replace (array_pattern, array_pattern_replace, my_string)  {
				var new_string = String (my_string);
					for (i=0; i<array_pattern.length; i++) {
						var reg_exp= RegExp(array_pattern[i], "gi");
						var val_to_replace = array_pattern_replace[i];
						new_string = new_string.replace (reg_exp, val_to_replace);
					}
					return new_string;
				}
			
			function no_accent (my_string) {
				var new_string = "";
				var pattern_accent = new Array("é", "è", "ê", "ë", "ç", "à", "â", "ä", "î", "ï", "ù", "ô", "ó", "ö");
				var pattern_replace_accent = new Array("e", "e", "e", "e", "c", "a", "a", "a", "i", "i", "u", "o", "o", "o");
				if (my_string && my_string!= "") {
					new_string = preg_replace(pattern_accent, pattern_replace_accent, my_string);
				}
				return new_string;
			}
			
			var newVal = no_accent(cVal);
			cInput.val(newVal);
			
			return true
			
		});
		
		/* Carrousel  liste promo */
		$(".gifi-btListScrollLink").click(function(e){
			e.preventDefault();
		}).mouseenter(function(){
			if($(this).hasClass("gifi-btListScrollLinkRight")){
				moveProduct(-1);
			}else if($(this).hasClass("gifi-btListScrollLinkLeft")){
				moveProduct(1);
			}else{
				return false;
			}
		}).mouseout(function(){
			clearTimeout(Timer);
		});
		
		/* Formulaire alerte */
		$("#gifi_alertFormLink").click(function(e){
			e.preventDefault();
			alertBox("M'avertir quand ce produit sera disponible !",this,true);
		});
		
	});
})(jQuery);

function alertBox(title,cObj,confirmation){
	var alertBoxTitle = $("<h6 id='gifi_alertBoxTitle'/>").text(title);
	var confirmationBts = $("<ul/>").addClass("gifi_confirmation").append(function(){
		return $("<li id='gifi_cancel' />").append(function(){
			return $("<span/>").text("Annuler").click(function(){
				removeAlertBox();
			});
		});
	}).append(function(){
		return $("<li id='gifi_ok' />").append(function(){
			return $("<span/>").text("Ok").click(function(e){
				okAlertBox(e);
			});
		});
	});
	
	var alertContent = "";
	var objId = $(cObj).attr("href");
	
	if(objId != ""){
		alertContent = $(objId);
	}

	var globalAlertBox = $("<div id='gifi_alertBox' /> ").click(function (e) {
		e.stopPropagation();
	}).append(alertBoxTitle).append(function(){
		var newClone = alertContent.clone().css("display", "block");
		newClone.wrap("<div id='gifi_alertContent' />");
		return newClone.parent();
	}).append(confirmationBts);
	var alertBlackBox = $("<div id='gifi_blackBox' />").click(function (e) {
		e.stopPropagation();
	}).css("height",$(document).height());
	
	if($("#gifi_blackBox").length < 1){
		$("body").append(globalAlertBox).prepend(alertBlackBox);
		$(globalAlertBox).css({
			"top": function(index){
				return (($(window).height() - $(this).height()) / 2);
			},
			"left": function(index){
				return (($(window).width() - $(this).width()) / 2);
			}
		});
	}

	$(window).resize(function(){
		var win = $(this);
		$(globalAlertBox).css({
			"top": function(index){
				return (($(win).height() - $(this).height()) / 2);
			},
			"left": function(index){
				return (($(win).width() - $(this).width()) / 2);
			}
		});
	});
}
function removeAlertBox(){
	$("#gifi_blackBox,#gifi_alertBox").remove();
}
function okAlertBox(event){
	event.preventDefault;
	
	var email = $("input[name=alertMail]:eq(1)").val();
	var regMail = new RegExp('^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,6}$','i');
	if(regMail.exec(email)!=null){
		
		$("#gifi_alertContent").children(":first").fadeOut('high', function(){
			
			$("#gifi_alertContent").html("<img src='iso_icons/gifi-loading.gif' />");
						
			$(".gifi_confirmation").css("visibility","hidden");
			var url = self.location.href;
			$.post(url, { pageMode: "ajax", ajaxFunc: "alertProductMail", ajaxMail: email, ajaxGifiVar1: gifiVar1, ajaxGifiVar2: gifiVar2}, function (data) {
				switch(parseInt(data)){
					case 0:
						$("#gifi_alertContent").html("<p>Erreur lors de l'enregistrement.<p>");
						$("#gifi_alertContent input[name=alertMail]").select();
						$(".gifi_confirmation").css("visibility","visible");
					break;
					case 1:
						$("#gifi_alertContent").html("<p>Enregistrement Effectué<p>");
						setTimeout("removeAlertBox()",1000);
					break;
					case 2:
						$("#gifi_alertContent").html("<p>Email non valide.<p>");
						$("#gifi_alertContent input[name=alertMail]").select();
						$(".gifi_confirmation").css("visibility","visible");
					break;
					case 3:
						$("#gifi_alertContent").html("<p>Email déjà enregistré<p>");
						setTimeout("removeAlertBox()",1000);
					break;
					default:
					break;
				}
			});

		
		});
		
	}else{
	
		console.log("non conforme !!!!");
		return false;
	}
}

function gifiSubmitSearchForm (formName)
{
	document.getElementById(formName).submit();
}

