﻿var intModalStack = 0;

var randomPhrases = new Array();

var _isUserLoggedIn = false;


randomPhrases.push("Un service hors pair ?");
randomPhrases.push("Consultation...");
randomPhrases.push("Assemblage");
randomPhrases.push("Formation sur place...");
randomPhrases.push("Accès direct au manufacturier...");
randomPhrases.push("Notre solide inventaire...");

var displayedPhrases = new Array();

function timeSloganSwapping() {
    $("#adRandom").animate({
        opacity: 0
    }, 1000, function () {
        findAnotherRandomPhrase();
    });
}


function findAnotherRandomPhrase() {

    var phraseToDisplay;

    if (randomPhrases.length == 0) {
        randomPhrases = displayedPhrases.reverse();
        displayedPhrases = new Array();
    }
    phraseToDisplay = randomPhrases.pop();
    displayedPhrases.push(phraseToDisplay);

    $("#adRandom").text(phraseToDisplay);


    $("#adRandom").animate({
        opacity: 1
    }, 1000, function () {
        setTimeout(function () { timeSloganSwapping(); }, 5000);
    });

}



function showWaitPanel(contentIdentifier) {
    $('#contentZone').css({ visibility: "visible", display: "block" });

    $('#' + contentIdentifier).html("<div id=\"waitPanel\" style=\"height: " + $('#' + contentIdentifier).height() + "px\"></div>");
}


function Modal(id, modal) {

    if (modal) {
        var width = $('#' + id).outerWidth();
        var height = $('#' + id).outerHeight();

        intModalStack++;


        $('#' + id).animate({ opacity: 0.9 }, 400, function () {
            $('#' + id).append('<div id="' + id + 'coverBlur" style="padding: 10px; position: absolute; left: 0px; top: 0px; width:' + width + 'px; height: ' + height + 'px">&nbsp;</div>');
        });
    }
    else {
        $('#' + id).animate({ opacity: 1 }, 400, function () {
            $('#' + id + 'coverBlur').remove();
        });

        intModalStack--;
    }
}

function IsModal() {
    return (intModalStack > 0);
}

function ModalWait(id, modal, backgroundColor, msg) {

    if (modal) {

        if (msg == null) {
            msg = "Veuillez patienter ...";
        }

        if (backgroundColor == null) {
            backgroundColor = "transparent";
        }

        var width = $('#' + id).outerWidth() - 4;
        var height = $('#' + id).outerHeight() - 4;

        var paddingTop = Math.ceil((height - 100) / 2);
        var paddingLeft = Math.ceil((width - 200) / 2);

        if ($('#' + id + 'waitPanel').get(0) == null) {

            $('#' + id).append(
                '<div id="' + id + 'waitPanel" style="background-color: ' + backgroundColor + '; position: absolute; left: 0px; top: 0px; width:' + width + 'px; height: ' + height + 'px">' +
                '    <div style="padding-left: ' + paddingLeft + 'px; padding-top: ' + paddingTop + 'px">' +
                '        <div style="padding-top: 7px; width: 200px; height: 30px; text-align: center; vertical-align: middle; background-color: #FFFFFF; font-family: arial; font-size: 11px; font-weight: bold; border: 1px solid gray;">' +
                '            <img src="images/loading2.gif" alt="" /><span id="' + id + 'message">&nbsp;&nbsp;' + msg + '</span>' +
                '        </div>' +
                '    </div>' +
                '</div>');
        }
        else {
            $('#' + id + 'message').html("&nbsp;&nbsp;" + msg);
        }
    }
    else {
        $('#' + id + 'waitPanel').remove();
    }
}

function applyCatalog() {
    $('#contentZone').css({ visibility: "hidden", display: "none" });
    $('#productsListZone').css({ visibility: "hidden", display: "none" });

    $('#productsZone').css({ visibility: "visible", display: "block" });
}


function loadProducts() {
    $('#productsZone').css({ visibility: "hidden", display: "none" });
    $('#productsListZone').css({ visibility: "hidden", display: "none" });
    showWaitPanel('contentZone');

    setTimeout(function () { applyCatalog(); }, 1000);

}

function loadContent(pageName) {

    var pageTitle;

    switch (pageName) {
        case "services":
            pageTitle = "Services";
            break;
    }

    applyContent("contentZone");
}



function applyProductList() {
    $('#contentZone').css({ visibility: "hidden", display: "none" });
    $('#productsZone').css({ visibility: "hidden", display: "none" });

    $('#productsListZone').css({ visibility: "visible", display: "block" });

}


function PreventSubmittingFormOnEnterKeyPress(event) {

    if ((event.which) || (event.keyCode)) {
        if ((event.which == 13) || (event.keyCode == 13)) {
            return true;
        }
    }


    return false;
}

function getScrollDiff() {
    var body = document.body;
    var html = document.documentElement;

    var maxHeight = Math.max(body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight);
    var scrollDiff = (maxHeight - $(window).height());

    if (scrollDiff < 0) {
        scrollDiff = 0;
    }

    return scrollDiff;
}


function getDocHeight() {
    var D = document;
    return Math.max(
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}

function getScrollTop() {
    var scrollTop = document.body.scrollTop;
    if (scrollTop == 0) {
        if (window.pageYOffset)
            scrollTop = window.pageYOffset;
        else
            scrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
    }

    return scrollTop;
}

function getContainerHeight(id, exclusions) {
    var availableHeight = getDocHeight();

    var i;

    if ($(id).get(0) != null) {

        // déterminer d'abord le positionnement TOP de l'objet à ajuster
        var offsetTop = $(id).offset().top;
        // s'il y a des éléments sous l'objet à ajuster dont on ne veut pas tenir compte, soustraire leur hauteur totale
        var excluded_heights = 0;
        if (exclusions != null) {
            for (i = 0; i < exclusions.length; i++) {
                excluded_heights += $(exclusions[i]).height();
            }
        }

        // calculer la pleine hauteur de l'objet en tenant compte des exclusions (retirer 10px au tout.)
        var newHeight = (availableHeight - offsetTop) - excluded_heights - 10;

        // s'assurer que le site ait une hauteur minimale de 600px et une hauteur maximale de 1080px.
        if ((availableHeight >= 600) && (availableHeight <= 1080)) {
            return newHeight;
        }
        else {
            return $(id).height();
        }
    }
}



function MasterControlsController_AfterShowPriceQuote(sender, args) {
    TextZoneAPI.HideTextZone();
}


function priceQuoteTitle_Click(s, e) {
    PriceQuoteAPI.SetFormVisible(false);

    // highlight title
    link_Click(null, ['#pricequote']);
    $("#priceQuoteTitle").css("color", "#FFFFFF");
    document.location.href = "#pricequote";

    if (_selectedLink != null) {
        _selectedLink.className = "";
    }

    MasterControlsController.ShowPriceQuote();

}

// event handling
/*
function AskSyncPriceQuoteHeightWithMenuHeight() {
}
*/

/*
function AskSyncSearchPanelHeightWithMenuHeight() {
}
*/
function AskResetLinkPriceQuote() {
    $("#priceQuoteTitle").css("color", "");
}

function AskShowOnlyCatalog() {
/*
    TextZoneAPI.HideTextZone();

    PriceQuoteAPI.HidePriceQuote();
    SearchAPI.HideSearchControl();
*/
}

function AskHideAllControls() {
/*
    TextZoneAPI.HideTextZone();
    CatalogAPI.HideCatalog();
    SearchAPI.HideSearchControl();

    PriceQuoteAPI.HidePriceQuote();
    */
}

function ShowCatalogChargeurs() {
    $("#themeImage").attr("src", "images/theme-image-01.png");

//    $("#controlFrameCurrentChoice").html("Chargeurs");
//    AskShowOnlyCatalog();

    TextZoneAPI.HideTextZone();
    MasterControlsController.ShowCatalogChargeurs();
    link_Click($('#lnkProduits').get(0), new Array("#navproduits"));
}

function ShowCatalogBatteries(chosenCategory) {
    $("#themeImage").attr("src", "images/theme-image-01.png");

    //    $("#controlFrameCurrentChoice").html("Batteries");
//    AskShowOnlyCatalog();

//    CatalogAPI.ShowCatalogBatteries(chosenCategory);

    TextZoneAPI.HideTextZone();
    MasterControlsController.ShowCatalogBatteries(chosenCategory);
    link_Click($('#lnkProduits').get(0), new Array("#navproduits"));
}

function ShowCatalogPiles(chosenCategory) {
    $("#themeImage").attr("src", "images/theme-image-01.png");

//    $("#controlFrameCurrentChoice").html("Piles");
//    AskShowOnlyCatalog();

    TextZoneAPI.HideTextZone();
    MasterControlsController.ShowCatalogPiles(chosenCategory);
    link_Click($('#lnkProduits').get(0), new Array("#navproduits"));
}

function AskUpdateControlFrame(choice) {
    switch (choice) {
        case "PileJetable":
            $("#controlFrameCurrentChoice").html("Piles jetables");
            break;
        case "PileRechargeable":
            $("#controlFrameCurrentChoice").html("Piles rechargeables");
            break;
        case "BatterieAcide":
            $("#controlFrameCurrentChoice").html("Batteries à l'acide");
            break;
        case "BatterieSlaGelAgm":
            $("#controlFrameCurrentChoice").html("Batteries AGM/SLA/GEL");
            break;
        case "Chargeur":
            $("#controlFrameCurrentChoice").html("Chargeurs");
            break;
        default:
            $("#controlFrameCurrentChoice").html("Sélectionnez une catégorie");
            break;

    }
}

function isUserLoggedIn() {
    return _isUserLoggedIn;
}

function prepareCatalogImageSwap() {
    
    // préparer le swap d'images thématiques lorsque le curseur passe sur un lien de famille
    $('.linkButton a').each(function () {

        var familyId = $(this).siblings("input[type='hidden']").val();

        if (familyId != null) {
            var img = $('.mapper' + familyId);
            var mainImg = img.closest('.divThemeFamilleWrapper').find('.divThemeFamilleMain');

            if (img.get(0) != null) {
                $(this).hover(
                    function () {
                        var t = $.data(document.body, "timer");
                        var oldImg = $.data(document.body, "hoverimg");
                        var oldMainImg = $.data(document.body, "mainimg");

                        if (t != null) {
                            if (mainImg.attr('id') != oldMainImg.attr('id')) {
                                oldMainImg.stop(true, true).fadeIn(500);
                            }
                            window.clearTimeout(t);
                            t = null;
                        }

                        if (oldImg != null) {
                            oldImg.stop(true, true).fadeOut(500);
                        }

                        mainImg.stop(true, true).fadeOut(500);
                        img.stop(true, true).fadeIn(500);
                    },
                    function () {
                        t = window.setTimeout(function () {
                            mainImg.stop(true, true).fadeIn(500);
                            img.stop(true, true).fadeOut(500);
                            t = null;
                        }, 1000);

                        $.data(document.body, "timer", t);
                        $.data(document.body, "hoverimg", img);
                        $.data(document.body, "mainimg", mainImg);

                    }
                );
            }

        }

    });

}


$(document).ready(function () {
    MasterControlsController.bind('AfterShowPriceQuote', MasterControlsController_AfterShowPriceQuote);
});





