//OpenModalDialog(sUrl, nWidth, nHeight, bScrollable, bResizable) //locks opener until dialog is released
//OpenDialog(sUrl, nWidth, nHeight, bScrollable, bResizable) //cleans itself up after leaving the opener page
//OpenWindow(sUrl, nWidth, nHeight, bScrollable, bResizable) //normal floating window

//To add additional parameters, use:
//DialogOptions.AddParam(sName, sValue)
var g_sModalWinName;
var g_bModalWinOpen;
var g_tmrModalWin;
var g_winMain;
var g_oOpener;

window.SupportsModalDialogs = true;
window.RefreshAfterCloseModal = false;
window.IsModalWindow = false;

if (window.opener) {
	g_oOpener = window.opener;
}

function OpenModalDialog(sUrl, nWidth, nHeight, bScrollable, bResizable) {
	var winModal;
	
	//if this is netscape or AOL, open a normal window
	var bIsAOL = (navigator.appVersion.indexOf('AOL') != -1);
	var bIsNetscape = (document.layers && !document.all);
	
	if (bIsAOL || bIsNetscape) {
		OpenDialog(sUrl, nWidth, nHeight, bScrollable, bResizable);
	}

	var sFeatures = DialogOptions.GetString(nWidth, nHeight, bScrollable, bResizable);
	DialogOptions.Clear();
	
	//open the window
	try {
		winModal = window.open(sUrl, DialogOptions.WindowName, sFeatures);
	} catch (e) {
		
	}
	
	//detect popup blocking software
	if (!VerifyWindowOpened(winModal)) {
		return;
	}
	
	ModalEnableWindow(window, false);
	
	if (Browser.UserAgent == BROWSER_KONQUEROR) {
		ResizeWindow(nWidth, nHeight, winModal);
	}
	
	//set the global variables so we can reference this window from other functions
	g_sModalWinName = 'window.ModalWinRef'; //TODO clean up later
	g_bModalWinOpen = true;
	
	//return a reference to the window
	window.ModalWinRef = winModal;
	return winModal;
}

function ResizeWindow(nWidth, nHeight, oWindow) {
	nHeight = ModalGetHeight(nHeight);
	
	var nX = (screen.availWidth / 2) - (nWidth / 2);
	var nY = (screen.availHeight / 2) - (nHeight / 2);
	
	if (!oWindow) {
		oWindow = window;
	}
	
	if (oWindow.statusbar && window.statusbar.visible) {
		nHeight += 20;
	}
	
	oWindow.resizeTo(nWidth, nHeight);
	oWindow.moveTo(nX, nY);
	
	//HACK workaround for IE on WinXP SP2- IE doesnt resize properly the first time
	//also fix for safari
	if (IsWinXPSP2() || Browser.UserAgent == BROWSER_KONQUEROR) {
		oWindow.setTimeout('window.resizeTo(' + nWidth + ', ' + nHeight + ');window.moveTo(' + nX + ', '+ nY + ');', 10);
	}
}

function ResizeWindowDontCenter(nWidth, nHeight, oWindow) {
	nHeight = ModalGetHeight(nHeight);

	if (!oWindow) {
		oWindow = window;
	}
	
	if (oWindow.statusbar && window.statusbar.visible) {
		nHeight += 20;
	}
	
	oWindow.resizeTo(nWidth, nHeight);
	
	//HACK workaround for IE on WinXP SP2- IE doesnt resize properly the first time
	if (IsWinXPSP2()) {
		oWindow.setTimeout('window.resizeTo(' + nWidth + ', ' + nHeight + ');', 10);
	}
}

function ModalDeadEnd(){
	try { //aol fix

		var winModal = window.ModalWinRef;
		//If the window already exists, Then refocus it
		if (winModal && !winModal.closed) {

			//workaround for netscape win98/ME bug
			if (document.all || (navigator.userAgent.indexOf('Windows NT') != -1)) {
				winModal.focus();
			} else {
				//occurs only on netscape  win98/ME
				setTimeout('eval(g_sModalWinName).focus();', 0);
			}
		//the window has been closed, or never has been opened- 
		} else {
			if (g_bModalWinOpen){
				//return the events to normal
				ModalEnableWindow(window, true);
				g_bModalWinOpen = false;
			}
		}
	} catch (e) {
		//alert(e.number);
	}
	return false;
}

function ModalEnableWindow(win, bDoEnable){
	var doc = win.document;
	var veilLayer = doc.getElementById('veilLayer');
	
	if (!veilLayer) {
		return;
	}

	//Netscape/Gecko browsers
	if (!win.document.all) {
		win.captureEvents(Event.CLICK | Event.MOUSEDOWN | Event.MOUSEUP | Event.FOCUS | Event.UNLOAD);
	}

	//add global event hanlders
	win.onfocus = bDoEnable ? null : ModalDeadEnd;
	win.onresize = bDoEnable ? null : ModalDeadEnd;
	win.onresizestart = bDoEnable ? null : ModalDeadEnd;
	win.onscroll = bDoEnable ? null : ModalDeadEnd;
	
	try {
		doc.body.onclick = bDoEnable ? null : ModalDeadEnd;
		doc.body.onselect = bDoEnable ? null : ModalDeadEnd;
		doc.body.onselectstart = bDoEnable ? null : ModalDeadEnd;
		doc.body.oncontextmenu = bDoEnable ? null : ModalDeadEnd;
	
		//win.onunload = bDoEnable ? null : ModalHandleErrors;

		veilLayer.style.display = bDoEnable ? 'none' : 'block';
		veilLayer.style.width = doc.body.scrollWidth-1;
		veilLayer.style.height = doc.body.scrollHeight-1;

	} catch (e) {
		return; //the document may have been closed
	}
	
	if (bDoEnable){
		win.focus();
	}
}

//if the user finds a way to unload the host page, get rid of the popup window too.
function ModalHandleErrors(){
	try {
		var winModal = window.ModalWinRef;
		if (winModal && !winModal.closed){
			winModal.close();
		}
		ModalEnableWindow(window, true);
	} catch (e) {
		//alert('error caught');
	}
}

//when the modal window is closed, this is called in IE
function ModalOnClose(){
	try {
		if (g_oOpener) {
			ModalEnableWindow(g_oOpener, true);
		}
	} catch (e) {
		//do nothing
	}

	try {
		if (g_oOpener.RefreshAfterCloseModal){
			//refresh the window, but dont reload- they may have post data
			if (g_oOpener.ProgressBar) {
				g_oOpener.ProgressBar.Show('Loading...');
			}	
			g_oOpener.document.location.reload(true);
		}
	} catch (e) {
		//alert('error- finish modal: ' + e.description);
	}
}

function ModalReleaseOpenerOnClose(){
	//attach the IE unload event
	window.onunload = ModalOnClose;
	if (!document.all) {
		window.captureEvents(Event.UNLOAD);
	}
}

function ModalAllowRefresh() {
	try {
		opener.RefreshAfterCloseModal = false;
	} catch (e) {
		//do nothing
	}
	RefreshAfterCloseModal = false;
	window.onunload = null;
}

function ModalWriteVeil(){
	document.write('<div id="veilLayer" style="position:absolute; left:1px; top:1px; width:1px; height:1px; z-index:20; display:none; background-image: url(' + fw_config_ImageDirectory + '/spacer.gif); layer-background-image: url(' + fw_config_ImageDirectory + '/spacer.gif)">');
	document.write('<img src="' + fw_config_ImageDirectory + '/spacer.gif" alt="" name="veilImageBlock" id="veilImageBlock" width="100%" height="100%" border="0" style="cursor: not-allowed;" onClick="ModalDeadEnd();return false;" galleryimg="no">');
	document.write('</div>');
}

//Modeless dialogs
function OpenWindow(sUrl, nWidth, nHeight, bScrollable, bResizable) {
	OpenDialog(sUrl, nWidth, nHeight, bScrollable, bResizable, true);
}

function OpenDialog(sUrl, nWidth, nHeight, bScrollable, bResizable, bLeaveOpen) {
	if (!bLeaveOpen) {
		bLeaveOpen = false;
	}
	
	//build feature string
	var sFeatures = DialogOptions.GetString(nWidth, nHeight, bScrollable, bResizable);
	DialogOptions.Clear();

	if (!g_winMain || g_winMain.closed) {
		try {
			g_winMain = window.open(sUrl, DialogOptions.WindowName, sFeatures);
		} catch (e) {
			//do nothing
		}
		//detect popup blocking software
		if (!VerifyWindowOpened(g_winMain)) {
			return;
		}
	} else {
		// bring existing window to focus
		g_winMain.close();
		g_winMain = window.open(sUrl, DialogOptions.WindowName, sFeatures);
		g_winMain.focus();
	}
	
	if (Browser.UserAgent == BROWSER_KONQUEROR) {
		ResizeWindow(nWidth, nHeight, g_winMain);
	}
	
	if (!bLeaveOpen) {
		if (!document.all) {
			window.captureEvents(Event.UNLOAD);
		}
		window.onunload = CleanDialog;
	}
}

function CleanDialog() {
	if (g_winMain) {
		g_winMain.close();
	}
}

function DialogOptionsManager() {
	this.ParamArray = new Array();
	this.ParamArrayValues = new Array();
	this.DoPopupBlockerMessage = true;
	this.WindowName = "_blank";
	
	this.AddParam = function(sName, sValue) {
		var nIndex = this.ParamArray.length;
		
		for (var i=0; i<this.ParamArray.length; i++) {
			if (this.ParamArray[i] == sValue) {
				nIndex = i;
				break;
			}
		}
		
		this.ParamArray[nIndex] = sName;
		this.ParamArrayValues[nIndex] = sValue;
	}
	
	this.GetString = function(nWidth, nHeight, bScrollable, bResizable) {
		nHeight = ModalGetHeight(nHeight);
		
		if (nWidth > screen.availWidth){
			nWidth = screen.availWidth;
		}
		if (nHeight > screen.availHeight){
			nHeight = screen.availHeight;
		}

		if (typeof(bScrollable) == "undefined") {
			bScrollable = true;
		}
		if (typeof(bResizable) == "undefined") {
			bResizable = true;
		}
		
		//open the window at the center of the screen by default
		var nX = (screen.availWidth / 2) - (nWidth / 2);
		var nY = (screen.availHeight / 2) - (nHeight / 2);
		
		//build feature string
		var sFeatures = 'width=' + nWidth + ',';
		sFeatures += 'height=' + nHeight + ',';
		sFeatures += 'screenX=' +  nX + 'px,';
		sFeatures += 'screenY=' +  nY + 'px,';
		sFeatures += 'top=' +  nY + 'px,';
		sFeatures += 'left=' +  nX + 'px,';
		sFeatures += 'status=no,';
		sFeatures += 'scrollbars=' + (bScrollable ? 'yes' : 'no') + ',';
		sFeatures += 'resizable=' + (bResizable ? 'yes' : 'no');
		
		
		for (var i=0; i<this.ParamArray.length; i++) {
			sFeatures += ',' + this.ParamArray[i] + '=' + this.ParamArrayValues[i];
		}

		return sFeatures;
	}
	
	this.Clear = function() {
		this.ParamArray = new Array();
		this.AddParam('status', 'no');
		this.AddParam('titlebar', 'no');
	}
	
	this.Clear();
}

function VerifyWindowOpened(oWin) {
	if (!oWin || oWin.closed) {
		if (DialogOptions.DoPopupBlockerMessage) {
			alert('This feature requires a dialog which was blocked by a popup blocking program in your browser. You must disable popup blocking for this site in order to use this feature.');
		}
		return false;
	}
	
	return true;
}

function IsWinXPSP2() {
	if (Browser.UserAgent == BROWSER_IE && Browser.OS == OS_WINXP && Browser.SP == 2) {
		return true;
	}
	
	return false;
}

//on winXP SP2, returns a slightly larger window size to compensate for the status bar
function ModalGetHeight(nHeight) {
	if (IsWinXPSP2()) {
		return nHeight + 20;
	} else {
		return nHeight;
	}
}

//*******************************************************
//Set global properties and event handlers
//*******************************************************

var DialogOptions = new DialogOptionsManager();

if (window.opener){
	try {
		if (window.opener.opener) {
			//alert('is modal');
			if (window.opener.opener.g_sModalWinName) {
				window.IsModalWindow = true;
			}
		}
		
		if (window.opener) {
			if (window.opener.g_sModalWinName) {
					//alert(window.opener.g_sModalWinName);
				//if (window.opener.g_sModalWinName == window.name) {
					window.IsModalWindow = true;
				//}
			}
		}
	} catch (e) {
		//alert('custom error: ' + e.description);
	}
}

if (window.IsModalWindow){
	window.focus();
	ModalEnableWindow(opener, false);
	ModalReleaseOpenerOnClose();
}

ModalWriteVeil();
