//****************************************************************
//****************************************************************
//	COPYRIGHT 2008, Vertex Software
//****************************************************************
//****************************************************************
var gPopinWindow_MouseX;
var gPopinWindow_MouseY;
var gSheetAdded = false;

//===========================================================
// PopinWindow
//===========================================================
function PopinWindow( url, name, features ) {
	var kCssBase = "PopinWindow_";
	var kTitleBarHeight = 20;
	var mURL = (url || "about:blank");
	var mName = (name || "window");
	var mFeatures = FeaturesListToObject(features || "");
	var mElementID = "popin_" + mName;
	var mPopinWindow = CreatePopinWindow( this, mElementID, mURL, mName, mFeatures );
	var Close = Close;

	this.SetURL = SetURL;
	SetURL( url );



	//-----------------------------------------------------------
	// SetURL
	//-----------------------------------------------------------
	function SetURL( url ) {
		try {
			if (!url.match(/^https?:\/\//)) url = "http://" + location.hostname + url;
			mPopinWindow.src = url;
			}
		catch (error) {
			alert( "PopinWindow.SetURL: " + error.description );
			}
		}


	//-----------------------------------------------------------
	// FeaturesListToObject
	//-----------------------------------------------------------
	function FeaturesListToObject( features ) {
		try {
			eval(  "features = {" + features.replace( /=([^,]+)/g, ":\"$1\"" ) + "}" );
			// TODO Need to set top/left based on mouse location
			//if (!features.top) features.top = 0; 
			//if (!features.left) features.left = 0; 
			}
		catch (error) {
			alert( "PopinWindow.FeaturesListToObject: " + error.description );
			}
		return features;
		}


	//-----------------------------------------------------------
	// createStyleSheet 
	// Required since Mozilla doesnt' have it
	//-----------------------------------------------------------
	function createStyleSheet( url, index ) {
		try {
			var head = document.getElementsByTagName ( "HEAD" )[0];
			var style = document.createElement( "link" );
			var beforeSheet = null;
			style.href = url;
			style.type = "text/css";
			style.rel = "stylesheet";
			if (index < document.styleSheets.length) { 
				var styleSheets = document.getElementsByTagName ( "LINK" );
				beforeSheet = styleSheets[index];
				}
			head.insertBefore( style, beforeSheet );
			}
		catch (error) {
			alert( "createStyleSheet : " + error.description  );
			}
		}


	//-----------------------------------------------------------
	// CreateWindowIframe
	//-----------------------------------------------------------
	function CreateWindowIframe( thisObject, features ) {
		try {
			var iFrame = document.createElement( "iframe" );
			iFrame.className = kCssBase + "iframe";
			with (iFrame.style) {
				margin = "0px";
				display = "block";
				width = "100%";
				clear = "both";
				if (features.width) width = features.width + "px";
				if (features.height) height = (features.height-kTitleBarHeight) + "px";
				if (features.top) top = features.top + "px";
				if (features.left) left = features.left + "px";
				}
			// 30OCT2008 RFM - Per Ru
			iFrame.frameBorder = 0;
			iFrame.scrolling = features.scrollbars || "no";
			return iFrame;
			}
		catch (error) {
			alert( "CreateWindowIframe: " + error.description );
			}
		return null;
		}


	//-----------------------------------------------------------
	// Close
	//-----------------------------------------------------------
	function Close( ) {
		try {
			mPopinWindow.parentNode.parentNode.removeChild(mPopinWindow.parentNode);
			}
		catch (error) {
			alert( "Close: " + error.description  );
			}
		}


	//-----------------------------------------------------------
	// GetScriptDirectory
	// Ruturns path of this script for page
	//-----------------------------------------------------------
	function GetScriptDirectory( ) {
		try {
			var scripts = document.getElementsByTagName("script");
			for (var item=0; item<scripts.length; item++) {
				var scriptTag = scripts[item];
				if (scriptTag.src && scriptTag.src.match(/popinwindow\.js/gi)) return scriptTag.src.replace( /\/[^\/]+$/g, "/" );
				}
			}
		catch (error) {
			alert( "GetScriptDirectory: " + error.description  );
			}
		return "";
		}


	//-----------------------------------------------------------
	// CreateWindowTitleBar
	//-----------------------------------------------------------
	function CreateWindowTitleBar(  thisObject, features ) {
		try {
			var titleBar = document.createElement( "div" );
			titleBar.className = kCssBase + "titleBar"
			with (titleBar.style) {
				height = kTitleBarHeight + "px";
				width = (features.width ? features.width + "px" : "101%");
				}
			titleBar.innerHTML = ["<div style='float:left' class='" + kCssBase + "title' >",features.title || "","</div>","<div style='float:right' class='" + kCssBase + "close'><a href='#'>close</a></div>"].join("");
			titleBar.childNodes.item(1).onclick = function() {
				try {
					document.body.removeChild(mPopinWindow.parentNode);
					}
				catch (error) {
					alert( "PopinWindow.Close: " + error.description );
					}
				}
			return titleBar;
			}
		catch (error) {
			alert( "CreateWindowTitleBar: " + error.description );
			}
		return null;
		}



	//-----------------------------------------------------------
	// CreatePopinWindow
	//-----------------------------------------------------------
	function CreatePopinWindow( thisObject, elementID, url, name, features ) {
		try {
			// Only allow one window open at a time - for now
			var iFrame = document.getElementById( mElementID );
			if (iFrame) {
				mPopinWindow = iFrame;
				Close();
				}
			var popinWindow = document.createElement( "div" );
			var titleBar = CreateWindowTitleBar( thisObject, features );
			iFrame = CreateWindowIframe( thisObject, features );
			iFrame.id = elementID;
			popinWindow.appendChild( titleBar );
			popinWindow.appendChild( iFrame );
			popinWindow.className = kCssBase + "frame";
			with (popinWindow.style) {
				position = "absolute";
				display = "block";
				var scrollTop = (document.body.scrollTop || document.documentElement.scrollTop);
				var scrollLeft = (document.body.scrollLeft || document.documentElement.scrollLeft);
				var documentWidth = (document.documentElement.clientWidth || document.body.clientWidth || window.innerWidth || 0);
				var documentHeight = (document.documentElement.clientHeight || document.body.clientHeight || window.innerHeight || 0);
				if (gPopinWindow_MouseX) left = gPopinWindow_MouseX + scrollLeft + "px";
				if (gPopinWindow_MouseY) top = gPopinWindow_MouseY + scrollTop + "px";
				if (features.width) width = features.width + "px";
				if (features.height) height = features.height + "px";
				if (features.top) top = (Number(features.top) + scrollTop) + "px";
				if (features.left) left = (Number(features.left) + scrollLeft) + "px";
				// 30OCT2008 RFM - Added check to make sure window does not exceed window bounds
				if (parseInt(top) + parseInt(height) > (documentHeight + scrollTop)) {
					top = Math.max(scrollTop, (documentHeight+scrollTop) - (parseInt(height)+20)) + "px"
					}
				if (parseInt(left) + parseInt(width) > (documentWidth + scrollLeft)) {
					left = Math.max(scrollLeft, (documentWidth+scrollLeft) - (parseInt(width)+20)) + "px"
					}
				}
			document.body.appendChild( popinWindow );
			// If default style sheet was not included, include it
			if (!popinWindow.style.borderStyle && !gSheetAdded) {
				createStyleSheet( GetScriptDirectory() + "PopinWindow.css", 0 );
				// only add once
				gSheetAdded = true;
				}
			try {
				$(popinWindow).draggable();
				if (features.resizable) { 
					$(popinWindow).resizable( { stop:function( e, ui) {
						with (iFrame.style) {
							width = popinWindow.offsetWidth + "px";
							height = (popinWindow.offsetHeight - titleBar.offsetHeight) + "px";
							}
						titleBar.style.width = iFrame.style.width;
						} });
					}
				}
			catch (error) {
				}
			return iFrame;
			}
		catch (error) {
			alert( "PopinWindow.CreatePopinWindow: " + error.description  );
			}
		return { focus:function(){} };
		}
	
	// 14JAN2009 RFM - Need to ruturn mPopinWindow to calling window.open to process focus
	return mPopinWindow;
	}

//-----------------------------------------------------------
// onmousemove Handler
// Required to save mouse position so popin can me opened
// near teh mouse
//-----------------------------------------------------------
document.onmousemove = window.onmousemove  = function(event) {
	if (!event) event = window.event; 
	gPopinWindow_MouseX = (event.x || event.clientX || 0);
	gPopinWindow_MouseY =  (event.y || event.clientY || 0);
	}
