// JavaScript Document
var videoList = {
	numofvids:0,
	videoListArray:0,
	numofpages:0,
	currentGroup:null,
	pickRandomVideoOnLoad:false,
	


	loadVideos: function(groupID, groupTitle) {
		//$('currenCategoryTitle').innerHTML = groupTitle;
		//videoList.highlightCurrentGroup(groupID);
		videoList.currentGroup = groupID;
		var query = 'scripts/loadGroupVideos.php';
		if(groupID) query = 'scripts/loadGroupVideos.php?groupID='+groupID;
		new Ajax.Request(query, {
		method:'get',
			onSuccess: function(transport){
				var json = transport.responseText.evalJSON(true);
				$('currenCategoryTitle').innerHTML = json.groupName;
				videoList.videoListArray = json;
				videoList.buildList(1);
		},
			onFailure: function(){$('currentList').update('<div class="listError">Sorry there are no videos for this group.</div>');}
		});	

	},
	loadFeatured: function() {
		$('currenCategoryTitle').innerHTML = 'Most Popular';
		var groupID = -1;
		//videoList.highlightCurrentGroup(groupID);
		videoList.currentGroup = groupID;
		query = 'scripts/loadFeatured.php';
		new Ajax.Request(query, {
		method:'get',
			onSuccess: function(transport){
			var json = transport.responseText.evalJSON(true);
			videoList.videoListArray = json;
			videoList.buildList(1);
		},
			onFailure: function(){$('currentList').update('<div class="listError">Sorry there are no featured videos at this time.</div>');}
		});	

	}, 
	
	loadByShow: function(showID) {
		if(!showID) return;
		
		//$('currenCategoryTitle').innerHTML = "Related Videos";
		videoList.highlightCurrentGroup();
		var query = 'scripts/loadByShow.php?s='+showID;

		new Ajax.Request(query, {
		method:'get',
			onSuccess: function(transport){
			var json = transport.responseText.evalJSON(true);
			videoList.videoListArray = json;
			videoList.buildList(1);
			$('currenCategoryTitle').innerHTML = json.showTitle;
		},
			onFailure: function(){$('currentList').update('<div class="listError">Sorry there are no videos for this show.</div>');}
		});	
	},
	
	highlightCurrentGroup: function(newGroup) {
		//alert("hi");
		if(newGroup != null) {
			$('group'+newGroup).className = 'active';
		}
		
		if(videoList.currentGroup || videoList.currentGroup == 0) {
			$('group'+videoList.currentGroup).className = '';
		}
		
	},
	
	clearDefault: function(toClear) {
 		 if (toClear.defaultValue==toClear.value) toClear.value = ""
	},
	
	buildPageination: function (currentPage){
		$("paginationControls").innerHTML = '<div id="prevBTN"></div><div id="nextBTN"></div>';

		lastPage = Math.ceil(this.numofvids/4);
		lastPage = parseInt(lastPage);
		this.numofpages = lastPage;
		if(this.numofvids > 4) {

			next = parseInt(currentPage)+1;
			prev = parseInt(currentPage)-1;
			prev_btn = "";
			next_btn = "";
			starter = currentPage;
			if(starter < 1) starter = 1;
			if(prev >=1) $("prevBTN").innerHTML = '<a href="javascript:videoList.buildList('+prev+')"><img border=0 src="/images/video/prev_btn.gif"></a>';
			if(next <= lastPage) $("nextBTN").innerHTML = '<a href="javascript:videoList.buildList('+next+')"><img border=0 src="/images/video/next_btn.gif"></a>';
			
		}

	},
	buildList: function(pagenum) {
		var videoDetails = new Template('<div class="thumbnailBox">\n'
					+ '	<div class="thumbnail"><a href="javascript:VideoPlayer.playVideo(#{id});"><img border=0 src="/images/video/thumbnails/#{image}" /></a></div>\n'
					+ '		<div><a href="javascript:VideoPlayer.playVideo(#{id});">#{title}</div>\n'
					+ '	</div>\n'
					);

				videoArray = this.videoListArray;
				var len = videoArray.videosbygroup.length;
				upper = (4*parseInt(pagenum))-1;
				lower = upper - 3;

				if(videoArray.videosbygroup != "none") {
					var content = '';
					if (len < 4) {upper = len-1;}
					this.numofvids=len;
					this.buildPageination(pagenum);

				if(this.numofpages == pagenum) {// if last page, only itterate last remaining videos
					var remain = upper - len;
					upper = upper - remain - 1;
				}

						for(var i = lower; i <= upper; i++) {
							var videoData = videoArray.videosbygroup[i];
							var thumb = (videoData.thumb)? videoData.thumb : 'video_generic.gif';
							var show = {id: videoData.id,
									image: thumb,
									title: videoData.title, 
									escapedTitle: escape(videoData.title),
									showID: videoData.showID }; 
							if (i%2 == 0) {content += '<div class="videoRow">';	}
							content += videoDetails.evaluate(show);
							if (i%2 != 0 && i != 0) {content += '</div>';}
						}//end loop
						$('listsArea').fire("videolists:loaded");
				} //end if not none
				else {content = '<div class="listError">Sorry your search returned zero videos.</div>';
					$("paginationControls").innerHTML = "";
				}
				$('currentList').innerHTML = content;
	}
	};