var flashPlayerUsed = true;
var gaPlayerType = 'flash';
var activeCameraID = 0;
var iframeID = 'default';
var timeStamp = new Date().getTime();
var activeEventID;
var activeVideoID;
var activeVideoFilePath;

function playVideo(obj, filePath, eventID, videoID, videoTitle, autoPlay){

	activeVideoFilePath = filePath;
	activeEventID = eventID;
	activeVideoID = videoID;
	
	playerMode = (filePath == null) ? "live" : "vod";
	autoPlay = ((autoPlay == true) ? "1" : "0");
	
	if(obj == null || obj == undefined){
		//phase is a global js variable
		if(phase == 5){
			//archive video
			obj = $("#sidebar .angle-button").get(0);
		}else{
			obj = $("#angles #0").get(0);
		}
	}

	$(".angle-button").css("background-image","url('"+cdn_uri+"/img/angle-off.jpg')");
	$(".angle-button .led").css("background-position","0");
	$(".angle-button .details").css("color","#717171");
	$(".angle-button .details h2").css("color","#717171");
	
	if(obj != null && obj != undefined){
		$(obj).css("background-image","url('"+cdn_uri+"/img/angle-on.jpg')");
		$("#"+obj.id+" .led").css("background-position","-17px");
		$("#"+obj.id+" .details h2").css("color","#fab021");
	}

	

	var getFlashHTML = '<a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a>';

	$("#playerWrapper").html('<div id="playerContainer">' + getFlashHTML + '</div>');

	var flashvars = {};
	flashvars.i = activeVideoID;
	flashvars.e = activeEventID;
	flashvars.o = network;
	flashvars.c = (playerMode == "live") ? activeCameraID : 0;
	flashvars.t = ((playerMode == 'live') ? 'ev' : 'vi');
	flashvars.a = autoPlay;
	flashvars.ts = timeStamp;
	flashvars.n = "nowlive";
	var params = {};
	params.allowfullscreen = "true";
	params.allowscriptaccess = "always";
	var attributes = {};
	attributes.id = "player";
	swfobject.embedSWF(playerFilePath, "playerContainer", "100%", "100%", "10.0.0", false, flashvars, params, attributes, checkFlashPlayerUsed);
	
}

function checkFlashPlayerUsed(e){

	/* EVENT OBJECT */
	//e.success = true or false
	//e.id = swf id
	//e.ref = html object reference (undefined when success == false)
	
	flashPlayerUsed = e.success;

	if(flashPlayerUsed == false){

		gaPlayerType = 'html5';
	
		$("#angles > .angle-button").hide();

		var videoTagHTML = '';
		var streamURL;
		var ext;
		var currentStreamIndex = 0;

		if(activeVideoFilePath == null){
			videoTagHTML = '<video src="' + video["file"] + '" width="100%" height="100%" preload="auto" controls="controls" x-webkit-airplay="allow">';
		}else{
			videoTagHTML = '<video width="100%" height="100%" preload="auto" controls="controls" x-webkit-airplay="allow">';
			videoTagHTML += ('<source src="' + activeVideoFilePath + '" type="video/mp4" />');
		}
		
		videoTagHTML += 'Sorry, your browser does not support any of the available video formats.';
		videoTagHTML += '</video>';
		
		$("#playerWrapper").html(videoTagHTML);

		var videoHTMLElement = $("#playerWrapper video").get(0);
		if(videoHTMLElement){
			videoHTMLElement.addEventListener("loadstart", onHTML5LoadStart, false);
			videoHTMLElement.addEventListener("play", onHTML5VideoPlaybackPlay, false);
			videoHTMLElement.addEventListener("pause", onHTML5VideoPlaybackPause, false);
			videoHTMLElement.addEventListener("ended", onHTML5VideoPlaybackComplete, false);
		}
	}
}

/*
returns a virtual page name constructed as follows:

1)  '/player/' 
2)  5 digit or longer event id, prefixed by zeros
3)  '/live/' or '/vod/'
4)  10 digit 'vod id' or 10 digit 'camera id'
5)  '/' symbol
6)  origin string (fb,fbfan,nowlive,fb_share,fbfan_share,nowlive_share,etc)
7)  '/' symbol
8)  uniqueid for player embed, getTime() from javascript
9) '/GA_PLAYER_TYPE/'
10) vPageSuffix
11) if there is supplementary page data, there will be a '?' symbol, followed by url query string

public static const GA_VPAGE_LOAD:String = 'player-load';
public static const GA_VPAGE_RESUME:String = 'player-controls-resume';
public static const GA_VPAGE_PAUSE:String = 'player-controls-pause';
public static const GA_VPAGE_SEEK:String = 'player-controls-seek';
public static const GA_VPAGE_PLAYBACK_START:String = 'player-playback-start';
public static const GA_VPAGE_PLAYBACK_COMPLETE:String = 'player-playback-complete';
	
*/

function googleTrackPageview(vPageSuffix){
	_gaq.push(['_trackPageview', '/player/' + padWithZero(activeEventID, 5) + '/' + playerMode + '/' + padWithZero(((playerMode == 'live') ? activeCameraID : activeVideoID), 10) + '/' + iframeID + '/' + timeStamp + '/' + gaPlayerType + '/' + vPageSuffix]);
}

function padWithZero(str, num) {
	while(str.length < num) {
		str = '0' + str;
	}
	return str;
}

function onHTML5LoadStart(){
	googleTrackPageview('player-load');
}

function onHTML5VideoPlaybackPlay(){
	googleTrackPageview('player-playback-start');
}

function onHTML5VideoPlaybackPause(){
	googleTrackPageview('player-playback-pause');
}

function onHTML5VideoPlaybackComplete(){
	googleTrackPageview('player-playback-complete');
}

function changeCamera(obj,cameraNum){
	
	$(".angle-button").css("background-image","url('"+cdn_uri+"/img/angle-off.jpg')");
	$(".angle-button .led").css("background-position","0 0");
	$(".angle-button .details").css("color","#717171");
	$(".angle-button .details h2").css("color","#717171");
	$(obj).css("background-image","url('"+cdn_uri+"/img/angle-on.jpg')");
	$("#"+obj.id+" .led").css("background-position","-17px");
	$("#"+obj.id+" .details h2").css("color","#fab021");

	if(flashPlayerUsed == true){
		activeCameraID = parseInt(obj.id);
		if(isNaN(activeCameraID)) activeCameraID = 0;
		$("#player").get(0).send({name:'change',value:activeCameraID});
	}
}		
