/*====================================================================
Filename:	videoFWC2009.js
Author: 	Didi Alamsaputra
Date: 		7/23/2009
Purpose: 	This is a second part of the FLW Media JS/ajax utilities
			for Forrest Wood Cup 2009
=======================================================================*/

/* ================
	INITIALIZATION
   ================*/
divName = "";

/* =========================================
	FUNCTIONS FOR STYLING THE VIDEO PLAYER
   =========================================*/
function instantiateVideoPlayerStyle(){
	plugin = $f("player").getControls().css("height","30px");
}

/* =========================================
	FUNCTIONS FOR VIEWING THE CHANNEL VIDEOS
   =========================================*/

//Create the html dynamically for the channels and the videos
function buildChannelVideos(numOfVideo,currentChannel){
	videosHTML = "";
	
	for (var counter=0;counter<numOfVideo;counter++){
		if (counter == 0){
			document.getElementById("flowPlayerDiv").innerHTML = "<a href=\"\" style=\"background: transparent url(" + aVideoThumbnail[0] + ") no-repeat scroll center center;display:block;width:464px;height:261px;\" id=\"player\" onclick=\"return false;\"><img style=\"padding-top:100px; padding-left:195px;\" src=\"/includes/flowplayer/play_large.png\" alt=\"Play Video\"/> </a>";
		}
		videosHTML += "<a id=\"videoLink" + counter + "\" href=\"\" onClick=\"loadVideo(" + counter + ",1);return false;\"><img class=\"\" style=\"width: 80px; height: 60px; display: block; float: left; margin-right: 4px;\" src=\"" + aVideoThumbnail[counter] + "\" />" + "<div class=linkVideoTitle>" + newLineToBR(aVideoTitle[counter]); 
		if (aVideoDuration[counter].length){
			videosHTML += "<span class=\"linkVideoDuration\"> (" + formatDuration(aVideoDuration[counter]) + ")</span>";
		}
		videosHTML += "</div><div class=linkVideoDescription>"  + newLineToBR(aVideoDescription[counter]) +  "</div><div class=\"fixFrame\"></div></a>";
	}
	document.getElementById("divChannelVideos").innerHTML = "<div id=\"div" + currentChannel + "\" class=\"openBox\" >" + videosHTML +"</div>";
}



/* =============================================
	FUNCTIONS FOR LOADING AND PLAYING THE VIDEOS 
   =============================================*/

// Load  and play a specific video using the video id
function loadVideo(vid,autoPlay){
	
	var videoTitleBottom = document.getElementById("videoTitleBottom");
	var videoTitleBottomRunningTime = document.getElementById("videoTitleBottomRunningTime");
	var videoDescription = document.getElementById("videoDescription");
	var suffix = "";
	var videoTitleText = "";
	currentVideoId = vid;
	if (autoPlay == 1){
		clip = {'url':aVideoStreamingPath[vid], 'autoplay':true};
		$f("player").play(clip);
	}else{
		clip = {'url':aVideoStreamingPath[vid], 'autoplay':false};
	}
	videoTitleText = aVideoTitle[vid];
	if (aVideoDuration[vid].length){
		videoTitleText += " (" + aVideoDuration[vid] + ") ";
	}
	videoTitleBottom.innerHTML = newLineToBR(aVideoTitle[vid]);
	videoDescription.innerHTML = newLineToBR(aVideoDescription[vid]);
	originalvideoDescription = newLineToBR(aVideoDescription[vid]);
	
	for (var i=0;i<aVideoFile.length;i++){
		videoLinkid = 'videoLink' + i;
		divCommentId = 'commentShow' + i;
		if (i == vid){
			changeStyle(videoLinkid,'currentlyPlaying');
		}else{
			changeStyle(videoLinkid,'');
		}
	}
	suffix = "_8" + "_" + aVideoId[vid];
	document.getElementById("commentDiv").innerHTML = "<div class=\"pod flwondemandcommentbox\" style=\"overflow:auto;\" id=\"commentarea" + suffix + "\"></div>";
	fillcommentarea(8,aVideoId[vid]);//8 is the comment type for media item 
	closeEmbeddedCode();
	setEmbeddedCode(currentVideoId);
	
}


// Load the caption using the video id
function loadText(vid){
	var videoTitleBottom = document.getElementById("videoTitleBottom");
	var videoDescription = document.getElementById("videoDescription");

	videoTitleBottom.innerHTML = aVideoTitle[vid];
	videoDescription.innerHTML = aVideoDescription[vid];
}

// Restore the caption of the current vidoe
function restoreText(){
	var videoTitleBottom = document.getElementById("videoTitleBottom");
	var videoDescription = document.getElementById("videoDescription");
	videoTitleTop.innerHTML = originalvideoTitle;
	videoTitleBottom.innerHTML = originalvideoTitle;
	videoDescription.innerHTML = newLineToBR(originalvideoDescription);
}



/* =======================================================
	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){
	
	var 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;
	
}

function setEmbeddedCode(vid){
	
	var embedLink = "http://www.flwoutdoors.com/flwondemand.cfm?cs=" + aVideoChannelCallsign[vid] +"&vid=" + aVideoId[vid];
	var embedLinkObject = document.getElementById("divEmbedLink");
	var embedCode = "";
	
	if (embedLinkObject){
		embedLinkObject.value = embedLink;
	}
	// Determine where we should get the player and the configuration for the flowplayer embedded code
	if (location.hostname){
		hostURL = "http://" + location.hostname;
		flowplayerURL = hostURL + "/FLWMedia/FLWVideoPlayer.swf";
		configURL = hostURL + "/FLWMedia/config.cfm?vid=";
	}
	
	// Form the embed code then set the field to that embedded code
	embedCode = "<object width=\"464\" height=\"291\"><param name=\"movie\" value=\""+ flowplayerURL + "?config=" + configURL + aVideoId[vid] + "\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><embed src=\"" + flowplayerURL + "?config=" + configURL + aVideoId[vid] + "\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"464\" height=\"261\"></embed></object>";
	
	var embedCodeObject = document.getElementById("divEmbedCode");
	if (embedCodeObject){
		embedCodeObject.value = embedCode;
	}

}


function viewEmbeddedCode(typeid){
	closeEmbeddedCode();
	if (parseInt(typeid)==2)
		var embedCodeObject = document.getElementById("divEmbedCode");
	else
		var embedCodeObject = document.getElementById("divEmbedLink");
	if(embedCodeObject){ 
		embedCodeObject.style.display = "inline";
		embedCodeObject.focus();
		embedCodeObject.select();
	}
	var shareInstructionObject = document.getElementById("divShareInstruction")
	if (shareInstructionObject) shareInstructionObject.style.display = "block";
	
	var linkCloseEmbedObject = document.getElementById("linkCloseEmbed")
	if (linkCloseEmbedObject) linkCloseEmbedObject.style.display = "inline";
	
	
}

function closeEmbeddedCode(){
	var embedCodeObject = document.getElementById("divEmbedCode");
	if (embedCodeObject) embedCodeObject.style.display = "none";
	
	var embedLinkObject = document.getElementById("divEmbedLink");
	if (embedLinkObject) embedLinkObject.style.display = "none";
	
	var linkCloseEmbedObject = document.getElementById("linkCloseEmbed");
	if (linkCloseEmbedObject) linkCloseEmbedObject.style.display = "none";
	
	var divShareInstructionObject = document.getElementById("divShareInstruction")
	if (divShareInstructionObject) divShareInstructionObject.style.display = "none";
}