////////////////////////////////////////////////////////////////////////////////
// utilities.js - 
////////////////////////////////////////////////////////////////////////////////

Utilities = { };

////////////////////////////////////////////////////////////////////////////////
// Function:    IncludeJavaScript
// Purpose:     Will include all javascript files requested
// Returns:     No Return
////////////////////////////////////////////////////////////////////////////////
Utilities.IncludeJS = function( aFilePaths )
{
	for ( var i = 0; i < aFilePaths.length; i++ )
	{
		document.write( "<script type='text/javascript' src='" + aFilePaths[ i ] + "'></script>" );
	}
}


////////////////////////////////////////////////////////////////////////////////
// Function:    IncludeCSS
// Purpose:     Will include all css files requested
// Returns:     No Return
////////////////////////////////////////////////////////////////////////////////
Utilities.IncludeCSS = function( aFilePaths )
{
	for ( var i = 0; i < aFilePaths.length; i++ )
	{
		document.write( "<link href='" + aFilePaths[ i ] + "' rel='stylesheet' type='text/css' />" );
	}
}


////////////////////////////////////////////////////////////////////////////////
// Function:    GetElement
// Purpose:     Shorthand for "document.getElementById"
// Returns:     Will return requested element.  If not found, returns ""
////////////////////////////////////////////////////////////////////////////////
Utilities.GetElement = function( iID )
{
	return document.getElementById( iID );
}


////////////////////////////////////////////////////////////////////////////////
// Function:    Debug
// Purpose:     Will print a debug message
// Returns:     No Return
////////////////////////////////////////////////////////////////////////////////
Utilities.Debug = function( szDisplayValue )
{
	this.GetElement( "iDebug" ).innerHTML += szDisplayValue + "<br style='clear:both' />";
}


////////////////////////////////////////////////////////////////////////////////
// Function:    Toggle
// Purpose:     Toggle visibility of given element
// Returns:     No Return
////////////////////////////////////////////////////////////////////////////////
Utilities.Toggle = function( iID )
{
	this.GetElement( iID ).style.display = ( ( this.GetElement( iID ).style.display == "block" ) ||
											 ( this.GetElement( iID ).style.display == "" ) ) ? 'none' : 'block';
}


////////////////////////////////////////////////////////////////////////////////
// Function:    CreateElement
// Purpose:     Create an element
// Returns:     The created element
////////////////////////////////////////////////////////////////////////////////
Utilities.CreateElement = function( szElement, pObj )
{
	var pElement = document.createElement( szElement );
	for ( pProp in pObj )
	{
		pElement[ pProp ] = pObj[ pProp ];
	}
	return pElement;
}


////////////////////////////////////////////////////////////////////////////////
// Function:    AppendChild
// Purpose:     Appends a child elemnt to a parent
// Returns:     The parent element if there was a child to attach
////////////////////////////////////////////////////////////////////////////////
Utilities.AppendChild = function( )
{
	if ( this.AppendChild.arguments.length > 1 )
	{
		var pParent = this.AppendChild.arguments[ 0 ];
		for ( i = 1; i < this.AppendChild.arguments.length; i++ )
		{
			if ( this.AppendChild.arguments[ i ] )
			{
				pParent.appendChild( this.AppendChild.arguments[ i ] );
			}
		}
		return pParent;
	}
	else
	{
		return null;
	}
}


////////////////////////////////////////////////////////////////////////////////
// Function:    RemoveChildren
// Purpose:     Removes all children of the given element
// Returns:     No return
////////////////////////////////////////////////////////////////////////////////
Utilities.RemoveChildren = function( pNode )
{
	if ( pNode == null )
	{
		return;
	}
	
	while ( pNode.hasChildNodes( ) )
	{
		pNode.removeChild( pNode.firstChild );
	}
}


////////////////////////////////////////////////////////////////////////////////
// Function:    AddListener
// Purpose:     Adds an event listener
// Returns:     True if successful
////////////////////////////////////////////////////////////////////////////////
Utilities.AddListener = function( pObj, szEventName, pListener )
{
	if ( pObj.attachEvent )
	{
		pObj.attachEvent( "on" + szEventName, pListener );
	}
	else if ( pObj.addEventListener )
	{
		pObj.addEventListener( szEventName, pListener, false );
	}
	else
	{
		return false;
	}
	return true;
}


////////////////////////////////////////////////////////////////////////////////
// Function:    RemoveListener
// Purpose:     Removes an event listener
// Returns:     True if successful
////////////////////////////////////////////////////////////////////////////////
Utilities.RemoveListener = function( pObj, szEventName, pListener )
{
	if ( pObj.detachEvent )
	{
		pObj.detachEvent( "on" + szEventName, pListener );
	}
	else if ( pObj.removeEventListener )
	{
		pObj.removeEventListener( szEventName, pListener, false );
	}
	else
	{
		return false;
	}
	return true;
}


////////////////////////////////////////////////////////////////////////////////
// Function:    ChangeOpacity
// Purpose:     Changes the opacity of the given element
// Returns:     No return
////////////////////////////////////////////////////////////////////////////////
Utilities.ChagneOpacity = function( fOpacity, iID )
{
	var pObject = Utilities.GetElement( iID ).style;
	pObject.opacity = fOpacity;
	pObject.MozOpacity = fOpacity;
	pObject.KhtmlOpacity = fOpacity;
	pObject.filter = "alpha( opacity = " + fOpacity + " )";
}


////////////////////////////////////////////////////////////////////////////////
// Function:    Log
// Purpose:     Logs messages to firebug (FIREFOX ONLY)
// Returns:     No return
////////////////////////////////////////////////////////////////////////////////
/*Utilities.Log = function( szMessage, szType )
{
	if ( navigator.userAgent.indexOf( 'Firefox' ) != -1 )
	{
		switch( szType )
		{
			case "log":
				console.log( szMessage );
				break;
			case "debug":
				console.debug( szMessage );
				break;
			case "info":
				console.info( szMessage );
				break;
			case "warn":
				console.warn( szMessage );
				break;
			case "error":
				console.error( szMessage );
				break;
			default:
				console.log( "Incorrect value for Utilities.Log( )" );
				break;
		}
	}
}*/

////////////////////////////////////////////////////////////////////////////////
// Function:    IncludeBrowserStyle
// Purpose:     Detects the browser and includes it's stylesheet
// Returns:     No return
////////////////////////////////////////////////////////////////////////////////
Utilities.IncludeBrowserStyle = function( )
{
	// Include browser specific style sheets
	var szBrowser = BrowserDetect.browser;
	var szVersion = BrowserDetect.version;
	var szOS = BrowserDetect.OS;
	var aFiles = new Array( );
	
	switch( szBrowser )
	{
		case "Firefox":
		{
			if ( szOS == "Mac" )
			{
				aFiles.push( "css/browserSpecific/firefoxMac.css" );
				break;
			}
			else if ( szOS == "Windows" )
			{
				aFiles.push( "css/browserSpecific/firefoxWin.css" );
				break;
			}
			else if ( szOS == "Linux" )
			{
				aFiles.push( "css/browserSpecific/firefoxLin.css" );
				break;
			}
		}
		case "Safari":
		{
			if ( szOS == "Mac" )
			{
				aFiles.push( "css/browserSpecific/safariMac.css" );
				break;
			}
			else if ( szOS == "Windows" )
			{
				aFiles.push( "css/browserSpecific/safariWin.css" );
				break;
			}
			else if ( szOS == "iPhone/iPod" )
			{
				aFiles.push( "css/browserSpecific/safariPhone.css" );
				break;
			}
		}
		case "Opera":
		{
			if ( szOS == "Windows" )
			{
				aFiles.push( "css/browserSpecific/operaWin.css" );
				break;
			}
			else if ( szOS == "Mac" )
			{
				aFiles.push( "css/browserSpecific/operaMac.css" );
				break;
			}
		}
		case "Explorer":
		{
			if ( szVersion == "6" )
			{
				aFiles.push( "css/browserSpecific/ie6.css" );
				break;
			}
			else if ( szVersion == "7" )
			{
				aFiles.push( "css/browserSpecific/ie7.css" );
				break;
			}
			else if ( szVersion == "8" )
			{
				aFiles.push( "css/browserSpecific/ie8.css" );
				break;
			}
		}
		case "Chrome":
		{
			aFiles.push( "css/browserSpecific/chrome.css" );
			break;
		}
		default:
		{
			aFiles.push( "css/browserSpecific/safariMac.css" );
			break;
		}
	}
	
	Utilities.IncludeCSS( aFiles );
}







