﻿//javascript


function ZoomFoto(UrlImg) {
    var Bl = $('#ZfBlanker');
    if (Bl.length==0) {
        $('body').append('<div id="ZfBlanker"></div>');
        Bl = $('#ZfBlanker');
        Bl.css("position", "absolute");
        Bl.css("top", "0px");
        Bl.css("left", "0px");
        Bl.css("width", "100%");
        Bl.css('opacity', 0.6);
    }
    var MaxZ = MaxZIndex() + 10;
    var bz = GetBodySize();
    Bl.css("height", bz.height + "px");
    Bl.css("z-index", MaxZ);
    Bl.css("display", "block");

    var win = GetVisWin();
    var WinH = win.height;
    var WinW = win.width;

    $('body').append('<div id="ZfWaiting"><img src="images/waiting.gif" /></div>');
    var Wait = $("#ZfWaiting");
    var WT = (WinH - 22) / 2;
    WT += $(window).scrollTop();
    var WL = (WinW - 220) / 2;
    Wait.css("position", "absolute");
    Wait.css("width", 220 + "px");
    Wait.css("height", 22 + "px");
    Wait.css("left", WL + "px");
    Wait.css("top", WT + "px");
    Wait.css("z-index", MaxZ + 15);

    
    var img = new Image();
    img.onload = function() {
        var ImgH = img.height;
        var ImgW = img.width;
        var ZfFoto = $('#ZfFoto');
        if (ZfFoto.length == 0) {
            $('body').append('<div id="ZfFoto"></div>');
            ZfFoto = $('#ZfFoto');
            ZfFoto.css("position", "absolute");
        }
        Wait.remove();

        var GlW = ImgW + 10;
        var GlH = ImgH + 10;
        var GlT = (WinH - GlH) / 2;
        if (GlT < 10) GlT = 10;
        GlT += $(window).scrollTop();

        var GlL = (WinW - GlW) / 2;
        if (GlL < 10) GlL = 10;

        ZfFoto.css("width", GlW + "px");
        ZfFoto.css("height", GlH + "px");
        ZfFoto.css("left", GlL + "px");
        ZfFoto.css("top", GlT + "px");
        ZfFoto.css("z-index", MaxZ + 10);

        ZfFoto.append('<img id="ZfImg" src="' + img.src + '" />');
        var XImg = $("#ZfImg");
        XImg.css("margin", "5px 0px 0px 5px");
        XImg.bind("click", function() {
            ZfFoto.remove();
            Bl.css("display", "none");
        });

    };
    img.onerror = function() {
        alert('ko');
        Wait.remove();
        Bl.css("display", "none");
    }
    img.src = UrlImg;
}

//---------------------------------------------------------------------------------------------------
function GetBodySize() {
    var winW = $(window).width();
    var winH = $(window).height();
    var bodyW = $('body').width();
    var bodyH = $('body').height();
    var x = winW > bodyW ? winW : bodyW;
    var y = winH > bodyH ? winH : bodyH;
    return { width: x, height: y }
}

//---------------------------------------------------------------------------------------------------
function GetVisWin() {
    var winW = $(window).width();
    var winH = $(window).height();
    var bodyW = 0;
    var bodyH = 0;
    var x = winW > bodyW ? winW : bodyW;
    var y = winH > bodyH ? winH : bodyH;
    return { width: x, height: y }
}

//---------------------------------------------------------------------------------------------------
function MaxZIndex() {
    var maxZ = 1000;
    var x = $('body > div');
    x.each(
        function() {
            var x = parseInt($(this).css('z-index'), 10);
            if (!isNaN(x)) maxZ = Math.max(maxZ, x);
        }
    );
    return maxZ;
}


var MyGallery = function(elem, base) {
    this.Elem = elem;
    this.Base = base;
    this.Gl = undefined;
    this.Bl = undefined;
    this.Opacity = 0.6;
    this.BoxNavH = 30;
    this.BoxNavW = 30;
    this.BoxCloseH = 25;
    this.BoxCloseW = 25;

    this.init();
    
    this.SelUrlFoto = '';
    this.SelNomeFoto = '';
    this.SelDescFoto = '';
    this.SelFotoPrec = -1;
    this.SelFotoSucc = -1;
    this.Border = 8;
    this.InfoH = 70;
    this.BoxCloseOut = 7;
    this.BoxNavOut = 20;
}


MyGallery.prototype = {

    init: function() {
        var MaxZ = this.MaxZIndex() + 10;

        $("body").append('<div id="GlBlanker" style="display: none;"></div>');
        this.Bl = $("#GlBlanker");
        this.Bl.css("position", "absolute");
        this.Bl.css("top", "0px");
        this.Bl.css("left", "0px");
        this.Bl.css("width", "100%");
        this.Bl.css('opacity', this.Opacity);
        this.Bl.css("z-index", MaxZ);
        
        var x = '<div style="position: absolute; width: 0px; height: 0px; overflow: hidden; top: 0px; left: 0px;">';
        
        var Html = '<div id="GlGallery" style="display: none">';
        Html += '<div id="GlClose"><a href="javascript:void(0)" id="GlLnkClose"></a></div>';
        Html += '<div id="GlPrev"><a href="javascript:void(0)" id="GlLnkPrev"></a></div>';
        Html += '<div id="GlNext"><a href="javascript:void(0)" id="GlLnkNext"></a></div>';
        Html += '<div id="GlImages">';
        for (var i = 1; i <= this.Elem; i++) {
            Html += '<img id="GlImage_' + i + '" src="images/gallery/' + this.Base + i + 'g.jpg" style="display: none;" />';
            x += '<img id="xGlImage_' + i + '" src="images/gallery/' + this.Base + i + 'g.jpg" />';
        }
        Html += '</div>';
        Html += '</div>';
        x += '</div>';
        $("body").append(Html);
        
        $("body").append(x);
        this.Gl = $("#GlGallery");
        this.Gl.css("position", "absolute");
        this.Gl.css("z-index", MaxZ + 10);

        this.Close = $("#GlClose");
        this.Close.css("position", "absolute");
        this.Close.css("top", "0px");
        this.Close.css("width", this.BoxCloseW + "px");    
        this.Close.css("height", this.BoxCloseH + "px");    

        this.Prev = $("#GlPrev");
        this.Prev.css("position", "absolute");
        this.Prev.css("width", this.BoxNavW + "px");    
        this.Prev.css("height", this.BoxNavH + "px");    

        this.Next = $("#GlNext");
        this.Next.css("position", "absolute");
        this.Next.css("width", this.BoxNavW + "px");    
        this.Next.css("height", this.BoxNavH + "px");    

    },

    ShowFoto: function(i) {
        this.Next.unbind("click");
        this.Prev.unbind("click");
        this.Close.unbind("click");

        var bz = this.GetBodySize();
        this.Bl.css("height", bz.height + "px");
        this.Bl.css("display", "block");

        this.Gl.find("img").css("display", "none");
        
        var win = this.GetVisWin();        
        var WinH = win.height;
        var WinW = win.width;
        
        var Img = $('#GlImage_' + i);        
        var xImg = $('#xGlImage_' + i);        
        var ImgH = xImg.height();
        var ImgW = xImg.width();
    
   
        var GlW = this.BoxNavW * 2 + ImgW;
        var GlH = ImgH;
        var GlT = ( WinH - GlH ) / 2;
        if (GlT < 10) GlT = 10;
        GlT += $(window).scrollTop();

        var GlL = ( WinW - GlW ) / 2;
        if (GlL < 10) GlL = 10;

        this.Gl.css("width", GlW + "px");    
        this.Gl.css("height", GlH + "px");    
        this.Gl.css("left", GlL + "px");    
        this.Gl.css("top", GlT + "px");    
        
        Img.css("position", "absolute")
        Img.css("left", this.BoxNavW + "px");    
        Img.css("top", "0px");    
        
        Img.css("display", "block");

        this.Close.css("left", (GlW - this.BoxCloseW) + "px");    

        var NTop = (ImgH - this.BoxNavH) / 2;
        this.Next.css("top", NTop + "px");
        this.Next.css("left", "0px");    
        
        this.Prev.css("top", NTop + "px");
        this.Next.css("left", (ImgW + this.BoxNavW) + "px");    
        
        var me = this;
        var n = i<this.Elem ? i+1 : 1;
        var p = i>1 ? i-1 : this.Elem;
        this.Next.bind("click",function(e){
            me.ShowFoto(n);
        });
        this.Prev.bind("click",function(e){
            me.ShowFoto(p);
        });
        this.Close.bind("click",function(e){
            me.ZoomClose();
        });

        this.Gl.css("display", "block");
    },

    ZoomClose: function() {
        this.Bl.css("display", "none");
        this.Gl.css("display", "none");
    },

    //---------------------------------------------------------------------------------------------------
    GetBodySize: function() {
        var winW = $(window).width();
        var winH = $(window).height();
        var bodyW = $('body').width();
        var bodyH = $('body').height();
        var x = winW > bodyW ? winW : bodyW;
        var y = winH > bodyH ? winH : bodyH;
        return { width: x, height: y }
    },

    //---------------------------------------------------------------------------------------------------
    GetVisWin: function() {
        var winW = $(window).width();
        var winH = $(window).height();
        var bodyW = 0;
        var bodyH = 0;
        var x = winW > bodyW ? winW : bodyW;
        var y = winH > bodyH ? winH : bodyH;
        return { width: x, height: y }
    },

    //---------------------------------------------------------------------------------------------------
    MaxZIndex: function() {
        var maxZ = 1000;
        var x = $('body > div');
        x.each(
            function() {
                var x = parseInt($(this).css('z-index'), 10);
                if (!isNaN(x)) maxZ = Math.max(maxZ, x);
            }
        );
        return maxZ;
    }

}














function GlZoom(i) {


}


