/****************************************************************************************************************
Cette fonction ne sert pas, mais ça pourrait changer
****************************************************************************************************************/

function crossBrowserWindowHeight()
{
    var myWidth = 0, myHeight = 0;
    if (typeof (window.innerWidth) == 'number')
    {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
    {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight))
    {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
    return myHeight;
}

/****************************************************************************************************************
Deprecated Popin load
****************************************************************************************************************/

function loadPopinOld(externalLink, type, closeWording)
{
    // document height for the mask
    myHeight = document.body.offsetHeight + 'px';
    // myHeight = crossBrowserWindowHeight()+'px';

    if ($.browser.msie)
    {
        $("select").css("visibility", "hidden"); // virer les selects pour IE
        $('body #masque, body #popinContainer').remove(); // gros bug aberrant IE qui comprend pas $('#popinContainer, #masque').remove();
    }

    // container and mask
    $("body").append('<div id="masque"></div><div id="popinContainer" class="' + type + '"><a href="#" id="closeButton">' + closeWording + '</a><div id="popinContentTop"></div><div id="popinContent"><div id="loader"><p>Chargement en cours...</p></div></div><div id="popinContentBtm"></div></div>');
    $("#masque").show();
    $("#masque").css("height", myHeight);


    // external HTML load
    $('#popinContainer #popinContent').load(externalLink, function()
    {

        //EXP-LMA : Remplissage du password/remember
        var popinEmail = $("#popinEmail").val();
        var popinPassword = $("#popinMotDePasse").val();
        var popinRetenir = $("#retenir").attr("checked");

        var tmp = $('#popinContainer #popinContent #popinContentToLoad').clone();

        $('#popinContainer #popinContent').empty();
        $('#popinContainer #popinContent').append(tmp);

        // forms stuff
        // initForms();
        initCheck();

        // Close buttons
        $('#closeButton, #masque').click(function()
        {

            $('#popinContainer, #masque').remove();

            if ($.browser.msie) { $("select").css("visibility", "visible"); } // remettre les selects pour IE
            return false;

        });

        //EXP-LMA : Remplissage du password/remember
        $("#popinEmail").val(popinEmail);
        $("#popinMotDePasse").val(popinPassword);
        $("#retenir").attr("checked", popinRetenir);
    });
}

/****************************************************************************************************************
Deprecated Popin init
****************************************************************************************************************/

function initPopin()
{

    $('.popin').click(function()
    {

        // on utilise les infos passées dans l'attribut "rel" du lien, à renseigner comme il faut donc !
        loadPopinOld($(this).attr('href'), $(this).attr('rel').split('-')[0], $(this).attr('rel').split('-')[1])
        $(this).blur();
        return false;

    });
}


/****************************************************************************************************************
Popin Load
****************************************************************************************************************/

function loadPopin(idControl)
{
    if ($.browser.msie)
    {
        $("select").css("visibility", "hidden"); // virer les selects pour IE
        $('body #masque').remove(); // gros bug aberrant IE qui comprend pas $('#popinContainer, #masque').remove();
    }

    myHeight = document.body.offsetHeight + 'px';
    // myHeight = crossBrowserWindowHeight()+'px';

    $("body").prepend('<div id="masque"></div>');
    $("#masque").show();
    $("#masque").css("height", myHeight);
    $(idControl).show();

    initCheck();

    //Stop caroussels
    articleStopScroll();
    serviceStopScroll();

    // Close buttons
    $(idControl + ' #closeButton, #masque').click(function() {
        //restart caroussel
        articleStartScroll("false");
        serviceStartScroll("false");

        $('#masque').remove();
        $(idControl).hide();

        if ($.browser.msie) { $("select").css("visibility", "visible"); } // remettre les selects pour IE
        return false;

    });
}

/****************************************************************************************************************
Popin siteMap
****************************************************************************************************************/

function initSiteMap()
{

    try
    {
        $('#siteMap').hide();

        $('#siteMapLink').click(function()
        {
            loadPopin('#siteMap');
            $(this).blur();
            return false;

        });
    }
    catch (e)
    {

    }

}

/****************************************************************************************************************
Popin Login
****************************************************************************************************************/

function initLogin()
{
    try
    {
        $('#loginContainer').hide();
        $('.loginPopin').click(function()
        {
            loadPopin('#loginContainer');
            $(this).blur();
            return false;
        });
    }
    catch (e)
    {

    }
}

/****************************************************************************************************************
Popin Spread
****************************************************************************************************************/

function initSpread()
{
    try
    {
        $('#spreadContainer').hide();

        $('.spreadPopin').click(function()
        {
            loadPopin('#spreadContainer');
            $(this).blur();
            return false;
        });
    }
    catch (e)
    {

    }
}

/****************************************************************************************************************
onLoad
****************************************************************************************************************/

$(document).ready(function()
{
    //initPopin();
    initSiteMap();
    initLogin();
    initSpread();
});