function UiQueue()
{
	this.commands = new Array();
	this.isScheduled = false;
	
	this.resize = function()
	{
		this.requiresResize = true;
		this._scheduleTimeout();
	}
	
	this.schedule = function( fn, requiresResize )
	{
		this._schedule( fn );
		this.needsResize = this.needsResize || requiresResize;
		
		this._scheduleTimeout();
	}
	
	this._schedule = function( fn )
	{		
		for ( var i = 0; i < this.commands.length; ++i )
		{
			if ( this.commands[ i ] == fn ) return;
		}
		this.commands.push( fn );
	}
	
	this._scheduleTimeout = function()
	{
		if ( this.isScheduled ) return;
		this._actualScheduleTimeout();
	}

	this._actualScheduleTimeout = function()
	{
		var this_queue = this;
		window.setTimeout( function() { this_queue._processNext() }, 2 );
	}
	
	
	this._processNext = function()
	{
		if ( this.commands.length > 0 )
		{
			var fn = this.commands.shift();
			fn();
		}
		
		if ( this.commands.length == 0 )
		{
			if ( this.requiresResize )
			{
				_resizeThis();
				this.requiresResize = false;
			}
			this.isScheduled = false;
		}
		else
		{
			this._actualScheduleTimeout();
		}		
	}
}

var UI_QUEUE = new UiQueue();

function openPopUp( url, width, height, popUpName )
{	
	if ( ! popUpName ) popUpName = "_blank";
	
	var screenX;
	var screenY;
	if ( window.screenX || window.screenY )
	{
		screenX = window.screenX;
		screenY = window.screenY;
	}
	else if ( window.screenLeft || window.screenTop )
	{
		screenX = window.screenLeft;
		screenY = window.screenTop;
	}

	//since we are always going to be usign the auto-resize script
	//set the height/width to the min widths/heights
	if ( height == -1 ) height = 100;
	if ( width == -1 ) width = 100;	

	//IE automatically takes into account of the offset
	if ( navigator.appName != "Microsoft Internet Explorer" )
	{
		screenY += 25; 
	}
	screenX += 25; 
	
	var popUp = window.open( 
		url, popUpName,
		"height=" + height + ",width=" + width +
		",left=" + screenX + ",screenX=" + screenX +
		",top=" + screenY + ",screenY=" + screenY +
		",scrollbars=yes,resizable=yes,status=yes,menubar=yes" );
		
	popUp.focus();
}

function downloadAndTryClose( url, msecBeforeClose )
{
   if ( window.opener )
   {
      window.opener.document.location = url;
      window.opener.focus();
      setTimeout( "window.close();", msecBeforeClose );
   }
   else
   {
	   document.location.href = url;
   }
}


function findDiv( divID )
{	
	var divRef;	
	if( document.getElementById ) { divRef = window.document.getElementById(divID); }
	else if( document.all ) { divRef = window.document.all[divID]; }
	else divRef = document[divID];
	
	return divRef;
}

function injectDiv( tag, divID )
{
	var div = document.createElement( "div" );
	div.name = divID;
	div.id = divID;
	div.className = divID;
	
	if( tag.children ) 	//IE
	{
		var numChildren = tag.children.length;
		while ( tag.children.length != 0 )
		{
			var el = tag.children[ 0 ];
	
			tag.removeChild( el );
			div.appendChild( el );
		}
		
		tag.appendChild( div );
	}
	else if ( tag.childNodes ) //FireFox, etc.
	{
		var numChildren = tag.childNodes.length;
		while ( tag.childNodes.length != 0 )
		{
			var el = tag.childNodes[ 0 ];
	
			tag.removeChild( el );
			div.appendChild( el );
		}
		
		tag.appendChild( div );
	}
}

function schedule( fn )
{
	UI_QUEUE.schedule( fn );
}

function resizeAfter( fn )
{
	UI_QUEUE.schedule( fn, true );
}

function resizeThis()
{
	UI_QUEUE.resize();
}

/*
script based on: http://www.howtocreate.co.uk/perfectPopups.html
modifier: philip s
date: 07-25-2005
*/
function _resizeThis()
{
	//@todo
	//check to see if this is opened in tab
	//if so, return
	
	var divID = "resizableLink";
	var divRef = findDiv( divID );
	if ( ! divRef )
	{
		injectDiv( document.body, divID );
		divRef = findDiv( divID );
		if ( ! divRef )	{ return false; }
	}

	/* this works for Mozilla and Netscape, but not IE */
	if(window.sizeToContent)
  	{		
			window.sizeToContent();
			window.sizeToContent();
  					
  		var newWidth = window.outerWidth;
  		var newHeight = window.outerHeight;
  		var doResize = false;
  		
  		if( newWidth > window.screen.availWidth * .9)
  		{
  			newWidth = window.screen.availWidth * .9;
  			doResize = true;
  		}
  		else if( newWidth < window.screen.availWidth * .1)
  		{
  			newWidth = window.screen.availWidth * .1;
  			doResize = true;
  		}
  			
  		if( newHeight > window.screen.availHeight * .9 )
  		{
  			newHeight = window.screen.availHeight * .9;
  			doResize = true;
  		}
			else if( newHeight < window.screen.availHeight * .1 )
  		{
  			newHeight = window.screen.availHeight * .1;
  			doResize = true;
  		}
  		
  		if( doResize )
  			window.resizeTo( newWidth, newHeight + 20 );
			else
			{	
				//Resizing to take care of the non-existent scrollbars whose
				//sizes Firefox mistakeningly accounts for.
				//Some description of the bug can be found on
				//http://marc.baffl.co.uk/browser_bugs/firefox_overflow_out.html
				//This is caused by the fact that IE always has a vertical scrollbar
				//while Firefox renders one only when needed.
				window.resizeBy( 20, 20 );
			}
	
			return false;
		}

	/* resizing for IE */
	window.resizeTo( 0, 0 );
	
	var oWidth = divRef.clip ? divRef.clip.width : divRef.offsetWidth;
	var oHeight = divRef.clip ? divRef.clip.height : divRef.offsetHeight;
	if( !oHeight || !oWidth ) { return false; }
	
	window.resizeTo( oWidth + 200 , oHeight + 200 );
	var myWidth = 0, myHeight = 0;
	
	if( window.innerWidth ){
		myWidth = window.innerWidth; myHeight = window.innerHeight; }
	else
		{
			var doc = window.document.documentElement;
			
			if( doc && doc.clientWidth ){
				myWidth = doc.clientWidth; myHeight= doc.clientHeight; }
			else
				{
					var bod = window.document.body;
					if( bod && bod.clientWidth ){
						myWidth = bod.clientWidth; myHeight = bod.clientHeight; }
				}
		}

	if( window.opera && !document.childNodes )
	{ myHeight += 16; }
	
	divRef = findDiv( divID );
	var oWidth2 = divRef.clip ? divRef.clip.width : divRef.offsetWidth;
	var oHeight2 = divRef.clip ? divRef.clip.height : divRef.offsetHeight;

	var newWidth = oWidth2 + ( ( oWidth + 200 ) - myWidth );
	var newHeight = oHeight2 + ( (oHeight + 200 ) - myHeight );

	if(newWidth > window.screen.availWidth * .9 )
		newWidth = window.screen.availWidth  * .9;
	else if(newWidth < window.screen.availWidth * .1 )
		newWidth = window.screen.availWidth  * .1;
		
	if(newHeight > window.screen.availHeight  * .9 )
		newHeight = window.screen.availHeight  * .9;
	else if(newHeight < window.screen.availHeight  * .1 )
		newHeight = window.screen.availHeight  * .1;

	window.resizeTo( newWidth, newHeight );
	//if( window.focus ) { window.focus(); }
	
	isResizeTimeoutSet = false;
	return false;
}