/*
 * Name:
 *	global_functions.js
 *
 * Description:
 *	Defines global javascript functions
 *
 * Pre-conditions:
 *	None
 *
 * Post-conditions:
 *	Defines the following functions:
 *
 * Log:
 *	Shaunak Kashyap		11/21/2005
 *	- Creation
 *	Doug Mears			4/28/2006
 *	- added functions:
 *		getBrowser, getOS, checkUserAgent, bVer, makeSream, changeStreamReload
 *	Toan Nguyen-Dinh	5/11/2006
 *	- added function:
 *		embedFlash
 
 */

//Set global variables to be used by more than one function
//needs to be set within script
//delay, numelements depend on  php elements

var delay = new Array();
var numElements = new Array();
var index = new Array();
var timeOutID = new Array();
var isRunning = new Array();



/*
 * Name:
 *	getElement
 *
 * Description:
 *	Returns an element searched by name or id
 *
 * Pre-conditions:
 *	elName		REQUIRED	Id or name of element
 *
 * Post-conditions:
 *	The element is returned
 *
 * Log:
 *	Shaunak Kashyap		11/21/2005
 *	- Creation
 *
 */

function getElement(elName) {

	if (document.getElementByName) {
		
		return document.getElementByName(elName);
		
	} else if (document.getElementById) {
		
		return document.getElementById(elName);
		
	} else {
		
		return false;
		
	} 
	
} // END function - getElement

/*
==================================================================
ltrim(string) : Returns a copy of a string without leading spaces.
==================================================================
*/
function ltrim(str)
/*
   PURPOSE: Remove leading blanks from our string.
   IN: str - the string we want to ltrim
*/
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      // We have a string with leading blank(s)...

      var j=0, i = s.length;

      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;
}

/*
==================================================================
rtrim(string) : Returns a copy of a string without trailing spaces.
==================================================================
*/
function rtrim(str)
/*
   PURPOSE: Remove trailing blanks from our string.
   IN: str - the string we want to rtrim

*/
{
   // We don't want to trip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with trailing blank(s)...

      var i = s.length - 1;       // Get length of string

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;


      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }

   return s;
}

/*
=============================================================
trim(string) : Returns a copy of a string without leading or trailing spaces
=============================================================
*/
function trim(str)
/*
   PURPOSE: Remove trailing and leading blanks from our string.
   IN: str - the string we want to trim

   RETVAL: A trimmed string!
*/
{
   return rtrim(ltrim(str));
}


/*
 * Name:
 *	getBrowser
 *
 * Description:
 *	gets and returns a string representing the user's browser
 *
 * Pre-conditions:
 *
 * Post-conditions:
 *
 * Author:
 *		Doug Mears		4/28/06
 *
 * Updates:
 *
 */

function getBrowser()
{
	
	var browser;
	
	//begin if checks for browser
	if (checkUserAgent('konqueror'))
	{
		browser = "Konqueror";
	}
	else if (checkUserAgent('safari')) browser = "Safari";
	else if (checkUserAgent('omniweb')) browser = "OmniWeb";
	else if (checkUserAgent('opera')) browser = "Opera";
	else if (checkUserAgent('webtv')) browser = "WebTV";
	else if (checkUserAgent('icab')) browser = "iCab";
	else if (checkUserAgent('msie')) browser = "IE"
	else if (checkUserAgent('firefox')) browser = "Firefox";
	else if (!checkUserAgent('compatible'))
	{
		browser = "Netscape"
	}
	else browser = "unknown";
	
	//return browser string
	return browser;
} //END function getBrowser

/*
 * Name:
 *	getOS
 *
 * Description:
 *	gets and returns a string representing the user's operating system
 *
 * Pre-conditions:
 *
 * Post-conditions:
 *
 * Author:
 *		Doug Mears		4/28/06
 *
 * Updates:
 *
 */
function getOS()
{
	var OS;
	
	//begin if checks to get the operating system
	if (checkUserAgent('konqueror'))
	{
		OS = "Linux";
	}
	else if (checkUserAgent('linux')) OS = "Linux";
	else if (checkUserAgent('x11')) OS = "Unix";
	else if (checkUserAgent('mac')) OS = "Mac"
	else if (checkUserAgent('win')) OS = "Windows"
	else OS = "unknown";

	//return the operating system string
	return OS;
} //END function getOS

/*
 * Name:
 *	checkUserAgent
 *
 * Description:
 *	figures out if the given string is in the navigator.userAgentString
 *	used to detect browser..and os info
 *
 * Pre-conditions:
 *
 * Post-conditions:
 *   return value > 0 means there was a match
 * Author:
 *		Doug Mears		4/28/06
 *
 * Updates:
 *
 */

function checkUserAgent(string)
{
	//return 1 or more (true) if userAgent contains input string
	return (navigator.userAgent.toLowerCase().indexOf(string) + 1);
}

/*
 * Name:
 *	bVer
 *
 * Description:
 *	gets the browser version
 *
 * Pre-conditions:
 *
 * Post-conditions:
 *   returns a float representation of the navigator's version
 * Author:
 *		Doug Mears		4/28/06
 *
 * Updates:
 *
 */

function bVer() {
  // return version number (e.g., 4.03)
  return parseFloat(navigator.appVersion)
} //END function bVer

/*
 * Name:
 *	makeStream
 *
 * Description:
 *	makeStream is an object contructor whose data holds the url and name
 *	usage: new makeStream('myfile.asx', 'Episode 1');
 *
 * Pre-conditions:
 *
 * Post-conditions:
 *  sets and creates an object with the property values url and name
 * Author:
 *		Doug Mears		4/28/06
 *
 * Updates:
 *
 */
function makeStream(url, name) {
	this.url = url;
	this.name = name;
} //END function - make stream

/*
 * Name:
 *	changeStreamReload
 *
 * Description:
 *	changes the current stream for Windows Internet Explorer users
 *	reloads the page for other users
 *
 * Pre-conditions:
 *		url					path to php/asx file to use in the stream
 *		episodeName			name of episode
 *		episodeDescription	description of episode
 *		reloadQuery			contains string with episode ID to reload with
 * Post-conditions:
 *   either changes video stream or reloads the page with a query 
 *	 with the episode id
 * Author:
 *		Doug Mears		4/28/06
 *
 * Updates:
 *
 */
function changeStreamReload(url, episodeName, episodeDescription, reloadQuery)
{
	var versionCode = bVer();
	var mac = (navigator.userAgent.indexOf("Mac")!=-1);
	var activeX = (getBrowser() == 'IE' && versionCode >= 4.0) ? true : false; 

	//if user is using Internet Explorer for Windows and has activeX controls installed
	if (activeX && !mac) //for IE
	{
		
		//insert episodeName and episodeDescription into respective divs
		getElement('episodeNameDiv').innerHTML = episodeName;
		getElement('episodeDescriptionDiv').innerHTML = episodeDescription;
		mediaPlayer.autoStart = true;
   		mediaPlayer.URL = url;

	} //END if user is using Internet Explorer for Windows and has activeX controls installed
	//else reload window
  	else
  	{
  		window.location.href = reloadQuery;
  	} //END else reload window
} //END function changeStreamReload

/*
 * Name:
 *	embedFlash
 * 
 * Description:
 *	prints out the HTML code using javascript instead of embedding the Flash object directly into
 *	the HTML code. This is a workaround for the ActiveX issue in IE that needs the ActiveX content
 *	to be activated before it can be used. The workaround removes the need for activation
 *
 * Pre-conditions:
 *		embedString			String containing all tags holding the Flash object in the HTML page
 *								i.e. "<object ...><param1...><param2...><embed...></embed></object>"
 *
 * Post-conditions:
 *	The Flash object is embedded in the webpage while avoiding the ActiveX issue
 *
 * Functions:
 *	None
 *
 * Log:
 *	Toan Nguyen-Dinh		5/11/2006
 *		- Creation
 * 
 */
function embedFlash(embedString)
{
	//echo the string into the page
	document.write(embedString);
}