
function AssoloPopupOL(id,myConfigs)
{
	this.Configs = {
		top: "100",
		left: "300",
		width: "800",
		height: "600",
		iFrameBorder: 0,
		POPUPclassName: "Popup",
		SHADOWclassName: "SHADOW",
		TITLEclassName: "PopupTitle",
        BODYclassName: "PopupBody",
		AbsoluteParent: false,
        forceDimension: false,
        closeImg: false,
        closeImgClass: false,
        Source: "about:blank",
        CloseCode: null, 
        IsMoveable: false,
        TITLE: false
    };

	if (myConfigs) {
		if (myConfigs.top)  this.Configs.top  = myConfigs.top;
		if (myConfigs.left) this.Configs.left = myConfigs.left;
		if (myConfigs.width)  this.Configs.width  = myConfigs.width;
		if (myConfigs.height) this.Configs.height = myConfigs.height;
		if (myConfigs.POPUPclassName) this.Configs.POPUPclassName = myConfigs.POPUPclassName;
		if (myConfigs.SHADOWclassName) this.Configs.SHADOWclassName = myConfigs.SHADOWclassName;
		if (myConfigs.closeImg) this.Configs.closeImg = myConfigs.closeImg;
		if (myConfigs.closeImgClass) this.Configs.closeImgClass = myConfigs.closeImgClass;
		if (myConfigs.TITLEclassName) this.Configs.TITLEclassName = myConfigs.TITLEclassName;
        if (myConfigs.BODYclassName) this.Configs.BODYclassName = myConfigs.BODYclassName;
        if (myConfigs.Source) this.Configs.Source = myConfigs.Source;
        if (myConfigs.forceDimension) this.Configs.forceDimension = myConfigs.forceDimension;
        if (myConfigs.CloseCode) this.Configs.CloseCode = myConfigs.CloseCode;
        if (myConfigs.IsMoveable) this.Configs.IsMoveable = myConfigs.IsMoveable;
        if (myConfigs.TITLE) this.Configs.TITLE = myConfigs.TITLE;
	}

    this.bckOverflow = document.body.style.overflow;

    this.FirstResize = true;
	this.ShadowDIVID = "shadow_"+id;
	this.ShadowDIV = document.createElement("div");
	this.ShadowDIV.id = this.ShadowDIVID;
	this.ShadowDIV.className = this.Configs.SHADOWclassName;
    this.ShadowDIV.style.top = "0px";
    this.ShadowDIV.style.left = "0px";
    this.ShadowDIV.style.bottom = "0px";
    this.ShadowDIV.style.right = "0px";        
	this.ShadowDIV.style.position="absolute";

    this.bckScrollTop = document.documentElement.scrollTop;
    this.bckScrollLeft = document.documentElement.scrollLeft;
    document.documentElement.scrollTop = 0;
    document.documentElement.scrollLeft = 0;

    if (!Browser.isIE) {
        document.documentElement.appendChild(this.ShadowDIV);
    } else {
        document.body.appendChild(this.ShadowDIV);
    }

	this.PopupDIVID  = id;
	this.PopupDIV    = document.getElementById(id);
	if (!this.PopupDIV) {
		this.PopupDIV = document.createElement("div");
        this.PopupDIV.style.visibility="hidden";
		this.PopupDIV.id = this.PopupDIVID;
		this.PopupDIV.className = this.Configs.POPUPclassName;
        this.PopupDIV.style.top = parseInt(this.Configs.top)+"px";
        this.PopupDIV.style.left = parseInt(this.Configs.left)+"px";        
		try {
            this.PopupDIV.style.width = parseInt(this.Configs.width)+"px";
            this.PopupDIV.style.height = parseInt(this.Configs.height)+"px";
		} catch(e) {
		}
		this.PopupDIV.style.position="absolute";
		this.ShadowDIV.appendChild(this.PopupDIV);
        
        var myself = this;
		if (this.Configs.closeImg || this.Configs.TITLE) {
            this.PopupTitle = document.createElement("div");
            this.PopupTitle.className = this.Configs.TITLEclassName;
            if (this.Configs.TITLE) {
                this.PopupTitle.innerHTML = this.Configs.TITLE;
            }

            if (this.Configs.closeImg) {
                this.PopupCloseImg = document.createElement("img");
                this.PopupCloseImg.src = this.Configs.closeImg;
                if (this.Configs.closeImgClass) {
                    this.PopupCloseImg.className = this.Configs.closeImgClass;
                }
                try {
                    this.PopupCloseImg.addEventListener('click',function() { myself.close(); },true);
                } catch(err) {
                    this.PopupCloseImg.attachEvent('onclick', function() { myself.close(); } );
                }
                this.PopupTitle.appendChild(this.PopupCloseImg);
            }
            this.PopupDIV.appendChild(this.PopupTitle);
        }
        
        if (this.Configs.IsMoveable) {
            try {
                if (window.AssoloUtils.Browser.isIE) {
                    document.body.attachEvent('onmousedown', function(event){ myself.MouseDown(event); } );
                } else {
                    window.addEventListener('mousedown', function(event){ myself.MouseDown(event); }, true);
                }
            } catch(err) {
            }
        }
	}

	this.PopupIFRAMEID  = "iframe_"+id;
	this.PopupIFRAME = document.getElementById(this.PopupIFRAMEID);
	if (!this.PopupIFRAME) {
    
        this.PopupBody = document.createElement("div");
        this.PopupBody.style.position="relative";
        this.PopupBody.className = this.Configs.BODYclassName;
		this.PopupDIV.appendChild(this.PopupBody);
		this.PopupIFRAME = document.createElement("iframe");
        this.PopupIFRAME.style.visibility="hidden";        
		this.PopupIFRAME.id = this.PopupIFRAMEID;
		this.PopupIFRAME.name = this.PopupIFRAMEID;
		this.PopupIFRAME.style.position="relative";        
		this.PopupIFRAME.style.border = "0px";
        this.PopupIFRAME.frameBorder = "0px";
		this.PopupIFRAME.src = this.Configs.Source;
		try {
            this.PopupIFRAME.style.width = parseInt(this.Configs.width)+"px";
            this.PopupIFRAME.style.height = parseInt(this.Configs.height)+"px";
		} catch(e) {
		}
		this.PopupBody.appendChild(this.PopupIFRAME);
	}

	var myself = this;
    this.myResizer = function(){ myself.resize(); };
    
    try {
        window.addEventListener('resize', this.myResizer,true);
    } catch(err) {
        window.attachEvent('onresize', this.myResizer );
    }    
	setTimeout(function() { // WORKAROUND firefox: permesso negato per ottenere la proprietà XULElement.accessibleType
		myself.resize();
		myself.render();
		myself.resize();
	}, 100);
}

AssoloPopupOL.prototype.resize = function()
{
	var myH = "";
	var myW = "";

    var ParentModalHeight;
    var ParentModalWidth;
    //if (!Browser.isIE) {
        ParentModalHeight = document.documentElement.clientHeight+"px";
        ParentModalWidth = document.documentElement.clientWidth+"px";
    //} else {
    //    ParentModalHeight = document.body.clientHeight+"px";
    //    ParentModalWidth = document.body.clientWidth+"px";
    //}

    if (this.Configs.AbsoluteParent) {
        myH = str_replace("px", "", ParentModalHeight);
        myW = str_replace("px", "", ParentModalWidth);
        myH = (parseInt(myH) + parseInt(document.documentElement.scrollTop))+'px';
        myW = (parseInt(myW) + parseInt(document.documentElement.scrollLeft))+'px';
    } else {
        myH = ParentModalHeight;
        myW = ParentModalWidth;
    }

    this.ShadowDIV.style.height = myH;
    this.ShadowDIV.style.width = myW;

    if (this.Configs.forceDimension) {
    
    } else {
        myH = parseInt(parseInt(ParentModalHeight)* 0.8);
        myW = parseInt(parseInt(ParentModalWidth) * 0.8);
//	myH = parseInt(ParentModalHeight)-40;
//	myW = parseInt(ParentModalWidth)-40;

        this.Configs.width  = myW;
        this.Configs.height = myH;
    }

    if (this.Configs.AbsoluteParent) {
        myH = str_replace("px", "", ParentModalHeight);
        myW = str_replace("px", "", ParentModalWidth);
        myH = parseInt((myH - this.Configs.height)/2);
        myW = parseInt((myW - this.Configs.width)/2);
        myH += this.Configs.ParentModal.scrollTop;
        myW += this.Configs.ParentModal.scrollLeft;
    } else {
        myH = str_replace("px", "", ParentModalHeight);
        myW = str_replace("px", "", ParentModalWidth);
        myH = parseInt((myH - this.Configs.height)/2);
        myW = parseInt((myW - this.Configs.width)/2);
    }
		
    this.Configs.top  = myH;
    this.Configs.left = myW;

	this.PopupDIV.style.top = this.Configs.top+'px';
	this.PopupDIV.style.left = this.Configs.left+'px';
    this.PopupDIV.style.width = this.Configs.width+'px';
    this.PopupDIV.style.height = this.Configs.height+'px';

    this.PopupBody.style.width=this.Configs.width+"px";
    this.PopupBody.style.height=this.Configs.height+"px";
    var DeltaX = this.PopupBody.clientWidth - this.Configs.width;
    var DeltaY = this.PopupBody.clientHeight - this.Configs.height;
    this.PopupBody.style.width=(this.Configs.width-DeltaX)+"px";
    if (this.PopupTitle) {
        this.PopupBody.style.height=(this.Configs.height-DeltaY-this.PopupTitle.offsetHeight)+"px";
    } else {
        this.PopupBody.style.height=(this.Configs.height-DeltaY)+"px";
    }

    this.PopupIFRAME.style.width = "100%";
    this.PopupIFRAME.style.height = "100%";

    if (this.FirstResize) {
        this.PopupDIV.style.visibility="visible";        
        this.PopupIFRAME.style.visibility="visible";
        this.FirstResize = false;
    }
}

AssoloPopupOL.prototype.render = function() 
{
    this.PopupDIV.style.display = '';
    this.ShadowDIV.style.display = '';
}

AssoloPopupOL.prototype.show = function() 
{
    if (!Browser.isIE) {
        this.ShadowDIV.style.zIndex = window.AssoloUtils.getMaxZIndex(document,this.ShadowDIV)+1;
    }
	this.ShadowDIV.style.display = '';
	this.PopupDIV.style.display = '';
}

AssoloPopupOL.prototype.hide = function() 
{
	this.Configs.Visible = false;
	this.PopupDIV.style.display = 'none';
	this.ShadowDIV.style.display = 'none';
}

AssoloPopupOL.prototype.close = function()
{
	this.PopupDIV.style.display = 'none';

    try {
        window.removeEventListener('resize', this.myResizer, true);
    } catch(err) {
        window.detachEvent('onresize', this.myResizer);
    }

    if (this.Configs.CloseCode)
        eval(this.Configs.CloseCode);

	this.ShadowDIV.removeChild(this.PopupDIV);
	this.PopupDIV = null;
    if (!Browser.isIE) {
        document.documentElement.removeChild(this.ShadowDIV);
    } else {
        document.body.removeChild(this.ShadowDIV);
    }
	this.ShadowDIV = null;

    document.body.style.display = 'none';
    
    var myself = this;
    setTimeout(function() {
        if (isdefined(myself.bckOverflow)) {
            document.body.style.overflow = myself.bckOverflow;
        } else {
            document.body.style.overflow = "visible";
        }
        document.body.style.display = '';

        if (isdefined(myself.bckScrollTop)) {
            document.documentElement.scrollTop = myself.bckScrollTop;
        }
        if (isdefined(myself.bckScrollLeft)) {
            document.documentElement.scrollLeft = myself.bckScrollLeft;
        }
    }, 1);
}

AssoloPopupOL.prototype.getIFrame = function()
{
	return this.PopupIFRAME;
}

AssoloPopupOL.prototype.getIFrameId = function()
{
	return this.PopupIFRAMEID;
}


AssoloPopupOL.prototype.MouseDown = function(evt)
{
    var w = window;
    ev = evt || w.event;
    el = evt.target || event.srcElement;

    window.AssoloUtils.RemoveStartDragElement(this);

    if ((el == this.PopupTitle) || ((el.parentNode == this.PopupTitle) && el.tagName != 'IMG')) {
        this.Configs.theTop  = parseInt(this.PopupDIV.style.top);
        this.Configs.theLeft = parseInt(this.PopupDIV.style.left);
        this.StartDragElementX = window.AssoloUtils.MOUSE_X;
        this.StartDragElementY = window.AssoloUtils.MOUSE_Y;

        window.AssoloUtils.AddStartDragElement(this);

        if (window.AssoloUtils.Browser.isIE) {
            document.selection.empty();
        } else {
            window.getSelection().removeAllRanges();
        }

        ev.cancelBubble = true;
        if (ev.stopPropagation) ev.stopPropagation();
        return false;
    }
}

AssoloPopupOL.prototype.MouseMove = function(ev)
{
    window.AssoloUtils.debug('mousemove ['+this.Configs.theTop+'] ['+this.StartDragElementY+'] ['+window.AssoloUtils.MOUSE_Y+']');

    var curLeft = parseInt(this.Configs.theLeft-(this.StartDragElementX-window.AssoloUtils.MOUSE_X));
    var curTop = parseInt(this.Configs.theTop-(this.StartDragElementY-window.AssoloUtils.MOUSE_Y));

    if (curLeft < 0) {
        curLeft = 0;
    }
    if (curTop < 0) {
        curTop = 0;
    }
    if (curLeft > (window.AssoloUtils.ScreenWidth-this.Configs.width)) {
        curLeft = parseInt(window.AssoloUtils.ScreenWidth-this.Configs.width);
    }
    if (curTop > (window.AssoloUtils.ScreenHeight-this.Configs.height)) {
        curTop = parseInt(window.AssoloUtils.ScreenHeight-this.Configs.height);
    }

    this.PopupDIV.style.top = curTop + "px";
    this.PopupDIV.style.left = curLeft + "px";
    
    if (window.AssoloUtils.Browser.isIE) {
        document.selection.empty();
    } else {
        window.getSelection().removeAllRanges();
    }

/*    ev.cancelBubble = true;
    if (ev.stopPropagation) ev.stopPropagation();
    return false;*/

    return true;
}

AssoloPopupOL.prototype.MouseMoveIF = function(evt)
{
    var ev = evt || w.event;
    var curX = 0;
    var curY = 0;
    var offsetX = 0;
    var offsetY = 0;
    if (this.PopupIFRAME) {
        try {
            var pos = window.AssoloUtils.GetXY(this.PopupIFRAME);
            offsetX = pos[0];
            offsetY = pos[1];
        } catch (err) {
        }
    }
    
    if (window.AssoloUtils.Browser.isIE) {
        curX = parseInt(ev.clientX+ offsetX);
        curY = parseInt(ev.clientY+ offsetY);
    } else {
        curX = parseInt(ev.pageX+ offsetX);
        curY = parseInt(ev.pageY+ offsetY);
    }

    var curLeft = parseInt(this.Configs.theLeft-(this.StartDragElementX-curX));
    var curTop = parseInt(this.Configs.theTop-(this.StartDragElementY-curY));

    if (curLeft < 0) {
        curLeft = 0;
    }
    if (curTop < 0) {
        curTop = 0;
    }
    if (curLeft > (window.AssoloUtils.ScreenWidth-this.Configs.width)) {
        curLeft = parseInt(window.AssoloUtils.ScreenWidth-this.Configs.width);
    }
    if (curTop > (window.AssoloUtils.ScreenHeight-this.Configs.height)) {
        curTop = parseInt(window.AssoloUtils.ScreenHeight-this.Configs.height);
    }

    this.PopupDIV.style.top = curTop + "px";
    this.PopupDIV.style.left = curLeft + "px";
    
    if (window.AssoloUtils.Browser.isIE) {
        document.selection.empty();
    } else {
        window.getSelection().removeAllRanges();
    }

/*    ev.cancelBubble = true;
    if (ev.stopPropagation) ev.stopPropagation();
    return false;*/

    return true;
}

AssoloPopupOL.prototype.MouseUp = function(evt)
{
    window.AssoloUtils.processResizer();
    window.AssoloUtils.RemoveStartDragElement(this);

    if (window.AssoloUtils.Browser.isIE) {
        document.selection.empty();
    } else {
        window.getSelection().removeAllRanges();
    }

    evt.cancelBubble = true;
    if (evt.stopPropagation) evt.stopPropagation();
    return false;
}

