﻿var gridToPosition = null;

function setMenuOffset() {
    var header = document.getElementById('header');
    if (!header)
        return;
    var currentOffset = document.documentElement.scrollTop || document.body.scrollTop; // body for Safari
    var startPos = parseInt(setMenuOffset.initialPos) || 190;
    var desiredOffset = startPos - currentOffset;
    if (desiredOffset < 10)
        desiredOffset = 10;
    if (desiredOffset != parseInt(header.style.top))
        header.style.top = desiredOffset + 'px';

    //    var currentLeftOffset = document.documentElement.scrollLeft || document.body.scrollLeft; // body for Safari
    //    if (currentLeftOffset != -parseInt(header.style.left))
    //        header.style.left = '-' + currentLeftOffset + 'px';
}

function resizeGrid() {
    if (gridToPosition) {
        var obj = document.getElementById(gridToPosition);
        if (!obj)
            return;

        var width = document.documentElement.clientWidth || document.body.clientWidth;

        var diff = 960 - parseInt(obj.clientWidth);

        obj.style.position = "relative";

        //        if (diff < 0 && parseInt(obj.clientWidth) > width)
        //            obj.style.left = "0px";
        //        else
        obj.style.left = (diff / 2) + "px";


        //        var currentLeftOffset = document.documentElement.scrollLeft || document.body.scrollLeft; // body for Safari
        //        if (currentLeftOffset != -parseInt(header.style.left))
        //            header.style.left = '-' + currentLeftOffset + 'px';
    }
}

//
// POPUP INGRANDIMENTO IMMAGINE
//

with (document) {
    write('<div id="divPopupImmagineSfondo" style="display:none;background-color:black;position:absolute;z-index:500"></div>');
    write('<div id="divPopupImmagine" style="display:none;border: 1px #cccccc solid; height: 300px; width: 400px; text-align: right;background-color:white;z-index:520;position:fixed;padding:5px">');
    write('<div style="border: 1px #cccccc solid">');
    write('<a href="javascript:hidePopupImmagine()">chiudi [x]</a>');
    write('<br />');
    write('<img id="imgPopup"/>');
    write('</div></div>');
}

var IsPopupInizialized = false;

function showPopupImmagine(immagine, alt, width, height) {

    var divSfondo = $("#divPopupImmagineSfondo"); //  document.getElementById("divPopupImmagineSfondo");
    var divPopup = $("#divPopupImmagine"); // document.getElementById("divPopupImmagine");
    var imgPopup = $("#imgPopup");

    if (divSfondo && divPopup) {

        var winheight = $(window).height();
        var winwidth = $(window).width();

        var docheight = $(document).height();
        var docwidth = $(document).width();

        // imposta lo sfondo

        divSfondo.css("left", "0px");
        divSfondo.css("top", "0px");
        divSfondo.width(docwidth);
        divSfondo.height(docheight);
        divSfondo.fadeTo("fast", 0.5);

        // carico l'immagine

        var maxlen = 600;

        var max = Math.max(width, height);
        if (max > maxlen) {
            var c = maxlen / max;
            width *= c;
            height *= c;
        }

        imgPopup.attr("alt", alt);
        imgPopup.attr("width", width);
        imgPopup.attr("height", height);
        imgPopup.attr("src", immagine);

        width += 10;
        height += 20;

        divPopup.width(width);
        divPopup.height(height);

        divPopup.css("left", ((winwidth - width) / 2 /*+ $(window).scrollLeft()*/) + "px");
        divPopup.css("top", ((winheight - height) / 2 /*+ $(window).scrollTop()*/) + "px");

        //        divPopup.offset({ top: (winheight - height) / 2 + $(window).scrollTop(), left: (winwidth - width) / 2 + $(window).scrollLeft() });

        divPopup.show();

    }
}

function hidePopupImmagine() {

    var divSfondo = $("#divPopupImmagineSfondo"); //  document.getElementById("divPopupImmagineSfondo");
    var divPopup = $("#divPopupImmagine"); // document.getElementById("divPopupImmagine");
    var imgPopup = $("#imgPopup");

    if (divSfondo && divPopup) {
        imgPopup.attr("src", "");
        divSfondo.fadeOut();
        divPopup.hide();
        //divPopup.offset({ top: 0, left: 0 });
    }
}

window.onscroll = document.documentElement.onscroll = setMenuOffset;
//window.onresize = document.documentElement.onresize = resizeGrid;

