(function($) {
    var strImgDir = http_path + 'js/fancyzoom/ressources/';

    var http_path_img = http_path + 'js/fancyzoom/img/';

    var oImgZoomBox = $('<div>');

    var div_contenitore = $('<div id="div_contenitore">').css({ 'background-color': 'white' });




    var div_sopra = $('<div id="div_sopra">').css({ overflow: 'hidden', width: '100%', height:'11px' });


    var div_top_sx = $('<div>').css({ height: '11px', 'background-image': 'url(' + http_path_img + 'top_left.gif)', width: '20px', 'float': 'left', 'background-repeat': 'no-repeat' });

    var div_top_center = $('<div>').css({ height: '11px', 'background-color': 'white', 'float': 'left', 'background-repeat': 'no-repeat' })

    var div_top_dx = $('<div>').css({ height: '11px', 'background-image': 'url(' + http_path_img + 'top_right.gif)', width: '20px', 'float': 'left', 'background-repeat': 'no-repeat' });



    var div_sotto = $('<div id="div_sotto">').css({ overflow: 'hidden', width: '100%', height: '11px' });


    var div_bottom_sx = $('<div>').css({ height: '11px', 'background-image': 'url(' + http_path_img + 'bottom_left.gif)', width: '20px', 'float': 'left', 'background-repeat': 'no-repeat' });

    var div_bottom_center = $('<div>').css({ height: '11px', 'background-color': 'white', 'float': 'left', 'background-repeat': 'no-repeat' })

    var div_bottom_dx = $('<div>').css({ height: '11px', 'background-image': 'url(' + http_path_img + 'bottom_right.gif)', width: '20px', 'float': 'left', 'background-repeat': 'no-repeat' });




    var oImgClose = $('<img>').css({ position: 'absolute', top: 0, left: 0, cursor: 'pointer', zIndex: 102 });


    $.fn.fancyzoom = function(userOptions) {
        var oOverlay = $('<div>').css({ height: '100%', width: '100%', position: 'fixed', left: 0, top: 0, cursor: "wait" });

        function ShowMenu() {
            document.getElementById("frTest").style.display = "inline";
        }
        function HideMenu() {
            document.getElementById("frTest").style.display = "none";
        }

        function openZoomBox(imgSrc, o) {

            if (o.showoverlay) {
                oOverlay.prependTo('body').click(function() {
                    closeZoomBox(o);
                });
                if ($.browser.msie && $.browser.version < 7) {
                    oOverlay.css({ position: 'absolute', height: $(document).height(), width: $(document).width() });
                }
            }
            pos = imgSrc.offset();
            o = $.extend(o, { dimOri: { /*width: imgSrc.outerWidth(),*/height: imgSrc.outerHeight(), left: pos.left, top: pos.top, 'opacity': 0} });

            if (o.sfondo_opaco) {
                $('#img_zoom_lente').each(function(i) {
                    var tmp = $(this);
                    tmp = tmp.offset();
                    $(this).css('left', tmp.left + 8);
                });
                var div_finto = $('<iframe id="progressBackgroundFilter" style=" position:absolute; top:0; left:0; background-color:#000; filter:alpha(opacity=30);opacity:0.3; z-index:50;" src="' + http_path + 'img/iframe.htm"></iframe>');
                div_finto.css({ 'height': $(document).height() });
                div_finto.css({ 'width': $(document).width() - 4 });
                if (navigator.appName == "Microsoft Internet Explorer") {
                    div_finto.css({ 'width': $(document).width() });
                }
                div_finto.appendTo('body');
            }



            oImgZoomBox.css({ 'opacity': 0, 'text-align': 'center', 'border': '0px solid red' }).appendTo('body');

            div_top_sx.appendTo(div_sopra);
            div_top_center.appendTo(div_sopra);
            div_top_dx.appendTo(div_sopra);
            div_sopra.prependTo(oImgZoomBox);

            var oImgDisplay = $('img', oImgZoomBox);




            var browserName = navigator.appName;
            oImgZoomBox.css({ 'z-index': 100 });
            var iWidth = oImgZoomBox.outerWidth();
            var iHeight = oImgZoomBox.outerHeight();
            document.getElementById("frTest").style.width = iWidth;
            document.getElementById("frTest").style.height = iHeight;
            dimBoxTarget = $.extend({}, { 'height': iHeight, 'opacity': 1 }, __posCenter((iWidth), (iHeight + 30)));
            //oImgClose.css({ left: (dimBoxTarget.left - 22 + ((dimBoxTarget.width - oImgDisplay.width()) / 2)), top: dimBoxTarget.top });
            oImgClose.css({ top: dimBoxTarget.top - 9 });

            var $fctEnd = function() {
                if (o.Speed > 0 && !$.browser.msie) {
                    oImgClose.fadeIn('slow');
                    $('div', oImgZoomBox).fadeIn('slow');

                    var width_immagine = $('#div_contenitore img').width();
                    oImgZoomBox.width(width_immagine + 20);

                    //impostazione dimensioni div
                    div_top_center.css('width', oImgZoomBox.width() - div_top_sx.width() - div_top_dx.width());
                    div_bottom_center.css('width', oImgZoomBox.width() - div_bottom_sx.width() - div_bottom_dx.width());

                    $('#table_descrizione').attr("width", width_immagine - 10);
                    $('#div_scrollerPane').css('width', width_immagine - 10);

                    oImgZoomBox.css("left", '50%');
                    oImgZoomBox.css("margin-left", -oImgZoomBox.width() / 2);

                    oImgClose.css("left", '50%');
                    oImgClose.css("margin-left", +oImgZoomBox.width() / 2 - 21);

                    crea_scroller();

                }
                else {
                    oImgClose.show();
                    $('div', oImgZoomBox).show();

                    var width_immagine = $('#div_contenitore img').width();
                    oImgZoomBox.width(width_immagine + 20);

                    //impostazione dimensioni div
                    div_top_center.css('width', oImgZoomBox.width() - div_top_sx.width() - div_top_dx.width());
                    div_bottom_center.css('width', oImgZoomBox.width() - div_bottom_sx.width() - div_bottom_dx.width());
                    //oImgClose.css('left', dimBoxTarget.left + oImgZoomBox.width() - 15);

                    $('#table_descrizione').attr("width", width_immagine - 10);
                    $('#div_scrollerPane').css('width', width_immagine - 10);

                    oImgZoomBox.css("left", '50%');
                    oImgZoomBox.css("margin-left", -oImgZoomBox.width() / 2);


                    oImgClose.css("left", '50%');
                    oImgClose.css("margin-left", +oImgZoomBox.width() / 2 - 21);


                    crea_scroller();

                }
            };
            $('div', oImgZoomBox).hide();
            if (o.Speed > 0) {
                oImgZoomBox.css(o.dimOri).animate(dimBoxTarget, o.Speed, $fctEnd);

            } else {
                oImgZoomBox.css(dimBoxTarget);
                $fctEnd();
            }


        }


        //chiusura fancyzoom
        function closeZoomBox(o) {
            $("#progressBackgroundFilter").remove();
            oImgClose.remove();
            div_sopra.empty().remove();
            div_top_sx.empty().remove();
            div_top_center.empty().remove();
            div_top_dx.empty().remove();
            div_contenitore.empty().remove();
            div_sotto.empty().remove();
            div_bottom_sx.empty().remove();
            div_bottom_center.empty().remove();
            div_bottom_dx.empty().remove();
            if ($.browser.msie && $.browser.version < 7) {
                HideMenu();
            }

            if (o.Speed > 0) {
                oImgZoomBox.animate(o.dimOri, o.Speed, function() {
                    oImgZoomBox.css('width', '0');
                    $(this).empty().remove();

                });
                if (o.showoverlay) {
                    oOverlay.animate({ 'opacity': 0 }, o.Speed, function() {
                        oImgZoomBox.css('width', '0');
                        $(this).empty().remove();
                    });
                }
            } else {
                oImgZoomBox.empty().remove();
                if (o.showoverlay) {
                    oOverlay.empty().remove();
                }
            }
        }


        //caricamento iniziale
        return this.each(function() {
            var $this = $(this);
            var imgTarget = $this.is('img') ? $this : $('img:first', $this);
            var imgTargetSrc = null;
            if ($this.attr('href')) {
                imgTargetSrc = $this.attr('href');
            }
            if ($this.is('img')) {
                imgTargetSrc = $this.attr('src'); $this.css('cursor', 'pointer');
            }
            var opts = $.extend({}, $.fn.fancyzoom.defaultsOptions, userOptions || {}, { dimOri: {}, oImgZoomBoxProp: { position: 'absolute', left: 0, top: 0} });
            oOverlay.css({ opacity: opts.overlay, background: opts.overlayColor });
            if (!/\.jpg|\.png|\.gif/i.test(imgTargetSrc) || ($('img', $this).size() === 0 && !$this.is('img'))) { return; }

            $this.click(function() {
                if (oLoading && oLoading.is(':visible') || timerLoadingImg) {
                    if (oImgZoomBox && $('img:first', oImgZoomBox).attr('src') != imgTargetSrc) {
                        __cancelLoading();
                    } else {
                        return false;
                    }
                }

                var o = $.extend({}, opts, userOptions);
                if (oImgZoomBox && oImgZoomBox.parent().size() > 0) {
                    var imCurrent = $('img:first', oImgZoomBox);
                    if (imgTargetSrc == imCurrent.attr('src')) {
                        pos = imgTarget.offset();
                        o = $.extend(o, { dimOri: { width: imgTarget.outerWidth(), height: imgTarget.outerHeight(), left: pos.left, top: pos.top, 'opacity': 0} });
                        closeZoomBox(o);
                        return false;
                    } else {
                        oImgClose.remove();
                        div_sopra.empty().remove();
                        div_top_sx.empty().remove();
                        div_top_center.empty().remove();
                        div_top_dx.empty().remove();
                        div_sopra.empty().remove();
                        div_contenitore.empty().remove();
                        div_sotto.empty().remove();
                        div_bottom_sx.empty().remove();
                        div_bottom_center.empty().remove();
                        div_bottom_dx.empty().remove();
                        oImgZoomBox.empty().remove();
                    }
                }
                if (o.showoverlay && oOverlay) {
                    oOverlay.empty().remove().css({ 'opacity': o.overlay });
                }

                oImgClose.attr('src', /*o.imgDir + 'closebox.gif'*/http_path_img + 'close.gif').appendTo('body').hide();
                if ($.fn.ifixpng) {
                    $.ifixpng(o.imgDir + 'blank.gif');
                    oImgClose.ifixpng(o.imgDir + 'blank.gif');
                }

                oImgClose.unbind('click').click(function() {
                    closeZoomBox(o);
                });

                oImgZoomBox = $('<div>').empty().css(o.oImgZoomBoxProp);
                var browserName = navigator.appName;
                var strTitle = '';
                var strTitle_orig = ' ';
                var strTitle_tmp = ''
                var strNomePorto = '';

                if (!(imgTarget.attr('fullsize') == '')) {
                    strTitle = imgTarget.attr('fullsize');
                    strTitle_orig = imgTarget.attr('fullsize');
                }

                if (imgTarget.attr('nomeporto')) {
                    strNomePorto = imgTarget.attr('nomeporto');
                               } else {
                    strNomePorto = '';
                }

                if (strNomePorto != '') {
                   
                    var div_nomePorto = $('<div id="div_nomePorto">').css({ marginTop: 0, color: '#cc0000', 'z-index': '200', position: 'relative', 'text-align': 'left', 'font-family': 'Arial', 'font-weight': 'bold', 'font-size': '16px', 'margin-left': '15px', 'margin-top': '3px' });
                    div_nomePorto.html(strNomePorto);
                    div_nomePorto.appendTo(div_contenitore);
                }

                if (!(strTitle == ' ') && !(strTitle == '')) {
                    var oTitle = $('<div id="div_oTitle" style="z-index:200; position:relative";><center><table id="table_descrizione" height=0 border="0" cellspacing=0 cellpadding=0><tr><td id="cella_descrizione" class="fancyTitle"><div id="div_scrollerPane" style="height:105px; text-align:left;">' + strTitle + '</div></td></tr></table></center></div>').css({ marginTop: 0 }); // marginRight: 15
                    var tdL = oTitle.find('td:first').css({ 'background': 'url(' + o.imgDir + 'zoom-caption-l.png)', width: '13px', height: '26px' });
                    var tdR = oTitle.find('td:last').css({ 'background': 'url(' + o.imgDir + 'zoom-caption-r.png)', width: '13px', height: '26px' });
                    var tdC = $('.fancyTitle', oTitle).css({ /*'background': 'url(' + o.imgDir + 'zoom-caption-fill.png)'*/'padding': '3px 10px 0px 10px', color: 'black', 'font-size': '11px', 'background-repeat': 'repeat', 'width': '100%', 'text-align': 'left' });

                    if ($.fn.ifixpng) {
                        tdL.ifixpng(o.imgDir + 'blank.gif'); tdR.ifixpng(o.imgDir + 'blank.gif'); tdC.ifixpng(o.imgDir + 'blank.gif');
                    }


                    oTitle.appendTo(div_contenitore);

                    oTitle.click(function(event) {
                        if (browserName == "Microsoft Internet Explorer") {
                            if (event.srcElement.id == 'span_continua') {
                                $('#cella_descrizione').html(imgTarget.attr('fullsize') + ' <span id="span_riduci" class="link_continua_descrizione">Riduci</span>');
                            }
                            if (event.srcElement.id == 'span_riduci') {
                                $('#cella_descrizione').html(strTitle);
                            }
                        }
                        else {
                            if (event.target.id == 'span_continua') {
                                $('#cella_descrizione').html(imgTarget.attr('fullsize') + ' <span id="span_riduci" class="link_continua_descrizione">Riduci</span>');
                            }
                            if (event.target.id == 'span_riduci') {
                                $('#cella_descrizione').html(strTitle);
                            }
                        }
                        oImgZoomBox.height(oImgZoomBox.height() + 15);
                    });
                }

                div_contenitore.prependTo(oImgZoomBox);

                div_bottom_sx.appendTo(div_sotto);
                div_bottom_center.appendTo(div_sotto);
                div_bottom_dx.appendTo(div_sotto);

                div_sotto.appendTo(oImgZoomBox);

                var oImgZoom = $('<img />').attr('src', imgTargetSrc).css({ 'cursor': 'pointer', zIndex: 100, marginTop: -3, 'padding': '1px', 'border': '1px solid white' }).click(function() {
                    closeZoomBox(o);
                }).appendTo(div_contenitore);




                var imgPreload = new Image();
                if ($.browser.msie && $.browser.version < 7) {
                    ShowMenu();
                }

                imgPreload.src = imgTargetSrc;

                var $fctEndLoading = function() {
                    if (bCancelLoading) {
                        bCancelLoading = false;
                    } else {
                        if (__getFileName(imgPreload.src) == __getFileName($('img:first', oImgZoomBox).attr('src'))) {
                            fctCalculateImageSize();
                            openZoomBox(imgTarget, o);
                            __stoploading();
                        }
                    }
                };

                var fctCalculateImageSize = function() {
                    var divCalculate = $('<div></div>').css({ position: 'absolute', 'top': 0, 'left': 0, opacity: 0, 'border': '0px solid red', 'z-index': 100 });
                    oImgZoom.appendTo(divCalculate);
                    divCalculate.appendTo('body');
                    imWidth = oImgZoom.width();
                    imHeight = oImgZoom.height();
                    maxWidth = $(window).width() * 0.9;
                    maxHeight = $(window).height() * 0.8;

                    if (maxHeight < imHeight) {
                        oImgZoom.css('height', maxHeight);
                    } else if (maxWidth < imWidth) {
                        oImgZoom.css('width', maxWidth);
                    }
                    divCalculate.remove();
                    oImgZoom.click(function() { closeZoomBox(o); });
                    oImgZoom.prependTo(div_contenitore);

                };
                if (imgPreload.complete) {
                    fctCalculateImageSize();
                    openZoomBox(imgTarget, o);
                }
                else {
                    __displayLoading();
                    imgPreload.onload = function() {
                        $fctEndLoading();
                    };
                }

                return false;
            });
        });


    };

    $.fn.fancyzoom.defaultsOptions = { overlayColor: '#000', overlay: 0.6, showoverlay: false, Speed: 400, shadowOpts: { color: "#000", offset: 4, opacity: 0.2 }, imgDir: strImgDir, sfondo_opaco: true };

    function __posCenter(iWidth, iHeight) {

        var iLeft = ($(window).width() - iWidth) / 2 + $(window).scrollLeft();
        var iTop = ($(window).height() - iHeight) / 2 + $(window).scrollTop();
        iLeft = (iLeft < 0) ? 0 : iLeft; iTop = (iTop < 0) ? 0 : iTop;
        if ($.browser.msie && $.browser.version < 7) {
            document.getElementById("frTest").style.position = 'absolute';
            document.getElementById("frTest").style.left = iLeft;
            document.getElementById("frTest").style.top = iTop;
        }
        return {
            left: iLeft, top: iTop
        };

    }

    var oLoading = null;
    var bCancelLoading = false;
    var timerLoadingImg = null;

    function __displayLoading() {
        if (!oLoading) {
            oLoading = $('<div></div>').css({ width: 50, height: 50, position: 'absolute', 'background': 'transparent', opacity: 8 / 10, color: '#FFF', padding: '5px', 'font-size': '10px' });
        }

        oLoading.css(__posCenter(50, 50)).html('<img src="' + $.fn.fancyzoom.defaultsOptions.imgDir + 'blank.gif" />').click(function() {
            __cancelLoading();
        }).appendTo('body').show();
        timerLoadingImg = setTimeout(__changeimageLoading, 400);
    }


    function __cancelLoading() {
        bCancelLoading = true; __stoploading();
    }

    function __stoploading() {
        oLoading.hide().remove();
        if (timerLoadingImg) {
            clearTimeout(timerLoadingImg);
            timerLoadingImg = null;
        }


    }


    function __changeimageLoading() {
        if (!oLoading.is(':visible')) {
            timerLoadingImg = null; return;
        }
        var $im = $('img', oLoading);
        if (!$im.attr('src') || /blank\.gif/.test($im.attr('src'))) {
            strImgSrc = $.fn.fancyzoom.defaultsOptions.imgDir + "zoom-spin-1.png";
        } else {
            tab = $im.attr('src').split(/[- .]+/);
            iImg = parseInt(tab[2]);
            iImg = (iImg < 12) ? (iImg + 1) : 1; strImgSrc = tab[0] + "-" + tab[1] + "-" + iImg + "." + tab[3];
        }

        var pLoad = new Image();
        pLoad.src = strImgSrc;
        var $fct = function() {
            oLoading.css(__posCenter(50, 50));
            $im.attr('src', strImgSrc);
            timerLoadingImg = setTimeout(__changeimageLoading, 100);
        };
        if (pLoad.complete) {
            $fct();
        } else {
            pLoad.onload = $fct;
        }


    }

    function __getFileName(strPath) {
        if (!strPath) {
            return false;
        }
        var tabPath = strPath.split('/');

        return ((tabPath.length < 1) ? strPath : tabPath[(tabPath.length - 1)]);
    }


})(jQuery);


//funzione che ctrea scroller sul div "div_scrollerPane"
function crea_scroller() {
    $('#div_scrollerPane').jScrollPane();
}