/*====================================================================
Filename:	video.js
Author: 	Didi Alamsaputra
Date: 		7/23/2009
Purpose: 	Main module for JI Media JS/ajax utilities
=======================================================================*/


/* ================
	INITIALIZATION
   ================*/
flwMediaAjaxPath = "/flwMedia/ajax.cfm"; 	// The main ajax file 
currentVideoId = 0;							// The video id that is playing
nVideoChannel = 0;							// The number of video channel 
aVideoChannelId = new Array();				// The array for the video channel ID
aChannelCallsign = new Array(); 			// The array for the video channels available
aChannelName = new Array();		 			// The array for the video channels available
aVideoId = new Array()						// The array for the video id
aVideoChannelCallsign = new Array(); 		// The array for the video channel
aVideoChannelName = new Array(); 			// The array for the video channel
aVisibility = new Array(); 					// The array for the video channel visibility
aVideoFile = new Array();					// The array for the video files
aVideoHDFile = new Array();					// The array for the high definition video 
aVideoTitle = new Array();					// The array for the video titles
aVideoDescription = new Array();			// The array for the video description
aVideoDuration = new Array();				// The array for the video duration
aVideoThumbnail = new Array();				// The array for the video thumbnails
aVideoSplash = new Array();					// The array for the video splash images
aVideoStreamingPath = new Array();			// The array for the video path akamai streaming
nVideoChannel = 1;
videoThumbnailId = "";
divCommentId = "";
videoLinkid = "";
originalvideoTitle = ''; 
originalvideoDescription = ''
clip = "";									// The clip variable that is used for flowplayer
addthis_share = {url:""};					// This variable is used for the "share this" button for sharing the video on different sites
googleTrackingID = "";
gaPlugin = "";

//Assign the google tracking ID based on the domain
if (location.hostname.search(/dev/i)>=0){
	googleTrackingID = "UA-10911906-3";
	environment = "dev"; // This is for tracking purposes, we want to use dev.services.jacobsinteractive.com
}else if (location.hostname.search(/test/i)>=0){
	googleTrackingID = "UA-10911906-4";
	environment = "test";
}else if (location.hostname.search(/collegefishing/i)>=0 ){
	googleTrackingID = "UA-1178649-9";
	environment = "www";	// For tracking, in production we want to use services.jacobsinteractive.com
}else{
	googleTrackingID = "UA-1178649-8";
	environment = "www";	// For tracking, in production we want to use services.jacobsinteractive.com
}

/* =========================================
	FUNCTIONS FOR GETTING THE CHANNEL VIDEOS 
   =========================================*/
// getting the videos in certain channel using ajax
function getVideosInChannel(callsign,index) {
	$.post(flwMediaAjaxPath, {
			method:				"getVideosInChannel"
			,callsign:		callsign
		}, function(response) {
			populateVideos(response,callsign,index,"getVideosInChannel");
	});	
}
function getVideosInChannelTournament(callsign,tournamentId){
	
	$.post(flwMediaAjaxPath, {
			method:				"getVideosInChannel"
			,callsign:		callsign
			,tournamentId: 	tournamentId
		}, function(response) {
			populateVideos(response,callsign,tournamentId,"getVideosInChannel");
	});	
}

// populating the videos from the ajax response
function populateVideos(response,callsign, index,method){
	var json = eval("("+response+")");		
	var counterMax = 0;
	var arrayLength =0;
	
	
	if (json.BERROR){
		if (callsign == 'collegefishingwebsite'){
			getVideosInChannel('collegefishingwebsite',1);
		}
		alert(json.SERROR);
	}
	if (method == "searchMediaItems"){
		aChannelName[index] = "Search";
		arrayLength = json.AFILE.length;
	}else{
		aChannelName[index] = json.CHANNEL.NAME;
		arrayLength = json.CHANNEL.AFILE.length;
	}
	
	for (var counter = 0;counter<arrayLength;counter++){	

		if (method == "searchMediaItems"){
			aVideoChannelId[counter] = 0; 
			aVideoChannelCallsign[counter] = "Search";
			aVideoChannelName[counter] = "Search";
			aVideoId[counter]= json.AFILE[counter].ID;
			aVideoTitle[counter]= json.AFILE[counter].TITLE;
			aVideoDescription[counter] = json.AFILE[counter].DESCRIPTION;
			aVideoDuration[counter] = json.AFILE[counter].DURATION;
			aVideoFile[counter] = json.AFILE[counter].PATH;
			aVideoThumbnail[counter] = json.AFILE[counter].THUMBNAIL; 
			aVideoStreamingPath[counter] = json.AFILE[counter].STREAMING_PATH.slice(36);
			aVideoHDFile[counter] = '';
		}else{
			aVideoChannelCallsign[counter] = json.CHANNEL.CALLSIGN;
			aVideoChannelName[counter] = json.CHANNEL.NAME;
			aVideoChannelId[counter] = json.CHANNEL.AFILE[counter].CHANNELID; 
			aVideoId[counter]= json.CHANNEL.AFILE[counter].ID;
			aVideoTitle[counter]= json.CHANNEL.AFILE[counter].TITLE;
			aVideoDescription[counter] = json.CHANNEL.AFILE[counter].DESCRIPTION;
			aVideoDuration[counter] = json.CHANNEL.AFILE[counter].DURATION;
			aVideoFile[counter] = json.CHANNEL.AFILE[counter].PATH;
			aVideoThumbnail[counter] = json.CHANNEL.AFILE[counter].THUMBNAIL; 
			aVideoStreamingPath[counter] = json.CHANNEL.AFILE[counter].STREAMING_PATH.slice(36);
			aVideoHDFile[counter] = json.CHANNEL.AFILE[counter].FILENAME_HD;
		}
	}
	
	if (arrayLength > 0){
		buildChannelVideos(arrayLength,aChannelName[index]);
		if (document.getElementById("player")){
			instantiateAkamaiVideoPlayer(currentVideoId);
			loadVideo(currentVideoId, 0);
		}
	}else{
		showNoVideosFound();
	}
	
}


/* ===================================================
	FUNCTIONS FOR SEARCHING VIDEOS IN CERTAIN CHANNELS
   ===================================================*/
function searchMediaItems(callsigns,searchKeywords,maxresults){
	$.post(flwMediaAjaxPath, {
			method:			"searchMediaItems"
			,callsigns:		callsigns
			,searchKeywords:searchKeywords
			,maxResults:	maxresults
		}, function(response) {
			populateVideos(response,callsigns,0,"searchMediaItems");
			
	});	
}



/* ===================================================
	FUNCTIONS FOR INSTANTIATING FLOW PLAYER
   ===================================================*/
// Load the flow player with the rtmp plugin for akamai
function instantiateAkamaiVideoPlayer(vid){
	var sFlowPlayerKey = "#@f00ca1a01f63b8d79e2";
	var hostURL = "http://" + location.hostname;
	var flowplayerURL = "";
	if (hostURL.search(/collegefishing/i)>0 ){ 	
		flowplayerURL = hostURL.replace("collegefishing","flwoutdoors") + "/FLWMedia/CFVideoPlayer.swf";
		sFlowPlayerKey = '#@1fef310ffb4c6ba088e';
		
		flowplayer("player", flowplayerURL,
			{	
				key:sFlowPlayerKey,
				clip: {
					url: aVideoStreamingPath[vid],
					provider:"akamai",
					autoPlay:true,
					scaling: 'fit'
					
				},
				play: { 
					width: 60, 
					height: 60 
				},
				plugins: {
					gatracker: {
						videoid:aVideoId[vid],
						env:environment,
						areaid:mediaItemAreaId,
						mediachannelid:aVideoChannelId[vid],
						google_id:googleTrackingID
					},
					content: { 
						backgroundColor: '#000000'
					}
		
				}
				
			}   
		);
	}else{
		flowplayer("player", "/FLWMedia/FLWVideoPlayer.swf",
			{	
				key:sFlowPlayerKey,
				clip: {
					url: aVideoStreamingPath[vid],
					provider: "akamai",
					autoPlay:true,
					scaling: 'fit'
				},
				play: { 
					width: 60, 
					height: 60 
				},
				plugins: {
					gatracker: {
						videoid: aVideoId[vid],
						env 	: environment,
						areaid: mediaItemAreaId,
						mediachannelid: aVideoChannelId[vid],
						google_id: googleTrackingID
					},
					content: { 
						backgroundColor: '#000000'
					}
		
				}
			}   
		);
	}
	$f("player").onLoad(function() { 
		instantiateVideoPlayerStyle(); 
	}); 
	// Every time we begin to load the clip, we want to set the video id for tracking
	$f("player").onBegin(function() { 
		gaPlugin = $f("player").getPlugin("gatracker");
		if (gaPlugin) 
			gaPlugin.setVID(aVideoId[currentVideoId]);
	}); 
}




