var videoIdCache = new Array();
var videoTitleCache = new Array();
var curVideoIndex = new Array();
var videoRowIndex = 0;
var videoColIndex = 0;
var videoCursorIndex = 0;
var startVideoIndex = 0;
var videoTitleTotal = 0;
var videoClick = 0;
var videoPlayStatus = false;
function getVideoInfo() {
	var widgetId = wrecords.wRecords[currImage].id;
	var useGraphic = wrecords.wRecords[currImage].useGraphic;
	useGraphic = useGraphic.substring(1, useGraphic.length);	
	var widgetTitle = wrecords.wRecords[currImage].title;
	var params = "method=getVideoInfo&widgetId=" + widgetId + "&serial=" + serial;
	var callbacks = { 
	    success: function (o) {
	        try {
				var response = o.responseText;
				var data = response.split("-?#-");
				videoIdCache = data[0].split("-@#-");
				videoTitleCache = data[1].split("-@#-");
				var newsHTML = "<table style='height:100%;text-align:left;font-weight:bold;border:1px solid gray;' width='100%' align='left'><tr><td style='border-bottom:1px solid gray;' width='100%'><table width='100%' ><tr><td width='80%' id='newsContent'><table><tr><td>"+ '<img  alt="" height="36px" width="36px" src=' + (useGraphic == 1 ? "/ImageHandlerMobi?value=" + widgetId + "&icon=2&imageType=2" : "/view/tv/images/widgets/wImages/widgetNA.png") + ' style="padding:1px;" border="0" align="middle"></td><td>' +widgetTitle+"</td></tr></table></td><td width='30%'><table width='50%' align='right' style='text-align:right;'><tr><td><img style='cursor:pointer;' onclick='videoKeyHandler(rcUP);' src='./view/mobi/images/uarrow.png'/></td><td><img onclick='videoKeyHandler(rcMENU);' style='cursor: pointer;' src='/view/mobi/images/home.png' alt=''/></td><td><img style='cursor:pointer;' onclick='videoKeyHandler(rcDOWN);' src='./view/mobi/images/darrow.png'/></td></tr></table></td></tr></table></td></tr><tr><td id='newsHTML'><table style='height:100%;' >";
				newsHTML += "<tr>";
				for(var k = 0; k < 3; k++) {
					curVideoIndex[k] = new Array(3); 
					for(var m = 0; m < 3; m++) {
						curVideoIndex[k][m] = 0;
					}
				}
				for(var i = 0; i < 9; i++) {
					if(i%3 == 0 && i != 0) {
						newsHTML += "</tr><tr>";
					}
					var rowIndex = parseInt(i/3);
					var colIndex = parseInt(i%3);
					if(i < videoIdCache.length) {
						curVideoIndex[rowIndex][colIndex] = 1;
					} else {
						curVideoIndex[rowIndex][colIndex] = 0;
					}
					newsHTML += "<td width='100px' height='55px' onmouseover='selVideoPanel("+ i +", "+ i +");' style='border:"+(i < videoIdCache.length ? '1px solid transparent;background:none;' : '1px solid Transparent;background:none;')+"' id='videoTitleId"+ i +"' onclick='openVideoContent("+ i +");'>"+ (i < videoIdCache.length ? '<img width="100px" height="55px" src=\'/ImageHandlerMobi?value=' + videoIdCache[i] + '&icon=1&imageType=8\' >' : '&nbsp;') +"</td>";
				}
				newsHTML += "</tr>";
				newsHTML += "</table></tr><tr><td height='10%' style='border-top:1px solid gray;' id='videoPanelDesc'>&nbsp;</td></tr></td></table>";
				fadeAll();
				document.getElementById("openChartId").innerHTML = '<span height="100%" width="100%" id="videoContentPanel" style="visibility:visible;">'+newsHTML + '</span>';
				videoTitleTotal = videoIdCache.length - 1;
				selVideoTitle (0, 0);
	        } catch(x) {
//	        	alert(x);
	        }
	    },
	    failure: function(o) {
//	    	failureInfo();
	    } 
	};			 
	YAHOO.util.Connect.asyncRequest('POST', serverUrl, callbacks, params);
	return false;
}

function selVideoPanel(curIndex, index) {
	try {
		if(index < videoIdCache.length) {
			updateVideoDesc(index);
			unselVideoPanel();
			videoColIndex = parseInt(curIndex%3); 
			videoCursorIndex = parseInt(curIndex/3);
			videoRowIndex =  parseInt(index/3); 
			//videoCursorIndex + videoClick;
			document.getElementById("videoTitleId" + curIndex).style.border = "1px solid white";	 
		}
	} catch(x) {
		
	}
}

function unselVideoPanel() {
	try {
		for(var i = 0; i < 9; i++) {
			document.getElementById("videoTitleId" + i).style.border = "1px solid transparent";
		}
	} catch(x) {
		
	}
}

function updateVideoDesc(index) {
	try {
		document.getElementById("videoPanelDesc").innerHTML = videoTitleCache[index];
	} catch(x) {
		
	}
}
function launchVideoPanel() {
	videoActive = true;
	getVideoInfo();
}
function unSelVideoTitle(rowIndex, columnIndex) {
	try {
		var index = rowIndex*3 + columnIndex;
		document.getElementById("videoTitleId" + index).style.border = "1px solid transparent";
	} catch(x) {
		
	}
}

function selVideoTitle (rowIndex, columnIndex) {
	try {
		var index = rowIndex*3 + columnIndex;
		if(curVideoIndex[rowIndex][columnIndex] == 1) {
			updateVideoDesc((videoRowIndex*3+videoColIndex));
			document.getElementById("videoTitleId" + index).style.border = "1px solid white";
		} else {
			--videoCursorIndex;
			--videoRowIndex;
			selVideoTitle (videoCursorIndex, videoColIndex);
		}
	} catch(x) {
		return;
	}
}
function videoKeyHandler(keyCodeNum) {
	switch(keyCodeNum) {
		case rcUP:
			if(videoRowIndex == 0)
				return;
			unSelVideoTitle(videoCursorIndex, videoColIndex);
			videoRowIndex--;
			if(videoCursorIndex != 0) { // if the cursor is moving up, no scroll is needed
				videoCursorIndex--;
				selVideoTitle(videoCursorIndex, videoColIndex);
			} else { // scroll	
				videoCursorIndex = 0;
				var start = 0;
				start = (startVideoIndex < 3 ? 0 : startVideoIndex -3);
				startVideoIndex = start;
				
				var end = ((videoTitleTotal - start) > 8 ? (start + 8) : videoTitleTotal);	
				scrollVideoTitles(start, end);
				selVideoTitle(videoCursorIndex, videoColIndex);
			}	
			videoClick = videoRowIndex - videoCursorIndex;
			break;
		case rcDOWN:
			var rowIndex = videoCursorIndex + 1;
			var columnIndex = videoColIndex;
			if(rowIndex < 3) {
				if(curVideoIndex[rowIndex][columnIndex] == 0) {
					return;
				}
			}
//			if(curVideoIndex[rowIndex][columnIndex] == 0) {
//				return;
//			}
			var videoCount = (videoTitleTotal/3);
 			if(videoRowIndex == parseInt(videoCount))
				return;
			unSelVideoTitle(videoCursorIndex, videoColIndex);
			videoRowIndex++;
					
			if(videoCursorIndex < 2) { 
				videoCursorIndex++;
				selVideoTitle(videoCursorIndex, videoColIndex);
			} else { // scroll	
				var start = 0;
				if(videoRowIndex < 3) {
					start = videoRowIndex;  
					startVideoIndex = start;
				} else {
					start = startVideoIndex + 3;
					startVideoIndex = start;
				}
				var end = ((videoTitleTotal - start) > 8 ? (start + 8) : videoTitleTotal);
				scrollVideoTitles(start, end);
				selVideoTitle(videoCursorIndex, videoColIndex);
			}	
			videoClick = videoRowIndex - videoCursorIndex;
			break;
		case rcRIGHT:
			var index = videoRowIndex*3 + videoColIndex;
			if(videoColIndex == 2 || (index == videoTitleTotal)) {
				return;
			}
			unSelVideoTitle(videoCursorIndex, videoColIndex);
			++videoColIndex;
			selVideoTitle(videoCursorIndex, videoColIndex);
			break;
		case rcLEFT:
			if(videoColIndex == 0) {
				return;
			}
			unSelVideoTitle(videoCursorIndex, videoColIndex);
			--videoColIndex;
			selVideoTitle(videoCursorIndex, videoColIndex);
			break;
		case rcOK:
			break;
		case rcMENU:
			if(videoPlayStatus) {
				videoPlayStatus = false;
				document.getElementById("epgPlayer").innerHTML = "";
				document.getElementById("videoContentPanel").style.visibility = "visible";
			} else {
				document.getElementById("openChartId").innerHTML = "";
				videoActive = false;
				showGuidePanel();
				resetVideoCache();
			}
			break;
	}
}

function resetVideoCache() {
	videoCache = new Array();
	videoRowIndex = 0;
	videoColIndex = 0;
	videoCursorIndex = 0;
	startVideoIndex = 0;
	videoTitleTotal = 0;
 	videoClick = 0;
 	videoActive = false;
 	videoPlayStatus = false;
}


function scrollVideoTitles(start, end) {
	var widgetId = wrecords.wRecords[currImage].id;
	var useGraphic = wrecords.wRecords[currImage].useGraphic;
	useGraphic = parseInt(useGraphic.substring(1, useGraphic.length));	
	var widgetTitle = wrecords.wRecords[currImage].title;
	var newsHTML = "<table style='height:100%;text-align:left;font-weight:bold;border:1px solid gray;' width='100%' align='left'><tr><td style='border-bottom:1px solid gray;' width='100%'><table width='100%' ><tr><td width='80%' id='newsContent'><table><tr><td>"+ '<img  alt="" height="36px" width="36px" src=' + (useGraphic == 1 ? "/ImageHandlerMobi?value=" + widgetId + "&icon=2&imageType=2" : "/view/tv/images/widgets/wImages/widgetNA.png") + ' style="padding:1px;" border="0" align="middle"></td><td>' +widgetTitle+"</td></tr></table></td><td width='30%'><table width='50%' align='right' style='text-align:right;'><tr><td><img style='cursor:pointer;' onclick='videoKeyHandler(rcUP);' src='./view/mobi/images/uarrow.png'/></td><td><img onclick='videoKeyHandler(rcMENU);' style='cursor: pointer;' src='/view/mobi/images/home.png' alt=''/></td><td><img style='cursor:pointer;' onclick='videoKeyHandler(rcDOWN);' src='./view/mobi/images/darrow.png'/></td></tr></table></td></tr></table></td></tr><tr><td id='newsHTML'><table style='height:100%;' >";
	newsHTML += "<tr>";
	for(var k = 0; k < 3; k++) {
		curVideoIndex[k] = new Array(3); 
		for(var m = 0; m < 3; m++) {
			curVideoIndex[k][m] = 0;
		}
	}
	for(var i = 0; i < 9; i++) {
		if(i%3 == 0 && i != 0) {
			newsHTML += "</tr><tr>";
		}
		var rowIndex = parseInt(i/3);
		var colIndex = parseInt(i%3);
		var index = start + i;
		
		if(index < videoIdCache.length) {
			curVideoIndex[rowIndex][colIndex] = 1;
		} else {
			curVideoIndex[rowIndex][colIndex] = 0;
		}
		newsHTML += "<td width='100px' height='55px' onmouseover='selVideoPanel("+ i +", "+ index +");' style='border:"+(index < videoIdCache.length ? '1px solid transparent;background:none;' : '1px solid Transparent;background:none;')+"' id='videoTitleId"+ i +"' onclick='openVideoContent("+ index +");'>"+ (index < videoIdCache.length ? '<img width="100px" height="55px" src=\'/ImageHandlerMobi?value=' + videoIdCache[index] + '&icon=1&imageType=8\' >' : '&nbsp;') +"</td>";
	}
	newsHTML += "</tr>"; 
	newsHTML += "</table></tr><tr><td height='10%' style='border-top:1px solid gray;' id='videoPanelDesc'>&nbsp;</td></tr></td></table>";
	fadeAll();
	document.getElementById("openChartId").innerHTML = '<span height="100%" width="100%" id="videoContentPanel" style="visibility:visible;">'+newsHTML + '</span>';
}

function openVideoContent (index) {
	var curPlayStream =  videoTitleCache[index];
	if(!videoTitleCache[index]) {
		return;
	}
	videoPlayStatus = true;
	var file = curPlayStream.substring(0, curPlayStream.indexOf("."));
	var exten = curPlayStream.substring(curPlayStream.indexOf(".")+1, curPlayStream.length).toUpperCase();
	document.getElementById("epgPlayer").innerHTML = "";
	document.getElementById("videoContentPanel").style.visibility = "hidden";
	document.getElementById("epgPlayer").innerHTML ='<table height="240px width="320px" bgcolor="#000000" style="position:relative;margin-top:0px;"><tr><td height="15px width="320px" align="left" style="position:relative;margin-top:0px;" >'
															+'<img height="15px" width="15px" id="miniGuideImage" align"right" onclick="videoKeyHandler(rcMENU);"  style="cursor:pointer;" alt="" src="/view/mobi/images/home.png" valign="top" align="left">'
															+'</td></tr><tr><td height="225px width="320px" style="position:relative;margin-top:0px;">' 
															+'<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="320px" height="225px" id="simplevideostreaming" align="top" >'
																+'<param name="allowScriptAccess" value="sameDomain" />'
																+'<param name="allowFullScreen" value="true" />'
																+'<param value="exactfit" name="scale"/>'
																+'<param value="opaque" name="wmode"/>'
																+'<param name="FlashVars" value="mymovie='+curPlayStream+'">'
																+'<param name="movie" value="./view/mobi/swf/mobileVodPlayer.swf" />' 
																+'<param name="quality" value="high" />' 
																+'<param name="bgcolor" value="#000000" />' 
																+'<embed src="./view/mobi/swf/mobileVodPlayer.swf" FlashVars="mymovie='+curPlayStream+'" quality="high" bgcolor="#000000" width="320px" height="225px" name="simplevideostreaming" align="top" wmode="opaque" allowScriptAccess="sameDomain" allowFullScreen="true" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />'
															+'</object></td></tr></table>'

		if(document.getElementById("epgPlayer") != null) {
			document.getElementById("epgPlayer").className = "epgPlayer";
		}
		fadeAll();		
}

































