function ptv_play(id, autoplay) {
	self.scrollTo(0, 0);
    
	if (autoplay==null) {
		var autoplay = true;
	}
   
	if (eval("typeof(pereztv_videodata_" + id + ")") != 'undefined') {
		var videodata = eval("pereztv_videodata_" + id);
		var channels_name = ptv_get_channels(id, ' ', 'yes');
	       
		$("#featured_video").html(getVideoViewer(id, {
			player_profile:'vega4-overlay',
			width:410,
			height:315,
			config: {
				autoplay: autoplay
			}
			}));
		$("#featured_video_infos").html('<span>' + videodata[0] + '</span><br/>Time: '+ videodata[1] +'<br/>Added: ' + videodata[2] + '<br/>Views: ' + videodata[3] + '<br/>Channels: ' + channels_name + '<div class="shareit_button"><img src="http://img.perezhilton.com/tv/images/share-it.png" onclick="ptv_toggleshareit();"/></div><div id="shareit"><input id="shareit_input"></div>');
		$("#shareit_input").attr("value","http://" + location.hostname + "/tv/index.php?ptvid="+id);
	}
    
	else document.write('<img src="http://img.perezhilton.com/tv/images/ptv-not-available.gif" align="left" border="0" hspace="0" vspace="0">');
}

function ptv_play_featured(autoplay) {
	self.scrollTo(0, 0);
    
	if (autoplay==null) {
		var autoplay = true;
	}
    
	if (eval("typeof(pereztv_videodata_" + pereztv_feature_movie[0] + ")") != 'undefined') {
		var videodata = eval("pereztv_videodata_"+pereztv_feature_movie[0]);
		var channels_name = ptv_get_channels(pereztv_feature_movie[0], ' ', 'yes');
	    
		$("#featured_video").html(getVideoViewer(pereztv_feature_movie[0], {
			player_profile:'vega4-overlay',
			width:410,
			height:315,
			config: {
				autoplay: autoplay
			}
			}));
		$("#featured_video_infos").html('<span>' + videodata[0] + '</span><br/>Time: '+ videodata[1] +'<br/>Added: ' + videodata[2] + '<br/>Views: ' + videodata[3] + '<br/>Channels: ' + channels_name + '<div class="shareit_button"><img src="http://img.perezhilton.com/tv/images/share-it.png" onclick="ptv_toggleshareit();"/></div><div id="shareit"><input id="shareit_input"></div>');
		$("#shareit_input").attr("value","http://" + location.hostname + "/tv/index.php?ptvid="+pereztv_feature_movie[0]);
	}
    
	else {
		document.write('<img src="http://img.perezhilton.com/tv/images/ptv-not-available.gif" align="left" border="0" hspace="0" vspace="0">');
	}
}
 
function ptv_get_channel_main(channel_array, channel_id) {
	var crop = 16;
	var main_list_end = conf_channel_max_item - 1;
	var i;
	var browser = navigator.appName;
	var browser_version = navigator.appVersion.substring(0,1);
    
	for (i=0; i<=1; i++) {
		if(channel_array[i]){
			if (eval("typeof(pereztv_videodata_" + channel_array[i] + ")") != 'undefined') {
				var videodata = eval("pereztv_videodata_" + channel_array[i]);
				var title = videodata[0];
				var replaced_title = remove_html_tags(title.replace(/&quot;/g, '"'));
				var url = 'index.php?ptvid=' + channel_array[i] + '&channel_id=' + channel_id + '&start=0&end=' + main_list_end;
				var title = videodata[0] + ' | ' + videodata[2] + ' | ' + videodata[3];
				var channels_name = ptv_get_channels(channel_array[i], ' ', 'no');
		        
				/*if (crop >= replaced_title.length) {
		          crop_title = replaced_title.substr(0, crop);
		      }
		      
		      else {
		          crop_title = replaced_title.substr(0, crop) + '...';
		      }*/
		      
				document.write('<div class="channelslist"><a href="' + url + '"><img src="http://vids.perezhilton.com/videos/' + channel_array[i] + '/screenshots/188w.jpg" border="0" /></a> <div id="crop_title_' + channel_id + '_' + channel_array[i] + '" class="crop_title"><a href="' + url + '"><strong>' + replaced_title + '</strong></a><br/>Time: '+ videodata[1] +'<br/>Added: ' + videodata[2] + '<br/>Views: ' + videodata[3] + '<br><br></div></div>');
			}
		}
	}
}

function ptv_get_newest(id, start, end) {
	var crop = 40;
	var i;
    
	if (eval("typeof(pereztv_videoids_" + id + ")") != 'undefined') {
		var channel_array = eval("pereztv_videoids_" + id);
		var last_item = channel_array.length-1;
	    
		if (last_item < end) end = last_item;
	    
		document.write('<div id="videolistcontainer">');
	    
		for (i=start; i<=end; i++) {
			if (eval("typeof(pereztv_videodata_"+pereztv_videoids_newest[i]+")") != 'undefined') {
				var videodata = eval("pereztv_videodata_"+pereztv_videoids_newest[i]);
				var title = videodata[0];
				var replaced_title = remove_html_tags(title.replace(/&quot;/g, '"'));
				var url = 'index.php?ptvid=' + pereztv_videoids_newest[i] + '&category_id=newest&category_start=' + start + '&category_end=' + end;
				var channels_name = ptv_get_channels(pereztv_videoids_newest[i], 'class="channel"', 'yes');
			      
				/*if (crop >= replaced_title.length) {
			          crop_title = replaced_title.substr(0, crop);
			      }
			      
			      else {
			          crop_title = replaced_title.substr(0, crop) + '...';
			      }*/
			      
				document.write('<div class="videolist"><a href="' + url + '"><img src="http://vids.perezhilton.com/videos/' + pereztv_videoids_newest[i] + '/screenshots/116w.jpg" border="0" title="' + remove_html_tags(videodata[0]) + '"/></a><div class="vid-info-container"><a href="' + url + '" title="' + remove_html_tags(videodata[0]) + '"><span>' + replaced_title + '</span></a><br/>Time: ' + videodata[1] + '<br/>Added: ' + videodata[2] + '<br/>Views: ' + videodata[3] + '<br/>Channels: ' + channels_name + '<br><br clear="all"/></div></div>');
			}
		}
	    
		document.write('</div>');
	    
		turn_category_page(id, last_item, start, end);
	}
}

function ptv_get_most_viewed(id, start, end) {
	var crop = 40;
	var i;
    
	if (eval("typeof(pereztv_videoids_" + id + ")") != 'undefined') {
		var channel_array = eval("pereztv_videoids_" + id);
		var last_item = channel_array.length-1;
	    
		if (last_item < end) end = last_item;
	    
		document.write('<div id="videolistcontainer">');
	    
		for (i=start; i<=end; i++) {
			if (eval("typeof(pereztv_videodata_" + pereztv_videoids_mostviewed[i] + ")") != 'undefined') {
				var videodata = eval("pereztv_videodata_"+pereztv_videoids_mostviewed[i]);
				var title = videodata[0];
				var replaced_title = remove_html_tags(title.replace(/&quot;/g, '"'));
				var url = 'index.php?ptvid=' + pereztv_videoids_mostviewed[i] + '&category_id=mostviewed&category_start=' + start + '&category_end=' + end;
				var channels_name = ptv_get_channels(pereztv_videoids_mostviewed[i], 'class="channel"', 'yes');
			      
				/*if (crop >= replaced_title.length) {
			          crop_title = replaced_title.substr(0, crop);
			      }
			      
			      else {
			          crop_title = replaced_title.substr(0, crop) + '...';
			      }*/
			      
				document.write('<div class="videolist"><a href="' + url + '"><img src="http://vids.perezhilton.com/videos/' + pereztv_videoids_mostviewed[i] + '/screenshots/116w.jpg" border="0" title="' + remove_html_tags(videodata[0]) + '"/></a><div class="vid-info-container"><a href="' + url + '" title="' + remove_html_tags(videodata[0]) + '"><span>' + replaced_title + '</span></a><br/>Time: ' + videodata[1] + '<br/>Added: ' + videodata[2] + '<br/>Views: ' + videodata[3] + '<br/>Channels: ' + channels_name + '<br><br clear="all"/></div></div>')
			}
		}
	    
		document.write('</div>');
	    
		turn_category_page(id, last_item, start, end);
	}
}

function ptv_get_channel_items(id, start, end) {
	var crop = 16;
    
	var id_name;
    
	if (eval("typeof(pereztv_videoids_" + id + ")") != 'undefined') {
		var channel_array = eval("pereztv_videoids_" + id);
		var last_item = channel_array.length-1;
		var i;
		var browser = navigator.appName;
		var browser_version = navigator.appVersion.substring(0,1);
	    
		if (id == 'newest' || id == 'mostviewed') id_name = 'category_id';
		else id_name = 'channel_id';
	    
		if (last_item < end) end = last_item;
	    
		for (i=start; i<=end; i++) {
			var even = isNumeric(i/2);
			var odd = isNumeric((i+1)/2);

			if (eval("typeof(pereztv_videodata_" + channel_array[i] + ")") != 'undefined') {
				var videodata = eval("pereztv_videodata_" + channel_array[i]);
				var title = videodata[0];
				var replaced_title = remove_html_tags(title.replace(/&quot;/g, '"'));
			      
				/*if (crop >= replaced_title.length) {
					crop_title = replaced_title.substr(0, crop);
				}
			      
				else {
					crop_title = replaced_title.substr(0, crop) + '...';
				}*/
			      
				var url = 'index.php?' + id_name + '=' + id + '&ptvid=' + channel_array[i] + '&start=' + start + '&end=' + end;
				var title = videodata[0] + ' | ' + videodata[2] + ' | ' + videodata[3];
				var channels_name = ptv_get_channels(channel_array[i], ' ', 'no');
			      
				if (i == start || even) document.write('<div class="channelbox-container"><div class="channelsbox">');
				if (i == start) document.write('<div class="channelsname">Videos (' + channel_array.length + ' items)</div>');
				//if (i != start && even) document.write('<div class="channelsname">&nbsp;</div>');

				document.write('<div class="channelslist"><a href="' + url + '"><img src="http://vids.perezhilton.com/videos/' + channel_array[i] + '/screenshots/188w.jpg" border="0" /></a> <div id="crop_title_' + channel_array[i] + '" class="crop_title"><a href="' + url + '"><strong>' + replaced_title + '</strong></a><br/>Time: '+ videodata[1] +'<br/>Added: ' + videodata[2] + '<br/>Views: ' + videodata[3] + '<br><br></div></div>');

				if (odd || i==end) document.write('</div></div>');
			}
	    	
			else {
				if (i == start || even) document.write('<div class="channelsbox">');
				if (i == start) document.write('<div class="channelsname">Videos (' + channel_array.length + ' items)</div>');
				if (i != start && even) document.write('<div class="channelsname">&nbsp;</div>');
				document.write('<div class="channelslist"></div>');
				if (odd || i==end) document.write('</div>');
			}
		}
	    
		turn_channel_page(id, last_item, start, end);
	}
}

function turn_channel_page(id, last_item, start, end) {
    
	var previous_start = start - conf_channel_max_item;
	var previous_end = end - conf_channel_max_item;
	var next_start = start + conf_channel_max_item;
	var next_end = end + conf_channel_max_item;
	var check = isNumeric(start/conf_channel_max_item);
    
	if (start != 0 && !check) parent.location.href="index.php";
    
	if (last_item == end) {
		previous_end = start - 1;
		previous_start = previous_end - conf_channel_max_item + 1;
	}
        
	document.write('<table border="0" cellpadding="0" cellspacing="0" width="100%" style="clear: both;"><tr>');
	if (start != 0) document.write('<td width="50%" align="left"><a href="index.php?channel_id=' + id + '&start=' + previous_start + '&end=' + previous_end + '#channel">&laquo; Previous</a></td>');
	if (last_item > end) document.write('<td width="50%" align="right"><a href="index.php?channel_id=' + id + '&start=' + next_start + '&end=' + next_end + '#channel">Next &raquo;</a></td>');
	document.write('</tr></table>');
}

function turn_category_page(id, last_item, start, end) {
    
	var previous_start = start - conf_category_max_item;
	var previous_end = end - conf_category_max_item;
	var next_start = start + conf_category_max_item;
	var next_end = end + conf_category_max_item;
	var check = isNumeric(start/conf_category_max_item);
    
	if (start != 0 && !check) parent.location.href="index.php";
    
	if (last_item == end) {
		previous_end = start - 1;
		previous_start = previous_end - conf_category_max_item + 1;
	}
        
	document.write('<p><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr>');
	if (start != 0) document.write('<td width="50%" align="left"><a href="index.php?category_id=' + id + '&category_start=' + previous_start + '&category_end=' + previous_end + '#playlist">&laquo; Previous</a></td>');
	if (last_item > end) document.write('<td width="50%" align="right"><a href="index.php?category_id=' + id + '&category_start=' + next_start + '&category_end=' + next_end + '#playlist">Next &raquo;</a></td>');
	document.write('</tr></table></p>');
}

function ptv_toggleshareit() {
	if($("#shareit").css("display")=="none") {
		self.scrollTo(0, 200);
		$("#shareit").css("display","block");
	}
	else {
		self.scrollTo(0, 0);
		$("#shareit").css("display","none");
	}
}

function ptv_infobox_on(id) {
	$("#infobox_" + id).css("display","block");
	$("#crop_title_" + id).css("display","none");
}

function ptv_infobox_off(id) {
	$("#infobox_" + id).css("display","none");
	$("#crop_title_" + id).css("display","block");
}

function ptv_get_channels(id, class_name, url) {
	var end = conf_channels_array_key.length - 1;
	var channels = '';
	var main_list_end = conf_channel_max_item - 1;
	var i;
    
	for (i=0; i<=end; i++) {
		if (eval("typeof(pereztv_videoids_" + conf_channels_array_key[i] + ")") != 'undefined') {
			var array = eval("pereztv_videoids_" + conf_channels_array_key[i]);
			array = array.toString();
			if (array.indexOf(id) != -1) {
				if (url == 'yes') channels = channels + '<a ' + class_name + ' href="index.php?channel_id=' + conf_channels_array_key[i] + '&start=0&end=' + main_list_end + '#channel">' + conf_channels_array_data[i].toLowerCase() + '</a> ';
				if (url == 'no') channels = channels + conf_channels_array_data[i].toLowerCase() + ' ';
			}
		}
	}
    
	if (channels == '') return 'no channel';
	if (url == 'yes') return channels.replace(/a> <a/g, "a>, <a");
	if (url == 'no') {
		channels = channels.replace(/ /g, ", ");
		var crop = channels.length - 2;
		return channels.substring(0, crop);
	}
}

function ptv_get_keyword_items(keyword, start, end) {
	keyword_array_all_length = get_keyword_array_length(keyword);
    
	if (keyword_array_all_length == 'empty') {
		document.write('<div class="emptychannelsbox">');
		document.write('<div class="channelsname">Not found.</div>');
		document.write('<div class="channelsname">&nbsp;</div>');
		document.write('</div>');
	}
    
	else {
		var crop = 16;
		var last_item = keyword_array_all_length-1;
		var browser = navigator.appName;
		var browser_version = navigator.appVersion.substring(0,1);
        
		if (last_item < end) end = last_item;
        
		var keyword_array = get_keyword_array(keyword, start, end);
		var i;
        
		for (i=start; i<=end; i++) {
			var even = isNumeric(i/2);
			var odd = isNumeric((i+1)/2);
        	
			if (eval("typeof(pereztv_videodata_" + keyword_array[i] + ")") != 'undefined') {
				var videodata = eval("pereztv_videodata_" + keyword_array[i]);
				var title = videodata[0];
				var replaced_title = remove_html_tags(title.replace(/&quot;/g, '"'));
		
				/*if (crop >= replaced_title.length) {
					crop_title = replaced_title.substr(0, crop);
				}
		
				else {
					crop_title = replaced_title.substr(0, crop) + '...';
				}*/
		
				var url = 'index.php?keyword=' + keyword + '&ptvid=' + keyword_array[i] + '&start=' + start + '&end=' + end;
				var title = videodata[0] + ' | ' + videodata[2] + ' | ' + videodata[3];
				var channels_name = ptv_get_channels(keyword_array[i], ' ', 'yes');
		          
				if (i == start || even) document.write('<div class="channelbox-container"><div class="channelsbox">');
				if (i == start) document.write('<div class="channelsname">Videos (' + keyword_array_all_length + ' items)</div>');
				//if (i != start && even) document.write('<div class="channelsname">&nbsp;</div>');

				document.write('<div class="channelslist"><a href="' + url + '"><img src="http://vids.perezhilton.com/videos/' + keyword_array[i] + '/screenshots/188w.jpg" border="0" /></a> <div id="crop_title_' + keyword_array[i] + '" class="crop_title"><a href="' + url + '"><strong>' + replaced_title + '</strong></a><br/>Time: '+ videodata[1] +'<br/>Added: ' + videodata[2] + '<br/>Views: ' + videodata[3] + '<br/>Channels: ' + channels_name + '<br><br></div></div>');

				if (odd || i==end) document.write('</div></div>');
			}
        	
			else {
				if (i == start || even) document.write('<div class="channelsbox">');
				if (i == start) document.write('<div class="channelsname">Videos (' + keyword_array_all_length + ' items)</div>');
				if (i != start && even) document.write('<div class="channelsname">&nbsp;</div>');
				document.write('<div class="channelslist"></div>');
				if (odd || i==end) document.write('</div>');
			}
		}
        
		turn_keyword_page(keyword, last_item, start, end);
	}
}

function get_keyword_array(keyword, start, end) {
	var source_array_length = pereztv_videoids_newest.length;
	var i;
	var number = 0;
	var answer_array = new Array();
    
	for (i=0; i<=source_array_length; i++) {
		if (eval("typeof(pereztv_videoids_newest[" + i  + "])") != 'undefined') {
			var videoid = eval("pereztv_videoids_newest[" + i  + "]");
	        
			if (eval("typeof(pereztv_videodata_" + videoid  + ")") != 'undefined') {
				var videodata = eval("pereztv_videodata_" + videoid);
				var title = videodata[0].toLowerCase();
		        
				if (i >= start || i <= end) {
					if (title.indexOf(keyword.toLowerCase()) !== -1) {
						answer_array[number] = videoid;
						number = number + 1;
					}
				}
		        
				if (number > end) break;
			}
		}
	}
    
	if (answer_array != null) return answer_array;
	else return 'empty';
}

function get_keyword_array_length(keyword) {
	var source_array_length = pereztv_videoids_newest.length - 1;
	var i;
	var number = 0;
	var answer_array = new Array();
    
	for (i=0; i<=source_array_length; i++) {
		if (eval("typeof(pereztv_videoids_newest[" + i + "])") != 'undefined') {
			var videoid = eval("pereztv_videoids_newest[" + i + "]");
		    
			if (eval("typeof(pereztv_videodata_" + videoid + ")") != 'undefined') {
				var videodata = eval("pereztv_videodata_" + videoid);
				var title = videodata[0].toLowerCase();
		        
				if (title.indexOf(keyword.toLowerCase()) !== -1) {
					answer_array[number] = videoid;
					number = number + 1;
				}
			}
		}
	}
    
	if (answer_array.length > 0) return answer_array.length;
	else return 'empty';
}

function turn_keyword_page(keyword, last_item, start, end) {
    
	var previous_start = start - conf_channel_max_item;
	var previous_end = end - conf_channel_max_item;
	var next_start = start + conf_channel_max_item;
	var next_end = end + conf_channel_max_item;
	var check = isNumeric(start/conf_channel_max_item);
    
	if (start != 0 && !check) parent.location.href="index.php";
    
	if (last_item == end) {
		previous_end = start - 1;
		previous_start = previous_end - conf_channel_max_item + 1;
	}
        
	document.write('<p><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr>');
	if (start != 0) document.write('<td width="50%" align="left"><a href="index.php?keyword=' + keyword + '&start=' + previous_start + '&end=' + previous_end + '#channel">&laquo; Previous</a></td>');
	if (last_item > end) document.write('<td width="50%" align="right"><a href="index.php?keyword=' + keyword + '&start=' + next_start + '&end=' + next_end + '#channel">Next &raquo;</a></td>');
	document.write('</tr></table></p>');
}

function check_channel(channel_id) {
	if (eval("typeof(pereztv_videoids_" + channel_id + ")") != 'undefined') {   
		var channel_array = eval("pereztv_videoids_" + channel_id);
		if (channel_array.length) return true;
		else return false;
	}
	
	else return false;
}

function remove_html_tags(text){
	var tag_stripped_text = text.replace(/<\/?[^>]+(>|$)/g, "");
 	
	return tag_stripped_text;	
}
