﻿/*____________________________________________
|     _     _ ___      __ _                   |
|    (_)___(_) __|___ / _| |_ __ _ _ _ ___    |
|    | |_ /| \__ | _ \  _|  _/ _` | '_/ -_)   |
|    |_/__||_|___|___/_|  \__\__,_|_| \___|   |
|                                             |
|       ©2010 iziSoftware - Version 1.0       |
|_____________________________________________|


WELightBox1 : gestion d'une fenêtre modale

*/

$(document).ready(function() {
    WELightBox1.Initialize()
});

var WELightBox1 = {
    Initialize: function() {
        if (WEInfoPage.RenderMode == "Editor") {
            return false
        }
        WELightBox1.listImg = new Array;
        // $('body').append('<div id="OELightBox1_fade"></div>');

        for (Elem in OEConfWELightBox1) {

            var $Elem = $("#" + Elem);
            $Elem.css('z-index', '100000') //Au dessus des autres éléments
            $Elem.append('<div class="OELightBox1_fade"></div>');
            $('#' + Elem + ' .OESZ_ContentLightBox').append('<div class="OESZ_LeaveLightBox OELightBox1_Buttons"></div>')
            $('#' + Elem + ' .OESZ_ContentLightBox').append("<div class='OESZ_GoLeftLightBox OELightBox1_Buttons' ></div>").append('<img />').append("<div class='OESZ_GoRightLightBox OELightBox1_Buttons'></div>")
            $Elem.find('.OESZ_DivContent').css('z-index', '100000'); //Correction bug IE 7
            WELightBox1.Init(OEConfWELightBox1[Elem].ConfigLightBox1, Elem);
        }
    },

    //Initialisation de la fenêtre modale
    Init: function(OEConfListImg, IDElem) {
        for (index in OEConfListImg) {

            var IDImg = OEConfListImg[index].IDElement.List[0]
            var $image = $("#" + IDImg);
            $image.css("cursor", "pointer");
            //On supprime les événements associés (si plusieurs images sont liées à un même élément sur la page
            $image.unbind();
            //on associe l'événement à l'élément
            $image.bind('click', { msg: index }, function(event) {
                WELightBox1.showPic(OEConfListImg, event.data.msg, IDElem);
                return false;
            });
        }
    },

    updatePic: function(OEConfListImg, index, IDElem) {
        var LightBox = $('#' + IDElem + ' .OESZ_ContentLightBox');
        var ZoomLink = escape(WEEdSiteCommon.LinkGetPath(OEConfListImg[index].ZoomLink));
        var Title = WEEdSiteCommon.GetLocalizableString(OEConfListImg[index].Title);
        var Overview = WEEdSiteCommon.GetLocalizableString(OEConfListImg[index].Overview);
        var URL = WEEdSiteCommon.LinkGetPath(OEConfListImg[index].URL);
        var ZoomWidth = OEConfListImg[index].ZoomWidth;
        var ZoomHeight = OEConfListImg[index].ZoomHeight;
        var MaxIndex = OEConfListImg.length;

        //calcul de la taille de l'image
        var widthPercent = 100;
        var heightPercent = 100;
        var percent;
        if (ZoomWidth + 80 >= $(window).width())
            widthPercent = ($(window).width()) / ZoomWidth * 100;
        if (ZoomHeight + 80 >= $(window).height() - 300)
            heightPercent = ($(window).height() - 300) / ZoomHeight * 100;
        if (widthPercent < heightPercent)
            percent = widthPercent
        else
            percent = heightPercent;

        var newHeight = ZoomHeight * percent / 100;
        var newWidth = ZoomWidth * percent / 100;

        //suppression des anciennes informations
        LightBox.children('.OESZ_TitleLightBox').remove();
        LightBox.children('.OESZ_OverviewLightBox').remove();
        LightBox.children('a').remove();

        //Remplacement de l'image
        LightBox.children("img").first().remove();
        LightBox.append("<img src='" + ZoomLink + "' width='" + newWidth + "' height='" + newHeight + "'/>");
        var $Image = LightBox.children("img").first();
        $Image.load(function() {
            //$Image.attr('src', ZoomLink).ready(function (){

            //repositionnement de la box
            LightBox.animate({
                'margin-top': 50,
                'margin-left': ($('#XBody').width() - (newWidth + 60)) / 2
            }, 'normal');
            $Image.animate({ height: newHeight, width: newWidth }, 'normal', function() {

                //Mise à jour des informations de l'image
                if (Title)
                    LightBox.append("<div class='OESZ_TitleLightBox'>" + Title + "</div>");
                if (Overview)
                    LightBox.append("<div class='OESZ_OverviewLightBox'>" + Overview + "</div>");
                var $link = $("<a href='"+URL+"'>" + URL + "</a>");
                if (URL) {
                    LightBox.append($link);
                    $link.bind('click', function() {
                        WEEdSiteCommon.OpenWindow(URL);
                        return false;
                    });
                }
                return false;
            });
            return false;
            //});
        });
    },

    showPic: function(OEConfListImg, index, IDElem) {
        var MaxIndex = OEConfListImg.length;
        var LightBox = $('#' + IDElem + ' .OESZ_ContentLightBox');
        var $Image = LightBox.children('img').first();
        //DEfinition des différentes zones de la fenetre modale
        var $Exit = $('.OESZ_LeaveLightBox');

        //Déclaration des actions de sortie de la fenêtre modale
        $Exit.add($Image).bind('click', function() {
            $('#' + IDElem + ' .OELightBox1_fade').fadeOut();
            $('#' + IDElem + ' .OESZ_ContentLightBox').fadeOut();
            return false;
        });

        //Ajout de la zone de style contenant le bouton de sortie de la fenêtre modale
        LightBox.prepend($Exit).append($Image)
        WELightBox1.updatePic(OEConfListImg, index, IDElem);

        //Affichage des flèches de navigation si il y a plusieurs images
        var $GoLeft = $(".OESZ_GoLeftLightBox");
        var $GoRight = $(".OESZ_GoRightLightBox");
        if (MaxIndex > 1) {


            // Effet over pour les boutons gauche et droite
            LightBox.bind('mouseover.CssEvents', function() { $GoLeft.show(); $GoRight.show() });
            LightBox.bind('mouseleave.CssEvents', function() { $GoLeft.hide(); $GoRight.hide() });

            //Evénements associés	
            $GoLeft.unbind();
            $GoLeft.bind('click', function() {
                if (index > 0)
                    index = index - 1;
                else
                    index = MaxIndex - 1;
                WELightBox1.updatePic(OEConfListImg, index, IDElem);
                return false;
            });
            $GoRight.unbind();
            $GoRight.bind('click', function() {
                if (index < MaxIndex - 1)
                    index = parseInt(index) + 1;
                else
                    index = 0;
                WELightBox1.updatePic(OEConfListImg, index, IDElem);
                return false;
            });
        }
        else {
            $GoRight.hide();
            $GoLeft.hide();
        }

        LightBox.fadeIn();
        $('#' + IDElem + ' .OELightBox1_fade').css({ 'filter': 'alpha(opacity=80)' }).fadeIn('slow');
    }
}
