var LIVE_EVENTS_REFRESH_INTERVAL=60000;

function Browse(name, defaultId, loadOnce)
{
	var m_name = name;
	var m_componentName = "_" + name.toLowerCase();
	var m_defaultId = defaultId;
	var m_loadOnce = loadOnce;
	var m_initialized = false;
	var m_servletpath = "browse";
	var m_programList = new ProgramList();
	
	this.display = function()
		{
			if(!m_initialized)
			{
				this.getCategory(m_defaultId);	
				m_initialized = true;
			}
			_console.lastReloadComponent = this;
		}
	
	var m_lastId = null;
	this.reload = function()
		{
			this.resetMode();
			this.getCategory(m_lastId);
		}
	
	this.resetMode = function()
		{
			m_programList.setMode();
		}
		
	this.setMode = function(m, cid)
		{
			m_programList.setMode(m);
			this.getCategory(cid);
		}
		
	this.getCategory = function(cid, pn)
		{
			if (m_loadOnce==true)
			{
				if (m_initialized)
					return;
			}

			m_initialized=true;
		
			var args = new Object();

			if(pn==null)
			{
				args.ispaging="false";
				pn = 1;
			}
			else
				args.ispaging="true";
			
			args.cid = cid;
			args.component = m_componentName;

			m_lastId = cid;
			args.menuChannelIndex=g_lastChannelIndex;
			args.menuChannelId=g_lastChannelId;
			
			if(cid=="newvideos")
			{
				args.newvideos = "true";
				args.count = ProgramList.PAGESIZE_THUMB;
				args.menuChannelIndex="newvideos";
				args.menuChannelId="newvideos";
			}
			if(String(cid).indexOf("hotvideos")==0)
			{
				args.hotvideos = cid;
				args.count = ProgramList.PAGESIZE_THUMB;
				args.menuChannelIndex="hotvideos";
				args.menuChannelId="hotvideos";
			}
			if(cid=="toprated")
			{
				args.toprated = "true";
				args.count = ProgramList.PAGESIZE_THUMB;
			}
			m_programList.setURLParams(args, pn);
			if(Console.MODE_LARGE==_console.m_consoleMode)
			{
				args.ptrs = ProgramList.PAGESIZE_THUMB_ROWSIZE_LARGE;
				args.large = "true";
			}

			document.getElementById("content_"+m_name).innerHTML = getLocalizedString("loading");
			_console.getComponentContent(m_servletpath, args, this.getCategoryCallBack);
			_util.cancelEvt(window.event);
		}
		
	this.getCategoryCallBack = function(result)
		{
			document.getElementById("content_"+m_name).innerHTML = result;

			if (g_defaultVideoId!="")
			{	// If deeplinked, don't play first video
				var objVideo = _console.findVideoTbl(g_defaultVideoId);
				_console.m_lastVideo.objVideo=objVideo;
				_console.selectVideoObj(objVideo);
				_console.populateComingUp();
				g_defaultVideoId="";
			}
			else if(!g_firstVideoPlayed)
			{
				if(m_name!="WhatsHot" && (m_name!="WhatsNew" || g_isNHL!="true")){
					g_firstVideoPlayed=true;
					var td = document.getElementById(m_componentName+"_0");
					if (td)
						td.onclick();
				}
			}
		}
}

function LiveEvent()
{
	var m_initialized = false;
	
	this.initialize = function()
		{
			m_initialized = true;
		}
	
	this.display = function()
		{
			if(!m_initialized)
			{
				this.getLiveEvents();	
				m_initialized = true;
			}
			_console.lastReloadComponent = this;
		}
	
	var m_lastId = null;
	this.reload = function()
		{
			if(m_lastId==null)
				this.getLiveEvents();
			else
			{
				var args = new Object();
				args.id = m_lastId;
				if(Console.MODE_LARGE==_console.m_consoleMode)
				{
					args.ptrs = ProgramList.PAGESIZE_THUMB_ROWSIZE_LARGE;
					args.large = true;
				}
				else
					args.ptrs = ProgramList.PAGESIZE_THUMB_ROWSIZE;
				_console.getComponentContent("liveevents", args, this.getLiveEventsCallBack);
			}
		}
	
	this.getLiveEvents = function(id, bAutoRefreshing)
		{
			if (id=="")
				return;
				
			var args = new Object();
			if(Console.MODE_LARGE==_console.m_consoleMode)
			{
				args.ptrs = ProgramList.PAGESIZE_THUMB_ROWSIZE_LARGE;
				args.large = true;
			}
			else
				args.ptrs = ProgramList.PAGESIZE_THUMB_ROWSIZE;
				
			args.catId=id;
			args.menuChannelIndex=g_lastChannelIndex;
			args.menuChannelId=g_lastChannelId;
			args.autoRefreshing=bAutoRefreshing;
			if (!bAutoRefreshing)
				document.getElementById("content_LiveEvents").innerHTML = getLocalizedString("loading");
				
			_console.getComponentContent("liveevents", args, this.getLiveEventsCallBack);
		}
	
	this.getLiveEventsCallBack = function(result)
		{
			var tbl=document.getElementById("content_LiveEvents");
			_util.clearMouseEvents(tbl);
			tbl.innerHTML = result;

			if (g_defaultVideoId!="")
			{	// If deeplinked, don't play first video
				var objVideo = _console.findVideoTbl(g_defaultVideoId, "div_LiveEvents");
				_console.m_lastVideo.objVideo=objVideo;
				_console.selectVideoObj(objVideo);
				_console.populateComingUp();
				g_defaultVideoId="";
			}
			else
			{
				if (g_firstVideoPlayed==false)
				{
					g_firstVideoPlayed=true;
					var tbls = _console.getVideoTables("div_LiveEvents");
					for (var i=0; i<tbls.length; i++)
					{	// Play the first NON-upcoming video
						if (tbls[i].getAttribute("isUpcoming")=="true")
							continue;
						if (tbls[i].onclick!=null)
						{
							tbls[i].onclick();
							break;
						}
					}
				}
				else
				{
					var div=document.getElementById("liveevents_autorefreshing");
					if (div && div.innerHTML=="true")   // On refresh, select the last video that was played
					{
						var objVideo = _console.findVideoTbl(_console.m_lastVideo.id, "div_LiveEvents");
						if (objVideo)
						{
							_console.selectVideoObj(objVideo);
							if (_console.m_lastVideo.isUpcoming)  // If the current video is upcoming and changed to Live, play it
							{
								if (objVideo.getAttribute("isLive")=="true")
									objVideo.onclick();
							}
						}
					}
				}
			}
	
			var strCmd = "_liveevent.getLiveEvents('"+g_lastChannelId+"',true)";
			clearTimeout(g_liveEventTimer);
			g_liveEventTimer = setTimeout(strCmd, LIVE_EVENTS_REFRESH_INTERVAL);
		}
}
