// The following functions must be implemented by specific applications
function __changeLayoutMode(){};
function __showComponent(){};
function __showDetails(){};
function __previewVideo(obj){};
function __previewVideoComplete(obj){};
function __validationComplete(){};
function __initComplete(){};
function __playCustomVideo(videoType, videoId, videoPath, videoName, videoFormats){return null};
function __showCurrentVideoCustomInfo(lastVideo){};
function __orderVideo(id, price)
{
	var ret=confirm(getLocalizedString("order_credit_card_will_be_charged") + " " + price + ".  " + getLocalizedString("order_confirm_press_ok_cancel") );
	if (ret!=true)
		return null;
		
	var params=new Object();
	params.id=id;
	return params;
}

function __updateCurrentBroadcast(table, row)
{
	for(var i=0;i<table.rows.length;i++)
	{
		if(table.rows[i]==row)
		{
			row.cells[1].className = "channelbgright"+(i%2)+" small color1 channelbgrightsel";
		}
		else
		{
			table.rows[i].cells[1].className = "channelbgright"+(i%2)+" small color1";
		}
	}
}

function Video(type, id, path, formats)
{
	this.type = type;
	this.id = id;
	this.path = path;
	this.formats = formats;
}

function Console(videoASXURL, channelURL, playlistURL, mode)
{
	this.m_consoleMode = mode;
	this.m_details = new Details();
	this.m_components = new Object();
	this.m_lastVideo = new Video(Console.VIDEO_VOD_FLV);
	
	this.prevDivCount=3;
	this.m_lastPreviewVideos = new Array();

	this.locProgramVideoASX = videoASXURL;
	this.locChannelVideo = channelURL;
	this.locPlaylistVideo = playlistURL;
	this.locUploadVideo = "";

	this.changeLayoutMode = __changeLayoutMode;
	this.lastReloadComponent = null;
	this.reload = function ()
		{
			if(this.lastReloadComponent!=null)
				this.lastReloadComponent.reload();
		}
		
	this.registerComponent = function(name, obj)
		{
			this.m_components[name] = obj;
		}
		
	this.showComponent = __showComponent;
		
	this.showDetails = __showDetails;

	// Wrapper function since modal pages don't include console.js
	this.getLocalizedString = function(key)
		{
			return getLocalizedString(key);
		}

	// Converts an object of arguments to string
	// Args can be of type Objects (with arrays inside a key), or strings
	// Returns an encoded/escaped url string for the args
	this.getArgsString = function(params)
	{
		var args = "";
		if(typeof(params)=="object")
		{
			var argArray = new Array();
			for(n in params)
			{
				if(params[n] != null)
				{
					if((typeof(params[n])=="object"))
					{ 
						for(var i=0; i<params[n].length; i++)
							argArray[argArray.length] = n + "=" + encodeURIComponent(params[n][i]);
					}
					else					
						argArray[argArray.length] = n + "=" + encodeURIComponent(params[n]);
				}
			}
			args = argArray.join("&");
		}
		else
			args = params;
		
		return args;
	}
			
	this.getComponentContent = function(url, params, callback, bNoPrefixURL)
		{
			if(bNoPrefixURL!=true)
			   url="servlets/"+url;
			   
			var args=this.getArgsString(params);

			var requester=null;
			if(_util.isie)
				requester = new ActiveXObject("Microsoft.XMLHTTP");
			else
				requester = new XMLHttpRequest();
				
			requester.open("POST", url, true);
			requester.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

			requester.onreadystatechange=function() 
				{
					if (requester.readyState==4) 
					{
						var iStatus=requester.status;
						if (iStatus>=200 && iStatus<300)
						{
							// Success
							if(requester.getResponseHeader("Content-Type").indexOf("text/xml")==0 || requester.getResponseHeader("Content-Type").indexOf("application/xml")==0)
								callback(requester.responseXML.documentElement)
							else
								callback(requester.responseText);
						}
						else if(iStatus==401)
						{
							window.location.href = "secure/loginform";
						}
						else
							callback("ERROR - Code="+requester.status);
					}
				}
			
			requester.send(args);
		}
		
	this.getComponentContentSynch = function(url, params, bNoPrefixURL)
		{
			if(bNoPrefixURL!=true)
			   url="servlets/"+url;
			   
			var args=this.getArgsString(params);

			var requester=null;
			if(_util.isie)
				requester = new ActiveXObject("Microsoft.XMLHTTP");
			else
				requester = new XMLHttpRequest();
				
			requester.open("POST", url, false);
			requester.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

			requester.send(args);
			
			var iStatus=requester.status;
			if (iStatus>=200 && iStatus<300)
			{
				// Success
				if(requester.getResponseHeader("Content-Type").indexOf("text/xml")==0 || requester.getResponseHeader("Content-Type").indexOf("application/xml")==0)
					return requester.responseXML.documentElement;
				else
					return requester.responseText;
			}
			else if(iStatus==401)
			{
				window.location.href = "secure/loginform";
			}
			else
				return "ERROR - Code="+requester.status;
		}
	
	this.initDetails = function(type, pid)
		{
			this.showDetails(type, pid);
		}

	this.showSimilar = function(type, pid, pn)
		{
			this.m_details.showSimilar(type, pid, pn);
			_util.cancelEvt(event);
		}


	// Preview Divs Animation
	var spots=new Array();          // Available Spots
	var previewDivs=new Array();    // Movie Divs
	var orderQueue=new Array();     // Queue Pointers
	
	for (var i=0; i<Console.prevDivCount; i++)
	{
		spots[i]=new Object();
		spots[i].used=null;
		spots[i].left=Console.prevDivStops[i];
	}
	
	this.initPreviewDivs=function()
	{
		for (var i=0; i<Console.prevDivCount; i++)
		{	
			previewDivs[i]=document.getElementById("prev"+i);
			if (_util.isie)
			{
				var mp = previewDivs[i].getElementsByTagName("object")[0];
				mp.attachEvent("playStateChange", previewStateChange);
				mp.network.maxBandwidth = Console.PREVIEW_BANDWIDTH;
			}
		}

	    this.resetMovies();	
	}
	
	this.resetMovies=function()
	{
		for (var i=0; i<Console.prevDivCount; i++)
		{
			previewDivs[i].destPos=Console.prevDivResetPos;
			previewDivs[i].style.left=previewDivs[i].destPos+"px";
		}
	}
	
	this.getAvailableSpot=function()
	{
		for (var i=0; i<Console.prevDivCount; i++)
		{
			if (spots[i].used==null)
			   return i;
		}
		
		return Console.prevDivCount-1;     // Default=last spot
	}
	
	this.getAvailablePreviewDiv=function()
	{
		for (var i=0; i<Console.prevDivCount; i++)
		{
			if (previewDivs[i].inuse==null)
			   return i;
		}
		
		return Console.prevDivCount;  // Default
	}	
	
	this.previewVideo=function(videoType, videoId, videoPath, videoFormats)
	{
		if (!_util.isie)
		{
			alert(getLocalizedString("preview_mode_only_work_in_ie"));
			return;
		}
	
		__previewVideo(this);
	
		var newPrev=this.getAvailablePreviewDiv();
	    var newSpot=this.getAvailableSpot();
		
		var item=previewDivs[newPrev];

		spots[newSpot].used=item;
		
		if (orderQueue.length>=Console.prevDivCount)
		   item=orderQueue.shift();    // Take top item from array
		
		orderQueue.push(item);
	
		if (newPrev==Console.prevDivCount) // All filled
	    {
			for (var i=0; i<Console.prevDivCount; i++)
			{
				if (orderQueue[i])
				   orderQueue[i].destPos=spots[i].left;
			}
	    }
	    	      
		item.destPos=spots[newSpot].left;
		item.inuse=newSpot;
		item.style.left=Console.prevDivResetPos+"px";
		this.m_lastPreviewVideos[newPrev] = new Video(videoType, videoId, videoPath, videoFormats);

		var mp=item.getElementsByTagName("object")[0];

		switch (videoType)
		{
		case Console.VIDEO_VOD:
			if(videoPath.indexOf(".asx")>0)
				mp.url = this.locProgramVideoASX+videoPath;
			else
				mp.url = videoPath;
			break;
		case Console.VIDEO_CHANNEL:
			mp.url = this.locChannelVideo+videoPath;
			break;
		case Console.VIDEO_PLAYLIST:
			mp.url = this.locPlaylistVideo+videoId;
			break;
		case Console.VIDEO_UPLOAD:
			mp.url = this.locUploadVideo+videoPath;
			break;
		case Console.VIDEO_CHANNEL_TRAILER:
			mp.url = videoPath;
			break;
		case Console.VIDEO_VOD_TRAILER:
			mp.url = videoPath;
			break;
		default:
			alert("Unknown Video Type:  "+videoType);
		    break;
		}

		mp.settings.mute=true;  // This must be set after the URL attribute changes

		
		setTimeout("_console.animatePrev()",1);
	}
	
	this.animatePrev=function()
	{
		var iSpeed=10;     // Animation speed -- lower is faster

		var bDoneAnimating=true;
		for (var i=0; i<Console.prevDivCount; i++)
		{
			if (previewDivs[i].offsetLeft > previewDivs[i].destPos)
			{	previewDivs[i].style.left=previewDivs[i].offsetLeft-iSpeed+"px";
				bDoneAnimating=false;
			}
		}
		
		if (bDoneAnimating)
	    {
	        if (spots[0].used==null)
			{   
				__previewVideoComplete(this);
			}
	
	        return;
	    }
	       	
		setTimeout("_console.animatePrev()",1);
	}
	
	this.previewSwap=function(index)
	{
		var num=this.getQueuePosition(index);
		if (num==-1)
		   return;
	
		// TODO:  Check the following code with video streaming - works fine with downloaded content

		var mainMovieDiv=document.getElementById("wmppane0");
		var mainMovie=mainMovieDiv.getElementsByTagName("object")[0];
		var mainMovie_VideoType=this.m_lastVideo.type;
		var mainMovie_VideoId=this.m_lastVideo.id;
		var mainMovie_VideoPath=this.m_lastVideo.path;
		var mainMovie_VideoFormats=this.m_lastVideo.formats;
		
		var swapMovieDiv=orderQueue[num];
		var swapMovie=swapMovieDiv.getElementsByTagName("object")[0];
		var swapMovie_VideoType=this.m_lastPreviewVideos[num].type;
		var swapMovie_VideoId=this.m_lastPreviewVideos[num].id;
		var swapMovie_VideoPath=this.m_lastPreviewVideos[num].path;
		var swapMovie_VideoFormats=this.m_lastPreviewVideos[num].formats;

		var main_position=mainMovie.controls.currentPosition;
		var main_url=mainMovie.url;
		var swap_position=swapMovie.controls.currentPosition;
		var swap_url=swapMovie.url;
		
		var bMute=mainMovie.settings.mute;

		wmpCtrlForChannel(false);
		// Update main WMP
		switch (swapMovie_VideoType)       // Also in previewVideo
		{
		case Console.VIDEO_VOD:
			if(swapMovie_VideoPath.indexOf(".asx")>0)
				mainMovie.url = this.locProgramVideoASX+swapMovie_VideoPath;
			else
				mainMovie.url = swapMovie_VideoPath;
			mainMovie.controls.currentPosition=swap_position;		
			this.showDetails(Console.VIDEO_VOD, swapMovie_VideoId);
			break;
		case Console.VIDEO_CHANNEL:
			mainMovie.url = this.locChannelVideo+swapMovie_VideoPath;
			wmpCtrlForChannel(true)
			this.showDetails(Console.VIDEO_CHANNEL, swapMovie_VideoId);
			break;
		case Console.VIDEO_PLAYLIST:
			mainMovie.url = this.locPlaylistVideo+swapMovie_VideoId;
			// TODO: get current playing video in playlist and:
			// 1. play current video at current position
			this.showDetails(Console.VIDEO_PLAYLIST, swapMovie_VideoId);
			break;
		case Console.VIDEO_UPLOAD:
			mainMovie.url = this.locUploadVideo+swapMovie_VideoPath;
			this.showDetails(Console.VIDEO_UPLOAD, swapMovie_VideoId);
			break;
		case Console.VIDEO_CHANNEL_TRAILER:
			mainMovie.url = swapMovie_VideoPath;
			mainMovie.controls.currentPosition=swap_position;
			this.showDetails(Console.VIDEO_CHANNEL, swapMovie_VideoId);
			break;
		case Console.VIDEO_VOD_TRAILER:
			mainMovie.url = swapMovie_VideoPath;
			mainMovie.controls.currentPosition=swap_position;
			this.showDetails(Console.VIDEO_VOD, swapMovie_VideoId);
			break;
		default:
			alert("Unknown Video Type:  "+swapMovie_VideoType);
		    break;
		}

		mainMovie.settings.mute=bMute;   // Preserve Mute state since setting the URL property resets the Mute
		
		this.m_lastVideo.type = swapMovie_VideoType;
		this.m_lastVideo.id = swapMovie_VideoId;
		this.m_lastVideo.path = swapMovie_VideoPath;
		this.m_lastVideo.formats = swapMovie_VideoFormats;

		// Update preview WMP
		switch (mainMovie_VideoType)       // Also in previewVideo
		{
		case Console.VIDEO_VOD:
			if(mainMovie_VideoPath.indexOf(".asx")>0)
				swapMovie.url = this.locProgramVideoASX+mainMovie_VideoPath;
			else
				swapMovie.url = mainMovie_VideoPath;
			swapMovie.controls.currentPosition=main_position;
			break;
		case Console.VIDEO_CHANNEL:
			swapMovie.url = this.locChannelVideo+mainMovie_VideoPath;
			break;
		case Console.VIDEO_PLAYLIST:
			swapMovie.url = this.locPlaylistVideo+mainMovie_VideoId;
			// TODO: get current playing video in playlist and play current video at current position
			break;
		case Console.VIDEO_UPLOAD:
			swapMovie.url = this.locUploadVideo+mainMovie_VideoPath;
			break;
		case Console.VIDEO_CHANNEL_TRAILER:
			swapMovie.url = mainMovie_VideoPath;
			swapMovie.controls.currentPosition=main_position;
			break;
		case Console.VIDEO_VOD_TRAILER:
			swapMovie.url = mainMovie_VideoPath;
			swapMovie.controls.currentPosition=main_position;
			break;
		default:
			alert("Unknown Video Type:  "+mainMovie_VideoType);
		    break;
		}
		swapMovie.settings.mute=true;    // This must be set after the URL attribute changes
		this.m_lastPreviewVideos[num].yype = mainMovie_VideoType;
		this.m_lastPreviewVideos[num].id = mainMovie_VideoId;
		this.m_lastPreviewVideos[num].path = mainMovie_VideoPath;
		this.m_lastPreviewVideos[num].formats = mainMovie_VideoFormats;
	}
	
	this.previewInfo=function(index)
	{
		var num=this.getQueuePosition(index);
		if (num==-1)
		   return;

		if (orderQueue[num]==null)
		   return;
	
		var videoType=this.m_lastPreviewVideos[num].type;
		var videoId=this.m_lastPreviewVideos[num].id;

		switch (videoType)       // Also in previewVideo
		{
		case Console.VIDEO_VOD:
			this.showDetails(Console.VIDEO_VOD, videoId);
			break;
		case Console.VIDEO_CHANNEL:
			this.showDetails(Console.VIDEO_CHANNEL, videoId);
			break;
		case Console.VIDEO_PLAYLIST:
			this.showDetails(Console.VIDEO_PLAYLIST, videoId);
			break;
		case Console.VIDEO_UPLOAD:
			this.showDetails(Console.VIDEO_UPLOAD, videoId);
			break;
		case Console.VIDEO_CHANNEL_TRAILER:
			this.showDetails(Console.VIDEO_CHANNEL, videoId);
			break;
		case Console.VIDEO_VOD_TRAILER:
			this.showDetails(Console.VIDEO_VOD, videoId);
			break;
		default:
			alert("Unknown Video Type:  "+videoType);
		    break;
		}		
	}
	
	// Returns the position of the item clicked in the preview pane
	this.getQueuePosition=function(index)
	{
		var num=-1;
		for (var i=0; i<orderQueue.length; i++)
		{
			if (orderQueue[i]==document.getElementById("prev"+index))
			{
				num=i;
				break;
			}
		}
		return num;
	}
	
	this.previewClose=function(index)
	{
		var num=this.getQueuePosition(index);
		if (num==-1)
		   return;
		   
		orderQueue[num].style.left=Console.prevDivResetPos+"px";
		orderQueue[num].destPos=Console.prevDivResetPos;
		orderQueue[num].inuse=null;

		var mp=orderQueue[num].getElementsByTagName("object")[0];
		if (mp)
		   mp.controls.stop();
	
		orderQueue.splice(num,1);
	
		spots[orderQueue.length].used=null;
		
		for (var i=0; i<Console.prevDivCount; i++)
		{ if (orderQueue[i])
		     orderQueue[i].destPos=spots[i].left;
		}
	
		setTimeout("_console.animatePrev()",1);
	}
	
	this.showPlaylistDetails=function(videoType, videoId, videoPath, videoFormats)
		{
			this.m_lastVideo.type = videoType;
			this.m_lastVideo.id = videoId;
			this.m_lastVideo.path = videoPath;
			this.m_lastVideo.formats = videoFormats;
			this.showDetails(videoType, videoId);
		}
	
	this.playVideo=function(videoType, videoId, videoPath, videoName, videoFormats, bDetailsButton)
		{
			var splash = document.getElementById("splash");
			if(splash!=null)
				splash.style.display = "none";
			
			wmpCtrlForChannel(false);
			
			var mp = document.getElementById("wmpMain");
			var mprow = document.getElementById("wmpTr")
			var fp = document.getElementById("fplayer");
			var fprow = document.getElementById("fpTr");
			
			if(this.m_lastVideo.type.charAt(0)=="f" && videoType.charAt(0)=="w")
			{
				// Was playing Flash, now needs to play WMV
				fp.stopVideo();
				fprow.style.display = "none";
				mprow.style.display = "";
			}
			else if(this.m_lastVideo.type.charAt(0)=="w" && videoType.charAt(0)=="f")
			{
				// Was playing WMV, now needs to play Flash
				if (!_util.isie)
					writeWMP("");
				else
					mp.url = "";
				mprow.style.display = "none";
				fprow.style.display = "";				
			}
			
			this.m_lastVideo.type = videoType;
			this.m_lastVideo.id = videoId;
			this.m_lastVideo.path = videoPath;
			this.m_lastVideo.formats = videoFormats;
			
			var p = null;
			var isLive = false;
			var autoDetect = false;
			var disableEncryptVideoPath = g_disableEncryptVideoPath;
			var trackString = null;
			var trackId = null;
			switch (videoType)       // Also in previewVideo
			{
			case Console.VIDEO_VOD:
			case Console.VIDEO_VOD_FLV:
			case Console.VIDEO_VOD_PAY:
			case Console.VIDEO_VOD_LIVE:
			case Console.VIDEO_VOD_FLV_PAY:
			case Console.VIDEO_VOD_FLV_LIVE:
				if(videoPath.indexOf(".asx")>0)
					p = this.locProgramVideoASX+videoPath;
				else
					p = videoPath;
				if(videoType==Console.VIDEO_VOD_PAY || videoType==Console.VIDEO_VOD_FLV_PAY)
				{
					if (bDetailsButton!=true)
						p = "";
				}
				if(videoType==Console.VIDEO_VOD_FLV || videoType==Console.VIDEO_VOD_FLV_PAY || videoType==Console.VIDEO_VOD_FLV_LIVE)
				{
					if(p.indexOf("rtmp")==0)
						autoDetect = true;
				}
				if(videoType==Console.VIDEO_VOD_FLV_LIVE || videoType==Console.VIDEO_VOD_LIVE)
				{
					isLive = true;
					trackString = "Live Video " + videoId + ": "+videoName;
				}
				else
					trackString = "Video " + videoId + ": "+videoName;
				trackId = trackString;
				this.showDetails(videoType, videoId);
				if(window._mediaevent!=null)
					_mediaevent.init(mp, videoId);
				break;
			case Console.VIDEO_CHANNEL:
				p = this.locChannelVideo+videoPath;
				wmpCtrlForChannel(true);
				this.showDetails(videoType, videoId);
				break;
			case Console.VIDEO_PLAYLIST:
				p = this.locPlaylistVideo+videoId;
				this.showDetails(videoType, videoId);
				break;
			case Console.VIDEO_UPLOAD:
				p = this.locUploadVideo+videoPath;
				this.showDetails(videoType, videoId);
				break;
			case Console.VIDEO_CHANNEL_TRAILER:
				p = videoPath;
				this.showDetails(Console.VIDEO_CHANNEL, videoId);
				break;
			case Console.VIDEO_VOD_TRAILER:
				p = videoPath;
				this.showDetails(Console.VIDEO_VOD, videoId);
				break;
			default:
				var obj = __playCustomVideo(videoType, videoId, videoPath, videoName, videoFormats);
				if(obj==null)
					alert("Unknown Video Type:  "+videoType);
				else
				{
					trackString = obj.trackString;
					trackId = obj.trackId;
					p = obj.path;
					if(obj.isLive)
						isLive = true;
					if(obj.autoDetect)
						autoDetect = true;
					if(obj.disableEncryptVideoPath)
						disableEncryptVideoPath = true;
				}
			    break;
			}

			if(p!=null)
			{
				/*if(videoId=="13129")
				{
					try
					{
						var result = this.getComponentContentSynch("checkcanada");
						result = _util.selectSingleNodeText(result, "/result/code");
						if(result=="canada")
						{
							alert("We are unable to stream Trade Deadline coverage in Canada.\n\nCanadian viewers should look for Trade Deadline coverage on NHL Network, TSN, or TSN.ca");
							return;
						}
					}
					catch (e){}
				}*/
			
				if(videoType.charAt(0)=="w")
				{
					if(!disableEncryptVideoPath)
						p = getEncryptedVideoPath(videoType, p);
					if (!_util.isie)
			   			writeWMP(p);
			   		else
			   			mp.url = p;
			   		
			   		if(p.length>0 && trackString!=null)
				   	{
				   		trackVideoStart(trackString, trackId);
				   	}
			   	}
			   	else if(videoType.charAt(0)=="f")
			   	{
			   		if(p.length==0)
			   			fp.stopVideo();
			   		else
				   		fp.playVideo(trackString, trackId, p,videoFormats,isLive,autoDetect,disableEncryptVideoPath);
			   	}
			}
		}

	this.maxBandwidthChange=function(select)
	{
		var mp=document.getElementById("wmpMain");
		mp.close();
		mp.network.maxBandwidth = Number(select.options[select.selectedIndex].value);
		var videoType=this.m_lastVideo.type;
		var videoId=this.m_lastVideo.id;
		var videoPath=this.m_lastVideo.path;
		var videoFormats=this.m_lastVideo.formats;
		wmpCtrlForChannel(false);
		switch (videoType)
		{
		case Console.VIDEO_VOD:
			if(videoPath.indexOf(".asx")>0)
				mp.url = this.locProgramVideoASX+videoPath;
			else
				mp.url = videoPath;
			break;
		case Console.VIDEO_CHANNEL:
			mp.url = this.locChannelVideo+videoPath;
			wmpCtrlForChannel(true);
			break;
		case Console.VIDEO_PLAYLIST:
			mp.url = this.locPlaylistVideo+videoId;
			//TODO: Get first video Id and play it
			break;
		case Console.VIDEO_UPLOAD:
			mp.url = this.locUploadVideo+videoPath;
			break;
		case Console.VIDEO_CHANNEL_TRAILER:
			mp.url = videoPath;
			break;
		case Console.VIDEO_VOD_TRAILER:
			mp.url = videoPath;
			break;
		default:
			alert("Unknown Video Type:  "+videoType);
		    break;
		}
	}
	
	this.fullScreen = function()
	{
		var mp=document.getElementById("wmpMain");
		mp.fullScreen = true;
	}
	
	this.showCurrentVideoInfo = function()
	{
		if(Console.MODE_REGULAR!=this.m_consoleMode)
			this.changeLayoutMode(Console.MODE_REGULAR);
		var videoId=this.m_lastVideo.id;
		switch (this.m_lastVideo.type)
		{
		case Console.VIDEO_VOD:
		case Console.VIDEO_VOD_FLV:
		case Console.VIDEO_VOD_PAY:
		case Console.VIDEO_VOD_LIVE:
		case Console.VIDEO_VOD_FLV_PAY:
		case Console.VIDEO_VOD_FLV_LIVE:
			this.showDetails(this.m_lastVideo.type, videoId);
			break;
		case Console.VIDEO_CHANNEL:
			var type = Console.VIDEO_CHANNEL;
			if(window._guide!=null && _guide.guideurl!="guide")
			{
				var u = document.getElementById("wmpMain").currentMedia.sourceURL;
				if(u.indexOf("id=")>0)
				{
					u = u.substr(u.indexOf("?")+1);
					u = u.split("&");
					for(var i=0;i<u.length;i++)
					{
						if(u[i].indexOf("id=")==0)
						{
							type = Console.VIDEO_VOD;
							videoId = u[i].substr(3);
							break;
						}
					}
				}
			}
			this.showDetails(type, videoId);
			break;
		case Console.VIDEO_PLAYLIST:
			this.showDetails(Console.VIDEO_PLAYLIST, videoId);
			break;
		case Console.VIDEO_UPLOAD:
			this.showDetails(Console.VIDEO_UPLOAD, videoId);
			break;
		case Console.VIDEO_CHANNEL_TRAILER:
			this.showDetails(Console.VIDEO_CHANNEL, videoId);
			break;
		case Console.VIDEO_VOD_TRAILER:
			this.showDetails(Console.VIDEO_VOD, videoId);
			break;
		default:
			__showCurrentVideoCustomInfo(this.m_lastVideo);
		}
	}
	
	this.shareCurrentVideo = function()
	{
		if(Console.MODE_REGULAR!=this.m_consoleMode)
			this.changeLayoutMode(Console.MODE_REGULAR);
		showDetailsTab(2);
	}
	
	this.orderVideo = function(id, price)
	{
		var args = __orderVideo(id, price);
		if(args!=null)
			_console.getComponentContent("servlets/order", args, _console.orderVideo_Callback,true);
	}
	
	this.orderVideo_Callback = function(xml)
	{
		var result = "";
			
		try
		{
			result = _util.selectSingleNodeText(xml, "/result/code");
			if (result==null)
			{ alert("orderVideo_Callback:  Invalid returned xml value:  "+xml.xml);
			  return;
			}
		}
		catch (e)
		{
			alert("orderVideo_Callback:  Invalid returned xml value: '"+xml+"'");
			return;
		}

		switch(result)
		{
			case "ordered":
				var id=_util.selectSingleNodeText(xml, "/result/data/id");
				var mediatype=_util.selectSingleNodeText(xml, "/result/data/mediatype");
				_console.showDetails(mediatype,id);
				break;
			case "nobilling":
				alert(getLocalizedString("ordered_no_billing"));
				_console.showComponent("Account");
				_account.getAccountBillingInfo();
				break;
			default:
		     	alert("orderVideo_Callback Error:  Unknown Return Code:  "+result);
				break;
		}
	}
	
}

function writeWMP(url, h, bDisableContextMenu, w, mute, containerId, id, notIEShowControls, windowless)
{
	if(h==null)
		h = 360;
	
	if(w==null)
		w = 480;
		
	if(mute==null)
		mute = false;
	
	if(bDisableContextMenu==null)
		bDisableContextMenu=true;
		
	if(notIEShowControls==null)
		notIEShowControls = true;
		
	if(windowless==null)
		windowless = true;

	var isie = window.navigator.userAgent.indexOf("MSIE")>0;

	var strUIMode="none";   // Some Firefox clients may have the plugin therefore we change UIMode for consistency
	if (!isie && notIEShowControls)
		strUIMode="full";
	
	if(id==null)
		id = "wmpMain";
	
	var strHTML ='<object id="'+id+'" classid="clsid:6bf52a52-394a-11d3-b153-00c04f79faa6" type="application/x-oleobject" height="'+h+'" width="'+((isie)?"100%":w)+'">';
		if (bDisableContextMenu==true)
			strHTML+='<param name="enableContextMenu" value="false" />';
		strHTML+='<param name="autoStart" value="true" />';
		if(windowless)
			strHTML+='<param name="windowlessVideo" value="true" />';
		strHTML+='<param name="url" value='+url+' />';
		strHTML+='<param name="uiMode" value="'+strUIMode+'" />';
		strHTML+='<param name="mute" value="'+((mute)?"true":"false")+'" />';
		if(mute)
			strHTML+='<param name="volume" value="0" />';
		strHTML+='<embed type="application/x-mplayer2" src="'+url+'" autostart="true" showcontrols="0" height="'+(h-3)+'" width="'+w+'" mute="'+((mute)?"1":"0")+'"';
		if(mute)
			strHTML+=' volume="0"';
		strHTML += ' /></object>';

	if(containerId==null)
		containerId = "wmppane0";
	document.getElementById(containerId).innerHTML=strHTML;

	if(Console.WMP_BANDWIDTH!=null)
	{
		var wmp = document.getElementById(id);
		if(wmp!=null)
		{
			try
			{
				wmp.network.maxBandwidth = Console.WMP_BANDWIDTH;
			}
			catch(e){};
		}
	}
}

function previewStateChange(newState)
{
	switch (newState)
    {
	    case 3: // playing, should reschedule when position change occurs
        	for (var i=0; i<_console.prevDivCount;i++)
	  		    try {document.getElementById("wmp"+i).settings.mute=true;}catch(e){}
    	    break;
    }
}
Console.VIDEO_VOD="wvod";
Console.VIDEO_VOD_FLV="fvod";
Console.VIDEO_VOD_PAY="wvodp";
Console.VIDEO_VOD_LIVE="wvodl";
Console.VIDEO_VOD_FLV_PAY="fvodp";
Console.VIDEO_VOD_FLV_LIVE="fvodl";
Console.VIDEO_CHANNEL="channel";
Console.VIDEO_PLAYLIST="playlist";
Console.VIDEO_UPLOAD="upload";
Console.VIDEO_CHANNEL_TRAILER="channeltrailer";
Console.VIDEO_VOD_TRAILER="wvodtrailer";

Console.PREVIEW_BANDWIDTH = 60000;

function wmpCtrlForChannel(channel)
{
	if(channel && window._guide!=null && _guide.guideurl=="guide")
	{
		document.getElementById("wmpCtrlPrevImg").disabled=true;
		document.getElementById("wmpCtrlPauseImg").disabled=true;
		document.getElementById("wmpCtrlNextImg").disabled=true;
		document.getElementById("wmpCtrlPrevImg").src=g_locImage+"images/wmpctrlprev_dis.gif";
		document.getElementById("wmpCtrlPauseImg").src=g_locImage+"images/wmpctrlpause_dis.gif";
		document.getElementById("wmpCtrlNextImg").src=g_locImage+"images/wmpctrlnext_dis.gif";
		document.getElementById("wmpCtrlPositionDiv").style.visibility = "hidden";
	}
	else
	{
		document.getElementById("wmpCtrlPrevImg").disabled=false;
		document.getElementById("wmpCtrlPauseImg").disabled=false;
		document.getElementById("wmpCtrlNextImg").disabled=false;
		document.getElementById("wmpCtrlPrevImg").src=g_locImage+"images/wmpctrlprev.gif";
		document.getElementById("wmpCtrlPauseImg").src=g_locImage+"images/wmpctrlpause.gif";
		document.getElementById("wmpCtrlNextImg").src=g_locImage+"images/wmpctrlnext.gif";
		document.getElementById("wmpCtrlPositionDiv").style.visibility = "visible";
	}
}

function wmpCtrlStop()
{
	document.getElementById("wmpMain").controls.stop();
}

function wmpCtrlPlay(play)
{
	document.getElementById("wmpMain").controls.play();
}

function wmpCtrlPause()
{
	document.getElementById("wmpMain").controls.pause();
}

function wmpCtrlRewind()
{
	var wmp = document.getElementById("wmpMain");
	if(wmp.settings.rate==1)
		wmp.controls.fastReverse();
	else
		wmp.settings.rate = 1;
}

function wmpCtrlFast()
{
	var wmp = document.getElementById("wmpMain");
	if(wmp.settings.rate==1)
		wmp.controls.fastForward();
	else
		wmp.settings.rate = 1;
}

function wmpCtrlBarMouseOver(table)
{
	table.style.display = "none";
	var tbl=document.getElementById("wmpCtrlBar");
	tbl.parentNode.style.display = "";
	tbl.style.top = "45px";

	if (tbl.offsetTop!=0)
		tbl.anim_destY=0;
	else
		tbl.anim_destY=45;

	if (window._animation!=null)
		_animation.startAnimation(tbl);
	
}
function wmpCtrlBarMouseOut(table)
{
	var obj = event.toElement;
	if(obj!=null)
	{
		while(obj!=null && obj.tagName!="BODY" && obj!=table)
		{
			obj = obj.parentNode;
		}
	}
	if(obj==null || obj!=table)
	{
		table.parentNode.style.display = "none";
		document.getElementById("wmpCtrlBarMin").style.display = "";
		wmpCtrlVolumeMouseUp(document.getElementById("wmpCtrlVolume"));
		wmpCtrlPositionMouseUp(document.getElementById("wmpCtrlPosition"),true);
	}
}

var g_wmpCtrlVolumeX = null;
function wmpCtrlVolumeMouseDown(img)
{
	img.setCapture();
	img.attachEvent("onmousemove", wmpCtrlVolumeMouseMove);
	g_wmpCtrlVolumeX = event.clientX - img.offsetLeft;
}

function wmpCtrlVolumeSet(x)
{
	var img = document.getElementById("wmpCtrlVolume");
	img.style.left = (x - 5) + "px";
	img.title = "Volume: " + x;
	document.getElementById("wmpCtrlVolumeBar").style.width = x + "px";
	wmpCtrlMute(false);
	document.getElementById("wmpMain").settings.volume = x;
}

function wmpCtrlVolumeMouseMove()
{
	var x = event.clientX - g_wmpCtrlVolumeX;
	if(x>=-5 && x<=95)
	{
		wmpCtrlVolumeSet(x+5);
	}
}
function wmpCtrlVolumeMouseUp(img)
{
	img.detachEvent("onmousemove", wmpCtrlVolumeMouseMove);
	document.releaseCapture();
}

var g_wmpCtrlPositionX = null;
function wmpCtrlPositionMouseDown(img)
{
	img.setCapture();
	img.attachEvent("onmousemove", wmpCtrlPositionMouseMove);
	g_wmpCtrlPositionX = event.clientX - img.offsetLeft;
}
function wmpCtrlPositionSet(x)
{
	var img = document.getElementById("wmpCtrlPosition");
	img.style.left = (x - 5) + "px";
	document.getElementById("wmpCtrlPositionBar").style.width = x + "px";
}
function wmpCtrlPositionMouseMove()
{
	var x = event.clientX - g_wmpCtrlPositionX;
	if(x>=-5 && x<=395)
	{
		wmpCtrlPositionSet(x+5);
	}
}
function wmpCtrlPositionMouseUp(img, mo)
{
	g_wmpCtrlPositionX = null;
	img.detachEvent("onmousemove", wmpCtrlPositionMouseMove);
	document.releaseCapture();
	if(mo==null || mo==false)
	{
		var wmp = document.getElementById("wmpMain");
		wmp.controls.currentPosition = Math.round(document.getElementById("wmpCtrlPositionBar").offsetWidth/400*wmp.currentMedia.duration);
	}
}
function wmpCtrlPositionClick(div)
{
	var img = document.getElementById("wmpCtrlPosition");
	var pos = document.getElementById("wmpCtrlPositionBar");
	if(event.srcElement==pos || event.srcElement==div)
	{
		var wmp = document.getElementById("wmpMain");
		wmp.controls.currentPosition = Math.round(event.offsetX/400*wmp.currentMedia.duration);
	}
}
function wmpCtrlMute(mute)
{
	if(mute)
	{
		document.getElementById("wmpCtrlSoundTd").style.display = "none";
		document.getElementById("wmpCtrlMuteTd").style.display = "";
	}
	else
	{
		document.getElementById("wmpCtrlSoundTd").style.display = "";
		document.getElementById("wmpCtrlMuteTd").style.display = "none";
	}
	document.getElementById("wmpMain").settings.mute = mute;
}

function wmpCtrlBitRateWarn(s)
{
	if(s==3)//playing
	{
		var wmp = document.getElementById("wmpMain");
		if(wmp.currentMedia.imageSourceWidth!=0 && wmp.currentMedia.imageSourceWidth<wmp.offsetWidth)
		{
			wmp.detachEvent("playStateChange", wmpCtrlBitRateWarn);
			alert(getLocalizedString("insufficient_network_bandwidth"));
		}
	}
}
function wmpCtrlUpdateTimer()
{
	var wmp =  document.getElementById("wmpMain");
	var span = document.getElementById("wmpCtrlTime");
	if(span!=null)
		span.innerText = wmp.controls.currentPositionString;
	
	var duration = wmp.currentMedia.duration;
	if(duration>0 && g_wmpCtrlPositionX==null)
	{
		var x = Math.round(wmp.controls.currentPosition/duration*100) * 4;
		var img = document.getElementById("wmpCtrlPosition");
		img.style.left = (x - 5) + "px";
		document.getElementById("wmpCtrlPositionBar").style.width = x + "px";
	}
}
var g_wmpCtrlTimerInt = null;
function wmpCtrlPlayPause(s)
{
	var statustd = document.getElementById("wmpCtrlStatus");
	if(statustd!=null)
		statustd.style.visibility = "hidden";
	
	if(s==3) // playing
	{
		if(g_wmpCtrlTimerInt==null)
			g_wmpCtrlTimerInt = setInterval(wmpCtrlUpdateTimer,1000);
	}
	else
	{
		clearInterval(g_wmpCtrlTimerInt);
		g_wmpCtrlTimerInt = null;
	}
	
	var obj = document.getElementById("wmpCtrlTimeBuffer");
	if(obj!=null)
	{
		obj.style.display = "none";
		document.getElementById("wmpCtrlTime").style.display = "";
	}
	
	switch(s)
	{
	case 3: //playing
		if(document.getElementById("wmpCtrlSoundTd").style.display=="none")
			wmpCtrlMute(true);
		document.getElementById("wmpCtrlPlayTd").style.display = "none";
		document.getElementById("wmpCtrlPauseTd").style.display = "";
		var mainMovieDiv=document.getElementById("wmppane0");
		if(mainMovieDiv!=null && window._guide!=null)
		{
			if(_console.m_lastVideo.type==Console.VIDEO_CHANNEL && _guide.guideurl!="guide")
			{
				var u = document.getElementById("wmpMain").currentMedia.sourceURL;
				if(u.indexOf("sp=")>0)
				{
					u = u.substr(u.indexOf("sp=")+3);
					var pos = u.substring(0, u.indexOf("&"));
					var table = document.getElementById("channel_div").getElementsByTagName("TABLE")[0];
					var row = null;
					for(var i=0;i<table.rows.length;i++)
					{
						if(table.rows[i].getAttribute("sp")==pos)
						{
							row = table.rows[i];
							break;
						}
					}
					if(row!=null)
					{
						__updateCurrentBroadcast(table, row);
						row.scrollIntoView();
					}
				}
			}
		}
		break;
	case 6: // buffering
	case 7: // waiting
		if(statustd!=null)
			statustd.style.visibility = "visible";
		document.getElementById("wmpCtrlTimeBuffer").style.display = "";
		document.getElementById("wmpCtrlTime").style.display = "none";
		break;
	default:
		var ptd = document.getElementById("wmpCtrlPlayTd");
		if(ptd!=null)
		{
			ptd.style.display = "";
			document.getElementById("wmpCtrlPauseTd").style.display = "none";
		}
	}
}
function wmpCtrlHandlePlayStatChangeEvents()
{
	document.getElementById("wmpMain").attachEvent("playStateChange", wmpCtrlBitRateWarn);
	document.getElementById("wmpMain").attachEvent("playStateChange", wmpCtrlPlayPause);
}
function wmpCtrlEnableMenu(id)
{
	var wmp = document.getElementById(id);
	if (wmp!=null)
		wmp.enableContextMenu=true;
}
function wmpCtrlEnableContextMenu(event)
{	// Enable right click on wmp if user CTRL+ALT+clicks
	if (event && event.ctrlKey==true && event.altKey==true)
	{ 
		wmpCtrlEnableMenu("wmpMain");
		wmpCtrlEnableMenu("ciwmp0");
		wmpCtrlEnableMenu("ciwmp1");
		wmpCtrlEnableMenu("ciwmp2");
		wmpCtrlEnableMenu("ciwmp3");
		wmpCtrlEnableMenu("ciwmpmain");
	  	window.status="Right Click Enabled";
	}
}
function openWin(url, target, width, height, bHideToolbars)
{
	if (target==null)
		target="_blank";

	var opts=new Array();
	opts[opts.length]="resizable=yes";
	opts[opts.length]="scrollbars=yes";

	if (width!=null)
		opts[opts.length]="width="+width;
	if (height!=null)
		opts[opts.length]="height="+height;
	if (bHideToolbars==true)
	{
		opts[opts.length]="toolbar=no";
		opts[opts.length]="location=no";
		opts[opts.length]="menubar=no";
		opts[opts.length]="status=no";
	}
	else
	{
		opts[opts.length]="toolbar=yes";
		opts[opts.length]="location=yes";
		opts[opts.length]="menubar=yes";
		opts[opts.length]="status=yes";
	}
	
	opts=opts.join(",");
	       		
	var win=window.open(url,target,opts);
	if (win!=null)
		win.focus();
	else
		alert(getLocalizedString("window_cannot_be_opened"));
	
	if (window._util!=null)
		_util.cancelEvt(window.event);
		
	return false;
}

// Gets a localized string for JavaScript usage
function getLocalizedString(key)
{
	var obj = document.getElementById("msg_"+key);
	if(obj!=null)
	{	return obj.innerHTML.replace(/\\n/g,"\n");
	}
	alert("Error - Localization key '"+key+"' not found.");
}

