// source: http://support.brightcove.com/en/docs/preparing-player-smart-player-api

function Player(param_width, param_height, param_playerID, param_playerKey, container, loading_on, event_code)
{
	console.log(new Date().toString());
	
	this.BC_PLAYER_NAME = "myExperience";

	// Parameters to create the Object Brightcove Player 
	this.BC_PARAMS = new Array();
	this.BC_PARAMS.bgcolor = "#000000";
	this.BC_PARAMS.width = param_width;
	this.BC_PARAMS.height = param_height;
	//param_playerID="1045959074001";
	//param_playerKey="AQ~~,AAAArZFA-qk~,0pLiqvxiD1PMAk4l-PUyD5Walpc8pQCA";
	this.BC_PARAMS.playerID = param_playerID;
	this.BC_PARAMS.playerKey = param_playerKey;
	this.BC_PARAMS.videoID = "";
	this.BC_PARAMS.isVid = "true";
	this.BC_PARAMS.isUI = "true";
	this.BC_PARAMS.dynamicStreaming = "true";
	this.BC_PARAMS.wmode = "opaque";
	this.BC_PARAMS.autoStart = "true";
	this.BC_PARAMS.includeAPI = "true";
	this.BC_PARAMS.templateLoadHandler = "player.on_player_loaded";
	this.BC_PARAMS.templateReadyHandler = "player.on_player_ready";
	this.BC_PARAMS.templateErrorHandler = "player.on_error";

	// DOMElement where the player will be created
	this.BC_CONTAINER = container;
	this.BC_PLAYER = brightcove.createElement("object");
	this.BC_PLAYER.id = this.BC_PLAYER_NAME;

	this.BC_EXPERI;		// Brightcove Experience (bcExp)
	this.BC_MODVID;		// Module::Video Player  (modVP)
	this.BC_MODEXP;		// Module:Experience     (modExp)
	this.BC_MODCON;		// Module:: Content      (modCon)

	this.VIDEO_ID = 0;	// Brightcove ID
	this.VIDEO_SEEK = 0;	// Scrub a video to a certain time (in seconds)
	this.VIDEO_NAME = "";	// Name of the video
	this.VIDEO_ARTIST = "";	// Name of the Artist
	
	this.LOADING_ON = loading_on.toUpperCase();
	//console.log(event_code);
	this.CODEVE = event_code;
	
	this.TRACKLIST = new Array();
	this.TRACKLIST_ITERATOR = 0;

	this.create_player = function()
	{
		//console.log('fn(create_player)');
		
		var parameter;
		for (var i in this.BC_PARAMS) {
		     parameter = brightcove.createElement("param");
		     parameter.name = i;
		     parameter.value = this.BC_PARAMS[i];
		     $(this.BC_PLAYER).append(parameter);
		}

		brightcove.createExperience(($(this.BC_PLAYER)[0]), $(this.BC_CONTAINER)[0], true);
	}

	this.on_player_loaded = function(experienceID)
	{
		//console.log('fn(on_player_loaded)');
		
		this.BC_EXPERI = brightcove.getExperience(experienceID);
		this.BC_MODVID = this.BC_EXPERI.getModule(APIModules.VIDEO_PLAYER);
		this.BC_MODEXP = this.BC_EXPERI.getModule(APIModules.EXPERIENCE);
		this.BC_MODCON = this.BC_EXPERI.getModule(APIModules.CONTENT);

		// Add Event Handlers to the Video Player
		this.BC_MODVID.addEventListener(BCMediaEvent.BEGIN, player.on_video_begin);
		this.BC_MODVID.addEventListener(BCMediaEvent.COMPLETE, player.load_next);
		this.BC_MODVID.addEventListener(BCMediaEvent.ERROR, player.on_error);
		this.BC_MODCON.addEventListener(BCContentEvent.VIDEO_LOAD, player.on_video_load);
		
		var isAdEnabled = this.BC_MODEXP.getAdEnabled();

		if (isAdEnabled) {
			//console.log('is_Ad_Enabled');
			this.BC_MODADS.addEventListener(BCAdvertisingEvent.AD_POSTROLLS_COMPLETE, player.load_next);
		} else {
			//console.log('is_NOT_Ad_Enabled');
			this.BC_MODVID.addEventListener(BCMediaEvent.COMPLETE, player.load_next);
		}
	}

	this.on_player_ready = function()
	{
		//console.log('fn(on_player_ready)');
		
		switch(player.LOADING_ON)
		{
			case 'ARCHIVE':
				//console.log('ARCHIVE has been selected');
				player.load_next();
			break;
			case 'PARTY':
				//console.log('PARTY has been selected');
				player.load_next();
			break;
			case 'STAGE':
				//console.log('STAGE has been selected');
				player.load_next();
			break;
		}
		
	}

	this.on_video_load = function()
	{
		//console.log('fn(on_video_load)');
		switch(player.LOADING_ON)
		{
			case 'PARTY':
				//console.log('PARTY has been selected');
				setShare(player.VIDEO_ID,
						 player.VIDEO_NAME,
						 player.VIDEO_ARTIST);
				showStageTitleOnLoad();
			break;
			case 'STAGE':
				//console.log('STAGE has been selected');
				setShare(player.VIDEO_ID,
						 player.VIDEO_NAME,
						 player.VIDEO_ARTIST);
				showStageTitleOnLoad();
			break;
		}
	}

	this.on_video_begin = function()
	{
		//console.log('fn(on_video_begin)');

		switch(player.LOADING_ON)
		{
			case 'ARCHIVE':
				//console.log('ARCHIVE has been selected');

				//console.log('player.TRACKLIST_ITERATOR: ' + player.TRACKLIST_ITERATOR);
				//console.log('player.TRACKLIST.length: ' + player.TRACKLIST.length);
				if(player.TRACKLIST_ITERATOR >= player.TRACKLIST.length) {
					player.TRACKLIST_ITERATOR = 0;
				}
				
				$.ajax({
					url: "index.php?r=archive/shared",
					data: {'video_id':player.TRACKLIST[player.TRACKLIST_ITERATOR]},
					type: 'post',
					success: function(data){
						video = JSON.parse(data);
						setShare(video[0],video[1],video[2]);
						//console.log(video[3]);

						// Update Info Tab
						var html  = "<p class='gigInfoTabHeaders black50'>Artist:</p>";
						    html += "<h5>"+video[2]+"</h5>";
						    html += "<p class='gigInfoTabHeaders black50' style='margin-top: 8px'>Gig:</p>";
						    html += "<h5>"+video[4]+"</h5>";
						    html += "<br />";
						    html += "<p style='margin-left: 8px'>"+video[3]+"</p>";
						$('div#gigInformationTab').html(html);
					
						//player.TRACKLIST_ITERATOR++;
					}
				});
			break;
			case 'PARTY':
				//console.log('PARTY has been selected');
				
				//console.log(player.VIDEO_SEEK);
		
				player.BC_MODVID.seek(player.VIDEO_SEEK);
				player.VIDEO_SEEK = 0;
			break;
			case 'STAGE':
				//console.log('STAGE has been selected');
				
				//console.log(player.VIDEO_SEEK);
		
				player.BC_MODVID.seek(player.VIDEO_SEEK);
				player.VIDEO_SEEK = 0;
			break;
		}
	}

	this.load_next = function()
	{
		//console.log('fn(load_next)');
		
		switch(player.LOADING_ON)
		{
			case 'ARCHIVE':
				//console.log('ARCHIVE has been selected');
				
				player.TRACKLIST_ITERATOR++;
			
				player.play_video(
						player.TRACKLIST[player.TRACKLIST_ITERATOR],
						$('.gigTraclistLine:eq('+(player.TRACKLIST_ITERATOR)+')')
				);
			break;
			case 'DATE':
				//console.log('DATE has been selected');
			break;
			case 'PARTY':
				//console.log('PARTY has been selected');
				//console.log(this.CODEVE);
				//console.log(player.CODEVE);
				$.ajax(
				{
					url: "index.php/?r=party/partyroom",
					data: {'next':'next','codeve':player.CODEVE},
					type: 'post',
					success: function(data){
						//console.log(data);
						//console.log(typeof data);
						video = JSON.parse(data);
				
						player.VIDEO_ID = video[0];
						player.VIDEO_SEEK = parseInt(video[1]);
						player.VIDEO_NAME = video[2];
						player.VIDEO_ARTIST = video[3];
				
						player.play_video(player.VIDEO_ID, null);
				
					}
				});
			break;
			case 'STAGE':
				//console.log('STAGE has been selected');
				$.ajax(
				{
					url: "index.php?r=stage/ajax",
					data: {'next':'next'},
					type: 'post',
					success: function(data){
						video = JSON.parse(data);
				
						player.VIDEO_ID = video[0];
						player.VIDEO_SEEK = parseInt(video[1]);
						player.VIDEO_NAME = video[2];
						player.VIDEO_ARTIST = video[3];
						
						//console.log('load_next - AJAX');
						//console.log(player.VIDEO_ID);
						//console.log(player.VIDEO_SEEK);
						//console.log(player.VIDEO_NAME);
						//console.log(player.VIDEO_ARTIST);
				
						player.play_video(player.VIDEO_ID, null);
				
					}
				});
				
			break;
		}
	}

	this.on_error = function(evt)
	{
		//console.log('fn(on_player_error)');
		//console.log("      [TYPE] " + evt.type);
		//console.log("[ERROR_TYPE] " + evt.errorType);
		//console.log("      [CODE] " + evt.code);
		//console.log("      [INFO] " + evt.info);
	}

	this.stop_video = function()
	{
		//console.log('fn(stop_video)');
		player.BC_MODVID.stop();
	}

	this.play_video = function(VIDEO_ID, DOMElement)
	{
		//console.log('fn(play_video)');
		
		switch(player.LOADING_ON)
		{
			case 'ARCHIVE':
				//console.log('ARCHIVE has been selected');
				
				if (!($(DOMElement).hasClass('add60'))){
				
					//console.log("OPTION 01");
					player.BC_MODVID.loadVideo(VIDEO_ID);
					player.BC_MODVID.loadVideo(VIDEO_ID);
				
					if(DOMElement != null) {
						// The followig line gets the video has been clicked on to play
						//console.log($('#gigTraclistTab li').index($(DOMElement)));
						player.TRACKLIST_ITERATOR = $('#gigTraclistTab li').index($(DOMElement));
					}

					$('#gigTraclistTab li').removeClass('add60').addClass('gigTraclistLineHover').css('color','#ffffff !important');
					$('#gigTraclistTab li:eq('+player.TRACKLIST_ITERATOR+')').removeClass('gigTraclistLineHover').addClass('add60').css('color','#000 !important');
					scrollPane();
					if ($('#playerWrapper').hasClass('bigScreenMode') == true) {
						$('.gigTRacklistPlay').removeClass('bigScreenIcon');
					}
				} else {
					//console.log("OPTION 02");
				}
			
			break;
			case 'PARTY':
				//console.log('PARTY has been selected');

				player.BC_MODVID.loadVideo(VIDEO_ID);
				//player.BC_MODVID.loadVideo(VIDEO_ID);
			break;
			case 'STAGE':
				//console.log('STAGE has been selected');
				console.log(VIDEO_ID);
				
				player.BC_MODVID.loadVideo(VIDEO_ID);
				//player.BC_MODVID.loadVideo(VIDEO_ID);
			break;
		}
	}
	
	this.clear_share = function()
	{
		//console.log('fn(clear_share)');
		//console.log(player.VIDEO_ID);
		//console.log(player.VIDEO_SEEK);
		//console.log(player.VIDEO_NAME);
		//console.log(player.VIDEO_ARTIST);
		
		player.VIDEO_ID = '';
		player.VIDEO_SEEK = '';
		player.VIDEO_NAME = '';
		player.VIDEO_ARTIST = '';
	}
}



