//window naming:------------------------------
var locationStr = new String(window.location);
var winName = locationStr.substring(locationStr.lastIndexOf("/")+1,locationStr.lastIndexOf("."));
window.name = winName;
//alert("winName: " + winName);

// Catch exception potentially caused by cross domain scripting
var openerAccessible = false;
try {
	// Check to see if opener exists
	if (typeof(opener) == 'object') {
		// Try and access a field (this may throw an exception)
		var openerName = opener.name;
		openerAccessible = true;
	} else {
		openerAccessible = false;	
	}
} catch (error) {
	openerAccessible = false;
}
//alert(openerAccessible);

// If not a main window get reference to main window so popup can interact with it:
var mainParent;
if (openerAccessible) {
	if (winName != 'map' && winName != 'query' && winName != 'text') { // Not a main window
		if (!opener.closed) {
			//alert("opener.name: " + opener.name);
			if (opener.name == 'results') { //2nd child (details when opened from results)
				if (!opener.mainParent.closed) {
					mainParent = opener.mainParent;
					//alert("2nd child: copy ref, mainParent.name: " + mainParent.name);
				} else {
					//main window closed:
					//alert("2nd child: mainParent.closed, mainParent: " + mainParent);
				}
			} else {
				//1st child (results, details opened from a main window)
				mainParent = opener;
				//alert("1st child: mainParent.name: " + mainParent.name);
			}
		} else {
			//main window closed:
			//alert(mainParent);
		}
	}
}

if (window.name == "results" || window.name == "details" || window.name == "map") {
	// focus the new window
	this.focus();
}

//------------------------------

//main window functions:------------------------------
//----------called from popup to open page in main window
	function openMainWin(URL, queryString) {
		if (typeof mainParent == 'object') {
			if (!mainParent.closed) {
				mainParent.location.href= URL + queryString;
			} else {
				mainParent = window.open(URL + queryString, '');
				if (typeof opener == 'object') {
					if (!opener.closed) {
						opener.mainParent = mainParent;
					}
				}
			}
		} else {
			mainParent = window.open(URL + queryString, '');
			if (typeof opener == 'object') {
				if (!opener.closed) {
					opener.mainParent = mainParent;
				}
			}
		}
		
		mainParent.focus();
		
	}

//------------------------------


// popup functions:------------------------------
var htmlwin;

	function closeHtmlWin() {
		if (typeof htmlwin == 'object') {
			if (!htmlwin.closed) {htmlwin.close();}
			return true;
		}	
	}

	function HtmlWin(src,name,width,height)    {
	
		if (src.length > 0) {
	
			var windoww = parseInt(width);
			var windowh = parseInt(height);
			
			//alert(htmlwin);
			
			closeHtmlWin();
			
			//alert(htmlwin);
			
			htmlwin = window.open(src,name,'toolbar=no,height=' + windowh + ',width=' + windoww + ',resizable=yes,menubar=yes,scrollbars=yes,status=yes,toolbar=yes');
			
		}
	
	}
//------------------------------

	function FocusWindow(winRef) {
		if (typeof(winRef) == 'object') {
			if (!winRef.closed) {
				winRef.focus();	
			}
		}
	}

