/*
 * Copyright (c) 2008 AKEEBO CORPORATION. All Rights Reserved.
 *
 * This software is the confidential and proprietary information ("Confidential Information") 
 * of AKEEBO CORPORATION ("AKEEBO CORP"). Any disclosure or use of such Confidential Information other 
 * than in accordance with the terms of the license under which it was received is strictly 
 * prohibited.
 *
 * AKEEBO CORP MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF 
 * THE SOFTWARE, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
 * OR NON-INFRINGEMENT. AKEEBO CORP SHALL NOT BE LIABLE FOR ANY DAMAGES
 * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
 * THIS SOFTWARE OR ITS DERIVATIVES.
 */

///////////////////////////
// GUIDE

// page selection - categories or channels
var guidePage = 0; 

var gSelectedCategory = 0;
var gCurrentCategory = 0;
var gCategoryCursor = 0;
var gHotCursor = 0;

var gSelectedChannel = 0;
var gCurrentChannel = 0;
var gChannelCursor = 0; 

var gPrevChannelCursor = 0;
var gPrevGuideCursor = 0;
var gPrevCategoryCursor = 0;

var gSelectedGuide = 0;
var gCurrentGuide = 0;
var gGuideCursor = 0; 

var gTotalCat = 0;
var gTotalCh = 0;
var gTotalGuide = 0;
var changePdCursor = 0;

var extraPanelIndex = 0;

var selChannelIdCache = new Array();
var selChannelOrderCache = new Array();
var selChannelNameCache = new Array();
var selFavoriteCache = new Array();
var selLockedCache = new Array();
var selUseGraphicCache = new Array();

var channelIdCache = new Array();
var channelOrderCache = new Array();
var favoriteCache = new Array();
var lockedCache = new Array();
var useGraphicCache = new Array();
var channelNameCache = new Array();
var mnemonicCache = new Array();
var categoryOrderCache = new Array();
var categoryNameCache = new Array();
var protocol = new Array();
var transcodeCache = new Array(); 
var transStatusCache = new Array(); 
var securityCache = new Array(); 


var getChannelClicks = 0;
var countCatClicks = 0;
var nCurrentChannel = 0;
var nSelectedChannel = 0;
var nLastSelectedChannel = 0;
var statusCache = new Array();

var uniqCategoryCache = new Array();
var uniqCatNameCache = new Array();
//uniqCatNameCache.sort();

var sUnLock = new Image(20, 20);
//	sUnLock.src = './view/online/images/unLock.png';
	
var sLogoCategory = new Image(48, 48);
	sLogoCategory.src = './view/online/images/logoCategory.png';	
	
	
var selGuidePageSet = new Array();

var favoriteCount = 0;

var updateChannelPanelTimer = null;

var gTopCategory = 0;
var gBottomCategory = 0;
var gTopChannel = 0;
var gBottomChannel = 0;
var moveCursor = 0;
var stbTypeId = 0;
var selCurCategory = 0;
var curLocale = "en_US";
//var menuActive = false;
//var miniguideActive = false;
var nCurrentProgramme = 0;
var nOldChannel = 0;
var bSwitchChannel = false;
var channelChanged = false;
var nSelectedCategory = 0;
var nCurrentCategory = 0;
var bKeyChannel = false;
var oSwitchTimer = null;
var oTriggerSwitchTimer = null;
var userSelChOrder = ""; 
//
/*	
var adArr = new Array();
adArr[0] = "Ad-insertion made contextual with Akeebo!";
adArr[1] = "Akeebo works with Google's Adsense & Yahoo's Panama!";
adArr[2] = "Akeebo can trigger a phone call using Vonage."; 
adArr[3] = "Use Akeebo middleware on your snazzy iPhone!";
adArr[4] = "Share your TV experience thru Akeebo iConnect.";
adArr[5] = "Social networking made quick with Akeebo Swift.";
adArr[6] = "AkeeboPod gives you VOD (video-on-demand)!";
adArr[7] = "Akeebo is touch screen enabled by design.";	

var adTimer = null;
var adIndex = 0;

function adTicker() {
	if(adTimer) {
		// if the adTimer handler is already active, clear it. 
		window.clearInterval(adTimer);
		adTimer = null;
		adIndex = 0;
	}	
	adTimer = window.setInterval('paintAd();', 10*1000); // every 10 seconds
}

function paintAd() {
	adIndex = (++adIndex) % 8;

	document.getElementById("adPanel").innerHTML = adArr[adIndex];	
}
*/

///////////////////////////
function launchGuide() {
	getCache();
}	

function updateHotIconPanel(start, end) {	
	return;
	var table = document.getElementById("hotExtrasPanel");   
	var rows = table.getElementsByTagName("td");
   	for(var i=start, j=0; i<end; i++, j++) {   
   		if(j == gHotCursor) {
   			rows[j].getElementsByTagName("td")[0].getElementsByTagName("img")[0].width = "36";
   			rows[j].getElementsByTagName("td")[0].getElementsByTagName("img")[0].height = "36";
			rows[j].getElementsByTagName("td")[0].getElementsByTagName("img")[0].src = "./view/online/images/hotIcons/sel" + (i+1) + ".png";
   		} else {
   			rows[j].getElementsByTagName("td")[0].getElementsByTagName("img")[0].width = "20";
   			rows[j].getElementsByTagName("td")[0].getElementsByTagName("img")[0].height = "20";
 			rows[j].getElementsByTagName("td")[0].getElementsByTagName("img")[0].src = "./view/online/images/hotIcons/unSel" + (i+1) + ".png";	   		
   		}
	}
}

function showFgCatPanel() {
	document.getElementById("categoryGuidePanel").innerHTML = '<table class="catGuidePanelWide"><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr></table>';
	expandCategoryPanel();
}

function showFgChannelPanel() {
	document.getElementById("channelGuidePanel").innerHTML = '<table class="chGuidePanel'+curLocale+'"><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr></table>';
}

function showFgGuidePanel() {
	document.getElementById("proGuidePanel").innerHTML = '<table class="nowNextPanel'+curLocale+'"><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr></table>';
}

function showFgWidgetPanel() {
	return;
}

function expandExtrasPanel() {
	return;
}

function separateContent(content) {
	var sepContent = "";
	for(var i = 0; i < content.length; i++) {
		sepContent += '<tr><td>' + content.charAt(i) + '</td></tr>'; 
	}
	return sepContent;
}

function showLWPanel () {
}

function expandChannelPanel() {
	showFgWidgetPanel();
	document.getElementById("categoryGuidePanel").innerHTML = '';
	showFgCatPanel();
	updateChannelPanel();
	updateProgramListPanel();
}

function expandCategoryPanel() {
	updateCategoryPanel();		
	updateChannelPanel();
	updateGuidePanel();
}

function updateCategoryPanel() {
	gTotalCat = uniqCatNameCache.length - 1;
	var catNameHTML = '';
	var countCategory = 0;	
	var i;	
	for(i=0; (uniqCatNameCache.length > 5 && i < 5) || (uniqCatNameCache.length <= 5 && i < uniqCatNameCache.length); ++i) { 
 		catNameHTML += '<tr><td id="selectedCategory' + i + '" style="width:100%;height:45px;text-align:left;white-space:nowrap;vertical-align:middle;cursor:pointer;" onmouseover="selCatPanel(' + i + ');"  onclick="getCatChannels(' + i + ');">' 
						+ '<span id="selCategoryOrder'+i+ '" style="width:200;text-align:left">' 
						+ '&nbsp;&nbsp;&nbsp;&nbsp;' + (uniqCatNameCache[i].length > 16 ? uniqCatNameCache[i] : uniqCatNameCache[i])	
						+ '</span>'					
					+ '</td></tr>';
		countCategory++;
	}
	for(var i=0; i < (5 - countCategory) ; ++i) { 
		if(countCategory == 0) {
			break;
		}
		catNameHTML += '<tr>'
					+ '<td style="width:100%;height:45px;text-align:left;white-space:nowrap;vertical-align:middle;">'
						+ '&nbsp;&nbsp;'					
					+ '</td>'												
					+ '</tr>';
	}
	
	document.getElementById("categoryGuidePanel").innerHTML = 
															'<table width="100%" align="center" valign="top" cellspacing="0" cellpadding="0" class="chGuidePanelWide">'+
															'<tr>' +
																'<td width="95%" height="90%" id="tagPanel">' +
																	'<table valign="top" height="100%" width="100%">' +
																		(uniqCatNameCache.length  > 0 ? catNameHTML : '<tr><td height="28%" width="100%" align="center" valign="middle"><span style="width:100%;text-align:center;vertical-align:middle;background-repeat:no-repeat;">no tags available</span></td></tr>')
																	+ '</table>' +
																'</td>' +
																'<td width="5%" height="90%">' +
																	'<table valign="top" height="100%" width="100%"><tr><td height="20%" style="cursor:pointer;" ><img src="./view/mobi/images/uarrow.png" onclick="guidePage=2;scrollPageUp();" /></td></tr><tr><td height="60%">&nbsp;</td></tr><tr><td height="20%" style="cursor:pointer;" ><img src="./view/mobi/images/darrow.png" onclick="guidePage=2;scrollPageDown();" /></td></tr></table>'
															  + '</td>'+
															'</tr>' +
														 '</table>';	
}

function getFavoriteCount() {
	var i;
	var cnt = 0;
	for(i=0; i<favoriteCache.length; ++i) {
		if(favoriteCache[i] == 1)
			cnt++;
	}
	return cnt;
}

function buildChannelCache() {	
	selChannelIdCache = new Array();
	selChannelOrderCache = new Array();
	selChannelNameCache = new Array();
	selFavoriteCache = new Array();
	selLockedCache = new Array();
	selUseGraphicCache  = new Array();
	
	if (gSelectedCategory == 0) { // all channels
		selChannelIdCache = channelIdCache;
		selChannelNameCache = channelNameCache;
		selChannelOrderCache = channelOrderCache;		
		selFavoriteCache = favoriteCache;
		selLockedCache = lockedCache;	
		selUseGraphicCache = useGraphicCache;	
	} else {
		var i;
		var cnt = 0;	
		for(i=0; i<categoryNameCache.length; ++i) {
			if(categoryNameCache[i] == uniqCatNameCache[gSelectedCategory]) {
				selChannelIdCache[cnt] = channelIdCache[i];
				selChannelNameCache[cnt] = channelNameCache[i]; 
				selChannelOrderCache[cnt] = channelOrderCache[i];
				selFavoriteCache[cnt] = favoriteCache[i];
				selLockedCache[cnt] = lockedCache[i];
				selUseGraphicCache[cnt] = useGraphicCache[i];				
				cnt++;
			}							
		}
	}
	gTotalCh = (selChannelNameCache.length == 0 ? 0 : selChannelNameCache.length - 1);
	updateChannelPanel();		
}

function updateChannelPanel() {
	resetChannelPanel();
	var countChannel = 0;
	var chNameHTML = '';
	
	for(var i=0; (selChannelNameCache.length >= 5 && i < 5) || (selChannelNameCache.length < 5 && i < selChannelNameCache.length); ++i) { 
		chNameHTML += '<tr>'
					+ '<td id="selectedChannel'+i+'" onmouseover="highLightChannels('+ i +');" onmouseout="hideChannels('+ i +');" onclick="openSelChannels('+ i +');" style="width:100%;height:45px;text-align:left;white-space:nowrap;vertical-align:middle;cursor:pointer;">'
					+ '<table width="100%" align="center" valign="middle" cellspacing="0" cellpadding="0"><tr>'
					+ '<td id="selChannelsOrder'+i+ '" style="width:60px;text-align:left">&nbsp;&nbsp;'
					+ selChannelOrderCache[i]
					+ '</td>'
					+ '<td id="selChannelNames'+i+ '" style="width:100px;text-align:left">'
						+ (selChannelNameCache[i].length > 28 ? selChannelNameCache[i] : selChannelNameCache[i])
					+ '</td>'
					+ '<td style="width:25px;text-align:left;">'
					+ '</td>'
					+ '</tr>'
					+ '</table>'						
					+ '</td>'												
					+ '</tr>';
					countChannel++;
	}
	
	for(var i=0; i < (5 - countChannel) ; ++i) { 
		if(countChannel == 0) {
			break;
		}
		chNameHTML += '<tr>'
					+ '<td style="width:100%;height:45px;text-align:left;white-space:nowrap;vertical-align:middle;">'
					+ '<table width="100%" align="center" valign="top" cellspacing="0" cellpadding="0"><tr>'
						+ '<td style="width:100px;text-align:left">&nbsp;&nbsp;'
						+ '</td>'
					+ '</tr></table>'						
					+ '</td>'												
					+ '</tr>';
	}
	document.getElementById("channelGuidePanel").innerHTML = 
															'<table width="100%" align="center" valign="top" cellspacing="0" cellpadding="0" class="chGuidePanelWide">'+
															'<tr>' +
																'<td width="95%" height="90%">' +
																	'<table valign="top" height="100%" width="100%">' +
																		(selChannelNameCache.length  > 0 ? chNameHTML : '<tr><td height="28%" width="100%" align="center" valign="middle"><span style="width:100%;text-align:center;vertical-align:middle;background-repeat:no-repeat;">no channels available</span></td></tr>')
																	+ '</table>' +
																'</td>' +
																'<td width="5%" height="90%">' +
																	'<table valign="top" height="100%" width="100%"><tr><td height="20%" style="cursor:pointer;" ><img src="./view/mobi/images/uarrow.png" onclick="guidePage=3;scrollPageUp();" /></td></tr><tr><td height="60%">&nbsp;</td></tr><tr><td height="20%" style="cursor:pointer;" ><img src="./view/mobi/images/darrow.png" onclick="guidePage=3;scrollPageDown();" /></td></tr></table>'
															  + '</td>'+
															'</tr>' +
														 '</table>';
								
		updateGuidePanel();						
}

function updateGuidePanel() {
	if(selChannelIdCache.length > 0) {
		resetGuidePanel();
		updateSelChannelPanel();
		updateProgramListPanel();
		updateProgramDataPanel();		
	}
}

function getOrigCacheIndex() {
	for(var i=0; i<channelIdCache.length; ++i) {
		if(channelIdCache[i] == selChannelIdCache[gSelectedChannel]) {
			return i;
		}
	}
}

function updateSelChannelPanel() {
	if(selUseGraphicCache[gSelectedChannel] == 1) {
		document.getElementById("chLogoPanel").innerHTML  = '<img alt="" width="60px" height="40px" src="/ImageHandler?value=' + selChannelIdCache[gSelectedChannel] + '&icon=-1&imageType=0" style="padding:1px;" border="0" align="middle">';
	} else {
		document.getElementById("chLogoPanel").innerHTML = '<img alt="" width="60px" height="40px" src="/view/online/images/logo_none.png" style="padding:1px;" border="0" align="middle">';
	}
	
}

function updateProgramListPanel() {
	selGuidePageSet = new Array();
	var origCacheIndex = getOrigCacheIndex();
	if(!mnemonicCache[origCacheIndex]) {
		document.getElementById("proGuidePanel").innerHTML = ''
							+ '<table width="100%" align="center" valign="middle" cellspacing="0" cellpadding="0" class="nowNextPanelWide">' 
									+ '<tr><td valign="middle" align="center">no program available</td></tr>'
							+ '</table>';
		return;
	}
	
	
	var nowIndex = getGuideIndexStart(origCacheIndex);
	var guidePageSetOrig = guideCache[origCacheIndex].split('-$#-'); 
	var count = 0;
	for(var i=nowIndex; i<guidePageSetOrig.length; ++i) {
		selGuidePageSet[count++] = guidePageSetOrig[i];
	}
	gTotalGuide = (count == 0 ? 0 : count - 1);
	var programListHTML = '';
	var countProgram = 0;
	for(var i=0; (selGuidePageSet.length >= 5 && i < 5) || (selGuidePageSet.length < 5 && i < selGuidePageSet.length); ++i) { 
		var guideNowPage = selGuidePageSet[i];
		var guideNowPage = selGuidePageSet[i];
		var nowData = guideNowPage.split('|');		
		var epiTitle = nowData[6];
		if(epiTitle.length > 18)
			epiTitle = epiTitle.substring(0, 15) + '...';
		var startTime = nowData[1];	
		if(epiTitle != "" && startTime != "") {
			programListHTML += '<tr>'
							+ '<td id="selectedProgramGuide'+i+'" onmouseover="selectPdPanel('+ i +')" onclick="fullGuideKeyHandler(' + rcOK + ');" style="width:100%;height:45px;text-align:left;white-space:nowrap;vertical-align:middle;cursor:pointer;">'
								+ '<table width="100%" align="center" valign="top" cellspacing="0" cellpadding="0"><tr>'
									+ '<td id="selProgramTitle'+i+ '" style="width:165;text-align:left;padding-left:5px;" nowrap>'
										+ (epiTitle)
									+ '</td>'								
									+ '<td style="text-align:right">'										
//										+ (isProgramScheduled(origCacheIndex, i) ? '<img alt="" id="selAutoTuneImg'+i+ '" alt="" src="'+autoTuneIconSelImg.src+'"/>' : '<img alt="" id="selAutoTuneImg'+i+ '" src="'+autoTuneIconUnSelImg.src+'"/>')
									+ '&nbsp;</td>'												
								+ '</tr></table>'	
							+ '</td>'
							+ '</tr>';
			countProgram++;
		}
	}
	for(var i=0; i < (5 - countProgram) ; ++i) { 
		if(countProgram == 0) {
			break;
		}
		programListHTML += '<tr>'
							+ '<td style="width:100%;height:45px;text-align:left;white-space:nowrap;vertical-align:middle;">'
								+ '&nbsp;'	
							+ '</td>'
							+ '</tr>';
	}
	document.getElementById("proGuidePanel").innerHTML = 
														'<table width="100%" align="center" valign="top" cellspacing="0" cellpadding="0" class="chGuidePanelWide">'+
															'<tr>' +
																'<td width="95%" height="90%">' +
																	'<table valign="top" height="100%" width="100%">' +
																		(countProgram > 0 ? programListHTML : '<tr><td height="28%" width="100%" align="center" valign="middle"><span style="width:100%;text-align:center;vertical-align:middle;background-repeat:no-repeat;">no program available</span></td></tr>')
																	+ '</table>' +
																'</td>' +
																'<td width="5%" height="90%">' +
																	'<table valign="top" height="100%" width="100%"><tr><td height="20%" style="cursor:pointer;" ><img src="./view/mobi/images/uarrow.png" onclick="guidePage=4;scrollPageUp();" /></td></tr><tr><td height="60%">&nbsp;</td></tr><tr><td height="20%" style="cursor:pointer;" ><img src="./view/mobi/images/darrow.png" onclick="guidePage=4;scrollPageDown();" /></td></tr></table>'+
															  '</td>'+
															'</tr>' +
														 '</table>';													 						
}
								
function updateProgramDataPanel() {
	var origCacheIndex = getOrigCacheIndex();
	if(!mnemonicCache[origCacheIndex]) {
		document.getElementById("programGuidePanel").innerHTML = '';
		document.getElementById("fromToGuidePanel").innerHTML = '';
		document.getElementById("titleGuidePanel").innerHTML = '';
		return;
	}
	var sInfoText = selGuidePageSet[gSelectedGuide];
	
	var infoData = sInfoText.split('|');
	
	var epiTitle = infoData[6];
	if(epiTitle.length > 20)
		epiTitle = epiTitle.substring(0, 17) + '...';	

	descHTML = (infoData[10] ? infoData[10] : "");
	if(descHTML.length > 115) 
		descHTML = descHTML.substring(0, 112) + '...';
			
	castHTML = (infoData[8] ? infoData[8] : "");
	if(castHTML.length > 32) 
		castHTML = castHTML.substring(0, 29) + '...';
		
	footerHTML = (infoData[7] ? "(" + infoData[7] + ")&nbsp;" : "")
							+ (infoData[3] ? "(" + infoData[3] + ")&nbsp;" : "")
							+ (infoData[4] ? "(" + infoData[4] + ")&nbsp;" : "")						
							+ (infoData[11] ? "(" + infoData[11] + ")&nbsp;" : "")
							+ (infoData[12] ? "(" + infoData[12] + ")&nbsp;" : "")
							+ (infoData[5] ? "(" + infoData[5] + ")&nbsp;" : "")
							+ "&nbsp;";		
	var progDate = new Date(infoData[1]);
	progDate = new Date(progDate.getTime() - (progDate.getTimezoneOffset()*60*1000));
	var nMonth = progDate.getMonth();
	var nDate = progDate.getDate();

	nMonth = nMonth < 10 ? '0'+nMonth : nMonth;
	nDate = nDate < 10 ? '0'+nDate : nDate;	
	document.getElementById("programGuidePanel").innerHTML = (epiTitle ? epiTitle : '');
	document.getElementById("fromToGuidePanel").innerHTML = (infoData[1] && infoData[2] ? getMonthText(nMonth) + "&nbsp;" + nDate + "&nbsp;&nbsp;&nbsp;" + formatTime(infoData[1]) + " - " + formatTime(infoData[2]) : '');
	if(descHTML && descHTML.length > 115)
		descHTML = descHTML.substring(0, 112) + '...';	
	document.getElementById("titleGuidePanel").innerHTML = (descHTML ? descHTML : '');
}

////////////////////////
// Resetting business //
////////////////////////
function resetCategoryPanel() {	
	gSelectedCategory = 0;
	gCurrentCategory = 0;
	gCategoryCursor = 0; 
	gPrevCategoryCursor = 0;
}

function resetChannelPanel() {	
	gSelectedChannel = 0;
	gCurrentChannel = 0;
	gChannelCursor = 0;
	gPrevChannelCursor = 0;
}

function resetGuidePanel() {	
	gSelectedGuide = 0;
	gCurrentGuide = 0;
	gGuideCursor = 0;
	gPrevGuideCursor = 0;
}

function resetExtrasPanel() {
	return;
	var table = document.getElementById("hotExtrasPanel");   
	var rows = table.getElementsByTagName("td");
   	for(var i=0; i<5; i++) { 
   		rows[i].getElementsByTagName("td")[0].getElementsByTagName("img")[0].width = "20";
   		rows[i].getElementsByTagName("td")[0].getElementsByTagName("img")[0].height = "20"; 
		rows[i].getElementsByTagName("td")[0].getElementsByTagName("img")[0].src = "./view/online/images/hotIcons/unSel" + (i+1) + ".png";
   	}
	gHotCursor = 0;
}

////////////////////////
// Highlighting stuff //
////////////////////////
function toggleImage() {
	if(guidePage == 2) { // on category
		highlightCategory();
	} else if(guidePage == 3) { // on channel
		highlightChannel();	
	} else if(guidePage == 4) {  // on programs
		highlightGuide();
	}
}

function highlightCategory() {
	if(document.getElementsByTagName) { 
		var genreTable = document.getElementById("tagPanel");   
		var genreRows = genreTable.getElementsByTagName("td");
		if(genreRows != null) {
			genreRows[gPrevCategoryCursor].className = "catUnSelPanel";
			genreRows[gCategoryCursor].className = "catSelPanel";
			selCurCategory = gCategoryCursor;
		}
		document.getElementById("chLogoPanel").innerHTML  = '<img alt="" height="48px" width="48px" src="'+ sLogoCategory.src + '" style="padding:1px;" border="0" align="middle">';
      	document.getElementById("fromToGuidePanel").innerHTML = "";    
		
	 	var titleTable = document.getElementById("channelGuidePanel");   
		var titleRows = titleTable.getElementsByTagName("td");
		if(titleRows[gChannelCursor] != null) {
			titleRows[gChannelCursor].className = "cgUnSelPanel";
		}
	} 	
}

function highlightChannel() {
	if(document.getElementsByTagName) { 
		var selPrevChan = "selectedChannel" + gPrevChannelCursor;
		if(document.getElementById(selPrevChan) != null) {
			document.getElementById(selPrevChan).className = "channelUnSelPanel";
		}
		
		selPrevChan = "selectedChannel" + gChannelCursor;
		if(document.getElementById(selPrevChan) != null) {
			document.getElementById(selPrevChan).className = "channelSelPanel";
		}
		var selPrevChan = "selectedProgramGuide" + gGuideCursor;
		if(document.getElementById(selPrevChan) != null) {
			document.getElementById(selPrevChan).className = "guideUnSelPanel";
		}
	} 	
}

function highlightGuide() {
	if(document.getElementsByTagName) {  
		var selPrevChan = "selectedProgramGuide" + gPrevGuideCursor;
		 
		if(document.getElementById(selPrevChan) != null) {
			document.getElementById(selPrevChan).className = "guideUnSelPanel";
		}
		
		var selPrevChan = "selectedProgramGuide" + gGuideCursor;
		if(document.getElementById(selPrevChan) != null) {
			document.getElementById(selPrevChan).className = "guideSelPanel";
		}				
	} 	
}

//////////////////////////////////////////
// Scrolling business

////////////////////////////////////////////	
// CATEGORIES 
////////////////////////////////////////////
function scrollCategories(start, end) {
	var scrollCount = 0;
	for(var i = start; i <= end; ++i) { 
		var selCategoryOrder = "selCategoryOrder" + scrollCount;
		document.getElementById(selCategoryOrder).innerHTML = '&nbsp;&nbsp;&nbsp;&nbsp;' + (uniqCatNameCache[i].length > 16 ? uniqCatNameCache[i] : uniqCatNameCache[i]);
		scrollCount ++;
	}
	highlightCategory();
}
////////////////////////////////////////////
// CHANNNELS 
////////////////////////////////////////////
function scrollChannels(start, end) {
	var scrollCount = 0;
	for(var i = start; i <= end; ++i) { 
		
		var selChannelsOrder = "selChannelsOrder" + scrollCount;
		var selChannelNames = "selChannelNames" + scrollCount;
		
		document.getElementById(selChannelsOrder).innerHTML = '&nbsp;&nbsp;' + selChannelOrderCache[i];
		document.getElementById(selChannelNames).innerHTML =  (selChannelNameCache[i].length > 28 ? selChannelNameCache[i] : selChannelNameCache[i]);
		scrollCount ++;
	}
	highlightChannel();
}
////////////////////////////////////////////
// GUIDE 
////////////////////////////////////////////
function scrollGuide(start, end) {
	var origCacheIndex = getOrigCacheIndex();
	var scrollCount = 0;
	for(var i = start; i <= end; ++i) { 
		var guideNowPage = selGuidePageSet[i];
		var nowData = guideNowPage.split('|');		
		var epiTitle = nowData[6];
		if(epiTitle.length > 18)
			epiTitle = epiTitle.substring(0, 15) + '...';
		var startTime = nowData[1];							

		if(epiTitle != "" && startTime != "") {
			var selProgramTitle = "selProgramTitle" + scrollCount;
			var selAutoTuneImg = "selAutoTuneImg" + scrollCount;

			document.getElementById(selProgramTitle).innerHTML = epiTitle;
			scrollCount ++;					
		}
	}
	
	highlightGuide();
}

function changeCategoryCursor() {
//	alert("gCategoryCursor=> " + gCategoryCursor + " :gPrevCategoryCursor=> " + gPrevCategoryCursor);
	if(document.getElementsByTagName) {  
		var table = document.getElementById("tagPanel");   
		var rows = table.getElementsByTagName("td");
		rows[gPrevCategoryCursor].className = "catUnSelPanel";
		rows[gCategoryCursor].className = "catSelPanel";
		selCurCategory = gCategoryCursor;
   		document.getElementById("chLogoPanel").innerHTML  = '<img alt="" height="48px" width="48px" src="'+ sLogoCategory.src + '" style="padding:1px;" border="0" align="middle">'; 
      	document.getElementById("fromToGuidePanel").innerHTML = ""; 
	} 
}

function changeChannelCursor() {
	if(document.getElementsByTagName) {  
		var selPrevChan = "selectedChannel" + gPrevChannelCursor;
		if(document.getElementById(selPrevChan) != null) {
			document.getElementById(selPrevChan).className = "channelUnSelPanel";
		}
		selPrevChan = "selectedChannel" + gChannelCursor;
		if(document.getElementById (selPrevChan) != null) {
			document.getElementById(selPrevChan).className = "channelSelPanel";
		}
	} 
}



function changeGuideCursor() {
	if(document.getElementsByTagName) {  
		var selPrevChan = "selectedProgramGuide" + gPrevGuideCursor;
		if(document.getElementById(selPrevChan) != null) {
			document.getElementById(selPrevChan).className = "guideUnSelPanel";
		}
		
		var selPrevChan = "selectedProgramGuide" + gGuideCursor;
		if(document.getElementById(selPrevChan) != null) {
			document.getElementById(selPrevChan).className = "guideSelPanel";
		}
	} 
}

function updateAutoTuneImg(active) {
	if(document.getElementsByTagName) {
		var selAutoImage = "selAutoTuneImg" + gGuideCursor;
		if(active == 1) {
			document.getElementById(selAutoImage).src = autoTuneIconSelImg.src;
		} else {
			document.getElementById(selAutoImage).src = autoTuneIconUnSelImg.src;			
		}
	} 
}

function widgetsInfo(currImage) {
	document.getElementById("programGuidePanel").innerHTML = "";
	document.getElementById("fromToGuidePanel").innerHTML = "";
	var useGraphic = wrecords.wRecords[currImage].useGraphic;
	useGraphic = useGraphic.substring(1, useGraphic.length);
	if((wrecords.wRecords[currImage].id).length != 0)					
		document.getElementById("chLogoPanel").innerHTML  = '<img  alt="" height="36px" width="36px" src=' + (useGraphic == 1 ? "/ImageHandler?value=" + wrecords.wRecords[currImage].id + "&icon=2&imageType=2" : "/view/online/images/widgets/wImages/widgetNA.png") + ' style="padding:1px;" border="0" align="middle">';
	else 
		document.getElementById("chLogoPanel").innerHTML  = '';
	var titleGuidePanel = document.getElementById("titleGuidePanel").innerHTML;
	switch(currImage) {
		case currImage:
			titleGuidePanel = wrecords.wRecords[currImage].description;
			titleGuidePanel = (titleGuidePanel.length > 150 ? titleGuidePanel.substring(0, 150)+ "..." : titleGuidePanel);
		break;
	}
	document.getElementById("titleGuidePanel").innerHTML = titleGuidePanel;
}

function updateWebExtras() {
	var currentImage = "./view/online/images/hotIcons/logo" + (extraPanelIndex + 1) + ".png";
	if(extraPanelIndex == 0) {			
		document.getElementById("chLogoPanel").innerHTML  = '<img alt="" height="48px" width="48px" src="'+ currentImage + '" style="padding:1px;" border="0" align="middle">';
		document.getElementById("programGuidePanel").innerHTML = "";
      	document.getElementById("titleGuidePanel").innerHTML = records.key330;
      	document.getElementById("fromToGuidePanel").innerHTML = "";
	} else if(extraPanelIndex == 1) {	
		document.getElementById("chLogoPanel").innerHTML  = '<img alt="" height="48px" width="48px" src="'+ currentImage + '" style="padding:1px;" border="0" align="middle">'; 				
		document.getElementById("programGuidePanel").innerHTML = "";
      	document.getElementById("titleGuidePanel").innerHTML = records.key331;
      	document.getElementById("fromToGuidePanel").innerHTML = "";
	} else if(extraPanelIndex == 2) {			
		document.getElementById("chLogoPanel").innerHTML  = '<img alt="" height="48px" width="48px" src="'+ currentImage + '" style="padding:1px;" border="0" align="middle">'; 		
		document.getElementById("programGuidePanel").innerHTML = "";
		document.getElementById("titleGuidePanel").innerHTML = records.key332;
      	document.getElementById("fromToGuidePanel").innerHTML = "";
	} else if(extraPanelIndex == 3) {
		document.getElementById("chLogoPanel").innerHTML  = '<img alt="" height="48px" width="48px" src="'+ currentImage + '" style="padding:1px;" border="0" align="middle">';
		document.getElementById("programGuidePanel").innerHTML = "";
		document.getElementById("titleGuidePanel").innerHTML = records.key333;
      	document.getElementById("fromToGuidePanel").innerHTML = "";
	} else if(extraPanelIndex == 4) {	
	 	document.getElementById("chLogoPanel").innerHTML  = '<img alt="" height="48px" width="48px" src="'+ currentImage+ '" style="padding:1px;" border="0" align="middle">';
		document.getElementById("programGuidePanel").innerHTML = "";
		document.getElementById("titleGuidePanel").innerHTML = records.key334;
      	document.getElementById("fromToGuidePanel").innerHTML = "";
	} else if(extraPanelIndex == 5) {			
      	document.getElementById("chLogoPanel").innerHTML  = '<img alt="" height="48px" width="48px" src="'+ currentImage + '" style="padding:1px;" border="0" align="middle">';
      	document.getElementById("programGuidePanel").innerHTML = "";
		document.getElementById("titleGuidePanel").innerHTML = records.key335;
      	document.getElementById("fromToGuidePanel").innerHTML = "";
	} else if(extraPanelIndex >= 8) {	
		widgetsInfo(currImage);
	} else {   			
		return;
	}
}
function unique(a) {
	tmp = new Array(0);
	for(i=0; i<a.length; i++){
		if(!contains(tmp, a[i])) {
			tmp.length += 1;
			tmp[tmp.length-1]=a[i];
		}
	}
	return tmp;
}

function contains(a, e) {
	for(j=0;j<a.length;j++)
		if(a[j] == e)
			return true;
	return false;
}
// on mouse overing the channel is selected 
function highLightChannels(index) {
	gPrevChannelCursor = gChannelCursor;
	var selPrevChan = "selectedChannel" + gPrevChannelCursor;
	if(document.getElementById(selPrevChan) != null) {
		document.getElementById(selPrevChan).className = "channelUnSelPanel";
	}
	selPrevChan = "selectedChannel" + index;
	gChannelCursor  = index; 
	gPrevChannelCursor = index;
	if(document.getElementById (selPrevChan) != null) {
		document.getElementById(selPrevChan).className = "channelSelPanel";
	}
	gCurrentChannel  = getChannelClicks + index;
	gSelectedChannel = gCurrentChannel;
	updateGuidePanel();	
}
// it hides the cur category or selected channels
function hideCatChannels() {
	var selPrevChan = "selectedChannel" + gChannelCursor;
	if(document.getElementById (selPrevChan) != null) {
		document.getElementById(selPrevChan).className = "channelUnSelPanel";
	}
	var genreTable = document.getElementById("tagPanel");   
	var genreRows = genreTable.getElementsByTagName("td");
	if(genreRows[gCategoryCursor] != null)
		genreRows[gCategoryCursor].className = "catUnSelPanel";
}




// on clicking the selected channel will open the miniguide with the current available program data 
function openSelChannels(index) {
	guidePage = 3;
	fullGuideKeyHandler(rcOK);
}
function hideChannels(index) {
	return;
	var selPrevChan = "selectedChannel" + index;
	if(document.getElementById (selPrevChan) != null) {
		document.getElementById(selPrevChan).className = "guideUnSelPanel";
	}
}

// The method is used to select the program data 
function selectPdPanel(index) {
	resetExtrasPanel();
	guidePage = 4; 
	gPrevGuideCursor = gGuideCursor;
	var selPrevChan = "selectedProgramGuide" + gPrevGuideCursor;
	if(document.getElementById(selPrevChan) != null) {
		document.getElementById(selPrevChan).className = "guideUnSelPanel";
	}
	
	selPrevChan = "selectedProgramGuide" + index;
	gPrevGuideCursor = index;
	if(document.getElementById(selPrevChan) != null) {
		document.getElementById(selPrevChan).className = "guideSelPanel";
	} 
	gGuideCursor = index;
	gCurrentGuide = index + changePdCursor;
	gSelectedGuide = gCurrentGuide;
	updateProgramDataPanel();
}
function selTagPanel() {
	statusKey = false;
	resetExtrasPanel();
	removerCursor();
	guidePage = 2; // make catGuidePanel
	expandCategoryPanel();
	toggleImage();	
}

function unSelTagPanel() {
	
}

function selCatPanel(index) {
	guidePage = 2;
	gPrevCategoryCursor = gCategoryCursor;
	var selCategory = "selectedCategory" + gPrevCategoryCursor;
	if(document.getElementById(selCategory) != null) {
		document.getElementById(selCategory).className = "catUnSelPanel";
	}
	selCategory = "selectedCategory" + index;
	gPrevCategoryCursor = index;
	if(document.getElementById(selCategory) != null) {
		document.getElementById(selCategory).className = "catSelPanel";
	} 
	gCategoryCursor = index;
	gCurrentCategory = index + countCatClicks;
	gSelectedCategory = gCurrentCategory;
	buildChannelCache();
}
function getCatChannels(index) {
	guidePage = 3;
	expandChannelPanel();
	toggleImage();
}
function hideControlPanelActive() {
	if(document.getElementById("controlPanel0") != null) {
		document.getElementById("controlPanel0").style.visibility = "hidden";
	}
}

function fullGuideKeyHandler(keyCodeNum) {
	switch(keyCodeNum) {
		case rcZERO: 
		case rcONE: 
		case rcTWO: 
		case rcTHREE: 
		case rcFOUR: 
		case rcFIVE:
		case rcSIX: 
		case rcSEVEN: 
		case rcEIGHT: 
		case rcNINE: 
			if(menuActive) {
				return;
			}
			var keyNum = keyCodeNum - 48; 
			if(!menuActive) {			
				fadeReset(nNumberFadeAmount); 
			}
			if(!oTriggerSwitchTimer) {
				if(oSwitchTimer != null) {
					window.clearTimeout(oSwitchTimer);
					oSwitchTimer = null;		
				}		
				userSelChOrder = keyNum.toString();
				showChannelKeyPanel();
				if(updateMBTimer != null ) {
					window.clearTimeout(updateMBTimer);
					updateMBTimer = null;
				}
				document.getElementById("findChOrder").innerHTML = userSelChOrder;
				oTriggerSwitchTimer = window.setTimeout('clearSwitchChannel();', 3000);
			} else if(bKeyChannel) {
				if(oSwitchTimer != null) {
					window.clearTimeout(oSwitchTimer);
					oSwitchTimer = null;	
				}
				if(oTriggerSwitchTimer != null ) {		
					window.clearTimeout(oTriggerSwitchTimer);
					oTriggerSwitchTimer = null;
				}
				userSelChOrder += keyNum.toString();
				showChannelKeyPanel();
				if(updateMBTimer != null ) {
					window.clearTimeout(updateMBTimer);
					updateMBTimer = null;
				}
				document.getElementById("findChOrder").innerHTML = userSelChOrder.substr(0,4);
				oTriggerSwitchTimer = window.setTimeout('clearSwitchChannel();', 3000);	
			}
			bKeyChannel = true;
		break;		
		case rcLEFT:
			if(menuActive) {
				if(guidePage == 4) { // was  nowNextPanel
					guidePage = 3; // make chGuidePanel
					toggleImage();	
				} else if(guidePage == 3) { // was chGuidePanel
					guidePage = 2; // make catGuidePanel
					var selPrevChan = "selectedChannel" + gChannelCursor;
					if(document.getElementById(selPrevChan) != null) {
						document.getElementById(selPrevChan).className = "channelUnSelPanel";
					}
					toggleImage();		
				} else if(guidePage == 2) { // was catGuidePanel
					var genreTable = document.getElementById("tagPanel");   
					var genreRows = genreTable.getElementsByTagName("td");
					if(genreRows[gCategoryCursor] != null) {
						genreRows[gCategoryCursor].className = "catUnSelPanel";
					}
					if(document.getElementById("programGuidePanel").innerHTML) {
						if(selGuidePageSet.length > 0) {
							guidePage = 4;
							toggleImage();
						}
					}  else {
						guidePage = 3; // make catGuidePanel
						expandChannelPanel();
						toggleImage();
					}
				} 
			} else if(miniguideActive){			
				if (bExtInfoMode && !bItsAllDark && getGuide()) { // Extended Panel Browsing
					minusGuideIndex();
					redrawLayers();
				} else if(!bExtInfoMode) { // CATEGORY BROWSING
					showMiniGuidePanel();
					nCurrentCategory--;
					nCurrentCategory = ( nCurrentCategory < 0 ) ? uniqCategoryCache.length - 1 : ( nCurrentCategory >  ( uniqCategoryCache.length - 1 ) ) ? 0 : nCurrentCategory;
					nCurrentChannel = getFirstChInCat(uniqCategoryCache[nCurrentCategory]); // first channel in this category
					resetGuideIndex(); // c13
					doFade();
				}		
			}
			break;
		case rcRIGHT:
			if(menuActive) {
				if(guidePage == 2) {  //	was catGuidePanel
					guidePage = 3;   // make chGuidePanel
					toggleImage();
				} else if(guidePage == 3) {  //	was chGuidePanel
					if(document.getElementById("programGuidePanel").innerHTML) {
						if(selGuidePageSet.length > 0) {
							guidePage = 4;
							toggleImage();
						}
					}  else {
						guidePage = 2; // make catGuidePanel
						var selPrevChan = "selectedChannel" + gChannelCursor;
						if(document.getElementById(selPrevChan) != null) {
							document.getElementById(selPrevChan).className = "channelUnSelPanel";
						}
						toggleImage();
					}
				} else if(guidePage == 4) { // was guidePanel
					guidePage = 2; // make catGuidePanel
					var selPrevChan = "selectedProgramGuide" + gGuideCursor;
					if(document.getElementById(selPrevChan) != null) {
						document.getElementById(selPrevChan).className = "guideUnSelPanel";
					}
					toggleImage();
				} 
			} else if(miniguideActive){
				if (bExtInfoMode && !bItsAllDark && getGuide()) {
					plusGuideIndex();
					redrawLayers();
				} else if(!bExtInfoMode) { // CATEGORY BROWSING
					showMiniGuidePanel();
					nCurrentCategory++;
					nCurrentCategory = ( nCurrentCategory < 0 ) ? uniqCategoryCache.length - 1 : ( nCurrentCategory >  ( uniqCategoryCache.length - 1 ) ) ? 0 : nCurrentCategory;
					nCurrentChannel = getFirstChInCat(uniqCategoryCache[nCurrentCategory]); // first channel in this category
					resetGuideIndex(); // c14
					doFade();
				}
			}
			break;
		case rcCHANNELPLUS:  // channel plus and minus functionality does not work on this action 
		case rckbPLUS: 
		   if(menuActive) 
				return 0;
		   bSwitchChannel = true; 
		   if(bItsAllDark) 
				bExtInfoMode = false; 
		   doFade();
		case rcUP:
			if(menuActive) {
				if(guidePage == 2) { // CATEGORIES PAGE
					if(gCurrentCategory == 0)  // first one
						gCurrentCategory = gTotalCat+1;
					gPrevCategoryCursor = gCategoryCursor;
					gCurrentCategory--;
					if(gCategoryCursor != 0 || gTotalCat <4) {//  image, don't scroll though
						(gCurrentCategory == gTotalCat) ? (gCategoryCursor = gTotalCat+1) : "";
						gCategoryCursor--;					
						changeCategoryCursor();
					} else {
						var start;
						var end;
						(gCurrentCategory == gTotalCat) ?  (start = gTotalCat -4, end = gTotalCat, gCategoryCursor = 4) : (start = gCurrentCategory, end = gCurrentCategory + 4) ;
						scrollCategories(start, end);
					}				
					gSelectedCategory = gCurrentCategory;
					countCatClicks = gSelectedCategory - gCategoryCursor;
					buildChannelCache();				
															
				} else if(guidePage == 3) { // CHANNELS PAGE	
					// keep in bounds
					if(gCurrentChannel == 0) // first one
						gCurrentChannel = gTotalCh+1;
					gPrevChannelCursor = gChannelCursor;
									
					gCurrentChannel--;
					if(gChannelCursor != 0 || gTotalCh < 4) { //  image, don't scroll though
						(gCurrentChannel == gTotalCh) ? (gChannelCursor = gTotalCh+1) : "";
						gChannelCursor--;					
						changeChannelCursor();
					} else { // don't  image, scroll though
						var start = "";
						var end = "";
						(gCurrentChannel == gTotalCh) ?  (start = gTotalCh -4, end = gTotalCh, gChannelCursor = 4) : (start = gCurrentChannel, end = gCurrentChannel + 4) ;
						scrollChannels(start, end);
					}				
					gSelectedChannel = gCurrentChannel;
					getChannelClicks = gSelectedChannel - gChannelCursor;
					updateGuidePanel();		
				} else if(guidePage == 4) { // GUIDE PAGE	
					// keep in bounds
					if(gCurrentGuide == 0) // first one
						gCurrentGuide = gTotalGuide+1;
						
					gPrevGuideCursor = gGuideCursor;
					gCurrentGuide--;
					if(gGuideCursor != 0 || gTotalGuide < 4) { //  image, don't scroll though
						(gCurrentGuide == gTotalGuide) ? (gGuideCursor = gTotalGuide+1) : "";
						gGuideCursor--;					
						changeGuideCursor();
					} else { // don't  image, scroll though
						var start = "";
						var end = "";
						(gCurrentGuide == gTotalGuide) ?  (start = gTotalGuide -4, end = gTotalGuide, gGuideCursor = 4) : (start = gCurrentGuide, end = gCurrentGuide + 4) ;
						scrollGuide(start, end);
					}		
					changePdCursor = gCurrentGuide - gGuideCursor;		
					gSelectedGuide = gCurrentGuide;		
					
					updateProgramDataPanel();															
					
				} 
			} else if(miniguideActive) {
			 	showMiniGuidePanel();
				nCurrentChannel++; 
				nCurrentChannel = ( nCurrentChannel < 0 ) ? ( channelIdCache.length - 1 ) : ( nCurrentChannel > ( channelIdCache.length - 1 ) ) ? 0 : nCurrentChannel;		
				resetGuideIndex();	
				doFade();
			}
			break;
		case rcCHANNELMINUS:
		case rckbMINUS: 
			if(menuActive) 
				return 0;
			bSwitchChannel = true; 
			if(bItsAllDark)
				bExtInfoMode = false; 
			doFade();
		case rcDOWN:
			if(menuActive) {
				if(guidePage == 2) { // CATEGORIES PAGE	
					if(gCurrentCategory == gTotalCat) { // last one		
						gCurrentCategory = -1;				
						//break;
					} 
					gPrevCategoryCursor = gCategoryCursor;
					gCurrentCategory++;
					// if the cursor is moving down, no scroll is needed
					if(gCategoryCursor < 4) {
						(gCurrentCategory == 0) ? (gCategoryCursor = -1) : "";
						gCategoryCursor++;					
						changeCategoryCursor();
					} else { // scroll
						var start = "";
						var end = "";
						(gCurrentCategory == 0) ? (start = 0, end = 4, gCategoryCursor = 0) : (start = gCurrentCategory - 4, end = gCurrentCategory);
						scrollCategories(start, end);
					}			
					gSelectedCategory = gCurrentCategory;
					countCatClicks = gSelectedCategory - gCategoryCursor;
					buildChannelCache();										
				} else if(guidePage == 3) {	// CHANNELS PAGE
					if(gCurrentChannel == gTotalCh) { // last one 						
						gCurrentChannel = -1;
					}
					gPrevChannelCursor = gChannelCursor; 
					gCurrentChannel++;
					// if the cursor is moving down, no scroll is needed
					if(gChannelCursor < 4) {
						(gCurrentChannel == 0) ? (gChannelCursor = -1) : "";
						gChannelCursor++;					
						changeChannelCursor();
					} else {
						var start = "";
						var end = "";
						(gCurrentChannel == 0) ?  (start = 0, end = 4, gChannelCursor = 0) : (start = gCurrentChannel - 4, end = gCurrentChannel) ;
						scrollChannels(start, end);
					}			
					gSelectedChannel = gCurrentChannel;
					getChannelClicks = gSelectedChannel - gChannelCursor;
					updateGuidePanel();	
				} else if(guidePage == 4) { // GUIDE PAGE	
					if(gCurrentGuide == gTotalGuide) // last one
						gCurrentGuide = -1;
	
					gPrevGuideCursor = gGuideCursor;
								
					gCurrentGuide++;
					
					// if the cursor is moving down, no scroll is needed
					if(gGuideCursor <4) {
						(gCurrentGuide == 0) ? (gGuideCursor = -1) : "";
						gGuideCursor++;					
						changeGuideCursor();
					} else { // scroll
						var start = "";
						var end = "";
						(gCurrentGuide == 0) ?  (start = 0, end = 4, gGuideCursor = 0) : (start = gCurrentGuide - 4, end = gCurrentGuide) ;
//						++changePdCursor;
						scrollGuide(start, end);
					}			
					changePdCursor = gCurrentGuide - gGuideCursor;	
					gSelectedGuide = gCurrentGuide;
					updateProgramDataPanel();	
				} 
			} else if(miniguideActive) {
				 	showMiniGuidePanel();
					nCurrentChannel --; 
					nCurrentChannel = ( nCurrentChannel < 0 ) ? ( channelIdCache.length - 1 ) : ( nCurrentChannel > ( channelIdCache.length - 1 ) ) ? 0 : nCurrentChannel;				
					resetGuideIndex();
					doFade();
				}
			break;
		case rcOK:
			if(menuActive) {
				if(guidePage == 2) { // CATEGORIES PAGE
					guidePage = 3;
					expandChannelPanel();
					toggleImage();
					//expandChannelPanel();						
				} else if(guidePage == 3) { // CHANNELS PAGE : tune to the channel
					for(var i=0; i<channelOrderCache.length; ++i) {
						if(channelOrderCache[i] == selChannelOrderCache[gSelectedChannel]) {
							nLastSelectedChannel = nSelectedChannel;	
							nSelectedChannel = nOldChannel = nCurrentChannel = i;	
							miniguideActive = true;
							showMiniGuidePanel();
							switchChannel();
							channelChanged = true;			
							launchViewer();	
							doFade();					
							menuActive = false;		
							// uniqCatNameCache.shift();
							// uniqCatNameCache.shift();									
							break;
						}					
					}	
				} else if(guidePage == 4) { // scheduling is avoided in this EPG
					for(var i=0; i<channelOrderCache.length; ++i) {
						if(channelOrderCache[i] == selChannelOrderCache[gSelectedChannel]) {
							nLastSelectedChannel = nSelectedChannel;	
							nSelectedChannel = nOldChannel = nCurrentChannel = i;																	
							miniguideActive = true;
							showMiniGuidePanel();
							switchChannel();
							channelChanged = true;			
							launchViewer();		
							doFade();				
							menuActive = false;		
							// uniqCatNameCache.shift();
							// uniqCatNameCache.shift();									
							break;
						}
					}	
					return 0;
				} 
			} else if(miniguideActive){
				showMiniGuidePanel();
				if(nSelectedCategory != nCurrentCategory) {
					//  category too
					nSelectedCategory = nCurrentCategory;
				}
				if(bKeyChannel) { // NEED TO REDESIGN?
					if(oTriggerSwitchTimer != null) {
						window.clearTimeout(oTriggerSwitchTimer);
						oTriggerSwitchTimer = null;
					}
					triggerSwitchChannel();
				}
				if ( nSelectedChannel != nCurrentChannel ) {
					nCurrentChannel = ( nCurrentChannel < 0 ) ? ( channelIdCache.length - 1 ) : ( nCurrentChannel > ( channelIdCache.length - 1 ) ) ? 0 : nCurrentChannel;
	
					nLastSelectedChannel = nSelectedChannel; // save currently selected channel before it is updated
					nSelectedChannel = nCurrentChannel;
					
					// Switch stream 
					if(oSwitchTimer) {
						window.clearTimeout ( oSwitchTimer );
						oSwitchTimer = null;
					}
					oSwitchTimer = window.setTimeout('switchChannel();', 1);
					 // Redraw panel               
					bExtInfoMode = false;			
					channelChanged = true;
					showMiniGuidePanel();
					launchViewer(); 	
				}
				doFade();
			}
			break;
		case rcMENU:
			if(menuActive) {
//				hideControlPanelActive();
				if(document.getElementById("goPanel") != null) {
					document.getElementById("goPanel").style.visibility = 'hidden';
				}
				// uniqCatNameCache.shift();
				// uniqCatNameCache.shift();		
					
				showMiniGuidePanel();
				channelChanged = true;
				launchViewer();	
				miniguideActive = true;
				menuActive = false;	
				doFade();		
			} else {
				menuActive = true;	
				miniguideActive = false;	
//				uniqCatNameCache.unshift(records.key339);
				if(updateMBTimer != null ) {
					window.clearTimeout(updateMBTimer);
					updateMBTimer = null;
				}
				nCurrentChannel = nOldChannel = nSelectedChannel;   
//				loadPage("fullGuide");
				showFullguidePanel();
//				showGuidePanel();	
			}
			break;
	}
	nCurrentChannel = ( nCurrentChannel < 0 ) ? ( channelIdCache.length - 1 ) : ( nCurrentChannel > ( channelIdCache.length - 1 ) ) ? 0 : nCurrentChannel;
	if ( nCurrentChannel != nOldChannel ) {
		if ( bSwitchChannel ) { // rcCHANNELPLUS, rcCHANNELMINUS
			bSwitchChannel = false;
			nLastSelectedChannel = nSelectedChannel;
			nSelectedChannel = nCurrentChannel;
			nOldChannel = nCurrentChannel;
			
			// Switch stream 
			if(oSwitchTimer) {
				window.clearTimeout ( oSwitchTimer );
				oSwitchTimer = null;
			}
			oSwitchTimer = window.setTimeout('switchChannel();', 1);
			
            // Redraw panel               
			bExtInfoMode = false;			
			channelChanged = true;
			launchViewer(); 
			// Fade out...
			doFade();
			//return 0;
		}

		// NOW SHOW THE NEW CHANNEL PANEL
		
		if(bItsAllDark) { // if it was already dark
			bExtInfoMode = false; // there is no question of extended info
			redrawLayers();
		} else {
			if(oFadeOutTimer) { // if the fade had already started -- like AlphaLevel = 60 instead of 100
				doFade(); // restart fading
				// Fahgeddahbahtit
				nCurrentChannel = nOldChannel;
			} else {
//				clearLockPanel();
				redrawLayers();         				
			}
		}
	} // if condition for (nCurrentChannel != nOldChannel) finished

	nOldChannel = nCurrentChannel;	
	return 0;	
}
//////////////////////////////////////// GET CACHE STARTS //////////////////////////////////////////////////////
function getCache () {
	var customerId = YAHOO.util.Cookie.get("customerId");
	var postData = "method=getCache" + "&customerId="+ customerId;
	createCacheResponse(postData);
}

function createCacheResponse(params) {
	createRequest();
	if(ajaxRequest == null) {
		return;
	}
	ajaxRequest.onreadystatechange = function() { paintCacheContent(); };
	ajaxRequest.open("POST", '/OnlineAdmin', true);
	ajaxRequest.setRequestHeader("Content-Type" , "application/x-www-form-urlencoded");
	ajaxRequest.send(params);
	return false;	
}

function paintCacheContent() {
   if (ajaxRequest.readyState == 4) {
   			if(ajaxRequest.status == 0 || ajaxRequest.status == 12029 || ajaxRequest.status == 500 ) {
		  		failureInfoMsg("Network Failure!");
			}
		try{
			if(ajaxRequest.status == 200) {
	    		if (document.getElementById("failureInfoPanel")) {
					document.getElementById("failureInfoPanel").style.display = "none";
				}
	    		getCacheDetails(ajaxRequest.responseText);
			} else if(ajaxRequest.status == 500) {
				if (document.getElementById("failureInfoPanel")) {
					document.getElementById("failureInfoPanel").style.display = "block";
				}
				failureInfoMsg("Network Failure!");	
			}
		} catch(x){
			failureInfoMsg("Network Failure!");	
		}	
    	
	} 
}
var statusTimer = null;
function getCacheDetails(ajaxResponse) {
//	alert(ajaxResponse);
//	return;
	try {
//	 	o.responseText = (o.responseText.length > 20000 ? o.responseText.toString().substring(0, 20000) : o.responseText.toString());
	 	if((ajaxResponse).length > 0) {
	 		if(clearIntervals() == 1) {
				return;
			}	
			response = ajaxResponse;
			var data = response.split('-?#-');
//			alert(data);
//			var data = response.split('-?#-');
			/**
			 *  cache information by using separator
			 */
			resetItemVars();
			menuActive = true;
			categoryOrderCache 	= data[0].split('-@#-');
			categoryNameCache 	= data[1].split('-@#-');
			channelIdCache 		= data[2].split('-@#-');
			channelOrderCache 	= data[3].split('-@#-');
			channelNameCache 	= data[4].split('-@#-');
			useGraphicCache 	= data[5].split('-@#-');
			graphicCache 		= data[6].split('-@#-');
			noVideoCache 		= data[7].split('-@#-');
			streamAddressCache 	= data[8].split('-@#-');
			mnemonicCache 		= data[9].split('-@#-');
			guideCache 		= data[10].split('-*$-');
			favoriteCache = data[12].split('-@#-');
			lockedCache = data[13].split('-@#-');
			protocol = data[15].split('-@#-');
			transcodeCache = data[16].split('-@#-');
			transStatusCache = data[17].split('-@#-');
			securityCache = data[18].split('-@#-');
//			alert("securityCache: " + securityCache);
			uniqCategoryCache = unique(categoryOrderCache);
			uniqCatNameCache = unique(categoryNameCache);
			uniqCatNameCache.sort();
			uniqCatNameCache.unshift(records.key339);
			// virendra changes
			document.getElementById("epgPlayerAtfg").innerHTML = paintPlayer();
//			alert(document.getElementById("epgPlayerAtfg").innerHTML);
			guidePage = 3; 	
			showFullguidePanel();
			resetCategoryPanel();
			buildChannelCache();
			expandChannelPanel(); //  2
		//	resetExtrasPanel();	
			highlightChannel(); //  1
//			getStatusNow();
	 	} else {
	 		if(clearIntervals() == 1) {
				return;
			}
			window.setTimeout('getCache();', 15*1000); //  this to 30 sec
//			if(document.getElementById("controlPanel0")) {
//				document.getElementById("controlPanel0").style.visibility = "hidden";
//			}
			if(document.getElementById("fullGuideHeader")) {
				document.getElementById("fullGuideHeader").style.visibility = "visible";
				getCurrentFullGuideTime();
			}
			
			buildChannelCache();
			expandChannelPanel();
		}
	
	 } catch (exception) { 
//	 	alert(exception);
	 }
}
function getStatusNow() {
	var customerId = YAHOO.util.Cookie.get("customerId");
	var params = "method=getStatusDetails&customerId="+ customerId;
	createRequest();
	if(ajaxRequest == null) {
		return;
	}
	ajaxRequest.onreadystatechange = function() { paintStatusContent(); };
	ajaxRequest.open("POST", '/OnlineAdmin', true);
	ajaxRequest.setRequestHeader("Content-Type" , "application/x-www-form-urlencoded");
	ajaxRequest.send(params);
	return false;	
}

function paintStatusContent() {
	 if (ajaxRequest.readyState == 4) {
	 	if(ajaxRequest.status == 0 || ajaxRequest.status == 12029 || ajaxRequest.status == 500 ) {
		  	failureInfoMsg("Network Failure!");
		}
		try{
			if(ajaxRequest.status == 200) {
				if (document.getElementById("failureInfoPanel")) {
					document.getElementById("failureInfoPanel").style.display = "none";
				}
	    		var ajaxResponse = ajaxRequest.responseText;
	    		if(ajaxResponse.length > 0) {
	    			response = YAHOO.lang.JSON.parse(ajaxResponse);
	    			for(var j = 0; j < response.length; j++) {
	    				statusCache[j] = response[j];
	    			}
	    			updateStatusPanel();
	    		} 
			} else if(ajaxRequest.status == 500) {
				if (document.getElementById("failureInfoPanel")) {
					document.getElementById("failureInfoPanel").style.display = "block";
				}
				failureInfoMsg("Network Failure!");
			}	
		}catch(e){
			failureInfoMsg("Network Failure!");
		}		
    	
	} 
}

function updateStatusPanel() {
//	if(statusTimer) {
//		window.clearTimeout(statusTimer);
//		statusTimer = null;
//	}
//	statusTimer = window.setTimeout("getStatusNow();", 7*1000);
	for(var k = 0; k < statusCache.length; k++) {
		if(statusCache[k] == 1)
			document.getElementById("unSelFavImage" + k).src = "./view/online/images/content/available.png";
		else 
			document.getElementById("unSelFavImage" + k).src = "./view/online/images/content/invisible.png";
	}
}
function getCategory() {
	var name = categoryNameCache[nCurrentChannel];
	/*
	if(name.length > 20)
		name = name.substring(0, 17) + '...';
	*/	
	return name;
}

function getCategoryOrder() {
	return categoryOrderCache[nCurrentChannel];
}

function getMnemonic() {
	return mnemonicCache[nCurrentChannel]; 
}

function getChannelId() {
	return channelIdCache[nCurrentChannel]; 
}

function getSelectedChannelId() {
	return channelIdCache[nSelectedChannel]; 
}

function getChannelName() {
	var name = channelNameCache[nCurrentChannel];
	/*
	if(name.length > 28)
		name = name.substring(0, 24) + '...';
	*/
	return name; 
}

function getChannelNameFor(channelIndex) {
	var name = channelNameCache[channelIndex];
	/*
	if(name.length > 28)
		name = name.substring(0, 24) + '...';
	*/
	return name; 
}

function getSelectedChannelName() {
	return channelNameCache[nSelectedChannel]; 
}

function getNoVideo() {
	return noVideoCache[nCurrentChannel]; 
}

function getStreamAddress() {
	return streamAddressCache[nCurrentChannel];
}

function getSelectedStreamAddress() {
	return streamAddressCache[nSelectedChannel];
}

function getChannelOrder() {
	return channelOrderCache[nCurrentChannel];
}

function getSelectedChannelOrder() {
	return channelOrderCache[nSelectedChannel];
}

function getChannelOrderIndex(chOrdVal) {
	var i= 0;
	for(i = 0; i < channelOrderCache.length; ++i) {
		if(chOrdVal == channelOrderCache[i])
			return i;
	}

	return -1;
}

function getFirstChInCat(catId) {
    var i= 0;
	var index = 0;
	for(i = 0; i < categoryOrderCache.length; ++i) {
		if(catId == categoryOrderCache[i])
			return i;
	}
	return 0; 
}

function isChannelFound(channelId) {
	var i = 0;
	for(i = 0; i < channelIdCache.length; ++i) {
		if(channelId == channelIdCache[i]) {
			nCurrentChannel = nSelectedChannel = nOldChannel = nLastSelectedChannel = i;
			var defaultCategory = getCategoryOrder();   
			nSelectedCategory = nCurrentCategory = getCategoryIndex(defaultCategory);			 
			return true;              
		}
    }
    return false;
}

function processDefaultChannel(defaultChannel) {
	var lastChannel = top.getCookie('lastChannel');
	if(lastChannel && isChannelFound(lastChannel)) {
		return;
	} else {
		//alert("defaultChannel: " + defaultChannel);
		isChannelFound(defaultChannel);
	}
}

function getCategoryIndex(defaultCategory) {
	var i = 0;
	for(i = 0; i < uniqCategoryCache.length; ++i) {
		if(defaultCategory == uniqCategoryCache[i])
			return i;
	}
	return 0;
}

function cloneLockedCache() {
	var i;
	for(i=0; i<lockedCache.length; ++i) {
		origLockedCache[i] = lockedCache[i];
	}		
}

/////////////////////////////////////////////////////////////////////
// GUIDE GETTERS
function getGuideIndexStart(selChannelIndex) { // c10
	//return 0; // c12

	var indexChannel = 0;	

	if(selChannelIndex == null) {
		// keep the channel id in bounds, just in case
		nCurrentChannel = ( nCurrentChannel < 0 ) ? ( channelIdCache.length - 1 ) : ( nCurrentChannel > ( channelIdCache.length - 1 ) ) ? 0 : nCurrentChannel;
	 	indexChannel = nCurrentChannel;
	} else {
		indexChannel = selChannelIndex;
	}	
	
	if(!mnemonicCache[indexChannel]) // a valid mnemonic indicates auto info
		return 0;
	
	var guidePageSet = guideCache[indexChannel].split('-$#-'); 
	var i = 0;		
	for(i=0; i < guidePageSet.length; ++i) {
		var infoText = guidePageSet[i];
		var infoData = infoText.split('|');
		var nowDate = new Date();
		nowDate = new Date(nowDate.getTime());
		var startDate = new Date(infoData[1]);
		var endDate = new Date(infoData[2]);
		if(nowDate >= startDate && nowDate < endDate)
			break;
	}
	i = (i >= guidePageSet.length? 0 : i);
	return i;
}

function resetGuideIndex() {
	nCurrentProgramme = getGuideIndexStart();
}

function getGuideLength() { // index within bounds already
//	return guideCache[nCurrentChannel].split('-$#-').length;
	return guideCache[nCurrentChannel].split('-$#-').length; 
//	return guideDataCache[nCurrentChannel].guideList.length;
	 
}

function plusGuideIndex() { // navigates on the cache
	var guideIndex = nCurrentProgramme + 1;

	// keep in bounds
	guideIndex = (guideIndex > getGuideLength() - 1 ? getGuideIndexStart() : guideIndex);
	
	nCurrentProgramme = guideIndex;
}

function minusGuideIndex() { // navigates on the cache
	var guideIndex = nCurrentProgramme - 1;
	// keep in bounds
	guideIndex = (guideIndex < getGuideIndexStart() ? getGuideLength() - 1 : guideIndex);
	
	nCurrentProgramme = guideIndex;
}

function getGuide() { // always at the first position
//	var guidePageSet = guideCache[nCurrentChannel].split('-$#-'); 
//	var infoText = guidePageSet[getGuideIndexStart()]; // c9

	var guidePageSet = guideCache[nCurrentChannel].split('-$#-'); 
	var infoText = guidePageSet[getGuideIndexStart()]; // c9 
 	return infoText;
//	var infoText = guideDataCache[nCurrentChannel].guideList[getGuideIndexStart()];
//	return infoText;
}

function getGuideAt(curIndex) { // index within bounds already
	var guidePageSet = guideCache[nCurrentChannel].split('-$#-'); 
	var infoText = guidePageSet[curIndex];
	return infoText; //  4
}

function getGuideForChAt(channelIndex, curIndex) { // index within bounds already
	var guidePageSet = guideCache[channelIndex].split('-$#-'); 
	var infoText = guidePageSet[curIndex];
	return infoText;
//	var infoText = guideDataCache[channelIndex].guideList[curIndex];
//	return infoText;
}

function formatTime(dTime) {
    var dNow = new Date(dTime);
//	dNow = new Date(dNow.getTime() + curOffSetValue());
    var nHours = dNow.getHours ();
    nHours = (nHours == 0) ? 12 : nHours;
    var nMinutes = dNow.getMinutes ( );
            
    var hr = (nHours > 12) ? nHours - 12 : nHours;          
    var temp = hr;
    temp += ((nMinutes < 10) ? ":0" : ":") + nMinutes;        
    temp += (nHours > 12) ? " PM" : " AM";
                    
    //Format: '2:53 PM'
    return temp;
}

//////////////////////////////////////// GET CACHE ENDS ////////////////////////////////////////////////////////

// displaying the scrolling arrows
function showArrow(index, value) {
	var moveFwdArrs = "";
	var moveRwdArrs = "";
	if(index == 0) {
		moveFwdArrs = "moveLeft";
		moveRwdArrs = "moveRight";
	} else {
		moveFwdArrs = "moveUp";
		moveRwdArrs = "moveDown";
	}
	var upArrow = document.getElementById(moveFwdArrs).style.background;
	var downArrow = document.getElementById(moveRwdArrs).style.background;
	(value == 1 ? upArrow = "skyblue" : downArrow = "skyblue");
	document.getElementById(moveFwdArrs).style.background = upArrow;
	document.getElementById(moveRwdArrs).style.background = downArrow;
}

// hiding the fullguide arrows.
function hideArrow(index, value) {
	var moveFwdArrs = "";
	var moveRwdArrs = "";
	if(index == 0) {
		moveFwdArrs = "moveLeft";
		moveRwdArrs = "moveRight";
	} else {
		moveFwdArrs = "moveUp";
		moveRwdArrs = "moveDown";
	}
	var upArrow = document.getElementById(moveFwdArrs).style.background;
	var downArrow = document.getElementById(moveRwdArrs).style.background;
	(value == 1 ? upArrow = "none" : downArrow = "none");
	document.getElementById(moveFwdArrs).style.background = upArrow;
	document.getElementById(moveRwdArrs).style.background = downArrow;
//	var upArrow = document.getElementById("moveUp").style.background;
//	var downArrow = document.getElementById("moveDown").style.background;
//	(value == 1 ? upArrow = "none" : downArrow = "none");
//	document.getElementById("moveUp" ).style.background = upArrow;
//	document.getElementById("moveDown").style.background = downArrow;
}

// selecting home to go to home
function selHome(index) {
	document.getElementById("fgHome").style.background = "skyblue";
}
// hiding selected home button
function unSelHome(index) { // 
	document.getElementById("fgHome").style.background = "none";
}

function scrollGridGuide(index) {
	if(index == 1) {
		fullGuideKeyHandler(rcLEFT);
	} else if(index == 2) {
		fullGuideKeyHandler(rcRIGHT);
	}
}
function scrollPageUp() {
	fullGuideKeyHandler(rcUP);
}

function scrollPageDown() {
	fullGuideKeyHandler(rcDOWN);	
}

function showHome() {
	fullGuideKeyHandler(rcMENU);
}
///////////////////////////
// mini gide panel starts//
///////////////////////////
var updateLayerInfoTimer = null;
var oTriggerSwitchTimer = null;
var updateMBTimer  = null;
var bExtInfoMode = false;
var bItsAllDark = false;

function launchViewer() {
	redrawLayers();
}

function redrawLayers() {
	if(updateLayerInfoTimer) {
		top.window.clearTimeout(updateLayerInfoTimer);
		updateLayerInfoTimer = null;			
	} 	
	updateLayerInfoTimer = top.window.setTimeout('updateLayerInfo();', 10);
}

function updateLayerInfo() {	
	updateLayer2(); 
	if(bExtInfoMode) {
		if(nCurrentChannel != nSelectedChannel) {
//			showMiniPanel();
			updateLayer3();
		} 		
	} else {
		updateLayer3();		
	}
}

function updateLayer2() {
	showMiniHomePanel();
	var selChName = getSelectedChannelName();
	if(useGraphicCache[nSelectedChannel] == 1) {	
		document.getElementById("selChImg").innerHTML = '<img  class="chLogoPanel" alt="" width="60px" height="40px" src="/ImageHandler?value=' + getSelectedChannelId() + '&icon=-1&imageType=0" border="0" align="middle">';
	} else {
		document.getElementById("selChImg").innerHTML = '<img class="chLogoPanel" alt="" width="60px" height="40px" src="/view/online/images/logo_none.png" border="0" align="middle">';
	}
	document.getElementById("channelInfoPanel").innerHTML = channelOrderCache[nSelectedChannel] + "&nbsp;&nbsp;&nbsp;" + channelNameCache[nSelectedChannel];
	updateMiniBarTime();
}


function updateMiniBarTime() {
	if(clearIntervals() == 1) {
		return;
	}
	var date = new Date();
//	date = new Date(date.getTime() + (date.getTimezoneOffset()*60*1000));
//	date = new Date(date.getTime());
	var hours = date.getHours();
	var hr = (hours > 12) ? hours - 12 : hours;	
	var mins = date.getMinutes();
	if(updateMBTimer != null ) {
		window.clearTimeout(updateMBTimer);
		updateMBTimer = null;
	}
	document.getElementById("findChOrder").innerHTML = ((hr < 10 ? ('0'+hr) : hr) + ":" + (mins < 10 ? ('0' + mins) : mins ) + (hours < 12 ? " AM" : " PM"));
	updateMBTimer = window.setTimeout("updateMiniBarTime();", 30*1000); 
}

function updateLayer3() {
	var nowIndex = getGuideIndexStart();
	var guidePageSet = guideCache[nCurrentChannel].split('-$#-'); 
	if(trim(guidePageSet[nowIndex]) == "") {
		document.getElementById("miniCategoryPanel").innerHTML = getCategory();
		document.getElementById("curChOrderPanel").innerHTML = getChannelOrder();
		var curChName = getChannelName();
		document.getElementById("curChNamePanel").innerHTML = curChName;
		document.getElementById("favoriteImg").innerHTML = '';//(favoriteCache[nCurrentChannel] == 1 ? '<img alt="" src="/view/online/images/hotIcons/unSel1.png">' : '');
		document.getElementById("nowTitlePanel").innerHTML = ''; //(epiTitleNow ? (epiTitleNow.length > 10 ? epiTitleNow.substring(0, 10)+'..' : epiTitleNow) : '') + (showTypeNow ? '&nbsp;(' + showTypeNow + ')' :  '');
		document.getElementById("nowFromToPanel").innerHTML = records.key452; //(mnemonicCache[nCurrentChannel] && startTimeNow && endTimeNow ? records.key452/*formatTime(startTimeNow) + ' - ' + formatTime(endTimeNow)*/ : records.key420);
		document.getElementById("nextTitlePanel").innerHTML = ''; //(epiTitleNext ? (epiTitleNext.length > 10 ? epiTitleNext.substring(0, 10)+'..' : epiTitleNext) : '') + (showTypeNext ? '&nbsp;(' + showTypeNext + ')' :  '');
		document.getElementById("nextFromToPanel").innerHTML = records.key453; //(mnemonicCache[nCurrentChannel] && startTimeNext && endTimeNext ? records.key453 /*formatTime(startTimeNext) + ' - ' + formatTime(endTimeNext)*/ : records.key421);
		return;
	} 
	var guideNowPage = guidePageSet[nowIndex];
	var nowData = guideNowPage.split('|');
	
	// NEXT data
	var nextIndex = nowIndex + 1;
	nextIndex = (nextIndex > guidePageSet.length - 1 ? nowIndex : nextIndex);
	var nextGuidePage = guidePageSet[nextIndex];	
	var nextData = nextGuidePage.split('|');
	/*
	var epiTitleNow = "";
	var showTypeNow = "";
	var startTimeNow = "";
	var epiTitleNext = "";
	var endTimeNow = "";
	var showTypeNext = "";
	*/
	var epiTitleNow = nowData[6];
//	alert("epiTitleNow = " + epiTitleNow);
	if(epiTitleNow.length > 30)
		epiTitleNow = epiTitleNow.substring(0, 27) + '...';
	var showTypeNow = nowData[7];
		if(showTypeNow.length > 13)
		showTypeNow = showTypeNow.substring(0, 11) + '...';
	var startTimeNow = nowData[1];
	var endTimeNow = nowData[2];
	
	var epiTitleNext = nextData[6];
	if(epiTitleNext.length > 30)
		epiTitleNext = epiTitleNext.substring(0, 27) + '...';	
	var showTypeNext = nextData[7];
		if(showTypeNext.length > 13)
		showTypeNext = showTypeNext.substring(0, 11) + '...';
	var startTimeNext = nextData[1];
	var endTimeNext = nextData[2];			
	
	document.getElementById("miniCategoryPanel").innerHTML = getCategory();
	document.getElementById("curChOrderPanel").innerHTML = getChannelOrder();
	
	var curChName = getChannelName();
	document.getElementById("curChNamePanel").innerHTML = curChName;
	document.getElementById("favoriteImg").innerHTML = (favoriteCache[nCurrentChannel] == 1 ? '<img alt="" src="/view/online/images/hotIcons/unSel1.png">' : '');
	// document.getElementById("lockImg").innerHTML = (lockedCache[nCurrentChannel] == 1 ? '<img alt="" src="/view/online/images/hotIcons/unSel2.png">' : '<img alt="" src="/view/online/images/unLock.png">');
	document.getElementById("nowTitlePanel").innerHTML = (epiTitleNow ? (epiTitleNow.length > 10 ? epiTitleNow.substring(0, 10)+'..' : epiTitleNow) : '') + (showTypeNow ? '&nbsp;(' + showTypeNow + ')' :  '');
	document.getElementById("nowFromToPanel").innerHTML = (mnemonicCache[nCurrentChannel] && startTimeNow && endTimeNow ? records.key452/*formatTime(startTimeNow) + ' - ' + formatTime(endTimeNow)*/ : records.key420);
	document.getElementById("nextTitlePanel").innerHTML = (epiTitleNext ? (epiTitleNext.length > 10 ? epiTitleNext.substring(0, 10)+'..' : epiTitleNext) : '') + (showTypeNext ? '&nbsp;(' + showTypeNext + ')' :  '');
	document.getElementById("nextFromToPanel").innerHTML = (mnemonicCache[nCurrentChannel] && startTimeNext && endTimeNext ? records.key453 /*formatTime(startTimeNext) + ' - ' + formatTime(endTimeNext)*/ : records.key421); 			
}
// Faith sees the invisible...
// Believe the unbelievable...
// And achievs the impossible...
function switchChannel() {
	startEpgStream();
//	timeLineUpdate();
	
	window.clearTimeout(oSwitchTimer);
	oSwitchTimer = null;

	window.clearTimeout(oTriggerSwitchTimer);
	oTriggerSwitchTimer = null;

	bKeyChannel = false;
	// write cookie now
//	top.setCookie('lastChannel', getSelectedChannelId());
}
function selOnNowHome(index) {
	var background = (index == 1 ? "#fcf3b8" : "none");
	document.getElementById("onNowPanelHome").style.background = background;
}

function scrollMgPanel(index) {
	switch (index) {
		case 1:
			fullGuideKeyHandler(rcLEFT);
		break;
		case 2:
			fullGuideKeyHandler(rcRIGHT);
		break;
		case 3:
			fullGuideKeyHandler(rcUP);
		break;
		case 4:
			fullGuideKeyHandler(rcDOWN);
		break;
	}
}

function selMgArr(index) {
	document.getElementById("mgkey" + index).style.background = "#fcf3b8";
}

function unSelMgArr(index) {
	document.getElementById("mgkey" + index).style.background = "none";
}
function fadeAll() {
	try {
		document.getElementById("miniGuidePanel").style.visibility = "hidden";
	} catch (exception) {
		
	}
}
function gotoHomePanel() {
	fullGuideKeyHandler(rcMENU);
}
function startEpgStream() {
	document.getElementById("epgPlayerAtfg").innerHTML = paintPlayer();
//	alert(document.getElementById("content").innerHTML);
//	alert(channelNameCache[nCurrentChannel] + " : " + protocol[nCurrentChannel]);
//	document.getElementById("epgPlayer").innerHTML = paintPlayer();
//	"<object width='640' height='377' id='videoPlayer' name='videoPlayer' type='application/x-shockwave-flash' classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' ><param name='movie' value='/view/online/embed/swfs/videoPlayer.swf' /> <param name='quality' value='high' /> <param name='bgcolor' value='#000000' /> <param name='allowfullscreen' value='true' /> <param name='flashvars' value= '&videoWidth=0&videoHeight=0&dsControl=manual&dsSensitivity=100&serverURL=rtmp://72.17.154.130:1935/live/"+ channelNameCache[nCurrentChannel] +"&DS_Status=true&streamType=live&autoStart=true'/>"
//														+ "<embed src='/view/online/embed/swfs/videoPlayer.swf' width='640' height='377' id='videoPlayer' quality='high' bgcolor='#000000' name='videoPlayer' allowfullscreen='true' pluginspage='http://www.adobe.com/go/getflashplayer' flashvars='&videoWidth=0&videoHeight=0&dsControl=manual&dsSensitivity=100&serverURL=rtmp://72.17.154.130:1935/live/"+ channelNameCache[nCurrentChannel] +"&DS_Status=true&streamType=live&autoStart=true' type='application/x-shockwave-flash'>" 
// 														+ "</embed>"
// 														+ "</object>";
}

/*
 * It loads the player when any channel is selected.
 */
function paintPlayer() {
//	alert(trim(securityCache[nCurrentChannel]));
	if(clearIntervals() == 1) {
		return;
	}
//	alert(channelNameCache[nCurrentChannel]);
	var html = "";
	var security = trim(securityCache[nCurrentChannel]);
	var hasRequestedVersion = DetectFlashVer(10, 0, 0);
	if(!hasRequestedVersion) {
		menuActive = false;
		miniguideActive = false;
		document.getElementById("content").innerHTML = '<a href="http://www.adobe.com/go/getflashplayer/" style="color:black;height:640px;weight:380px;"><img src="./view/online/embed/msg.jpg" width="640" height="380" style="margin-top:20px;" /></a>';
	} else {
		var hostAddr = fmsServerIp + ":" + fmsServerPort;
//		alert("hostAddr = " + hostAddr);
		if(protocol[nCurrentChannel] == "http") { // for internet radio.
			html = '<embed width="624" height="499" flashvars="radioStation=http://'+ streamAddressCache[nCurrentChannel] +'&amp;stationName=Bollywood Music Radio&amp;isPopout=true" allowscriptaccess="always" wmode="transparent" bgcolor="#000000" wmode="opaque" name="shoutcast" id="shoutcast" src="http://www.in.com/music/player/shoutcast_new3.swf" type="application/x-shockwave-flash" style="opacity:.0;filter:alpha(opacity=00);" />';
		} else if (protocol[nCurrentChannel] == "rtmp") {
//			alert(trim(channelNameCache[nCurrentChannel])); 
			var streamAddr = trim(channelNameCache[nCurrentChannel]); 
			
			html = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="624" height="475" id="mediacasterstreammanagerLive" align="middle">'
						+ '<param name="allowScriptAccess" value="sameDomain" />'
						+ '<param name="allowFullScreen" value="true" />'
						+ '<param name="movie" value="/view/online/embed/player/liveStreamPlayer.swf" />'
						+ '<param name="quality" value="high" />'
						+ '<param name="bgcolor" value="#000000" />'
						+ '<param name="wmode" value="opaque">'
						+ '<param name="FlashVars" value="streamName='+ trim(channelNameCache[nCurrentChannel]) +'&ip='+ hostAddr +'&security='+ security +'" />'
						+ '<embed src="/view/online/embed/player/liveStreamPlayer.swf" FlashVars="streamName='+ trim(channelNameCache[nCurrentChannel]) +'&ip='+ hostAddr +'&security='+ security +'" wmode="opaque" quality="high" bgcolor="#000000" width="624" height="475" name="mediacasterstreammanagerLive" align="middle" allowScriptAccess="sameDomain" allowFullScreen="true" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />'
					+ '</object>';
			
		} else {
				/*
				 * Commented bacause igmp is no longer supported.
				 */
				/* 
				var streamAddr = channelNameCache[nCurrentChannel] + ".stream"; 
				html = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="624" height="475" id="mediacasterstreammanagerLive" align="middle">'
							+ '<param name="allowScriptAccess" value="sameDomain" />'
							+ '<param name="allowFullScreen" value="true" />'
							+ '<param name="movie" value="/view/online/embed/player/liveStreamPlayer.swf" />'
							+ '<param name="quality" value="high" />'
							+ '<param name="bgcolor" value="#000000" />'
							+ '<param name="wmode" value="opaque">'
							+ '<param name="FlashVars" value="streamName='+ streamAddr +'" />'
							+ '<embed src="/view/online/embed/player/liveStreamPlayer.swf" FlashVars="streamName='+ streamAddr +'" wmode="opaque" quality="high" bgcolor="#000000" width="624" height="475" name="mediacasterstreammanagerLive" align="middle" allowScriptAccess="sameDomain" allowFullScreen="true" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />'
						+ '</object>';
				*/
				
				var streamAddr = trim(channelNameCache[nCurrentChannel]); 
				html = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="624" height="475" id="mediacasterstreammanagerLive" align="middle">'
							+ '<param name="allowScriptAccess" value="sameDomain" />'
							+ '<param name="allowFullScreen" value="true" />'
							+ '<param name="movie" value="/view/online/embed/player/liveStreamPlayer.swf" />'
							+ '<param name="quality" value="high" />'
							+ '<param name="bgcolor" value="#000000" />'
							+ '<param name="wmode" value="opaque">'
							+ '<param name="FlashVars" value="streamName='+ trim(channelNameCache[nCurrentChannel]) +'&ip='+ hostAddr +'&security='+ security +'" />'
							+ '<embed src="/view/online/embed/player/liveStreamPlayer.swf" FlashVars="streamName='+ trim(channelNameCache[nCurrentChannel]) +'&ip='+ hostAddr +'&security='+ security +'" wmode="opaque" quality="high" bgcolor="#000000" width="624" height="475" name="mediacasterstreammanagerLive" align="middle" allowScriptAccess="sameDomain" allowFullScreen="true" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />'
						+ '</object>';
			}
	}
 	return html;
}

function showFullguidePanel() {
	getCurrentFullGuideTime();
	hideminiHomePanel();
	document.getElementById("miniGuidePanel").style.visibility = "hidden";
	document.getElementById("fullGuidePanel").style.visibility = "visible";
	document.getElementById("fullGuideHeader").style.visibility = "visible";
}
function showMiniGuidePanel() {
	if(fullGuideTimer) {
		window.clearTimeout(fullGuideTimer);
		fullGuideTimer = null;
	}
	showMiniHomePanel();
	document.getElementById("fullGuideHeader").style.visibility = "hidden";
//	document.getElementById("controlPanel0").style.visibility = "hidden";
	document.getElementById("fullGuidePanel").style.visibility = "hidden";
	document.getElementById("miniGuidePanel").style.visibility = "visible";
}
function hideminiHomePanel() {
	if(document.getElementById("onNowPanelHome"))	
		document.getElementById("onNowPanelHome").style.visibility = "hidden";
	if(document.getElementById("onMiniGuidePanel"))	
		document.getElementById("onMiniGuidePanel").style.visibility = "hidden";
}
function showMiniHomePanel() {
	hideMiniguideBtn();
	if(document.getElementById("onNowPanelHome"))	
		document.getElementById("onNowPanelHome").style.visibility = "visible";
}

function showMiniGuideBtn() {
	if(document.getElementById("onMiniGuidePanel"))	
		document.getElementById("onMiniGuidePanel").style.visibility = "visible";
}
function hideMiniguideBtn() {
	if(document.getElementById("onMiniGuidePanel"))	
		document.getElementById("onMiniGuidePanel").style.visibility = "hidden";
}

function gotoMiniguidePanel() {
	if(document.getElementById("onMiniGuidePanel"))	
		document.getElementById("onMiniGuidePanel").style.visibility = "hidden";
	showMiniGuidePanel();
	doFade();
}

function selMiniGuideHome(index) {
	var colorHTML = (index == 1 ? "#fcf3b8" : "none");
	document.getElementById("onMiniGuidePanel").style.background = colorHTML;
}
var fullGuideTimer = null;

function getCurrentFullGuideTime() {
	if(clearIntervals() == 1) {
		return;
	}
//	alert('getcurrenttime called');
	var nowTime = new Date();
	
	var nDay = nowTime.getDay();
	if (nDay==0)
  		nDay="Sun";
	else if (nDay==1)
  		nDay="Mon";
	else if (nDay==2)
  		nDay="Tue";
	else if (nDay==3)
  		nDay="Wed";
	else if (nDay==4)
  		nDay="Thu";
	else if (nDay==5)
  		nDay="Fri";
	else if (nDay==6)
  		nDay="Sat";
  	
	var nMonth = nowTime.getMonth();
	var nDate = nowTime.getDate();	
	var nHours = nowTime.getHours();
	var nMinutes = nowTime.getMinutes();
	nDate = nDate < 10 ? '0'+nDate : nDate;	

	var hr = (nHours > 12) ? nHours - 12 : nHours;         
	hr = hr == 0 ? '12' : hr;
	
	var p = new String(":");
	// top right
	if(fullGuideTimer) {
		window.clearTimeout(fullGuideTimer);
		fullGuideTimer = null;
	}	
	if(document.getElementById("clockieFG2")) {
		document.getElementById("clockieFG2").innerHTML = nDay + ", " + getCurMonthText(nMonth) + " " + nDate;
		document.getElementById("clockieFG5").innerHTML = hr + ((nMinutes < 10) ? ":0" : ":") + nMinutes + ((nHours >= 12) ? " PM" : " AM");
	}		
	fullGuideTimer = window.setTimeout("getCurrentFullGuideTime();", 15*1000);
}

function getCurMonthText(index) {
	return index == 0 ? "Jan" : (index == 1 ? "Feb" : (index == 2 ? "Mar" : (index == 3 ? "Apr" : (index == 4 ? "May" : (index == 5 ? "June" : (index == 6 ? "Jul" : (index == 7 ? "Aug" : (index == 8 ? "Sep" : (index == 9 ? "Oct" : (index == 10 ? "Nov" : (index == 11 ? "Dec" : "")))))))))));		
}

function clearIntervals() {
	var returnValue = 0;
	if(document.getElementById("fullGuidePanel") == null) {
		menuActive = false;
		miniguideActive = false;
		
		if(updateMBTimer != null ) {
			window.clearTimeout(fullGuideTimer); // full guide timer to print date and time 
			fullGuideTimer = null;
		}
		if(oSwitchTimer != null ) {
			window.clearTimeout(oSwitchTimer); //  channel switch timer
			oSwitchTimer = null;
		}
		if(oTriggerSwitchTimer != null ) {
			window.clearTimeout(oTriggerSwitchTimer); // trigging channel timer 
			oTriggerSwitchTimer = null;
		}
		if(updateMBTimer != null ) {
			window.clearTimeout(updateMBTimer);
			updateMBTimer = null;
		}
		if(updateLayerInfoTimer) {
			top.window.clearTimeout(updateLayerInfoTimer);
			updateLayerInfoTimer = null;			
		} 	
		returnValue = 1;
	}
	return returnValue;
}
function selMiniGuideOk(index) {
	document.getElementById("miniGuideOkBtn").style.background = (index == 1 ? "#fcf3b8" : "none");
}

function gotoTuneChannel() {
	fullGuideKeyHandler(rcOK);
}
function showChannelKeyPanel() {
	document.getElementById("miniGuidePanel").style.visibility = "visible"; // should be visible when any button pressed
	if(document.getElementById("onNowPanelHome") != null)
		document.getElementById("onNowPanelHome").style.visibility = "visible";
}

function clearSwitchChannel() {
	userSelChOrder = "";
	document.getElementById("findChOrder").innerHTML = "&nbsp;";
	updateMiniBarTime();
	if(oTriggerSwitchTimer != null) {
		top.window.clearTimeout(oTriggerSwitchTimer);
		oTriggerSwitchTimer = null;
	}
}

function triggerSwitchChannel() {

	// My Changes
	userSelChOrder = userSelChOrder.substr(0,4);
	// My Changes Up To Here	
	
	var index = getChannelOrderIndex(userSelChOrder);

	clearSwitchChannel();
	if(index == -1 || index == nSelectedChannel) 		
		return;
	
	resetGuideIndex();
    nCurrentChannel = index;
    nLastSelectedChannel = nSelectedChannel; // save currently selected channel before it is updated

	// Keep in bounds
    nCurrentChannel = ( nCurrentChannel < 0 ) ? ( channelIdCache.length - 1 ) : ( nCurrentChannel > ( channelIdCache.length - 1 ) ) ? 0 : nCurrentChannel;
    nSelectedChannel = nCurrentChannel;

	// Switch stream 
	if(oSwitchTimer) {
		window.clearTimeout ( oSwitchTimer );
		oSwitchTimer = null;
	}
	oSwitchTimer = window.setTimeout ( 'switchChannel()', 1);
    // Redraw panel               
	bExtInfoMode = false;			
	channelChanged = true;
	launchViewer(); 
}

function resetItemVars() {
	menuActive = false;
	vodActive = false;
	surveyActive = false;
	horoscopeActive = false;
	calcActive = false;
	newsActive = false;
	blinkStatus = true;
	blinkCursor = null;
	GeneralNewsActive = false;
	loginActive = false;
	videoActive = false;
	gameActive = false;
	calendarActive = false;
	miniguideActive = false;
	blogActive = false;
	mealActive = false;
	painAlertActive = false;
	spiritualActive = false;
	assignedActive = false;
	miniguideActive = false;
}

// END
/////////////////////////

