/*====================================================================
Filename:	videoCFHomepage.js
Author: 	Didi Alamsaputra
Date: 		7/23/2009
Purpose: 	Viewing JS module for College Fishing home page
=======================================================================*/


/* ================
	INITIALIZATION
   ================*/
divName = "";
urlDivisionName = ""; // This is to keep track whether it's coming from bass fishing or walleye


/* =========================================
	FUNCTIONS FOR STYLING THE VIDEO PLAYER
   =========================================*/
function instantiateVideoPlayerStyle(){
	var controlPlugin = $f("player").getPlugin("controls")
	if(controlPlugin){ 
		controlPlugin.css("height","17px");
		controlPlugin.enable({scrubber: false,time: false});
	}
}

/* =========================================
	FUNCTIONS FOR VIEWING THE CHANNEL VIDEOS
   =========================================*/
   


//Create the html dynamically for the channels and the videos
function buildChannelVideos(numOfVideo,currentChannel){
	if (numOfVideo > 3){
		numOfVideo=3;
	}
	videosHTML = "";
	for (var counter=0;counter<numOfVideo;counter++){
		if (counter == 0){
			document.getElementById("flowPlayerDiv").innerHTML = "<a href=\"\" style=\"display:block;width:200px;height:143px;\" id=\"player\" onclick=\"return false;\"><img style=\"width:200px;height:143px;\" src=\"" + aVideoThumbnail[0] + "\" alt=\"Play Video\"/>" + "<img style=\"position:absolute; top:30px;left:55px;\" src=\"/includes/flowplayer/play_large.png\" alt=\"Play Video\"/></a>";
		}
		if (counter == 0){
			videosHTML += "<a id=\"videoLink" + counter + "\" href=\"\" onMouseOver=\"loadText(" + counter + ");\" onMouseOut=\"restoreText("+ counter + ");\" onClick=\"loadVideo(" + counter + ",1);return false;\"><img id=\"videoThumb" + counter + "\" class=\"hpvideothumbActive\" style=\"width: 66px; height: 50px; display: block; float: left; margin-right: 0px;\" src=\"" + aVideoThumbnail[counter] + "\" />"; 
		}else{
			videosHTML += "<a id=\"videoLink" + counter + "\" href=\"\" onMouseOver=\"loadText(" + counter + ");\" onMouseOut=\"restoreText("+ counter + ");\" onClick=\"loadVideo(" + counter + ",1);return false;\"><img id=\"videoThumb" + counter + "\" class=\"hpvideothumb\" style=\"width: 66px; height: 50px; display: block; float: left; margin-right: 0px;\" src=\"" + aVideoThumbnail[counter] + "\" />";
		}
	}
	document.getElementById("divChannelVideos").innerHTML = "<div id=\"div" + currentChannel + "\" class=\"openBox\" >" + videosHTML +"</div>";
	
}


function viewLargerVideo(){
	var urlVideo = "/ondemand.cfm?vid=" + aVideoId[currentVideoId];
	window.location = urlVideo;
}



/* =============================================
	FUNCTIONS FOR LOADING AND PLAYING THE VIDEOS 
   =============================================*/

// Load Video using the video id
function loadVideo(vid,autoPlay){
	
	var videoTitleBottom = document.getElementById("videoTitleBottom");
	var suffix = "";
	var videoTitleText = "";
	var videoThumbDiv = "";
	// Setting the current video id to what is playing currently
	currentVideoId = vid;
	if (autoPlay == 1){
		clip = {'url':aVideoStreamingPath[vid], 'autoplay':true, provider: provider};
		$f("player").play(clip);
	}else{
		clip = {'url':aVideoStreamingPath[vid], 'autoplay':false, provider: provider};
	}
	videoTitleBottom.innerHTML = newLineToBR(aVideoTitle[vid]);

	for (var i=0;i<aVideoFile.length;i++){
		videoThumbDiv = 'videoThumb' + i;
		if (i == currentVideoId){
			changeStyle(videoThumbDiv,'hpvideothumbActive');
		}else{
			changeStyle(videoThumbDiv,'hpvideothumb');
		}
	}
	
}


// Load the caption using the video id

function loadText(vid){
	var videoTitleBottom = document.getElementById("videoTitleBottom");
	videoTitleBottom.innerHTML = aVideoTitle[vid];
}

function restoreText(){
	var videoTitleBottom = document.getElementById("videoTitleBottom");
	videoTitleBottom.innerHTML = aVideoTitle[currentVideoId];
}

/* ====================================================
	FUNCTIONS FOR SEARCHING VIDEOS FOR CERTAIN CHANNELS
   ====================================================*/

// Search the videos
function searchVideos(searchKeywords,maxResultsReturned, tabSource){
	var searchChannels = "";
	for (i=1;i<=5;i++){
		if (i<=1){
			searchChannels = aVideoChannel[i];
		}else{
			searchChannels = searchChannels + "," + aVideoChannel[i];
		}
	}
	searchMediaItems(searchChannels,searchKeywords,maxResultsReturned);
}

// If we can't find any video from the search then display this message
function showNoVideosFound(){
	document.getElementById("divChannelVideos").innerHTML = "<div style='color:#000000;padding:10px;'>No videos found using the specified keyword(s)</div>";
}

//clear the search text
function clearSearchText(){
	if (document.getElementById("keywordsField").value == 'Keyword(s)'){
		document.getElementById("keywordsField").value = "";
	}
}




/* ==================================
	FUNCTIONS FOR TRACKING THE VIDEOS 
   ==================================*/
   
// for tracking the video
function trackMedia(){
	
	$.post(flwMediaAjaxPath, {
		method:				"trackMediaItem"
		,id: aVideoId[currentVideoId]
		,areaid:mediaItemAreaId
		,mediachannelid: aVideoChannelId[currentVideoId]
		
	}, function(response) {
		
	});
}


/* ======================================================
   FUNCTIONS FOR STRING/STYLE FORMATTING AND MANIPULATION 
   ======================================================
*/

// replace all the new line string with a <br /> tag for html
function newLineToBR(stringToConvert){
	return stringToConvert.replace(/\n/g, "<br />");
}


// Change the style of a specific element
function changeStyle(id, newClass) {
	var identity = document.getElementById(id);

	if (identity)
		identity.className=newClass;
}  

//Format the duration of the videos
function formatDuration(strDuration){
	
	strDuration = strDuration.substr(0,8);
	var strIndex = strDuration.length;
	do
	{
		if (strDuration.substr(0,1) == '0' || strDuration.substr(0,1) == ':'){
			strDuration = strDuration.substr(1);
		}
		strIndex = strIndex - 1;
	}
	while ((strDuration.substr(0,1) == '0' || strDuration.substr(0,1) == ':') && strIndex >= 5);
	return strDuration;
	
}