﻿function ModalPopup(Div, OkButton, CancelButton, HeaderText) {
	// Local Vars
	var _Self = this;
	var _Div = Div;
	var _DraggableControl;
	var _okbutton = OkButton;
	var _cancelbutton = CancelButton;
	var _BackgroundCanvas;
	var _ModalWindow;
	var _divHolder;
	var ScreenTop = 0;
	var ScreenLeft = 0;
	var ScreenWidth = 0;
	var ScreenHeight = 0;
	var _ModalPopupHeader;
	var _DivParent = _Div.parentNode;
	var _HeaderText = HeaderText;

	var _CustomParamArray = new Array();
	for (var i = 0; i < ModalPopup.arguments.length - 6; i++) {
		_CustomParamArray[i] = ModalPopup.arguments[i + 6];
	}

	// Functions
	this.Open = Open;
	this.Close = Close;
	this.element = _Div;
	this.BasicClose = BasicClose;
	this.OKClicked = OKClicked;
	this.CancelClicked = CancelClicked;

	return this;

	function Open() {
		SetScreenSizes();

		var BackgroundCanvas = document.createElement("div");
		BackgroundCanvas.style.position = "absolute";
		BackgroundCanvas.style.zIndex = "999999";
		BackgroundCanvas.style.top = ScreenTop + "px";
		BackgroundCanvas.style.left = ScreenLeft + "px";
		BackgroundCanvas.style.height = ScreenHeight + "px";
		BackgroundCanvas.style.width = ScreenWidth + "px";
		BackgroundCanvas.style.filter = "alpha(opacity=20)";
		BackgroundCanvas.style.opacity = ".20";
		BackgroundCanvas.style.backgroundColor = "#000";
		document.body.appendChild(BackgroundCanvas);

		var ModalWindow = document.createElement("div");
		ModalWindow.style.position = "absolute";
		ModalWindow.style.zIndex = "999999";
		ModalWindow.style.border = "1px solid black";
		ModalWindow.style.backgroundColor = "#fff";
		ModalWindow.ModalObj = this;
		
		if (typeof (DragableDiv) != 'undefined') {
		    var ModalPopupHeader = document.createElement("div");
		    ModalPopupHeader.style.backgroundColor = "#3F79A9";
		    ModalPopupHeader.style.height = "15px";
		    ModalPopupHeader.style.margin = "0";
		    ModalPopupHeader.style.padding = "0";
		    ModalPopupHeader.style.cursor = "pointer";
		    if (typeof (_HeaderText) != 'undefined') {
		        ModalPopupHeader.innerHTML = "<div style='float:left;color:#fff;font-size:10px;margin:1px 0 0 2px;font-weight:bold;font-family:arial;padding:0;'>" + _HeaderText + "</div>";
		    }
		    ModalWindow.appendChild(ModalPopupHeader);
		}
		

		var divHolder = document.createElement("div");
		
		ModalWindow.appendChild(divHolder);
		_Div.style.display = "block";
		divHolder.appendChild(_Div);

        

		document.body.appendChild(ModalWindow);
		
		divHolder.style.width = _Div.offsetWidth + "px";
		divHolder.style.margin="0";
		divHolder.style.padding="0";

		var WindowTop = (ScreenHeight / 2) - ((ModalWindow.offsetHeight) / 2) + ScreenTop;
		var WindowLeft = (ScreenWidth / 2) - (ModalWindow.offsetWidth / 2) + ScreenLeft;

		ModalWindow.style.top = WindowTop + "px";
		ModalWindow.style.left = WindowLeft + "px";

		_BackgroundCanvas = BackgroundCanvas;
		_ModalWindow = ModalWindow;
		_divHolder = divHolder;
		_ModalPopupHeader = ModalPopupHeader;

		if (typeof (DragableDiv) != 'undefined') {
		    ModalPopupHeader.style.width = _Div.offsetWidth + "px";
			_DraggableControl = new DragableDiv(ModalWindow, _ModalPopupHeader, document.body, null, false, null, true);
		}
		
		if (window.attachEvent){
            window.attachEvent("onresize", UpdateBackgroundCanvas);
            window.attachEvent("onscroll", UpdateBackgroundCanvas);
        }
        else{
            window.addEventListener("resize", UpdateBackgroundCanvas, false);
            window.addEventListener("scroll", UpdateBackgroundCanvas, false);
        }

	}

	function Close() {
		_Div.style.display = "none";
		_DivParent.appendChild(_Div);

		if (window.attachEvent){
            window.detachEvent("onresize", UpdateBackgroundCanvas);
            window.detachEvent("onscroll", UpdateBackgroundCanvas);
        }
        else{
            window.removeEventListener("resize", UpdateBackgroundCanvas, false);
            window.removeEventListener("scroll", UpdateBackgroundCanvas, false);
        }
        
		_ModalWindow.parentNode.removeChild(_ModalWindow);
		_BackgroundCanvas.parentNode.removeChild(_BackgroundCanvas);
		_divHolder = null;
		_ModalWindow = null;
		_BackgroundCanvas = null;
	}
	
	function BasicClose() {
		_Div.style.display = "none";

		if (window.attachEvent){
            window.detachEvent("onresize", UpdateBackgroundCanvas);
            window.detachEvent("onscroll", UpdateBackgroundCanvas);
        }
        else{
            window.removeEventListener("resize", UpdateBackgroundCanvas, false);
            window.removeEventListener("scroll", UpdateBackgroundCanvas, false);
        }
        
		_ModalWindow.parentNode.removeChild(_ModalWindow);
		_BackgroundCanvas.parentNode.removeChild(_BackgroundCanvas);
		_divHolder = null;
		_ModalWindow = null;
		_BackgroundCanvas = null;
	}

	function OKClicked(args) {
		if (_okbutton) 
		{   
		    if (args)
		    {
		       _okbutton(_CustomParamArray, args);
		    } else {
		       _okbutton(_CustomParamArray);
		    }
		}
		Close();
	}

	function CancelClicked() {
		if (_cancelbutton) _cancelbutton(_CustomParamArray);
		Close();
	}

	function SetScreenSizes() {
		ScreenWidth = 0;
		ScreenHeight = 0;
		if (typeof (window.innerWidth) == 'number') {
			//Non-IE
			ScreenWidth = window.innerWidth;
			ScreenHeight = window.innerHeight;
		} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
			//IE 6+ in 'standards compliant mode'
			ScreenWidth = document.documentElement.clientWidth;
			ScreenHeight = document.documentElement.clientHeight;
		} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
			//IE 4 compatible
			ScreenWidth = document.body.clientWidth;
			ScreenHeight = document.body.clientHeight;
		}

		ScreenTop = 0;
		ScreenLeft = 0;
		if (typeof (window.pageYOffset) == 'number') {
			//Netscape compliant
			ScreenTop = window.pageYOffset;
			ScreenLeft = window.pageXOffset;
		} else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
			//DOM compliant
			ScreenTop = document.body.scrollTop;
			ScreenLeft = document.body.scrollLeft;
		} else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
			//IE6 standards compliant mode
			ScreenTop = document.documentElement.scrollTop;
			ScreenLeft = document.documentElement.scrollLeft;
		}



	}

	function UpdateBackgroundCanvas() {
		SetScreenSizes();
		_BackgroundCanvas.style.top = ScreenTop + "px";
		_BackgroundCanvas.style.left = ScreenLeft + "px";
		_BackgroundCanvas.style.height = ScreenHeight + "px";
		_BackgroundCanvas.style.width = ScreenWidth + "px";
	}

}
