function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

var CurrentPage;

function getVideoShowData(Menutab,View,TimeSpan,PageSize,PageNo) {
//	alert(Menutab+View+TimeSpan+PageSize+PageNo);
	if (Menutab == 'V')
		{
		var myAjax = new Ajax.Request(
				'/_site/channel/get_video_show_data.asp?Menutab='+Menutab+'&View='+View+'&TimeSpan='+TimeSpan+'&PageSize='+PageSize+'&PageNo='+PageNo, 
				{
					method: 'get', 
					parameters: '', 
					asynchronous: false,  
					onComplete: function(response) {
						currentPage = eval('(' + response.responseText + ')');
						drawTableVideos(currentPage, $('view'), View, TimeSpan, PageSize);
									}
				});
		}
	else
		{
		var myAjax = new Ajax.Request(
				'/_site/channel/get_video_show_data.asp?Menutab='+Menutab+'&View='+View+'&TimeSpan='+TimeSpan+'&PageSize='+PageSize+'&PageNo='+PageNo, 
				{
					method: 'get', 
					parameters: '', 
					asynchronous: false,  
					onComplete: function(response) {
						currentPage = eval('(' + response.responseText + ')');
						drawTableShows(currentPage, $('view'), View);
									}
				});
		}
}


/*---------------------------------------------------------------*/

function drawTableShows(page, contain, View) {
contain.innerHTML = '';

menu  = '<a href="#" id="link1" class="Menu_links" onClick="ChangeTimeSpan(\'S\',' + '\'' + View + '\'' + ',1);return false;">Most Recent</a>&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" id="link2" class="Menu_links" onClick="ChangeTimeSpan(\'S\',' + '\'' + View + '\'' + ',2);return false;">Viewers</a>&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" id="link3" class="Menu_links" onClick="ChangeTimeSpan(\'S\',' + '\'' + View + '\'' +',3);return false;">Popularity</a>&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" id="link4" class="Menu_links" onClick="ChangeTimeSpan(\'S\',' + '\'' + View + '\'' +',4);return false;">Rating</a>&nbsp;&nbsp;&nbsp;&nbsp;|';
document.getElementById('VideosMenu').innerHTML = menu;
document.getElementById('menutab').innerHTML = 'Shows';

strV = 'V';
table =  '';
thumbPath='';
table += '	<table width="100%" height="470" border="0" cellspacing="23" cellpadding="0" class="maintext">';

var i =0;
var rowCount=0;
strLoop = 'Y';
while (strLoop == 'Y') {
	
	table += '<tr valign="top">';
	
	rowCount=rowCount+1;

	for(j = 0; j < 3; j++) {
		
		if (i < page['shows'].length)
			{
				
			if (page['shows'][i].Show_Thumbnail == "no_video_thumbnail.jpg")
				{
				thumbPath='/_site/graphics/';	
				}
			else
				{
				thumbPath='../' + page['shows'][i].Channel_Name_Internal + '/Video_Thumbnails/'+ page['shows'][i].Show_Name_Internal + '/';	
				}
				
			  table += '<td width="156" valign="top">';
			  table += '<div style="height:130px; width:146px; padding:4px" onClick="setShowID('+ page['shows'][i].Show_ID +');getVideoShowData(\'V\',' + '\'' + View  + '\''+ ',' + '\'' + 99999 + '\',' + 4 + ',' + 1 + ');getVideoShowCounts();setLinks(1);return false; "><img src="../'+ thumbPath + page['shows'][i].Show_Thumbnail +  '" width="143" height="80" class="border images-offset" style="position:absolute" ><div class="mouseover_open_btn" onMouseOver="this.className=\'mouseover_open_btn_mouseover\'" onMouseOut="this.className=\'mouseover_open_btn\'" title="Show Date : ' + page['shows'][i].Show_Date + '&#13Viewers Watching Now : ' + page['shows'][i].Show_Watching + '&#13Popularity : ' + page['shows'][i].Show_Views + '&#13Average Viewer Rating : ' + page['shows'][i].Show_Rating + ' / 5"/></div>';
			  table += '<div style="margin-top:10px; text-align:left"><span class="titles">'+ page['shows'][i].Show_Name + '</span>';
			  table += '</div>';
			  table += '<div style="margin-top:5px; text-align:left"><span class="sub_titles">Videos: </span><span class="maintext">' + page['shows'][i].Show_Clips_Count + '</span>';
			  table += '</div>';
			  table += '</td>';
			}
		else
			{
			  table += '<td width="156">';
			  table += '</td>';
			}
			i=i+1;
	}
	table += '</tr>';
	
	if (i > page['shows'].length) {
		strLoop = 'N';
		if (rowCount < 3) {
			for(j = rowCount; j < 3; j++) {	
				table += '<tr valign="top" colspan="3">';
				  table += '<td width="156" height="146">';
				  table += '</td>';
					table += '</tr>';
				}
		}
		table += '</table>';
		}
}

contain.innerHTML += table;
}


function drawTableVideos(page, contain, View, TimeSpan, PageSize) {
contain.innerHTML = '';

menu = '';
menu +=	'<table width="100%" cellspacing="0" cellpadding="0" bgcolor="#CCCCCC"';
menu +=	'<tr>';
menu +=	'<td align="left"><a href="#" id="link1" class="Menu_links" onClick="ChangeTimeSpan(\'V\',' + '\'' + View + '\'' +',1);return false;">First Added</a>&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" id="link2" class="Menu_links" onClick="ChangeTimeSpan(\'V\',' + '\'' + View + '\'' +',2);return false;">Popularity</a>&nbsp;&nbsp;&nbsp;&nbsp;|</td>';
if (page['clips'][0].Show_Name) {
	menu +=	'<td align="right" ><a href="#" class="widget_names" onclick="getVideoShowData(\'S\',' + '\'list\',' + '\'' + 99999 + '\',' + 9 + ',' + 1 + ');getVideoShowCounts();return false;"' +  '><img src="/_site/graphics/channel/btn_back_to_shows_small.png" alt="Back to shows" title="Back to shows" border="0"></a></td>';
}
menu +=	'</tr>';
menu +=	'</table>';

document.getElementById('VideosMenu').innerHTML = menu;

if (page['clips'][0].Show_Name) {
		document.getElementById('menutab').innerHTML = '<a href="#" class="widget_names" onclick="getVideoShowData(\'S\',' + '\'list\',' + '\'' + 99999 + '\',' + 9 + ',' + 1 + ');getVideoShowCounts();return false;"' +  '>Shows</a> > Videos for '  + page['clips'][0].Show_Name ;
	}

strV = 'S';
table = ''

initialVideo = '';

table += '<table width="100%" height="430" border="0" cellspacing="23" cellpadding="0">';
table += '<tr align="center">';
table += '<td valign="top">';

for(i = 0; i < page['clips'].length; i++) {
	strFlashLink = page['clips'][i].Clip_ID;
	if (initialVideo == '') {
		initialVideo = strFlashLink;
		}
	table += '<table width="509" border="0" cellspacing="4" cellpadding="0" onClick="playVid(\'OnDemand\',' + strFlashLink + ');">';
	table += '<tr>';
	table += '<td width="143"><img src="/'+ page['clips'][i].Channel_Name_Internal + '/Video_Thumbnails/'+ page['clips'][i].Show_Name_Internal + '/' + page['clips'][i].Clip_Thumbnail +  '" alt="Watch : '+ page['clips'][i].Clip_Name + '" title="Watch : '+ page['clips'][i].Clip_Name + '" class="border images-offset" width="143" height="80" border="0" style="position:absolute"><div class="mouseover_play_btn" onMouseOver="this.className=\'mouseover_play_btn_mouseover\'" onMouseOut="this.className=\'mouseover_play_btn\'"></div></td>';
	table += '<td width="1" style="padding:4px"></td>';
	table += '<td width="233" align="left" valign="top" style="padding:4px">';
	table += '<span class="titles">'+ page['clips'][i].Clip_Name + '</span><br>';
	table += '<span class="sub_titles">Date Added: </span><span class="maintext">' + page['clips'][i].Clip_Date_Added + '</span><br /><br />';
	table += '<span class="sub_titles">Duration: </span><span class="maintext">' + page['clips'][i].Clip_Duration + '</span><br />';
	table += '</td>';
	table += '<td width="110" align="left" valign="top" style="padding-left:10px; padding-top:4px; border-left:dotted #555555 1px">';
	table += '<div class="maintext" style="height:15px; padding-bottom:5px"><img src="/_site/graphics/icon_viewers-now.png" alt="Viewers Watching Now : ' + page['clips'][i].Clip_Watching + '" title="Viewers Watching Now : ' + page['clips'][i].Clip_Watching + '" width="17" height="12" border="0">&nbsp;&nbsp;&nbsp;' + page['clips'][i].Clip_Watching + '</div>';
	table += '<div class="maintext" style="height:15px; padding-bottom:5px"><img src="/_site/graphics/icon_popularity.png" alt="Popularity : ' + page['clips'][i].Clip_Views + '" title="Popularity : ' + page['clips'][i].Clip_Views + '" width="17" height="12" border="0">&nbsp;&nbsp;&nbsp;' + page['clips'][i].Clip_Views + '</div>';
	table += '<div class="maintext" style="height:15px; padding-bottom:5px"><img src="/_site/graphics/icon_rating.png" alt="Average Viewer Rating : ' + page['clips'][i].Clip_Rating + ' / 5" title="Average Viewer Rating : ' + page['clips'][i].Clip_Rating + ' / 5" width="17" height="12" border="0">&nbsp;&nbsp;&nbsp;' + page['clips'][i].Clip_Rating + ' / 5</div>';
	table += '</td>';
	table += '</tr>';
	table += '</table>';
	table += '<div style="height:10px; width:100%"></div>';
}

table += '</td>';
table += '</tr>';
table += '</table>';
	
if (initialVideo != '' && PageSize != 99999 && TimeSpan != 99999) {
	callExternalInterface(initialVideo);
	}

contain.innerHTML += table;
	
}


function ChangeTimeSpan(Menutab,View,TimeSpan) {
	
    setLinks(TimeSpan);  
	getVideoShowData(Menutab,View,TimeSpan,99999,1);
	getVideoShowCounts();

}

function setLinks(TimeSpan) {
	
	
	switch(TimeSpan)
		{
		case '1':
			if (document.getElementById('link1')) {
				document.getElementById('link1').className="Menu_links_selected";
				}
			break;
		case '2':
			if (document.getElementById('link2')) {
				document.getElementById('link2').className="Menu_links_selected";
				}
			break;
		case '3':
			if (document.getElementById('link3')) {
				document.getElementById('link3').className="Menu_links_selected";
				}
			break;
		case '4':
			if (document.getElementById('link4')) {
				document.getElementById('link4').className="Menu_links_selected";
				}
			break;
		}
		
}



function setShowID(Show_ID) {
	var myAjax = new Ajax.Request(
			'/_site/channel/set_show_ID.asp?Show_ID='+Show_ID,  
			{
				method: 'get', 
				parameters: '', 
				asynchronous: false,  
				onComplete: function(response) {
					resp(response.responseText);
				}
			});
}


/*---------------------------------------------------------------*/

function resp(x) {
}