
var defaultsZoomIci = {
    MoyenneLargeur: 350,
    MoyenneHauteur: 438,
    Image_width: 350,
    Image_height: 438,
    Zoom_width: 350,
    Zoom_height: 438,
    Image_Zoom_height: 876,
    Image_Zoom_width: 700,
    Video_width: 660,
    Video_height: 390,
    calageLeft: 0,
    calageTop: 0,
    Coef_reduc: 0,
    offsetX: 0,
    offsetY: 0,
    waitbackgroundPosition: '50% 50%',
    wait: 'url(/Image/WEB/loading.gif)',
    Recentrage_Image_Zoom_top: 0,
    Recentrage_Image_Zoom_left: 0,
    mouseposx: '',
    mouseposy: '',
    cadre_width: '',
    cadre_height: ''
};

var OptionsZoomIci = { calageLeft: 2, calageTop: -1 };
OptionsZoomIci = $.extend(defaultsZoomIci, OptionsZoomIci);
OptionsZoomIci.cadre_width = parseInt(OptionsZoomIci.Image_width * OptionsZoomIci.Zoom_width / OptionsZoomIci.Image_Zoom_width);
OptionsZoomIci.cadre_height = parseInt(OptionsZoomIci.Image_height * OptionsZoomIci.Zoom_height / OptionsZoomIci.Image_Zoom_height);

/* test si une seule photo et btn zoom */

$(document).ready(function () {

    var num_img = $("#detailArticl_Galr ul > li").size();
    num_class = $("#detailArticl_img ul > li").attr("class");
    if (num_img == 1) {
        $("#bloc_DA_vignettes").css("display", "none");
    };

    if (num_class == 'NoZoom') {
        $("#bloc_DA_fichier").css("display", "none");
    };
});

function MontrerGalery(Num) {
    zoomIci_Out();
    //cacher tous 
    $(".detailArticl_img li:visible").each(function () { $(this).hide(); });
    $(".detailArticl_Galr li.selectImg").each(function () { $(this).removeClass("selectImg"); });
    $(".detailArticl_img li:eq(" + Num + ")").show();
    $(".detailArticl_Galr li:eq(" + Num + ")").addClass("selectImg");
}

function zoomIci_init() {
    try {
        $('#zoomx-visuel').css({ "left": OptionsZoomIci.calageLeft + OptionsZoomIci.MoyenneLargeur, "top": OptionsZoomIci.calageTop - OptionsZoomIci.MoyenneHauteur });
        $('#detailArticl_img').live('mousemove', function (e) {
            captureMouseMove(e.pageX, e.pageY);
        });
        $('#RepereZoom').css({ "width": OptionsZoomIci.cadre_width, "height": OptionsZoomIci.cadre_height });
    }
    catch (e) {
        //alert(e.message);
    }
}

function zoomIci_In(typeZoom) {
    //recherche url
    try {
        var URLImgZoom;

        $(".detailArticl_img li").each(function () {
            if ($(this).is(":visible")) {
                if ($(this).hasClass('NoZoom')) { URLImgZoom = false } else { URLImgZoom = $(this).children("a").attr("href"); }
            }
        })

        if (typeZoom == 'XL') {
            if (URLImgZoom != false) { ZoomPleinePage(URLImgZoom); } /*zoom XL*/
        }
        else {
            if (URLImgZoom != false) { ImageChargee(URLImgZoom); }   /*sur place*/
        }
    }
    catch (e) {
        //alert(e.message);
    }
}

function ImageChargee(imgSrc) {
    var ImageXL = new Image();
    ImageXL.src = imgSrc;
    $('.picto_zoom').css('display', 'block');
    $('#RepereZoom').show();
    $("#zoomx-visuel").show();
    $("#zoomx-visuel").css({ "backgroundImage": OptionsZoomIci.wait });

    if (ImageXL.width == 0) { //si l'image est nouvelle
        ImageXL.onload = function () { //apres chargement
            if (ImageXL.width > 1) {
                // lancer zoom
                $("#zoomx-visuel").css({ "backgroundImage": 'url(' + imgSrc + ')' });
                OptionsZoomIci.Recentrage_Image_Zoom_top = (OptionsZoomIci.Image_Zoom_height - ImageXL.height) / 2; // preparer recentrage
                OptionsZoomIci.Recentrage_Image_Zoom_left = (OptionsZoomIci.Image_Zoom_width - ImageXL.width) / 2;
            }
            else {
                //mettre au besoin un message Image Zoom non Dispo
            } 
        }
    }
    else //image connue
    {
        if (ImageXL.width > 1) {
            // lancer zoom
            $("#zoomx-visuel").css({ "backgroundImage": 'url(' + imgSrc + ')' });
            OptionsZoomIci.Recentrage_Image_Zoom_top = (OptionsZoomIci.Image_Zoom_height - ImageXL.height) / 2; // preparer recentrage
            OptionsZoomIci.Recentrage_Image_Zoom_left = (OptionsZoomIci.Image_Zoom_width - ImageXL.width) / 2;
        }
        else {
            //mettre au besoin un message Image Zoom non Dispo
        }
    }
}

function captureMouseMove(Ozmouseposx, Ozmouseposy) {

    OptionsZoomIci.offsetX = $('#detailArticl_img').offset().left;
    OptionsZoomIci.offsetY = $('#detailArticl_img').offset().top;

    if ((OptionsZoomIci.Image_Zoom_height * 100 / OptionsZoomIci.Image_height) > (OptionsZoomIci.Image_Zoom_width * 100 / OptionsZoomIci.Image_width)) {
        OptionsZoomIci.Coef_reduc = Math.floor(OptionsZoomIci.Image_Zoom_height * 100 / OptionsZoomIci.Image_height) / 100;
    }
    else {
        OptionsZoomIci.Coef_reduc = Math.floor(OptionsZoomIci.Image_Zoom_width * 100 / OptionsZoomIci.Image_width) / 100;
    }

    //Position de la souris
    x = Ozmouseposx - OptionsZoomIci.offsetX;
    y = Ozmouseposy - OptionsZoomIci.offsetY;

    var markerX = 0;
    var markerY = 0;

    if (x <= (-1) || x >= (OptionsZoomIci.Image_width + OptionsZoomIci.cadre_width / 2) || y <= (-1) || y >= (OptionsZoomIci.Image_height + OptionsZoomIci.cadre_height / 2)) return;

    if (x <= (-1 + OptionsZoomIci.cadre_width / 2)) {
        markerX = 0;
    } else if (x >= (OptionsZoomIci.Image_width - 2 - OptionsZoomIci.cadre_width / 2)) {
        markerX = (OptionsZoomIci.Image_width - 2 - OptionsZoomIci.cadre_width);
    } else {
        markerX = (x - OptionsZoomIci.cadre_width / 2);
    }

    if (y <= (-1 + OptionsZoomIci.cadre_height / 2)) {
        markerY = 0;
    } else if (y >= (OptionsZoomIci.Image_height - 2 - OptionsZoomIci.cadre_height / 2)) {
        markerY = (OptionsZoomIci.Image_height - 2 - OptionsZoomIci.cadre_height);
    } else {
        markerY = (y - OptionsZoomIci.cadre_height / 2);
    }

    x2 = -(parseInt(markerX)) * OptionsZoomIci.Coef_reduc + OptionsZoomIci.Recentrage_Image_Zoom_left;
    y2 = -(parseInt(markerY)) * OptionsZoomIci.Coef_reduc + OptionsZoomIci.Recentrage_Image_Zoom_top;

    $('#RepereZoom').css({ "left": Number(markerX) + "px" });
    $('#RepereZoom').css({ "top": Number(markerY) + "px" });

    if (document.getElementById("zoomx-visuel").style.backgroundImage != OptionsZoomIci.wait)
        document.getElementById('zoomx-visuel').style.backgroundPosition = x2 + "px " + y2 + "px";
    else
        document.getElementById('zoomx-visuel').style.backgroundPosition = OptionsZoomIci.OptionsZoomIci.waitbackgroundPosition;
}

function zoomIci_Out() {

    if ($('#zoomx-visuel').length) $("#zoomx-visuel").hide();
    if ($('#RepereZoom').length) $("#RepereZoom").hide();
}

function ZoomPleinePage(urlImage) {

    zoomIci_Out();
    if (jQuery.browser.msie) { $('html').css({ "overflow": "hidden" }) } else { $('body').css({ "overflow": "hidden" }) }
    if (!$('#ZoomXL').length) { $('BODY').append("<div id='ZoomXL'></div>"); }
    if (($('#ZoomXL').html().length < 1)) {
        $('#ZoomXL').css({ "position": 'absolute',
            "top": $('#detailArticl_img').offset().top + 'px',
            "left": $('#detailArticl_img').offset().left + 'px',
            "width": OptionsZoomIci.MoyenneLargeur + 'px',
            "height": OptionsZoomIci.MoyenneHauteur + 'px',
            "display": 'none',
            "border": '1px solid #ccc',
            "backgroundImage": OptionsZoomIci.wait
        });
        $('#ZoomXL').fadeIn('slow', function () {
            $('#ZoomXL').css({ "backgroundImage": "none" });
            $('#ZoomXL').animate({
                left: '20',
                top: $("html").scrollTop() + $("body").scrollTop() + 20,
                width: $(window).width() - 42,
                height: $(window).height() - 42
            }, 1000, function () {
                chargerBoutonFermer();
                $('#ZoomXL').append("<div id='GrandeImageDrag' style='display:none' ><img id='ImgXl' class='ImgXl' style='cursor: move;' src='" + urlImage + "' /></div>");
                $('#GrandeImageDrag').css({ width: $('#ZoomXL').width(), textAlign: 'center' });
                $('#GrandeImageDrag').fadeIn('slow', function () {
                    $('#GrandeImageDrag').easydrag();
                    if (!$('#ZoomXLBack').length) { //garantir arrire plan blanc
                        $('BODY').append("<div id='ZoomXLBack' style='display:none'></div>");
                    }
                    $('#ZoomXLBack').css({ opacity: 0, display: "block" });
                    $('#ZoomXLBack').animate({ opacity: 0.8 }, '200');
                });
            });
        });

    }
    else {
        $('#ZoomXL').html("<div id='GrandeImageDrag' style='display:none'><img id='ImgXl' class='ImgXl' style='cursor: move;' src='" + urlImage + "'  /></div>");
        $('#GrandeImageDrag').css({ width: $('#ZoomXL').width(), textAlign: 'center' });
        $('#GrandeImageDrag').fadeIn('slow', function () {
            $('#GrandeImageDrag').easydrag();
        });
    }
}

function chargerBoutonFermer() {

    //bouton
    if (!$('#ZoomXLClose').length) { $('BODY').append("<div id='ZoomXLClose' style='display:none'></div>"); }
    $('#ZoomXLClose').click(function () { fermerZoomXXL(); });
    $('#ZoomXLClose').css({ "top": $("html").scrollTop() + $("body").scrollTop() + 25 + "px" });
    $('#ZoomXLClose').fadeIn('400');

    //vignettes
    if (!$('#ZoomXLVignettes').length) { $('BODY').append("<div id='ZoomXLVignettes' style='display:none'></div>"); }
    $('#ZoomXLVignettes').html($('#detailArticl_Galr').html());
    $('#ZoomXLVignettes').css({ "top": $("html").scrollTop() + $("body").scrollTop() + 40 + "px" });
    $('#ZoomXLVignettes ul').css({ width: 'auto' });
    $('#ZoomXLVignettes').fadeIn('400');
    listenKeyXL(27, "fermerZoomXXL()"); //fermer au clavier

}

function fermerZoomXXL() {
    $('#ZoomXL').fadeOut('400', function () {
        $('#ZoomXL').html("");
    });
    $('#ZoomXLBack').fadeOut('500');
    $('#ZoomXLVignettes').fadeOut('500');
    $('#ZoomXLClose').fadeOut('500');
    if (jQuery.browser.msie) { $('html').css({ "overflow": "auto" }) } else { $('body').css({ "overflow": "auto" }) }
}

function listenKeyXL(num, action) { //execute action si la touche num est activée
    $(window).keydown(function (e) {
        switch (e.keyCode) {
            case num:
                jQuery.globalEval(action);
                break;
        }
    });

}

function VideoIci(video) {
    zoomIci_Out();

    if (!$('#VideoIci').length) {
        $('.detailArticl_RefZoom').append("<div id='VideoIci' style='display:none'></div>");
        $('#VideoIci').css({
            "position": "absolute",
            "top": -OptionsZoomIci.MoyenneHauteur,
            "left": 0,
            "width": OptionsZoomIci.MoyenneLargeur,
            "height": OptionsZoomIci.MoyenneHauteur,
            "backgroundImage": OptionsZoomIci.wait
        });
    }
    else {
        $('#VideoIci').css({
            "width": OptionsZoomIci.MoyenneLargeur,
            "height": OptionsZoomIci.MoyenneHauteur,
            "backgroundImage": OptionsZoomIci.wait
        });
    }

    $('#VideoIci').fadeIn('slow', function () {
        $('#VideoIci').animate({
            width: OptionsZoomIci.Video_width,
            height: OptionsZoomIci.Video_height
        }, 1000, function () {
            chargerFlash(video);
            if (!$('#VideoIciClose').length) { $('.detailArticl_RefZoom').append("<div id='VideoIciClose' style='display:none'></div>"); }
            $('#VideoIciClose').click(function () { $('#VideoIci').fadeOut('300'); $('#VideoIciClose').fadeOut('300'); });
            $('#VideoIciClose').css({
                top: -OptionsZoomIci.MoyenneHauteur + 1,
                left: OptionsZoomIci.Video_width + 1
            });
            $('#VideoIciClose').fadeIn('400');

        });

    });

}

function chargerFlash(flashVideo) {
    var so = new SWFObject('/flash/Player.swf', flashVideo, OptionsZoomIci.Video_width, OptionsZoomIci.Video_height, '9');
    so.addParam('allowscriptaccess', 'always');
    so.addParam('allowfullscreen', 'true');
    so.addVariable("file", "/File.aspx?FileName=" + flashVideo);
    so.addVariable("displayclick", "fullscreen");
    so.addVariable('autostart', 'true');
    so.addVariable('controlbar', 'over');
    so.write('VideoIci');
}

function lancerGallery() {
    MontrerGalery(0);
    zoomIci_init();
}

		
	
