/**
 * Javascript headerFooter.
 *
 * @copyright (c) 2009 Travelsoft
 * @author emmanuel.sammut
 * @version 1.0
 */


// Create array for banner images.
var cv_headerBanImages = [];
var isActiveTab = true;

/**
 * Protected jQuery alias $.
 */
(function($){

    /**
     * Function cvToggleFocus.
     * Set default value of element into data object and initialize jQuery blur() and focus() method.
     *
     * use : $("input").toggleFocus();
     *       $(":text, :password").toggleFocus();
     *       $("#first, #second").toggleFocus();
     */
    $.fn.cvToggleFocus = function(){
        return this.each(function(){
            var el = $(this);
            el.data("defaultValue", el.val())
                .blur(function(){if(el.val() === ""){el.val(el.data("defaultValue"))}})
                .focus(function(e){el.val("")});
            });
    }

    /**
     * Function cvInitBanner.
     * First add external Carrefour images into a initial internal Carrefour voyages array.
     * Second insert all links and images into HTML page.
     *
     * @param {Array} arrayImages
      * @return {String} html format <a href="" target=""><img src="" alt="" /></a>
     *
     * use : $("#bannerWrap").cvInitBanner();
     */
    $.fn.cvInitBanner = function(arrayImages){
        // Defined carrefour path to local images and redirect pages.
        var C_PATH = "http://www.carrefour.fr/static/cfr/bannieres/voyages/";
        var arrayImagesExt = [
                                                        "<a href=\"" + C_PATH + "page1.html\" target=\"_blank\"><img src=\"" + C_PATH + "img1.gif\" alt=\"image1\" class=\"cv_headerExteralImages\" /></a>",
                                                        "<a href=\"" + C_PATH + "page2.html\" target=\"_blank\"><img src=\"" + C_PATH + "img2.gif\" alt=\"image2\" class=\"cv_headerExteralImages\" /></a>",
                                                        "<a href=\"" + C_PATH + "page3.html\" target=\"_blank\"><img src=\"" + C_PATH + "img3.gif\" alt=\"image3\" class=\"cv_headerExteralImages\" /></a>",
                                                        "<a href=\"" + C_PATH + "page4.html\" target=\"_blank\"><img src=\"" + C_PATH + "img4.gif\" alt=\"image4\" class=\"cv_headerExteralImages\" /></a>"
                                                    ];
        var lengthExt = arrayImagesExt.length;
        for(var i = 0; i < lengthExt; i++){
            arrayImages.push(arrayImagesExt[i]);
        }
        return this.html(arrayImages.join(""));
    }

    /**
     * Function cvSlideBanner.
     * Create a slide banner animation, with check error image fonctionality.
     *
     * @param {Int} timeout [optional] 	set the rotation time of the banner, default to 7000 (7 seconds).
     *
     * use : $("#bannerWrap").cvSlideBanner();
     */
    $.fn.cvSlideBanner = function(timeout){
        // Defined variables
        var wrap = $(this);
        var img = wrap.find("img");
        var lengthImage = img.length;
        var index = 0;
        // Hide all images and if a error is detected remove link and image.
        img.hide()
             .error(function(){
               $(this).parent("a").remove();
             });
        // Set interval for loop images.
        setInterval(
            function(){
                if (index === 0){
                    img = wrap.find("img");
                    lengthImage = img.length;
                    index = 1;
                }
                else if (index > lengthImage){
                    index = 1;
                }
                img.hide().eq(index - 1).fadeIn("slow");
                index++;
            },
            timeout || 7000 // Timeout of loop. 1000 equal 1 second.
        );
    }

    /**
     * active tab
     */
    $.activeTab = function(){
     var pageId = $.cookies.get("pageId");
      $("#cv_headerNavigation.tabOn").removeClass("tabOn");
     $("#cv_headerTab"+pageId).addClass("tabOn");
    }

    /**
     * set pageId in cookies
     */
    $.setPageId= function(pageId){
    $.cookies.set("pageId", pageId, {path : "/"});

    }


    /**
     * set pageId in cookies
     */
    $.setIsLogged= function(value){
    var date = new Date();
        date.setTime(date.getTime() + (0.5 * 60 * 60 * 1000));
    $.cookies.set("isLogged", value, {path : "/", expires: date});

    }


    /**
     * document.ready event.
     */
    $(function(){

        // Set input text and password element with cvToggleFocus.
        $("#ipt_cvHeaderLogin, #ipt_cvHeaderPassword, #ipt_cvHeaderNewsletter,.formvalues input").cvToggleFocus();

        // Initialize banner.
        $("#cv_headerBanner").cvInitBanner(cv_headerBanImages).cvSlideBanner();

        if (isActiveTab) {
            $.activeTab();
        }

        extractHideParameter();
        var cookieDissimExists = document.cookie.indexOf("dissim_phone=")
        if (cookieDissimExists >= 0) {
            $("#cv_headerLinkCallNumber").remove();
        }

    });

})(jQuery);


/**
 * Debug tools
 */
function info(param){
    if(window.console && window.console.info){
        window.console.info(param);
    }
}

/**
 * Function which adds cookie if there is a parameter 'hide' in the URL
 */
function extractHideParameter(){
    var t = location.search.substring(1).split('&');
    var hide = -1;
    for (var i=0; i<t.length; i++){
        var x = t[ i ].split('=');
        if (x[0] == "hide") {
            hide = x[1];
            if (hide > 15) {
                hide = 0;
            }
        }
    }
    if (hide != -1) {
        var expDate = new Date();
        expDate.setTime(expDate.getTime() + (hide * 24 * 3600 * 1000));
        document.cookie = "dissim_phone=" + escape(hide) + ";expires=" + expDate.toGMTString() + ";path=/;domain=.voyages.carrefour.fr";
    }
}