/***************************************************************************
 [PopupTemplate.js]
 
 Copyright (C) 2004 Next IT Corporation, Inc. Spokane, WA. All Rights Reserved. 
 This document is confidential work and intellectual property of Next IT 
 Corporation. Permission to copy, distribute or use any portion of this file 
 is prohibited without the express written consent of Next IT Corporation.
 
 Notes:
	This file extends "AgentTemplate.js" to add popup functionality

*****************************************************************************/

function PopupTemplate() {}

// Add Parent window functionality (for doing layout to parent)
PopupTemplate.ParentWindow = null;
PopupTemplate.DoLayout = true;

// Dimensions and position of the window
PopupTemplate.Width = 250;
PopupTemplate.Height = 300;
PopupTemplate.Top = 0;
PopupTemplate.Left = 0;

PopupTemplate.Align = 'Left'; // Left or Right
PopupTemplate.ParentWidth = -1; // -1 or means fill rest of window width
PopupTemplate.ParentHeight = -1; // -1 means fill rest of window height

PopupTemplate.FocusMinimizedPopup = true;

//////////////////////////////////////
// Setup
//////////////////////////////////////
PopupTemplate.Init = function()
{
	if (PopupTemplate.DoLayout) // Some agents are positioned absolutely on-top
	{
		// Layout self
		PopupTemplate.DoLayout();
		
		// Setup window if running in popup mode
		if (window.opener != null)
		{
			PopupTemplate.ParentWindow = window.opener;
			
			var windowOpen = false;
			try // getting around permission problem on closed windows
			{
				windowOpen = (window.opener.closed == false);
			}
			catch(e) { windowOpen = false; }
			
			if (windowOpen)
			{
				PopupTemplate.LayoutParent();
			}
		}
	}
	else
	{
	    if (window.opener != null)
	    {
	        PopupTemplate.ParentWindow = window.opener;
	    }
	}

	// Setup event handlers
	Agent.attachEvent("OnRedirect", PopupTemplate.Redirect);
};
AddListener(window, "onload", PopupTemplate.Init); // Init on every load

//////////////////////////////////////
// Public Methods
//////////////////////////////////////

PopupTemplate.DoLayout = function()
{
	var top = PopupTemplate.Top; 
	var left = PopupTemplate.Left;
	var w = PopupTemplate.Width;
	var h = PopupTemplate.Height;
		
	if ( !IsNumeric(w) && w.indexOf('%') > -1)
	{
		w = w.substring(0,w.indexOf('%'));
		w = screen.availWidth*w/100;
	}
	if ( !IsNumeric(h) && h.indexOf('%') > -1)
	{
		h = h.substring(0,h.indexOf('%'));
		h = (screen.availHeight*h/100) - top;
	}
	
	// Make sure they're numeric
	w = parseInt(w);
	h = parseInt(h);
	
	w = (w<125)?125:w; // Minimum of 125, since browser wont behave well below that width usually
	
	var parentWidth;
	if( PopupTemplate.ParentWidth > 0)
	{
		parentWidth = parseInt(PopupTemplate.ParentWidth);
	}
	else
	{
		parentWidth = screen.availWidth - w;
	}
	
	if (PopupTemplate.Align == 'Right') // Position on the left or right?
	{
		if ( parentWidth + parseInt(w) > screen.availWidth) // If not enough space, align on the right screen edge
		{
			left = screen.availWidth - w;
		}
		else // Align to the right of the parent window
		{
			left = parentWidth;
		}
	}
	
	// Remember pixel values (no percentages after first run)
	PopupTemplate.Width = w;
	PopupTemplate.Height = h;

	try
	{
		window.moveTo(left, top); // In IE, this doesn't calculate visible space properly while using 'localhost' (will get behind taskbar)
		window.resizeTo(w, h);
	}
	catch(e)
	{}
};
PopupTemplate.LayoutParent = function()
{
	var win = PopupTemplate.ParentWindow;
	/*var offset = (window.outerWidth)?window.outerWidth:document.body.clientWidth+12; // guesstimate for IE
	var w=screen.availWidth-offset;
	var h=screen.availHeight;*/
	var w;
	var h;
	
	// set parent width
	if (PopupTemplate.ParentWidth > 0)
	{
		w = PopupTemplate.ParentWidth;
	}
	else
	{
		w = screen.availWidth-PopupTemplate.Width;
	}
	
	// set parent height
	if (PopupTemplate.ParentHeight > 0)
	{
		h = PopupTemplate.ParentHeight;
		if (h > screen.availHeight)
		{
			h = screen.availHeight;
		}
	}
	else
	{
		h = screen.availHeight;
	}
	
	
	var left;
	if (PopupTemplate.Align == 'Right') // Leave space on left or right side?
	{
		left = 0;
	}
	else
	{
		// figure agent window overlap if there's not enough width for both
		if (PopupTemplate.Width + parseInt(w) > screen.availWidth)
		{
			left = screen.availWidth - w;
		}
		else
		{
			left = PopupTemplate.Width;
		}
	}
	try
	{
		win.moveTo(0,0); // This helps it work better when it's maximized... (IE seems to get confused sometimes)
		win.resizeTo(w,h);
		win.moveTo(left, 0);
	}
	catch(e) // Permission denied? Happens if we change out of our domain, we should make sure it doesn't happen in any other situation, or comment out throw below
	{
		if ( e.message )
		{
			if(e.message.indexOf('denied') == -1)
			{
				throw e;
			}
		}
		else
		{
			if( e.toString().indexOf('denied') == -1 )
			{
				throw e;
			}
		}

	}
};
PopupTemplate.Redirect = function(url)
{
	var win = PopupTemplate.ParentWindow;
	//if (win == null)
		//location.href = url; // Should check that we stay on our site, if not, do popup?
	var windowClosed = false;
	try // getting around permission problem on closed windows
	{
		windowClosed = (win == null || win.closed);
	}
	catch(e) { windowClosed = true; }
	
	if (windowClosed) // Parent window was closed or never open, open it again
	{
		var width = screen.availWidth - AgentTemplate.Width;
		var height = screen.availHeight;
	
		var popup = window.open('', 'parent', 'width='+width+'px,height='+height+'px,scrollbars=yes,menubar,resizable,toolbar,status,resizeable,location=yes" ');


		if(popup == null)
		{
		    var AgentName = AgentTemplate.AgentName;
		    if(AgentName == null || AgentName == '')
		        AgentName = "I";

            alert(AgentName + ' tried to open a window, but was stopped by a pop-up blocker.\r\nPlease allow pop-ups for this site and ask your question again.');
            PopupTemplate.ParentWindow = null;
		}
		else
		{
		    PopupTemplate.ParentWindow = popup;
		    PopupTemplate.LayoutParent();
		}
	}
	if (PopupTemplate.ParentWindow != null) // Make sure popup was successful
	{
		// Redirect parent window
		PopupTemplate.ParentWindow.location.href = url;
		
		//if browser is minimized or behind other browsers bring it to the front.  Works in IE, only works in Firefox if they have the browser setting "dom.disable_window_flip=false"
		if (PopupTemplate.FocusMinimizedPopup)
		{	
			PopupTemplate.ParentWindow.focus();
			self.focus();
		}		

	}
};