/*
Copyright © Interactive Services 2013 - 2015 All Rights Reserved.  
No part of this code may be used, with or without modification, without prior consent from Interactive Services. 
Version 2.1, 01 March 2015
*/

//var videoWidth = "100%";
//var videoHeight = 500;
var videoFinished = false;
var vObj;
var videoPlaying = false;
var vidObjArray = new Array();

function playVideo(dest, target) {
	if(audioAsked!=1){ 
		$("#mediaLock").removeClass("invisible"); 
		return false;
	}else{
		for(var i=0; i<vidObjArray.length; i++){
			vidObjArray[i].pause();
		}
		vidObjArray = [];
	}

	/*for(var i=0; i<vidObjArray.length; i++){
		vidObjArray[i].pause();
	}*/

	if(target == undefined || target.toString().toLowerCase()=="overlay"){		
		$("#videoBlackout").css({display: "block"});
		$("#videoOverlay").removeClass("hidden");
		run_gs_animation($("#videoOverlay"), "gs_pop", "vid_open");
		target = $('#vid_overlay');
	}

	if(target.hasClass("localVideo")){
		var video_src = "course/assets/"+currentPage.src.split(".html")[0]+"/"+dest;
	}else{
		var video_src = videoStreamLocation + videoStreamFolder + dest;
	}
	var video_sources = [{src:video_src,type:'video/mp4'},{src:video_src.replace('mp4', 'webm'),type:'video/webm'}];

	getDeviceInfo();
   	var browserVer = jscd.browserVersion.substring(0, jscd.browserVersion.indexOf('.'));

	//enter fullscreen
    MediaElementPlayer.prototype.enterFullScreen_org = MediaElementPlayer.prototype.enterFullScreen;
    MediaElementPlayer.prototype.enterFullScreen = function() {
    	if (jscd.browser == "Microsoft Internet Explorer") {
    		$(".top_nav_bar, .bottom_nav_bar").hide();
    	} else if (jscd.os == "Android" && jscd.browser == "Chrome" && browserVer >= 33) {
			 $(".top_nav_bar, .bottom_nav_bar").hide();
		}
        this.enterFullScreen_org();
    }
    //exit fullscreen
    MediaElementPlayer.prototype.exitFullScreen_org = MediaElementPlayer.prototype.exitFullScreen;
    MediaElementPlayer.prototype.exitFullScreen = function() {
    	if(!onPhoneCourse){
        $(".top_nav_bar, .bottom_nav_bar").show();
        this.exitFullScreen_org();
      }
    }
	
	if(target.attr("vidRef") == undefined || target.toString().toLowerCase()=="overlay"){

		target.attr("vidRef", "");

		var videoObj = new MediaElementPlayer(target, {
				success: function(video_player) {
					
					vObj = video_player
					$(".mejs-overlay-button").hide();
					$("<div></div>").appendTo(".mejs-mediaelement").addClass("videoPlaceholder").css({
						position: "absolute",
						width: "100%",
						height: "100%",
						backgroundColor: "#000"
					}).insertBefore(".mejs-mediaelement video");


					video_player.pause();
					video_player.setSrc(video_sources);
					video_player.load();
					video_player.play();
					video_player.addEventListener('timeupdate', function(e) { 
						   
						if(video_player.currentTime > 0.2) {
							$(".videoPlaceholder").remove();
						}
					}, false);
					
					video_player.addEventListener('canplay', function() {
						if($('.audioBtn').hasClass('paused')){
							vObj.setVolume(0); 
						}else{
							vObj.setVolume(1);  
						}
          }, false);
					
					if ($(".mejs-video").hasClass("controlnav")) $(".mejs-video").removeClass("controlnav");
					video_player.addEventListener('ended', function(e) {     
						onVideoFinish();
						if(target == $('#vid_overlay')) {
							$("#closeX").trigger(tEvent);
						}
					 }, false);

			    	if (jscd.os == "Android" && jscd.browser == "Chrome" && browserVer < 33) {
			    		$(".mejs-fullscreen-button").remove();
			    	}
				}
			}
		);

		vidObjArray.push(videoObj)
		target.attr("vidRef", vidObjArray.length-1);

	} else {
		
		var existingPlayer = parseInt(target.attr("vidRef"));
		if(isIE8) {
			vidObjArray[existingPlayer].setSrc(video_sources);
			vidObjArray[existingPlayer].load();
		}
		vidObjArray[existingPlayer].play();

	}
};


function playVimeoVideo(dest) {
	$("#videoBlackout").addClass("blackout");	
	$("#videoBlackout").show();
	//html5video=false;
	$("video").hide();
	$("#videoFrame").show();
	$("#videoFrame").attr("src",dest);
	$("#videoOverlay").removeClass("hidden");
	videoPlaying = true;
}

function onVideoFinish() {
	if ($("#wrapper").data("screen_type") != undefined && $("#wrapper").data("screen_type") == "video") {
		$("#vimeoplayer").removeClass("controlnav");
		$("#vid1").removeClass("controlnav");
	}else if ($("#wrapper").data("screen_type") != undefined && $("#wrapper").data("screen_type") == "accordion" && $(".currentSlide").parent().data("video") !="vimeo") {
		$(".currentSlide").removeClass("controlnav");
		$("#cnVideo").removeClass("controlnav");
		$(".currentSlide").addClass("visited")
	}else if ($("#wrapper").data("screen_type") != undefined && $("#wrapper").data("screen_type") == "accordion" && $(".currentSlide").parent().data("video") =="vimeo") {
		$(".vimeoVideo").removeClass("controlnav");
		$(".currentSlide").removeClass("controlnav");
		$(".currentSlide").addClass("visited")
	}
	checkRestrictedNav();
}

function closeVideo() {
	for(var i=0; i<vidObjArray.length; i++){
		vidObjArray[i].pause();
	}
	if(!$("#videoOverlay").hasClass("hidden")) {
		if (isIE8){
			$("#videoOverlay").addClass("hidden");
		}else{
			run_gs_animation($("#videoOverlay"), "gs_hide", "vid_close");
		}
	}
	return true;
}

function clearVideos() {
	/*for(var i=0; i<vidObjArray.length; i++){
		vidObjArray[i].pause();
		vidObjArray[i].remove();
	}*/
	vidObjArray = [];
}

//++++++++++++++++++++++++++++++ toggleVideo() ++++++++++++++++++++++++++++++++++++++++++
// Pause or unPause the different video objects if a component is opened eg menu
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function toggleVideo(state){
	
	//If a MediaElementPlayer video is playing.
	if(vidObjArray[0] != undefined) {
		var isPaused = vObj.paused;
		
		if(state=="pause" && isPaused==true){
			$('#wrapper').addClass("alreadyPaused"); //wrapper is just a convienient place to store the flag
		}else if(state=="pause" && isPaused==false){
			vObj.pause(); 
			$('#wrapper').removeClass("alreadyPaused")
		}else if(state=="unpause" && isPaused==true && !$('#wrapper').hasClass("alreadyPaused")){
			vObj.play(); 
			$('#wrapper').removeClass("alreadyPaused")
		}
	}
	
	//If a Vimeo video is playing.
	if($('.vimeoVideo').length > 0) {
		$f($('.vimeoVideo')[0]).api('paused', function(isPaused) {
			if(state=="pause" && isPaused==true){
				$('.vimeoVideo').addClass("alreadyPaused")
			}else if(state=="pause" && isPaused==false){
				$f($('.vimeoVideo')[0]).api('pause'); 
				$('.vimeoVideo').removeClass("alreadyPaused")
			}else if(state=="unpause" && isPaused==true && !$('.vimeoVideo').hasClass("alreadyPaused")){
				$f($('.vimeoVideo')[0]).api('play'); 
				$('.vimeoVideo').removeClass("alreadyPaused")
			}
		});
	}
}