﻿function ModalPopup(Div, OkButton, CancelButton) {
	// 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 _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;

	return this;

	function Open() {
		SetScreenSizes();

		var BackgroundCanvas = document.createElement("div");
		BackgroundCanvas.id = "webpos_ModalPopupCanvas";
		BackgroundCanvas.style.position = "absolute";
		BackgroundCanvas.style.zIndex = "999998";
		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=55)";
		BackgroundCanvas.style.opacity = ".55";
		BackgroundCanvas.style.backgroundColor = "#000";
		document.body.appendChild(BackgroundCanvas);

		$addHandler(window, "resize", UpdateBackgroundCanvas);
		$addHandler(window, "scroll", UpdateBackgroundCanvas);

		var ModalWindow = document.createElement("div");
		ModalWindow.id = "webpos_ModalPopupWindow";
		ModalWindow.style.position = "absolute";
		ModalWindow.style.zIndex = "999999";
		ModalWindow.ModalObj = this;

		var divHolder = document.createElement("div");
		divHolder.id = "ModaldivHolder";
		ModalWindow.appendChild(divHolder);
		_Div.style.display = "block";
		divHolder.appendChild(_Div);



		document.body.appendChild(ModalWindow);

		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;

		if (typeof (DragableDiv) != 'undefined') {
			ModalPopupHeader.style.cursor = "pointer";
			_DraggableControl = new DragableDiv(ModalWindow, document.getElementById('ModalPopupHeader'), document.body, null, false, null, true);
		}

	}

	function Close() {
		_Div.style.display = "none";
		document.body.appendChild(_Div);

		$removeHandler(window, "resize", UpdateBackgroundCanvas);
		$removeHandler(window, "scroll", UpdateBackgroundCanvas);
		_ModalWindow.parentNode.removeChild(_ModalWindow);
		_BackgroundCanvas.parentNode.removeChild(_BackgroundCanvas);
		_divHolder = null;
		_ModalWindow = null;
		_BackgroundCanvas = null;
	}

	function OKClicked() {
		if (_okbutton) {
			_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";
	}

}
