/*----------------------------------------------------------------------------//
// Javascript pour le site client  -------------------------------------------//
//----------------------------------------------------------------------------//
// Client : Camping Québec / Site internet
// Date   : 9 fevrier 2009
//----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*/
// Inclusion dynamique des fichiers externes javascripts
/*----------------------------------------------------------------------------*/
function importJavascript(src)
{
	document.write("<script type=\"text/javascript\" src=\"" + src + "\"></scr" + "ipt>");
}
function importCss(href, media)
{
	document.write('<link rel="stylesheet" type="text/css" media="' + media + '" href="' + href + '" />');
}

/*----------------------------------------------------------------------------*/
// Importations de fichiers
/*----------------------------------------------------------------------------*/
/* Flash library */
importJavascript("/includes/js/swfobject.js");

/* Abso Accordeon > Accueil */
importJavascript("/includes/js/plugins/abso.accordeon.js");

/* Abso LabelFocus */
importJavascript("/includes/js/plugins/abso.labelfocus.js");

/* Galerie Photo > Fiche camping */
importJavascript("/includes/js/plugins/jquery.jcarousel.js");
importCss("/includes/js/plugins/jquery.jcarousel.css", "screen");

/* Date Picker */
importJavascript("/includes/js/plugins/jquery.date.js");
importJavascript("/includes/js/plugins/jquery.date.fr.js");
importJavascript("/includes/js/plugins/abso.datepicker.js");
importCss("/includes/js/plugins/abso.datepicker.css", "screen");

/* table order */
importJavascript("/includes/js/plugins/jquery.metadata.js");
importJavascript("/includes/js/plugins/jquery.tablesorter.js");


/* Tooltip */
importJavascript("/includes/js/plugins/jquery.bgiframe.js");
importJavascript("/includes/js/plugins/jquery.dimensions.js");
importJavascript("/includes/js/plugins/jquery.tooltip.js");

/* Thickbox */
importJavascript("/includes/js/plugins/abso.thickbox.js");
importCss("/includes/js/plugins/abso.thickbox.css", "screen");

/* Autocomplete */
importJavascript("/includes/js/plugins/jquery.autocomplete.js");
importCss("/includes/js/plugins/jquery.autocomplete.css", "screen");

/* Block UI > Formulaire exploitant */
importJavascript("/includes/js/plugins/jquery.blockui.js");

/* Image Flow > Offres speciales */
importCss("/includes/js/plugins/abso.imageflow.css", "screen");
importJavascript("/includes/js/plugins/abso.imageflow.js");

/* Thicker > Pub Camping */
//importJavascript("/includes/js/plugins/jquery.jcarousellite.js");

/* PNGfix */
if($.browser.msie) {
	importJavascript("/includes/js/plugins/jquery.pngfix.js");
}

/*----------------------------------------------------------------------------*/
// Declarations jQuery
/*----------------------------------------------------------------------------*/
var currentPopupSearch = null; 
var timerPopupSearch = null;
var focusPopupSearch = false; 
var mouseenterPopupSearch = false;
var manuallyClose = false;

$(document).ready(function() {

    /* - Label Focus
    ------------------------------------*/
    $("input.LabelFocus").labelfocus();

    /* - Page de Résultats
    ------------------------------------*/
    if ($.browser.msie) {
        $("img.PictoCamping").pngfix();
    }


    /* - Formulaire Recherche
    ------------------------------------*/

    /* Popup Form Recherche */
    if ($("a.BtnPopupRecherche").length > 0) {

        // On Click Action
        $("a.BtnPopupRecherche").click(function() {
            manuallyClose = false;
            if (timerPopupSearch != null)
                clearTimeout(timerPopupSearch);
            if (currentPopupSearch != null) { // si une autre boite est ouverte
                $("#" + currentPopupSearch).fadeOut("fast"); // on la ferme
            }
            $(this).next("div.PopupRecherche").fadeIn("fast");
            currentPopupSearch = $(this).next("div.PopupRecherche").attr("id");
        });
        $("div.PopupRecherche a.Fermer").click(function() {
            $(this).parent().fadeOut("fast");
            focusPopupSearch = false;
            mouseenterPopupSearch = false;
            manuallyClose = true;
            if (timerPopupSearch != null)
                clearTimeout(timerPopupSearch);
        });

        // Mouse Action
        $("div.PopupRecherche").bind("mouseenter", function() {
            //console.log("MOUSEENTER");
            mouseenterPopupSearch = true;
            checkPopupSearchState();
        }).bind("mouseleave", function() {
            mouseenterPopupSearch = false;
            checkPopupSearchState();
        });

        //Champ focus
        $("#txtCampingName").focus(function() {
            focusPopupSearch = true;
        }).blur(function() {
            focusPopupSearch = false;
            checkPopupSearchState();
        });

        function checkPopupSearchState() {
            //console.log(focusPopupSearch + " && " + mouseenterPopupSearch + " && " + manuallyClose);
            if (!focusPopupSearch && !mouseenterPopupSearch && !manuallyClose) {
                //console.log("START TIMER");
                timerPopupSearch = setTimeout(function() { $("div.PopupRecherche").fadeOut('fast'); }, 3000);
            }
            else {
                //console.log("CLEAR TIMER");
                if (timerPopupSearch != null)
                    clearTimeout(timerPopupSearch);
            }
        }
    }

    /* Toogle Criteres */
    if ($("h3.BtnToggle").length > 0) {

        /* Toogle box */
        $("h3.BtnToggle").each(function() {
            if (!$(this).hasClass("On"))
                $(this).next("div.BoxToogle").hide();
        })
	    .click(openBox)
	    .mouseover(function() {
	        $(this).addClass("Over");
	    })
	    .mouseout(function() {
	        $(this).removeClass("Over");
	    });
    }

    /* Onglet */
    if ($("ul.TabMenu a").length > 0) {
        $("div.TabContenu").css("display", "none");
        $("div.TabContenu:first").css("display", "block");
        $("ul.TabMenu a:first").addClass("On");

        $("ul.TabMenu a").each(function() {
            var currentTabId = $(this).attr("href").substr(1);
            $(this).attr("href", "javascript:void(0);");
            $(this).click(function() {
                $("div.TabContenu").css("display", "none");
                $("div.BoxBasForm").css("display", "none");
                $("ul.TabMenu a").removeClass("On");
                $("#boxServices").css("display", "block");
                $("#" + currentTabId).css("display", "block");
                $("#" + currentTabId + "2").css("display", "block");
                $(this).addClass("On");
            });

        });

    }

    /* - Boite de login
    ------------------------------------*/
    if ($("div#BoiteLoginExploitant").length > 0) {

        /* Toogle box */
        //$("div#BoiteLoginExploitant div.BoxToogle").hide();
        $("div#BoiteLoginExploitant a.BtnToggle").click(openBox);

    }

    /* - Accordeon Accueil
    ------------------------------------*/
    $("#AccordeonAcc").accordion({
        speed: 500,
        selectedClass: "selected",
        headerClass: "Entete",
        contentClass: "Contenu",
        minHeight: 208
    });

    /* - Equalcols height Accueil
    ------------------------------------*/
    if ($("body").hasClass("Accueil")) {
        equalHeight($("#AccordeonAcc, #BoiteEspaceCampeur, #BoiteCamperQuebec, #BoiteTrouverCamping"));
    }

    /* - Calendriers
    ------------------------------------*/
    if (($("input.DateOpen").length > 0) || ($("input.DateClose").length > 0)) {
        //Initialisation du francais
        if ($("body").attr("id") == "lang-FR") { init_FR(); }
        //Creation des datepicker
        //$(".DateOpen, .DateClose").datePicker({ clickInput: true })
        $(".DateOpen, .DateClose").datePicker({
            clickInput: true,
            createButton: false,
            showYearNavigation: false
        });
    }

    /* Equalcols height */
    if ($("body.Fiche").length > 0) {
        equalHeight($("#BoxHoraire, #BoxReservation"), false);
        equalHeight($("#BoxTarif, #BoxEmplacements"), false);
        equalHeight($("#BoxPromotions, #BoxReglements"), false);
        equalHeight($("#BoxCalendrier, #BoxComment"), false);
        equalHeight($("#BoxInfo, #BoxPrix"), false);
    }

    BuildToolTip();
    
    /* - Formulaire des exploitants
    ------------------------------------*/
    if ($("select.BtnSousQuestion").length > 0) {

        $("select.BtnSousQuestion").change(function() {
            var selectSousQuestionBox = $(this).nextAll(".ToogleSousQuestion");
            if ($(this).attr('selectedIndex'))
                selectSousQuestionBox.addClass("On");
            else
                selectSousQuestionBox.removeClass("On");
        });

    }
    if ($(".BtnSousQuestion input[type=checkbox]").length > 0) {
        $(".BtnSousQuestion input[type=checkbox]").click(function() {
            var chkSousQuestionBox = $(this).parent().nextAll(".ToogleSousQuestion");
            if ($(this).is(":checked")) {
                chkSousQuestionBox.addClass("On");
            } else
                chkSousQuestionBox.removeClass("On");
        });

    }
    
    /* Equalcols height > Boutique */
    if ($("body.Exploitant").length > 0) {
        equalHeight($("div#ListeBoutique div.ProduitBox"), false);
    }

});

function BuildToolTip() {
    /* - Tooltips
    ------------------------------------*/
    if ($("a.tooltip, a.tooltipInline, img.tooltip").length > 0) {

        // Title tooltip
        $("a.tooltip, img.tooltip").each(function() {
            var currentHref = $(this).attr("href");
            var currentTitle = $(this).attr("title");
            $(this).attr("rel", currentTitle);
            $(this).removeAttr("title");
        });
        $("a.tooltip, img.tooltip").tooltip({
            bodyHandler: function() {
                if ($(this).attr("rel")) {
                    $("#tooltip").css("visibility", "visible");
                    var curTitle = $(this).attr("rel").split("|");
                    var curOut = "";
                    if (curTitle.length == 2) {
                        curOut = "<h6>" + curTitle[0] + "</h6>";
                        curOut += "<p>" + curTitle[1] + "</p>";
                    }
                    else {
                        curOut = "<p>" + curTitle[0] + "</p>";
                    }
                    return curOut + "<div class=\"pointer\"></div>";
                }
                else {
                    $("#tooltip").css("visibility", "hidden");
                    return "";
                }
            },
            track: true,
            delay: 0,
            showURL: false,
            orientation: "top",
            left: -86
        });

        // Inline tooltip
        $("a.tooltipInline").each(function() {
            var currentHref = $(this).attr("href");
            if (currentHref == "#")
                $(this).attr("href", "javascript:void(0);");
        });
        $("a.tooltipInline").tooltip({
            bodyHandler: function() {
                $("#tooltip").css("visibility", "visible");
                return $($(this).attr("rel")).html() + "<div class=\"pointer\"></div>";
            },
            track: true,
            delay: 0,
            showURL: false,
            orientation: "top",
            left: -86
        });

    }
}

/* - Fonctions : Utilitaire Loading Formulaire
   ----------------------------------------------*/
function showLoading(text, obj) {
    var text = (text ? text : ($("body").attr("id") == "lang-EN" ? "Loading..." : "Traitement en cours...") );
    if (obj) {
        $(obj).block({
		    message: "<div class=\"LoadingBox\"><h6>" + text + "</h6></div>",
		    css: { 
			    border: "3px solid #7aa443",
			    width: "285px"
			},
			showOverlay: false
		});
	}
	else {
		$.blockUI({
		    message: "<div class=\"LoadingBox\"><h6>" + text + "</h6></div>", 
			css: { 
                border: "3px solid #7aa443",
				top:  ($(window).height() - 100) /2 + 'px', 
                left: ($(window).width() - 285) /2 + 'px', 
                width: "285px" 
            }
        });

    }
    return true;
}
function hideLoading(obj) {
	if(obj) {
		$(obj).unblock();
	}
	else {
		$.unblockUI();
	}
}

/* - Fonctions : Utilitaire ToogleBox
   ------------------------------------*/
function changeBtStyle(obj) {
	if(!$(obj).hasClass("On"))
		$(obj).addClass("On");
	else
		$(obj).removeClass("On");
}
function openBox() {
	var btn = $(this);
	$(btn).next("div.BoxToogle").slideToggle("fast", function(){ 
		changeBtStyle(btn);
	});
}

/* - Fonctions : Open/Close Search box
------------------------------------------*/
function openSearchCriteriaBox() {
   $("#FormulairePage").slideDown('normal', function(){
		$("#BoxPreciser").hide();
	});
}
function closeSearchCriteriaBox() {
   $("#FormulairePage").slideUp('normal', function(){
		$("#BoxPreciser").show();
	});
}

/* - Fonction : Equalcols height
   ------------------------------------*/
function equalHeight(group, important) {
    tallest = 0;
    important = (important == false ? "" : " !important");
    group.each(function() {
        thisHeight = $(this).height();
        if(thisHeight > tallest) {
            tallest = thisHeight;
        }
    });
    group.attr("style", "height:" + tallest + "px" + important + ";");
}
function clearEqualHeight(group) {
	group.each(function() {
        $(this).css("height","");
    });
}

/* - Fonction : Initialisation Galerie
   ------------------------------------*/
function initGalerie() {
	$("#mycarousel").jcarousel({scroll: 4});
	if($("#GaleriePlaceHolder").length > 0)
	{
	    $("#mycarousel li a").each(function() {
	        $(this).removeAttr("target");
	        var imgBig = $(this).attr("href");
	        $(this).attr("href", "javascript:void(0);");
	        $(this).click(function() {
	            var imgObj = $("#GaleriePlaceHolder");
	            imgObj.attr("src", imgBig);
	            var imgRel = $(this).attr("rel");
	            if (imgRel)
	                imgObj.attr("rel", imgRel);
	            else
	                imgObj.removeAttr("rel");
	        });
	        $(this).children("img").removeAttr("title").removeAttr("alt");
	    });
	}
}

/* - Fonction : Ajustement entete du tableau de comparaison
   -----------------------------------------------------------*/
function adjustCompareHeader() {
	var compHeaderHeight = $("#ComparaisonBox div.TableauEntete").height();
	var compTotalHeight = $("#ComparaisonBox").css("height");
	//alert("compHeaderHeight:"+compHeaderHeight+" | compTotalHeight:"+compTotalHeight);
	compTotalHeight= compTotalHeight.substr(0, (compTotalHeight.length-2));
	$("#ComparaisonBox div.Conteneur").height((compTotalHeight-compHeaderHeight));
}

/* - Fonction : Lancement du popup save comparaison
   -----------------------------------------------------------*/
function ShowSavePopup() {
    tb_show(null, '#TB_inline?height=150&amp;width=350&amp;inlineId=QuestionPopup', false);
}


/* - Fonction : Ajustement Plan du site
   -----------------------------------------------------------*/
function AdjustSiteMap() {
	
	if($("ul#SiteMap")) {		
		$("ul#SiteMap li:first").css("border-top","0px");
		$("ul#SiteMap ul li.SiteMapSousItem:first-child").each(function() {
			var sm_height = $(this).height();
			
			if($(this).next().height() > sm_height)
				sm_height = $(this).next().height();
			else
				$(this).next().height(sm_height);
			
			/*if($(this).next().next().height() > sm_height)
				sm_height = $(this).next().next().height();
			else
				$(this).next().next().height(sm_height);*/
		});
		
		$("ul#SiteMap li.SiteMapSousItem:nth-child(2n+1)").css("clear","both");	
	}
}