/*CommunityIQ.com - copyright 1999*/

//global for history count - this keeps track of how many times help anchors have been clicked
var iHistCount = -1;

//Globals for the help popup
var popupHelp = false;
var visibleHelp = false;
var firstTimeHelp = true;


//function addHistCount - decrement counter to pass to goBack, which uses history.go
function addHistCount()
{
	iHistCount -= 1;
}

//function findFrame - finds a particular frame
function findFrame( frame, name )
{
	if ( frame.name == name )
	{
		return frame;
	}
	else
	{
		if ( frame.frames.length > 0 )
		{
			for ( var i = 0; i < frame.frames.length; i++ )
			{
				var newFrame = findFrame( frame.frames[i], name );
				if ( newFrame ) return newFrame;
			}
		}
	}
	return false;
}

//function - go back in the history (from the parent if necessary)
function goHistoryBack()
{
	if ( history.length > 1 )
	{
		history.back();
	}
	else
	{
		parent.history.back();
	}
}

//function - go back to the action bar
function goBack( url, frameName )
{
	url = url.replace( /\!\!/g, "%" );

	if ( typeof( frameName ) == "undefined" ) frameName = "main";
	
	var frame = findFrame( top, frameName );

	if ( frame )
	{
		frame.location.href = url;
	}
	else
	{
		top.location.href = url;
	}

}

//function showHelp - to display help text
function showHelp(file)
{
	options = "directories=no,menubar=no,location=no,status=no,toolbar=no,height=500,width=400,scrollbars=yes,resizable=yes";
	var w = window.open(file, "help", options, true);
	w.focus();
}

/*
 * Displays the help popup.  Calls 'displayHelp' after setting a delay
 */
function showPopup( wait, offset )
{
	if((!popupHelp) && (!visibleHelp))
	{
		if ( typeof( wait ) == "undefined" )
		{
			wait = 300;
		}
		
		popupHelp       = true;
		visibleHelp     = false;

		if ( document.layers )
		{
			helpPopupObject = document.layers['helpPopup'];
		}
		else if ( document.getElementById )
		{
			helpPopupObject = document.getElementById('helpPopup');
		}
		else if ( document.all )
		{
			helpPopupObject = document.all['helpPopup'];
		}
		
		if ( typeof( offset ) == "undefined" )
		{
			offset = 245;
		}

		if ( typeof( helpPopupObject ) != "undefined" )
		{
			if ( wait > 0 )
			{
				setTimeout( "displayHelp(" + offset + ")", wait );
			}
			else
			{
				displayHelp( offset );
			}
		}
	}
	
	return;
}


/*
 * Called by showPopup().  Repsponsible for actually displaying the help popup.
 */
function displayHelp( offset )
{
	if ( ( visibleHelp ) || ( !popupHelp ) )
		return;

	popupHelp   = false;
	visibleHelp = true;

	if ( typeof( offset ) == "undefined" )
	{
		offset = 245;
	}

	if ( document.layers )
	{
		if ( document.layers['helpPopup'] != null )
		{
			document.layers['helpPopup'].left = document.layers['helpPosition'].pageX - offset;
			document.layers['helpPopup'].top = document.layers['helpPosition'].pageY;
			document.layers['helpPopup'].visibility = "show";
		}
	}
	else if ( document.getElementById( 'helpPopup' ) != null )
	{
		var helpPopupElement = document.getElementById( 'helpPopup' );
		
		helpPopupElement.style.left = document.getElementById( 'helpPosition' ).offsetLeft - offset + 12;
		helpPopupElement.style.top  = document.getElementById( 'helpPosition' ).offsetTop + 5;
		
		if ( firstTimeHelp )  // This fixes a rendering bug in Nav 6
		{
			setTimeout( "document.getElementById( 'helpPopup' ).style.visibility = 'visible'", 50 );

			firstTimeHelp = false;
		}
		else
		{
			helpPopupElement.style.visibility = "visible";		
		}
	}
	else if ( document.all )
	{
		if ( document.all['helpPopup'] != null )
		{
			document.all['helpPopup'].style.left = document.all['helpPositionIE'].offsetLeft - offset;
			document.all['helpPopup'].style.top = document.all['helpPositionIE'].offsetTop - 5;
			document.all['helpPopup'].style.visibility = "visible";
		}
	}
}


/*
 * Hides the help popup
 */
function hidePopup()
{
	visibleHelp = false;
	popupHelp   = false;

	if ( document.layers )
	{
		if ( document.layers['helpPopup'] != null )
		{
			document.layers['helpPopup'].visibility = "hide";
		}
	}
	else if ( document.getElementById( 'helpPopup' ) != null )
	{
		document.getElementById( 'helpPopup' ).style.visibility = "hidden";
	}
	else if ( document.all )
	{
		if ( document.all['helpPopup'] != null )
		{
			document.all['helpPopup'].style.visibility = "hidden";
		}
	}

	return;
}


/*
 * Checks to see if the help popup can be timed out (ie. still within the popup delay time)
 */
function timeoutPopup()
{
	if((!visibleHelp) && (popupHelp))
	{
		hidePopup();
	}
}


/*
 * Displays the help popup.  Calls 'dispalyPleaseWait' after setting a delay
 */
function showPleaseWait( wait, vertOffset )
{	
	if ( ( typeof( wait ) == "undefined" )
		|| ( wait == "" ) )
	{
		wait = 800;
	}

	if ( ( typeof( vertOffset ) == "undefined" )
		|| ( vertOffset == "" ) )
	{
 		vertOffset = "-1";
	}
	
	setTimeout( "displayPleaseWait(" + vertOffset + ")", wait );

	return;
}


/*
 * Called by showPleaseWait().  Responsible for actually displaying the please wait popup.
 */
function displayPleaseWait( vertOffset )
{
	// kept for legacy reasons:
	if ( navigator.userAgent.indexOf( "MSIE 4" ) > -1  ) return;	
	
	hidePopup();

	if ( typeof( vertOffset ) == "undefined" ) vertOffset = -1;
	
	if ( document.layers ) // NN4 support only
	{
		if ( document.layers['pleaseWaitPopup'] != null )
		{
			var x = Math.round( ( innerWidth  / 2 ) - ( document.layers['pleaseWaitPopup'].clip.width  / 2 ) );
			var y = Math.round( ( innerHeight / 3 ) - ( document.layers['pleaseWaitPopup'].clip.height / 2 ) );

			document.layers['pleaseWaitPopup'].left = x;
			document.layers['pleaseWaitPopup'].top  = ( ( vertOffset < 0 ) ? 10 : vertOffset );
			document.layers['pleaseWaitPopup'].visibility = "show";
		}
	}
	else if ( document.getElementById )
	{
		if ( document.getElementById( 'adminPageDiv' ) != null ) document.getElementById( 'adminPageDiv' ).style.visibility = "hidden";		
		
		var pleaseWaitPopupElement = document.getElementById( 'pleaseWaitPopup' );
		
		if (window.innerWidth) {
			var x = Math.round( ( innerWidth  / 2 ) - ( pleaseWaitPopupElement.offsetWidth  / 2 ) );
			var y = Math.round( ( innerHeight / 3 ) - ( pleaseWaitPopupElement.offsetHeight / 2 ) );
		} else if (document.body.clientWidth) {
			var x = Math.round( ( document.body.clientWidth  / 2 ) - ( document.all['pleaseWaitPopup'].clientWidth  / 2 ) );
			var y = Math.round( ( document.body.clientHeight / 3 ) - ( document.all['pleaseWaitPopup'].clientHeight / 2 ) );
		}
		
		pleaseWaitPopupElement.style.left = x;
		pleaseWaitPopupElement.style.top  = ( ( vertOffset < 0 ) ? y : vertOffset );
		
		//setTimeout( "document.getElementById( 'pleaseWaitPopup' ).style.visibility = 'visible'", 50 );
		document.getElementById( 'pleaseWaitPopup' ).style.visibility = 'visible';
	}
	else if ( document.all )
	{
		if ( document.all['pleaseWaitPopup'] != null )
		{
			if ( document.all['adminPageDiv'] != null ) document.all['adminPageDiv'].style.visibility = "hidden";		
			
			var x = Math.round( ( document.body.clientWidth  / 2 ) - ( document.all['pleaseWaitPopup'].clientWidth  / 2 ) );
			var y = Math.round( ( document.body.clientHeight / 3 ) - ( document.all['pleaseWaitPopup'].clientHeight / 2 ) );

			document.all['pleaseWaitPopup'].style.left = x;
			document.all['pleaseWaitPopup'].style.top  = ( ( vertOffset < 0 ) ? y : vertOffset ); 
			document.all['pleaseWaitPopup'].style.visibility = "visible";		
		}
	}
	
	window.scrollTo(0, 0);
}

function redirectWithPleaseWait( url, vertOffset )
{
	if ( ( typeof( vertOffset ) == "undefined" )
		|| ( vertOffset == "" ) )
	{
 		vertOffset = "105";
	}	

	showPleaseWait( "", vertOffset );

	url = url.replace( /\!\!/g, "%" );
	
	window.location = url;
}

function exit(url)
{
	parent.location = url;
}

function easy_alert(label)
{
	alert(label+" is not content editable but can be configured\n through the My Site administrative page.");
} 

