//Auto Hookup
window.CurrentPage = new LICurrentPage();

/*############################################################
	Liquid Intelligence:
	 Current Page Class
 ############################################################*/
 function LICurrentPageDebugKeyPress()
 {
	var key = window.event.keyCode;
	if(key == 19){ debugger; return; }
	if(window.event.shiftLeft && window.event.ctrlLeft && key == 46) 
	{
		url = window.Application.RootPath + "/FirestormCommon/LIApplication/DebugConsole.html";
		window.open(url, null, "resizable=yes,status=yes,toolbar=no,menubar=no,location=no"); 
		return;
	}
	//alert(window.event.keyCode);
	//alert(window.event.shiftLeft);
 }
/*==========================
 Constructor
 ==========================*/ 
function LICurrentPage(win)
{
	if( (typeof(win) == "undefined") || (null == win) ) { win = window; }
	this.CurrentWindow = win;

	window.document.attachEvent("onkeydown", LICurrentPageDebugKeyPress);

	//Property Objects
	//---------------------
	this.Request	= new LIPageRequest		 (win, this);	
	this.Api		= new LICurrentPageApi	 (win, this);
	this.Export		= new LICurrentPageExport(win, this);

	//VB Proxy for LICurrentPage_ShowConfirmation
	win.execScript('function LICurrentPage_vbConfirm(n_message)\nLICurrentPage_vbConfirm = msgbox(n_message, vbYesNo + vbQuestion, "Confirmation") = vbYes\n end function ', "vbscript");
	
	//-------------------------
	// New CurrentPage Instance
	//-------------------------
	this.NewInstance = function LICurrentPage_NewInstance(win)
	{ 
		return new LICurrentPage(win); 
	}
	
	//-------------------------
	// Show Information
	//-------------------------
	this.ShowInformation = function LICurrentPage_ShowInformation(message)
	{

		this.CurrentWindow.alert(message);
	}

	//-------------------------
	// Set/Clear Page Busy
	//---------------------
	this.SetPageBusy = function LICurrentPage_SetPageBusy(continueFunctionName)
	{
		//Do we need a busy message?
		if(typeof(Application) != "undefined" && Application != null)
		{
			var busyMessage = Application.GetStringValue(CurrentPage.BusyMessage,"");
			
			if(busyMessage != "")
			{
				var busyDiv = document.getElementById( "LICurrentPageBusyDiv" );
				if(busyDiv == null)
				{	
					busyDiv = document.createElement("div");
					busyDiv.id = "LICurrentPageBusyDiv";
					busyDiv.style.position = "absolute";
					busyDiv.innerHTML = busyMessage;
					busyDiv.className = "liLoadingDiv";
					document.body.appendChild(busyDiv);
				}
			}
			
		}

		document.body.style.cursor = "wait";
		document.body.setCapture();
		
		if( typeof(Application) != "undefined" && typeof(continueFunctionName) != "undefined" )
		{
			window.setTimeout( continueFunctionName, 10 );
		}
	}

	this.ClearPageBusy = function LICurrentPage_ClearPageBusy()
	{
		//Clean up
		var busyDiv = document.getElementById( "LICurrentPageBusyDiv" );
		if(busyDiv != null){busyDiv.removeNode(true);}
		document.body.releaseCapture();
		document.body.style.cursor = "default";
	}

	//---------------------
	// Add To Favorites
	//---------------------
	this.AddToFavorites = function LICurrentPage_AddToFavorites(title)
	{
		Application.Html.AddFavorite(this.CurrentWindow, null, title);
	}

	//---------------------
	// Print
	//---------------------
	this.Print = function LICurrentPage_Print()
	{
		this.CurrentWindow.print();
	}
	
	//---------------------
	// Set Clipboard Text
	//---------------------
	this.SetClipboardText = function LICurrentPage_SetClipboardText(v)
	{
		window.clipboardData.setData("text", v);
	}

	//---------------------
	//SHOW CONFIMATION
	//---------------------
	this.ShowConfirmation = function LICurrentPage_ShowConfirmation(n_message)
	{
		return LICurrentPage_vbConfirm(n_message);
	}

	//---------------------
	// SHOW INPUTBOX
	//---------------------
	this.ShowInputBox = function LICurrentPage_ShowInputBox(titlebarText, prompt, initialValue, fieldName)
	{
		var args = new Object();
		args.Window = this.CurrentWindow;
		args.TitlebarText = titlebarText;
		args.Prompt = prompt;
		args.InitialValue = initialValue;
		args.FieldName = fieldName;

		var parameters = "scroll: no; help: no; status: no; resizable: yes; dialogHeight: 140px;"	
		var url = Application.ResolveUrl("~/FirestormCommon/LIGeneralForms/LIGetSimpleValue.aspx");
		url = url + "?globalClientContext=" + Application.GlobalClientContext;

		return this.ShowModalDialog(url, args, parameters);
	}

	//---------------------
	// SHOW MODAL DIALOG
	//---------------------
	this.ShowModalDialog = function LICurrentPage_ShowModalDialog(url, args, parameters)
	{
		return Application.ShowModalDialog( this.CurrentWindow, url, args, parameters );
	}

	//---------------------
	// SHOW MODELESS DIALOG
	//---------------------
	this.ShowModelessDialog = function LICurrentPage_ShowModelessDialog(url, args, parameters)
	{
		return Application.ShowModelessDialog( this.CurrentWindow, url, args, parameters );
	}

	//---------------------
	// Close the current window
	//---------------------
	this.Close = function LICurrentPage_Close()
	{
		if (this.CurrentWindow == Application.MasterWindow) {
			try { //by default try to imitate "Alt+F4" keypress
				var shell = new ActiveXObject("WScript.Shell");
				if ("object" == typeof(shell)) {
					this.CurrentWindow.focus();
					shell.SendKeys("%{F4}");
				}
			}
			catch (e) { //fallback to the default method
				this.CurrentWindow.close();
				document.body.innerHTML = "Please close this window";
			}
		}
		else {
			this.CurrentWindow.close();
		}
	}
}


/*############################################################
	Liquid Intelligence:
	 LI Current Page Export
 ############################################################*/
/*==========================
 Constructor
 ==========================*/ 
function LICurrentPageExport(win, page)
{
	this.CurrentWindow = win;
	this.CurrentPage = page;

	//---------------------
	// Email Html
	//---------------------
	this.EmailHtml = function LICurrentPageExport_EmailHtml(html)
	{
		try	
		{
			var outlook = this.GetOutlook();
			var msg = outlook.CreateItem(0);
			msg.HtmlBody = html;
			msg.Display();
		}
		catch(e)
		{
			this.CurrentPage.ShowInformation("A problem arose while creating email.\n" + e.message);
		}
	}

	//---------------------
	// Email Page Url
	//---------------------
	this.EmailPageUrl = function LICurrentPageExport_EmailPageUrl()
	{
		var html = "<a href='" + this.CurrentWindow.location + "'>" + this.CurrentWindow.document.title + "</a>";
		this.EmailHtml(html);
	}
	//---------------------
	// Email Page Body Url
	//---------------------
	this.EmailPageBodyUrl = function LICurrentPageExport_EmailPageBodyUrl(body)
	{
		var html = body;
		this.EmailHtml(html);
	}
	//---------------------
	// Get Outlook
	//---------------------
	this.GetOutlook = function LICurrentPageExport_GetOutlook()
	{
		return this.GetOfficeApplication("Outlook.Application", "Outlook");
	}

	//---------------------
	// Get Office Application
	//---------------------
	this.GetOfficeApplication = function LICurrentPageExport_GetOfficeApplication(appID, name)
	{
		//Create the Object
		try	{ 
			var app = new ActiveXObject(appID); }
		catch(e){
			var err = new Error(-1,"Instantiation Error");
			err.name = "InstantiationError";
			err.innerException = e;
			err.message = "Could not load " + name + ".\n" +
					"Common reasons include:\n\n" +
					" * " + name + " is not installed\n" +
					" * Your browser security settings disallow it.";

			throw err;
		}

		//Create the Object
		var version = (app.Version.length > 4) ? app.Version.substring(0,4) : app.Version;
		//this.CurrentPage.CurrentWindow.alert("Outlook version: " + version);
		if( version != "9.0" && version != "9.0." && version != "10.0" && version != "11.0")
		{
			var err = new Error(-1,"Version Error");
			err.name = "VersionError";
			err.message = "Either " + name + " 2000, "+ name + " XP, or " + name + " 2003\n is required for this feature.";
			throw err;
		}

		return app;
	}

}


/*############################################################
	Liquid Intelligence:
	 API Class
 ############################################################*/
/*==========================
 Constructor
 ==========================*/ 
function LICurrentPageApi(win, page)
{
	this.CurrentWindow = win;
	this.CurrentPage = page;
	LICurrentPageApi.prototype.ContentRegistry = new LICurrentPageContentRegistry(win, page);
}


/*############################################################
	Liquid Intelligence:
	 Content Registry Class
 ############################################################*/
/*==========================
 Constructor
 ==========================*/ 
function LICurrentPageContentRegistry(win, page)
{
	this.CurrentWindow = win;
	this.CurrentPage = page;
}

/*================================
 FUNCTION: ShowContentHistory
 ================================*/
function LICurrentPageContentRegistry_ShowContentHistory(contentGuid)
{

	var URL_ITEM_HISTORY_FORM  = Application.ResolveUrl( "~/FirestormCommon/LIGeneralForms/LIContentHistoryForm.aspx" );
	var ARGS_ITEM_HISTORY_FORM = "dialogWidth:650px;status:yes;resizable:yes;help:no;unadorned:yes";

	if (contentGuid == "" || contentGuid == null) 
	{ 
		Application.ShowInformation("No file selected to view history for.");
		return; 
	}
	
	var url = URL_ITEM_HISTORY_FORM + "?CR_GUID=" + contentGuid;
	url = url + "&globalClientContext=" + Application.GlobalClientContext;

	Application.ShowModalDialog(this.CurrentWindow, url, this.CurrentWindow, ARGS_ITEM_HISTORY_FORM);
}
LICurrentPageContentRegistry.prototype.ShowContentHistory = LICurrentPageContentRegistry_ShowContentHistory;


/*================================
 FUNCTION: ShowActionUI
 Arguments = Object with Expando Properties
 ================================*/
function LICurrentPageContentRegistry_ShowActionUI(n_action, n_contentGuid, n_parentGuid, n_arguments)
{
	return Application.Api.ContentRegistry.ShowActionUI(this.CurrentWindow, n_action, n_contentGuid, n_parentGuid, n_arguments);
}
LICurrentPageContentRegistry.prototype.ShowActionUI = LICurrentPageContentRegistry_ShowActionUI;

/*================================
 FUNCTION: DoAction
 Arguments = Object with Expando Properties
 ================================*/
function LICurrentPageContentRegistry_DoAction( contentGuid, action, comment )
{
	return Application.Api.ContentRegistry.DoAction( contentGuid, action, comment );
}
LICurrentPageContentRegistry.prototype.DoAction = LICurrentPageContentRegistry_DoAction;


/*================================
 FUNCTION: ShowCreationUI
 Arguments = Object with Expando Properties
 ================================*/
function LICurrentPageContentRegistry_ShowCreationUI(n_classGuid, n_parentGuid, n_arguments)
{
	return Application.Api.ContentRegistry.ShowCreationUI(this.CurrentWindow, n_classGuid, n_parentGuid, n_arguments);
}
LICurrentPageContentRegistry.prototype.ShowCreationUI = LICurrentPageContentRegistry_ShowCreationUI;

/*================================
 FUNCTION: GetActionUI
 Arguments = Object with Expando Properties
 ================================*/
function LICurrentPageContentRegistry_GetActionUI(n_inputGuid, n_action)
{
	return Application.Api.ContentRegistry.GetActionUI( n_inputGuid, n_action );
}
LICurrentPageContentRegistry.prototype.GetActionUI = LICurrentPageContentRegistry_GetActionUI;

/*================================
 FUNCTION: DownloadContent
 ================================*/
function LICurrentPageContentRegistry_DownloadContent(n_contentGuid,n_contentVersionGuid)
{ 
	var rawUrl = "~/FirestormCommon/Download/DownloadStreamer.ashx?CR_GUID=" + n_contentGuid + "&CR_VERSION_GUID=" + n_contentVersionGuid + "&method=attachment";
	var url = Application.ResolveUrl( rawUrl );

	var win = this.CurrentWindow;
	
	if (typeof(win.LIDownloaderFrame) != "undefined" && win.LIDownloaderFrame != null)
	{
		win.LIDownloaderFrame.removeNode(true);
		win.LIDownloaderFrame = null;
		CollectGarbage();
	}
	
	win.LIDownloaderFrame = this.CurrentWindow.document.createElement("iframe");
	win.LIDownloaderFrame.style.position = "absolute";
	win.LIDownloaderFrame.style.display = "none";
	win.LIDownloaderFrame.src = url;
	
	win.document.body.appendChild(win.LIDownloaderFrame);
	//this.CurrentWindow.open(url, null, "height=100; width=100; scrollbars=no; toolbar=no; titlebar=no; menubar=no; location=no;" );
}
LICurrentPageContentRegistry.prototype.DownloadContent = LICurrentPageContentRegistry_DownloadContent;


/*############################################################
	Liquid Intelligence:
	 Page Request Class
 ############################################################*/
/*==========================
 Constructor
 ==========================*/ 
function LIPageRequest(win, page)
{
	this.CurrentWindow = win;
	this.CurrentPage = page;

	//-----------------------------
	//FUNCTION: GetQueryString
	//Attempt to emulate the server 
	//QueryString function
	//-----------------------------
	this.QueryString = function LIPageRequest_QueryString(key)
	{
		return Application.Request.QueryString(this.CurrentWindow, key);
	}
}

/*############################################################
	Liquid Intelligence:
	 LIWebPageLocal
 ############################################################*/
function LIWebPageLocal(webPage)
{
	this.Url = webPage.Url;
	this.Width = webPage.Width;
	this.Height = webPage.Height;
}
LIWebPageLocal.prototype.SysName = "LIWebPageLocal";

LIWebPageLocal.prototype.ShowModal = function() 
{ 
	return CurrentPage.ShowModalDialog( this.Url, window, this.GetBaseModalStyle() );
}

LIWebPageLocal.prototype.ShowModeless = function() 
{ 
	return CurrentPage.ShowModelessDialog( this.Url, window, this.GetBaseModalStyle() );
}

LIWebPageLocal.prototype.GetBaseModalStyle = function() 
{ 
	return Application.GetBaseModalStyle(this.Width, this.Height);
}

/*############################################################
  Global convenience methods  
  ############################################################*/
//---------------------------
// Switch class on the element that generated
// current event to "highlighted button"
//---------------------------
function highlightButton()
{
	var div = highlight_privateFindDiv(this.CurrentWindow.event.srcElement);
	if(!div.disabled){ div.className = "LIToolButtonHL liInterfaceFontSmall"; }
}

//---------------------------
// Switch class on the element that generated
// current event to "normal button"
//---------------------------
function unhighlightButton()
{
	var div = highlight_privateFindDiv(this.CurrentWindow.event.srcElement);
	if(!div.disabled){ div.className = "LIToolButton liInterfaceFontSmall"; }
}

//---------------------------
// Find the nearest DIV in the element hierarchy.
// If DIV is not found 2 levels up, the element
// 2 levels up is used instead
//---------------------------
function highlight_privateFindDiv(element)
{
	if ("DIV" != element.tagName) {
		element = element.parentElement;
		if ("DIV" != element.tagName) {
			element = element.parentElement;
		}
	}
	return element;
}

//---------------------------
//---------------------------
function LiToolbarButtonClick()
{
	//make sure we are in context of an event
	//and prevent additional event handling
	//(e.g. following of "href" in <a> tags)
	if (!Application.HasValue(window.event)) { return; }
	Application.Html.CancelEvent(window.event);
	
	//find and unfocus the source element
	var button = event.srcElement;
	while (Application.HasValue(button) && button != button.parentElement) {
		if (Application.HasValue(button.LiToolbarCallback)) { break; }
		button = button.parentElement;
	}
	if (!Application.HasValue(button)) { return; }
	button.blur();

	//ignore disabled buttons
	if (button.className.indexOf("disabled") >= 0) { return; }
	if (button.disabled) { return; }

	//if present, invoke callback
	var callback = button.LiToolbarCallback;
	if ("string" == typeof(callback)) {
		callback = this.window[callbackName];
	}
	if (Application.HasValue(callback)) {
		callback();
	}
}
