/*
 * @desc This file contains the javascript functions that get called in triggers throughout the player either by player events or user decisions.
 *
 */

/*
 * global variable for the window stores the state and other information
 */
window.agree = false;
window.code = 0;
window.pingAt = 5; // start ping at 5 seconds 
window.stage = 'start';
window.intervalId = 0;
/*
 * @desc function called when code is entered
 */
 
function setCode() {
	
	
	window.code = $('.codeInput').val();
	if(!window.agree){
		// reset code
		$('.codeInput').empty();
	}
	
	$.post('video_control/validate_code.php', { code: window.code }, 
	function(data) {
		// process response
		var response = $.parseJSON(data);
		window.stage = response.stage;
		if(response.status == 'valid'){
			// unload code popup
			$('#agms_code_popup a.close').click();
			$('#agms_disclaimer_popup a.close').click();
			
			if(window.agree){
				playVideo(response.access_key, window.code, response.byte);	
			}
			else {
				showDisclaimer();
			}
			
			// check for byte
			if(response.byte != 0){
				window.pingAt = response.time;
			}
		}
		else {
			$("#agms_code_popup").fadeOut("100", function(){
			$('#belowCodeInput').html(response.status);
			$('#agms_code_popup').css('background-image', 'url(images/bg-03.jpg)');
			$("#agms_code_popup").fadeIn("100");
		});
		}
	});
	

} // setCode()


// function called when link is pressed
function requestCode() {
	// close open pop up
	$('#agms_code_popup a.close').click();
	
	// hack to scroll the pop up
	$('.agms_popup_block').css('position','absolute');			
	$('.agms_popup_block').css('top','10%');


	// load page content in div
	$('#requestCode').load('video_content/request_code.php');
	
	// load video pop up
		//Fade in the Popup and add close button
		$('#agms_request_code_popup').fadeIn().css({ 'width': Number( 640 ) }).prepend('<a href="#" class="close"><img src="images/fancy_close.png" class="btn_close" title="Close Window" alt="Close" /></a>');
				
		//Define margin for center alignment (vertical + horizontal) - we add 40 to the height/width to accomodate for the padding + border width defined in the css
		//var popMargTop = ($('#agms_request_code_popup').height() + 20) / 2;
		//var popMargLeft = ($('#agms_request_code_popup').width() + 40) / 2;
		var popMargTop = 190;
		var popMargLeft= 330;
				
		//Apply Margin to Popup
		$('#agms_request_code_popup').css({ 
					'margin-top' : -popMargTop,
					'margin-left' : -popMargLeft
		});
				
		//Fade in Background
		$('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
		$('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer 

}

// function called on each time to ping Video
function pingVideo(pos) {		
	
	// ping called every 5 seconds
	if(pos > window.pingAt){
			// debug
			//alert('ping');
			// increment ping position
			window.pingAt = +(window.pingAt) + 5*1;
			//send request
			$.post('http://maanas.agms.co/servers/opportunity_video/ping.php', { code: window.code, position: pos});
			//var uri = 'http://maanas.agms.co/servers/opportunity_video/ping.php?code=' + window.code + '&position=' + pos;
			//$.get(uri);
	} 
	
	
	return false;
} // pingVideo()



// function called when code is validated
function playVideo(path, file, start) {		

		// unload disclaimer pop up	
		$('#agms_disclaimer_popup a.close').click();
		
		// reset all variables
		window.pingAt = 5; // start ping at 5 seconds 
		
		if (mobileCheckIsMobile()) {
			jwplayer("flashVideo").setup({
				controlbar: "false",
				height: 360,
				width: 640,
				file: "http://stream.agms.co/servers/free_video/11.mp4",
				'bufferlength': '1',
				autostart: true,
				
				'logo.file': 'http://maanas.agms.co/servers/opportunity_video/' + escape(path) + '/logo.png',
				'logo.hide':false,
			});
		}
		else {
		
			if(start == 'null' || start == 0){
				var source = escape(path + '/' + file + '.flv');
			}
			else {
				var source = escape(path + '/' + file + '.flv?start=' + start);
			}
			//load flash player and events
			jwplayer("flashVideo").setup({
				'height': 360, 
				'width': 640,
				'autostart': true,
				'controlbar':false,
				'icons':false,
				flashplayer: 'http://maanas.agms.co//players/jwplayer/player.swf',
				'logo.file': 'http://maanas.agms.co/servers/opportunity_video/' + escape(path) + '/logo.png',
				'logo.hide':false,
				file:'http://maanas.agms.co/servers/opportunity_video/' + source, 
				provider:'http',
				stretching: 'exactfit',		// some hack to ensure video show fully in restricted play back
			});
		}
		
		// ping code function only in flv video as yet

		if(start == 'null' || start == 0){
			jwplayer().onTime(function(event) { 
				pingVideo(event.position, event.duration); 
			});	
			
		}
		else {
			// ping stop when player is buffering or paused
			jwplayer().onPause(function(event) { 
				clearInterval(window.intervalId);
			});	
	
			jwplayer().onBuffer(function(event) { 
				clearInterval(window.intervalId);
			});	
			
			// ping function when player resume in play mode
			jwplayer().onPlay(function(event) { 
				pingTime = +(window.pingAt)+5*1;
				window.intervalId = setInterval (function () {pingVideo(pingTime);}, 5000 );
			});	
		}		
			
		
		jwplayer().onComplete(function (){
			//clear interval
			clearInterval(window.intervalId);
			// update the ping for onComplete to disable video
			$.post('http://maanas.agms.co/servers/opportunity_video/play_over.php', { code: window.code});

			//display popup
			showFinish();
			
		});
			
				
		// load video pop up
		//Fade in the Popup and add close button
		$('#agms_video_popup').fadeIn().css({ 'width': Number( 640 ) }).prepend('<a href="#" class="close"><img src="images/fancy_close.png" class="btn_close" title="Close Window" alt="Close" /></a>');
				
		//Define margin for center alignment (vertical + horizontal) - we add 40 to the height/width to accomodate for the padding + border width defined in the css
		var popMargTop = ($('#agms_video_popup').height() + 40) / 2;
		var popMargLeft = ($('#agms_video_popup').width() + 40) / 2;
				
		//Apply Margin to Popup
		$('#agms_video_popup').css({ 
					'margin-top' : -popMargTop,
					'margin-left' : -popMargLeft
		});
				
		//Fade in Background
		$('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
		$('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer 
		
		return false;

} // playVideo()


// function called when link is pressed
function getCode() {
		//Fade in the Popup and add close button
		$('#agms_code_popup').fadeIn().css({ 'width': Number( 640 ) }).prepend('<a href="#" class="close"><img src="images/fancy_close.png" class="btn_close" title="Close Window" alt="Close" /></a>');
				
		//Define margin for center alignment (vertical + horizontal) - we add 40 to the height/width to accomodate for the padding + border width defined in the css
		var popMargTop = ($('#agms_code_popup').height() + 40) / 2;
		var popMargLeft = ($('#agms_code_popup').width() + 40) / 2;
				
		//Apply Margin to Popup
		$('#agms_code_popup').css({ 
					'margin-top' : -popMargTop,
					'margin-left' : -popMargLeft
		});
				
		//Fade in Background
		$('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
		$('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer 
} //getCode					


// function called when link is pressed
function showDisclaimer() {
		
		//Fade in the Popup and add close button
		$('#agms_disclaimer_popup').fadeIn().css({ 'width': Number( 640 ) }).prepend('<a href="#" class="close"><img src="images/fancy_close.png" class="btn_close" title="Close Window" alt="Close" /></a>');
				
		//Define margin for center alignment (vertical + horizontal) - we add 40 to the height/width to accomodate for the padding + border width defined in the css
		var popMargTop = ($('#agms_disclaimer_popup').height() + 40) / 2;
		var popMargLeft = ($('#agms_disclaimer_popup').width() + 40) / 2;
				
		//Apply Margin to Popup
		$('#agms_disclaimer_popup').css({ 
					'margin-top' : -popMargTop,
					'margin-left' : -popMargLeft
		});
				
		//Fade in Background
		$('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
		$('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer 
		
} //showDisclaimer					


//Close Code Popups and Fade Layer
$('#agms_disclaimer_popup a.close').live('click', function() { //When clicking on the close or fade layer...
  	$('#fade , .agms_popup_block').fadeOut(function() {
			//$('#fade, a.close').remove();  // disabled, it removed the close button alltogather 
		}); //fade them both out
		return false;
});

//Close Code Popups and Fade Layer
$('#agms_code_popup a.close').live('click', function() { //When clicking on the close or fade layer...
  	$('#fade , .agms_popup_block').fadeOut(function() {
			//$('#fade, a.close').remove();  // disabled, it removed the close button alltogather 
		}); //fade them both out
		return false;
});

//Close Request Code Popups and Fade Layer
$('#agms_request_code_popup a.close').live('click', function() { //When clicking on the close or fade layer...
		$('#requestCode').empty();
  	$('#fade , .agms_popup_block').fadeOut(function() {
			$('#fade, a.close').remove();  
		}); //fade them both out
		
		// set back the position to fixed i.e. return to normal
		$('.agms_popup_block').css('position','fixed');
		$('.agms_popup_block').css('top','50%');

		return false;
});

//Close Video Popups and Fade Layer
$('#agms_video_popup a.close').live('click', function() { //When clicking on the close or fade layer...
  	// stop jwplayer
  	jwplayer().stop();
  	$.post('http://maanas.agms.co/servers/opportunity_video/logger.php', { code: window.code, log: 'player window closed'});

  	$('#fade , .agms_popup_block').fadeOut(function() {
			//$('#fade, a.close').remove();  
		}); //fade them both out
		
		window.pingAt = 5; // start ping at 5 seconds 
		
		$('#flashVideo').empty();
		$('#canvas').empty();

		//clear interval
		clearInterval(window.intervalId);
		
		return false;
});
	



// function count down timer
function disclaimerExit() {
	
	$.post('http://maanas.agms.co/servers/opportunity_video/logger.php', { code: window.code, log: 'disagree'});
	//disable video
	$.post('http://maanas.agms.co/servers/opportunity_video/play_over.php', { code: window.code});
	$('#agms_disclaimer_popup a.close').click();
			
}

// function count down timer
function disclaimerAgree() {
	window.agree = true;
	$.post('http://maanas.agms.co/servers/opportunity_video/logger.php', { code: window.code, log: 'agree'});
	setCode();
}



// function called when link is pressed
function showFinish() {
		
		$('#agms_video_popup a.close').click();
		
		//Fade in the Popup and add close button
		$('#agms_finish_popup').fadeIn().css({ 'width': Number( 640 ) }).prepend('<a href="#" class="close"><img src="images/fancy_close.png" class="btn_close" title="Close Window" alt="Close" /></a>');
				
		//Define margin for center alignment (vertical + horizontal) - we add 40 to the height/width to accomodate for the padding + border width defined in the css
		var popMargTop = ($('#agms_finish_popup').height() + 40) / 2;
		var popMargLeft = ($('#agms_finish_popup').width() + 40) / 2;
				
		//Apply Margin to Popup
		$('#agms_finish_popup').css({ 
					'margin-top' : -popMargTop,
					'margin-left' : -popMargLeft
		});
				
		//Fade in Background
		$('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
		$('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer 
		
} //showDisclaimer					


//Close Code Popups and Fade Layer
$('#agms_finish_popup a.close').live('click', function() { //When clicking on the close or fade layer...
  	$('#fade , .agms_popup_block').fadeOut(function() {
			//$('#fade, a.close').remove();  // disabled, it removed the close button alltogather 
		}); //fade them both out
		return false;
});


// function called when code is validated
function playFreeVideo(medid) {		
		if (mobileCheckIsMobile()) {
			jwplayer("flashVideo").setup({
				controlbar: "false",
				height: 360,
				width: 640,
				file: "http://stream.agms.co/servers/free_video/11.mp",
				'bufferlength': '1',
				autostart: true,
				
				'logo.file': 'http://maanas.agms.co/servers/free_video/logo.png',
				'logo.hide':false,
			});
		}
		else {
		
			//load flash player and events
			jwplayer("flashVideo").setup({
				'height': 360, 
				'width': 640,
				'autostart': true,
				'controlbar':false,
				'icons':false,
				flashplayer: 'http://maanas.agms.co//players/jwplayer/player.swf',
				'logo.file': 'http://maanas.agms.co/servers/free_video/logo.png',
				'logo.hide':false,
				file:'http://maanas.agms.co/servers/free_video/' + medid + '.flv', 
				provider:'http',
				stretching: 'exactfit',		// some hack to ensure video show fully in restricted play back
			});
		}
		
				
		// load video pop up
		//Fade in the Popup and add close button
		$('#agms_video_popup').fadeIn().css({ 'width': Number( 640 ) }).prepend('<a href="#" class="close"><img src="images/fancy_close.png" class="btn_close" title="Close Window" alt="Close" /></a>');
				
		//Define margin for center alignment (vertical + horizontal) - we add 40 to the height/width to accomodate for the padding + border width defined in the css
		var popMargTop = ($('#agms_video_popup').height() + 40) / 2;
		var popMargLeft = ($('#agms_video_popup').width() + 40) / 2;
				
		//Apply Margin to Popup
		$('#agms_video_popup').css({ 
					'margin-top' : -popMargTop,
					'margin-left' : -popMargLeft
		});
				
		//Fade in Background
		$('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
		$('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer 
		
		return false;

} // playFreeVideo()


