/*
 * iframe.js
 * author: Joel Stevick
 * date: 4.2.07
 * Copyright (c) 2007, CorData, Inc
 * Program Name: C.H.I.P. - CorData Hierarchical Inference Program
 */

/*
 * Get the document for an iframe
 */
function
Get_Iframe_Doc(iframeElement)
{
	if (iframeElement == null) {
		return null;
	}
		
    var IFrameDoc = null; 
    if (iframeElement.contentDocument) { 
		 /* For NS6 */
        IFrameDoc = iframeElement.contentDocument; 
    } else if (iframeElement.contentWindow) { 
		 /* For IE5.5 and IE6 */
		 IFrameDoc = iframeElement.contentWindow.document; 
    } else if (iframeElement.document) { 
		 /* For IE5 */
		IFrameDoc = iframeElement.document; 
	}
	return IFrameDoc;
}

/*
 * Load an Iframe
 */
function
Load_Iframe(iframeid, url)
{

	iFrameDoc = Get_Iframe_Doc(document.getElementById(iframeid));
	
	if (iFrameDoc)
		iFrameDoc.location.replace(url);
}

/*
 * Update message content (part 2)
 */
function
Update_Message_Content_2(completionObject) 
{
	/*
	 * Recover parameters
	 */
	var messageid = completionObject.messageid;
	var caller_completionObject = completionObject.caller_completionObject;
	var contentURL = completionObject.results;

	if (contentURL == "") {
		contentURL = 'blank.html';
	}

	/*
	 * Refresh the iFrame
	 */
	setTimeout('Load_Iframe(\'messagecontentiframe\', \'' + contentURL + '\')', 500);
						  
	/*
	 * Call the caller's completion routine
	 */
	if (caller_completionObject)
		caller_completionObject.AjaxCompletion(caller_completionObject);
}

/*
 * Update message content
 */
function
Update_Message_Content(messageid, caller_completionObject) 
{
	parameters = new Object;
	parameters['messageid'] = messageid;

	/*
	 * Store parameters into completion context
	 */
	var completionObject = new Object;
	completionObject.AjaxCompletion = Update_Message_Content_2;
	completionObject.caller_completionObject = caller_completionObject;

	Ajax_Call_Web_Method_async('GetMessageContentURL', parameters, completionObject);
}

/*
 * Scan for windows that have been closed
 */
var expandedWindow = null;
var thumbnailSrc = null;
var expandedSrc = null;
function Scan_For_Closed_Expanded_Window() {
	try {
			
		/*
		 * Has the expanded window been closed?
		 */
		 if (expandedWindow.closed) {
		    /*
		     * Yes, re-activate the thumbnail window
		     */						 
			if (thumbnailSrc != null && thumbnailSrc != undefined) {
			    var iFrameDoc = Get_Iframe_Doc(document.getElementById('messagecontentiframe'));
				iFrameDoc.location=thumbnailSrc;
			}

			/*
			* Show the "Expand" link
			*/
			document.getElementById('expandlink').style.visibility='visible';
			document.getElementById('expandlink').style.display = 'block';
			/*
			* We are done
			*/
			return;
		 }
		/*
		 * Schedule a timer to check for the window closing
		 */
		setTimeout("Scan_For_Closed_Expanded_Window()", 200);
	} catch (ex) {}
	
}
/*
 * Close the thumbnail
 */
function 
Reset_Thumbnail(url)
{
	var iFrameDoc = Get_Iframe_Doc(document.getElementById('messagecontentiframe'));

	try
	{
		/*
		 * Reset the URL for the thumbnail to a blank document so that any audio stops playing
		 */
		iFrameDoc.location=url;
		
		
	} catch (e) {}
}
/*
 * Open an iframe in a new browser window
 */
function
Open_Iframe_In_Browser(src, src2)
{
	var iFrameDoc = Get_Iframe_Doc(document.getElementById('messagecontentiframe'));
	
	try
	{
		/*
		 * Reset the URL for the thumbnail to a blank document so that any audio stops playing
		 */
		Reset_Thumbnail('blank.html');
		
		/*
		 * Hide the "Expand" link
		 */
		document.getElementById('expandlink').style.visibility='hidden'; 
		
		
	} catch (e) {}

	/*
	 * Open the expanded window
	 */
	expandedWindow = window.open(src, '_blank', 'menubar=yes,toolbar=yes,status=yes,resizable=yes,scrollbars=yes,width=840,height=640,location=yes');

	/*
	 * Store the source for the thumbnail
	 */
	thumbnailSrc = src2;
	expandedSrc = src;
	
	/*
	 * Schedule a timer to check for the window closing
	 */
	setTimeout("Scan_For_Closed_Expanded_Window()", 1000);
}
