// MEDIA PLAYER

	//window.onload = init;

	var W3CDOM = (document.createElement && document.getElementsByTagName);
	var mouseOvers = new Array();
	var mouseOuts = new Array();
	
function loadMediaPlayer()
{
	if(!W3CDOM) return;
	var nav = document.getElementById('controls');
	var imgs = nav.getElementsByTagName('img');

	for (var i=0;i<imgs.length;i++){
		
		imgs[i].onmouseover = mOver;
		imgs[i].onmouseout = mOut;
		
		var suffix = imgs[i].src.substring(imgs[i].src.lastIndexOf('.'));
		
		mouseOuts[i] = new Image();
		mouseOuts[i].src = imgs[i].src;
		
		mouseOvers[i] = new Image();
		mouseOvers[i].src = imgs[i].src.substring(0,imgs[i].src.lastIndexOf('.')) + "_over" + suffix;
		
		imgs[i].number = i;
	}
}
function mOver(){
	this.src = mouseOvers[this.number].src;
}

function mOut(){
	this.src = mouseOuts[this.number].src;
}


	// some variables to save
	var currentPosition;
	var currentVolume;
	var currentItem;
	var total;
	var loadCounter = 0;

	// these functions are caught by the JavascriptView object of the player.
	function setNewVolume(prm){
		var newVolume = prm;
		if(prm < 0){ newVolume = 1;}
		if(prm > 100){ newVolume = 100;}
		sendEvent('volume', newVolume);
	}
	
	function sendEvent(typ,prm) { thisMovie("videoPlayer").sendEvent(typ,prm); };
	
	// create a var to store the current time and total time
	var currTime = 0;
	var totalTime = 0;
	//
	var lastState = "";
	var currPid = "";
	// record the currFileName on load, set vid1 as default since it is hard coded into the flash and is not set in js as of now
	var currFileName = "What_is_Crohns_disease.flv";
	// listens for updates by the flash video player
	function getUpdate(typ,pr1,pr2,pid) {
		currPid = pid;
		if(typ == "time"){
			currentPosition = pr1;			
			if(pr1 == "0") {total = pr2;}
			var percent = Number(Math.round((pr1/total)*100));		
			document.getElementById("ctl00_ContentPlaceHolder1_progressBarImg").style.width = percent * 3 + "px";
			
			// due to bugs in the current player, do a check for NaN
			if (!isNaN(pr1) && !isNaN(pr2))
			{
				// update the current and total video time
				if (pr1 != 0)
				{
					currTime = pr1;
				}
				totalTime = pr2;	
			}
		}
		else if(typ == "volume") 
		{ 
			currentVolume = pr1;
		}
		else if(typ == "item")
		{
			currentItem = pr1;
			setTimeout("getItemData(currentItem)",100);
		}
		else if(typ == "state")
		{
			//alert("state : typ = " + typ + " : pr1 = " + pr1 + " : pr2 = " + pr2 + " : pid = " + pid);
			// will only recieve pr1 as 0 for stop / pause and 2 for play and seek
			if(pr1 == 0 && (currTime == 0 && totalTime == 0))
			{
				// initial load - skip this and use the actual load state
			}
			else if (pr1 == 0 && lastState != "close" && lastState != "rewind")
			{
				// pause or stop video
				// set it off to Omniture s.Media.stop(name, offset);
				//alert("s.Media.stop(name:"+currFileName+", offset:"+currTime+")");
				var s=s_gi(s_account);
				//s.Media.stop(currFileName, currTime);
				lastState = "stop";
			}
			else if (pr1 == 1)
			{
				// the video has finished playing
				// now call the Omniture s.Media.close(name);
				//alert("s.Media.close(name:"+currFileName+")");
				var s=s_gi(s_account);
				//s.Media.close(currFileName);
				lastState = "close";
			}
			// don't call play if the last state was open
			else if (pr1 == 2 && lastState != "open")
			{
				// playing the video
				// and set it off to Omniture tracking s.Media.play(name,offset);
				// if the last state is open then delay the play command
				if (lastState == "open")
				{
					setTimeout("delayPlay()", 500);
				}
				else
				{
					//alert("s.Media.play(name:"+currFileName+", offset:"+currTime+")");		
					var s=s_gi(s_account);
					//s.Media.play(currFileName, currTime);	
				}				
				lastState = "play";
			}
			else if (pr1 == 3)
			{
				// rewind video to beginning and reset the current time
				currTime = 0;
				// set the last state flag
				lastState = "rewind";
			}
		}
		else if(typ == "load")
		{						
			if (pr1 == 0)
			{
				// the load has started and reported 0 percent load
				// now set it off to Omniture tracking s.Media.open(name, length, playerName, player);
				//alert("s.Media.open(name:"+currFileName+", length:"+totalTime+", playerName:"+pid+")");
				// delay the open call to make sure we have the total time reported
				setTimeout("delayOpen()", 400);
				//s.Media.open(currFileName, totalTime, pid);
				lastState = "open";
			}
			else if (pr1 == 100)
			{
				// the video has fully loaded
			}
		}
		else if (typ == "datarate")
		{		
			// no datarate tracking at this time
		}
		else if (typ == "size")
		{
			// no size tracking at this time
		}
		if(pid != "null") {	};		
	};
	
	//
	function delayOpen()
	{
		//alert("s.Media.open(name:"+currFileName+", length:"+totalTime+", playerName:"+currPid+")");
		var s=s_gi(s_account);
		//s.Media.open(currFileName, totalTime, currPid);
	}
	//
	function delayPlay()
	{
		//alert("s.Media.play(name:"+currFileName+", offset:"+currTime+")");	
		var s=s_gi(s_account);
		//s.Media.play(currFileName, currTime);
	}

	// These functions are caught by the feeder object of the player.
	function loadFile(obj)
	{	
		// close the previous file
		if (loadCounter > 0)
		{
			s.Media.close(currFileName);
		}		
		//
		loadCounter++;
		// record the current file url
		var tempArray = String(obj.file).split("/");
		currFileName = tempArray[(tempArray.length - 1)];
		//
		thisMovie("videoPlayer").loadFile(obj);
		
	};
	
	function addItem(obj,idx) { thisMovie("videoPlayer").addItem(obj,idx); }
	function removeItem(idx) { thisMovie("videoPlayer").removeItem(idx); }
	function getItemData(idx) {
		var obj = thisMovie("videoPlayer").itemData(idx);
		var nodes = "";
		for(var i in obj) { 
			nodes += "<li>"+i+": "+obj[i]+"</li>"; 
		}

	};

	// This is a javascript handler for the player and is always needed.
	function thisMovie(movieName) {
	    if(navigator.appName.indexOf("Microsoft") != -1) {
			return window[movieName];
		} else {
			return document[movieName];
		}
	};

