var isIE = (navigator.appName.indexOf("Microsoft") != -1);
var playerId = 'videoPlayer';

	//These controls need to be revisited as they do not have an affect on video in Firefox
	function playVideo(containerId)
	{				
		var videoBlock = document.getElementById(containerId);
		if (videoBlock.hasChildNodes() && isIE)
		{
			player = document.getElementById(playerId);
			player.Play();
		}
	}
	
	function stopVideo(containerId)
	{
		var videoBlock = document.getElementById(containerId);
		if (videoBlock.hasChildNodes() && isIE)
		{
			player = document.getElementById(playerId);
			player.Stop();
		}
		else
		{
		document.getElementById(playerId).controls.stop();
		}
	}
	
	
	

	// This method takes a select element and an Id of the element in which 
	//	to place the newly created OBJECT element as well as the dimensions of the video.
	function chooseVideo(select, targetId, h, w)
	{
		var videoBlock = document.getElementById(targetId);
		
		if (select.selectedIndex != 0)
		{	
			removeChildren(videoBlock);
						
			var obj = getObjectTag(playerId, h, w, select.options[select.selectedIndex].value,0);
			if (isIE)
				videoBlock.innerHTML = obj;
			else
				videoBlock.appendChild(obj);
		}
		else
		{
			removeChildren(videoBlock);
		}
	}
	
	function linkVideo(uri, containerId, h, w, sc)
	{
		var videoBlock = document.getElementById(containerId);
		
		removeChildren(videoBlock);
					
		var obj = getObjectTag(playerId, h, w, uri, sc);
		if (isIE)
			videoBlock.innerHTML = obj;
		else
			videoBlock.appendChild(obj);
	}


	// Builds either string (object tag) or DOM Element.  IE seems to have a problem 
	// when building up the element.  This method allows Firefox to use the Object
	// tag properly and does not require the Embed tag hack.
	function getObjectTag(id, h, w, uri, sc)
	{

		//Don't add the proprietary element for IE
		if (isIE)
		{			
			var obj= "<object id='" + id + "' ";
			obj += "	codebase='http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701' ";
			obj += " 	data='" + uri + "'";
			obj += " 	standby='Loading Microsoft Windows Media Player components...' ";
			obj += " 	width='" + w + "' height='" + h + "' ";
			obj += " 	type='video/x-ms-wmv'> ";

			obj += createParam('fileName', uri);
			obj += createParam('animationatStart', 0);
			obj += createParam('transparentatStart', 1);
			obj += createParam('autoStart', 1);
			obj += createParam('showControls', sc);
			obj += createParam('showStatusBar', 0);
			obj += createParam('windowlessVideo', 1);
			obj += " </object>";
		
			return obj;
		}
		else
		{
			//h -= 10;
			
			var obj = document.createElement('object');
			obj.setAttribute('id', id);
			obj.setAttribute('type', 'application/x-ms-wmp');
			obj.setAttribute('standby', 'Loading windows media player components...');
			obj.setAttribute('codebase', 'http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701');
			obj.setAttribute('height', h);		
			obj.setAttribute('width', w);	
			obj.setAttribute('data', uri);
			
			var embed = document.createElement('embed');
			embed.setAttribute('width',w);
			embed.setAttribute('height',h);
			embed.setAttribute('type','application/x-ms-wmp');
			embed.setAttribute('src',uri);
			embed.setAttribute('autostart', 1);
			embed.setAttribute('showstatusbar', 0);
			embed.setAttribute('windowlessVideo', 1);
			embed.setAttribute('animationatstart', 0);
			embed.setAttribute('showcontrols', 0);
			embed.setAttribute('showdisplay', 0);
			obj.appendChild(embed);			  
			
			obj.appendChild(createParam('filename', uri));
			obj.appendChild(createParam('animationatstart', '1'));
			obj.appendChild(createParam('autostart', '1'));
			obj.appendChild(createParam('transparentatstart', '0'));
			obj.appendChild(createParam('showcontrols', '0'));
			obj.appendChild(createParam('showstatusbar', '0'));
			obj.appendChild(createParam('windowlessvideo', '0'));
			obj.appendChild(createParam('showdisplay', '0'));
			obj.appendChild(createParam('uimode', 'none'));
								
			var p_m = document.createElement('param');
			p_m.setAttribute('name','filename');
			p_m.setAttribute('value',uri);
			obj.appendChild(p_m);
			var p_m = document.createElement('param');
			p_m.setAttribute('name','animationatstart');
			p_m.setAttribute('value',true);
			obj.appendChild(p_m);			
			var p_m = document.createElement('param');
			p_m.setAttribute('name','autostart');
			p_m.setAttribute('value',true);
			obj.appendChild(p_m);	
			var p_m = document.createElement('param');
			p_m.setAttribute('name','transparentatstart');
			p_m.setAttribute('value',false);
			obj.appendChild(p_m);	
			var p_m = document.createElement('param');
			p_m.setAttribute('name','showcontrols');
			p_m.setAttribute('value',false);
			obj.appendChild(p_m);	
			var p_m = document.createElement('param');
			p_m.setAttribute('name','showstatusbar');
			p_m.setAttribute('value',false);
			obj.appendChild(p_m);	
			var p_m = document.createElement('param');
			p_m.setAttribute('name','showdisplay');
			p_m.setAttribute('value',false);
			obj.appendChild(p_m);	
			var p_m = document.createElement('param');
			p_m.setAttribute('name','uimode');
			p_m.setAttribute('value','none');
			obj.appendChild(p_m);	
			

			
			return obj;
		}
		
	}
		
	//Returns string for IE or param Element for Firefox and other browsers
	function createParam(n, v)
	{
		if(isIE)
		{
			return '<param name="'+n+'" value="'+v+'">';	
		}
		else
		{
			param = document.createElement('param');
			param.setAttribute(n, v);
			return param;
		}
	}

	// This method removes all child elements from the target element.
	function removeChildren(elem)
	{
		if (elem.hasChildNodes()){
			while (elem.firstChild) {
				elem.removeChild(elem.firstChild);
			}
		}
	}