﻿function popClose() {
    var t = this;
    var d = t.document;
    var id = document.getElementById('popup');
    while (id.hasChildNodes()) {
        id.removeChild(id.lastChild); 
    }
    t.focus();
}
function Browser() {
    var ua, s, i;
    this.isIE = false;
    this.isNS = false;
    this.version = null;
    ua = navigator.userAgent;
    s = "MSIE";
    if ((i = ua.indexOf(s)) >= 0) {
        this.isIE = true;
        this.version = parseFloat(ua.substr(i + s.length));
        return;
    }
    s = "Netscape6/";
    if ((i = ua.indexOf(s)) >= 0) {
        this.isNS = true;
        this.version = parseFloat(ua.substr(i + s.length));
        return;
    }
    s = "Gecko";
    if ((i = ua.indexOf(s)) >= 0) {
        this.isNS = true;
        this.version = 6.1;
        return;
    }
}
var browser = new Browser();
var dragObj = new Object();
dragObj.zIndex = 0;
function dragStart(event, id) {    
    var x, y;
    if (id)
        dragObj.elNode = document.getElementById(id);
    else {
        if (browser.isIE)
            dragObj.elNode = window.event.srcElement;
        if (browser.isNS)
            dragObj.elNode = event.target;
        if (dragObj.elNode.nodeType == 3)
            dragObj.elNode = dragObj.elNode.parentNode;
    }
    if (browser.isIE) {
        x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
        y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
    }
    if (browser.isNS) {
        x = event.clientX + window.scrollX;
        y = event.clientY + window.scrollY;
    }
    dragObj.cursorStartX = x;
    dragObj.cursorStartY = y;
    dragObj.elStartLeft = parseInt(dragObj.elNode.style.left, 10);
    dragObj.elStartTop = parseInt(dragObj.elNode.style.top, 10);
    if (isNaN(dragObj.elStartLeft)) dragObj.elStartLeft = 0;
    if (isNaN(dragObj.elStartTop)) dragObj.elStartTop = 0;
    dragObj.elNode.style.zIndex = ++dragObj.zIndex;
    if (browser.isIE) {
        document.attachEvent("onmousemove", dragGo);
        document.attachEvent("onmouseup", dragStop);
        window.event.cancelBubble = true;
        window.event.returnValue = false;
    }
    if (browser.isNS) {
        document.addEventListener("mousemove", dragGo, true);
        document.addEventListener("mouseup", dragStop, true);
        event.preventDefault();
    }
}
function dragGo(event) {
    var x, y;
    if (browser.isIE) {
        x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
        y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
    }
    if (browser.isNS) {
        x = event.clientX + window.scrollX;
        y = event.clientY + window.scrollY;
    }
    dragObj.elNode.style.left = (dragObj.elStartLeft + x - dragObj.cursorStartX) + "px";
    dragObj.elNode.style.top = (dragObj.elStartTop + y - dragObj.cursorStartY) + "px";
    if (browser.isIE) {
        window.event.cancelBubble = true;
        window.event.returnValue = false;
    }
    if (browser.isNS)
        event.preventDefault();
}
function dragStop(event) {
    if (browser.isIE) {
        document.detachEvent("onmousemove", dragGo);
        document.detachEvent("onmouseup", dragStop);
    }
    if (browser.isNS) {
        document.removeEventListener("mousemove", dragGo, true);
        document.removeEventListener("mouseup", dragStop, true);
    }
}
//Resize
var theobject = null;
function getDirection(el) {
    var xPos, yPos, offset, dir;
    dir = "";
    xPos = window.event.offsetX;
    yPos = window.event.offsetY;
    offset = 8; //The distance from the edge in pixels
    if (yPos < offset) dir += "s";
    else if (yPos > el.offsetHeight - offset) dir += "n";
    if (xPos < offset) dir += "e";
    else if (xPos > el.offsetWidth - offset) dir += "w";
    return dir;
}
function resizeObject() {
    this.el = null; //pointer to the object
    this.dir = "";//type of current resize (n, s, e, w, ne, nw, se, sw)
    this.grabx = null;//Some useful values
    this.graby = null;
    this.width = null;
    this.height = null;
    this.left = null;
    this.top = null;
}
function doDown(id) {
    var el = document.getElementById(id);
    var dir = getDirection(el);
    if (dir == "") return;
    theobject = new resizeObject();
    theobject.el = el;
    theobject.dir = dir;
    theobject.grabx = window.event.clientX;
    theobject.graby = window.event.clientY;
    theobject.width = el.offsetWidth;
    theobject.height = el.offsetHeight;
    theobject.left = el.offsetLeft;
    theobject.top = el.offsetTop;
    if (browser.isIE) {
        document.attachEvent("onmousemove", doResize);
        document.attachEvent("onmouseup", stopResize);
        window.event.cancelBubble = true;
        window.event.returnValue = false;
    }
    if (browser.isNS) {
        document.addEventListener("mousemove", doResize, true);
        document.addEventListener("mouseup", stopResize, true);
        event.preventDefault();
    }
}
function stopResize() {
    if (theobject != null) {
        theobject.el.style.cursor = "auto";
        theobject = null;
    }
    if (browser.isIE) {
        document.detachEvent("onmousemove", doResize);
        document.detachEvent("onmouseup", stopResize);
    }
    if (browser.isNS) {
        document.removeEventListener("mousemove", doResize, true);
        document.removeEventListener("mouseup", stopResize, true);
    }
}
function doResize() {
    var el, xPos, yPos, str, xMin, yMin, dir;
    xMin = 100; //The smallest width possible
    yMin = 100; //             height
    el = theobject.el;
    dir = theobject.dir;
    str = getDirection(el);    
    //Fix the cursor
    if (str == "") {
        str = "default";
    }
    else {
        str += "-resize";
    }
    el.style.cursor = str;
    //Dragging starts here
    if (theobject != null) {
        if (dir.indexOf("e") != -1)
            el.style.width = Math.max(xMin, theobject.width + window.event.clientX - theobject.grabx) + "px";

        if (dir.indexOf("s") != -1)
            el.style.height = Math.max(yMin, theobject.height + window.event.clientY - theobject.graby) + "px";

        if (dir.indexOf("w") != -1) {
            el.style.left = Math.min(theobject.left + window.event.clientX - theobject.grabx, theobject.left + theobject.width - xMin) + "px";
            el.style.width = Math.max(xMin, theobject.width - window.event.clientX + theobject.grabx) + "px";
        }
        if (dir.indexOf("n") != -1) {
            el.style.top = Math.min(theobject.top + window.event.clientY - theobject.graby, theobject.top + theobject.height - yMin) + "px";
            el.style.height = Math.max(yMin, theobject.height - window.event.clientY + theobject.graby) + "px";
        }
        if (browser.isIE) {
            window.event.cancelBubble = true;
            window.event.returnValue = false;
        }
        if (browser.isNS)
            event.preventDefault();
    }
}
//opacità
var stOp = 100;
var inOp = 0;
var fdOp = 5;
var spOp = 10;
function startFade(fdd) {
    var elImg = document.getElementById(fdd);
    if (elImg) {
        if (inOp <= stOp) {
            inOp = inOp + fdOp;
            if (elImg.filters) {
                elImg.style.filter = 'alpha(opacity=' + (inOp) + ')';
            }
            else {
                elImg.style.opacity = (inOp / 100);
            }
            setTimeout('startFade("' + fdd + '")', spOp);
        }
    }
}
function startAll() {
    //sessione
    if (!sessvars.visto) {
        sessvars.visto = true;
        sessvars.num = 1;
        centraDiv('popup');
        periodo();
    }
    else {
        if (sessvars.num < 3) {
            sessvars.num += 1;
            centraDiv('popup');
            periodo();
        }
    }
}
function periodo() {
    //periodo
    var d = new Date();
    if (d.getFullYear() == 2011) {
        if (d.getMonth() == 9) {
            if (d.getDate() == 26) {
                document.getElementById('popup').style.display = 'block';
                startFade('foto');
            }
        }
    }
}
function centraDiv(mpp) {    
    var cnt = document.getElementById(mpp);
    var wCnt = cnt.style.width.split('p')[0];
    //var hCnt = cnt.style.height.split('p')[0];
    if (browser.isNS) {
        cnt.style.left = parseInt((top.innerWidth - wCnt) / 2) + 'px';
        //cnt.style.top = parseInt((top.innerHeight - hCnt) / 2) + 'px';
    }
    else {
        cnt.style.left = parseInt((document.body.offsetWidth - wCnt) / 2) + 'px';
        //cnt.style.top = parseInt((document.body.offsetHeight - hCnt) / 2) + 'px';    
    }        
}
