/* functions to control audio playback */

/* check audio to play on intro when navigating from menu or nav */
function checkTopicAudio(id){	
	audioSection = null;
	//alert('id = '+ id)
	document.currentSection = Number(id.split("_")[1]);
	//alert('document.currentSection = ' + document.currentSection);	
	if(document.intArr[document.currentSection - 1] == 0){
		document.intComplete = true;
	}	
	if(document.audioArr[document.currentSection - 1] == 1){
		playAudio(document.currentSection); /* play audio */
		//$(".audioControlHolder").css("visibility","visible"); /* display audio controls */		
	}else{
		$(".audioControlHolder").css("visibility","hidden"); /* hide audio controls */
		document.audioComplete = true;
	}	
	if(document.trackingArr[document.currentSection - 1] == 1){
		document.intComplete = true;
		document.audioComplete = true;
	}
	isPageComplete(document.currentSection);
	if(isWeb){
		//checkforNextSectionAudio();
	}
}

/* play audio called from scroll_activity.js */
function playAudio(audio){	
	audioPlaying = audio;
	//alert(audio);
	/* only play audio if section is visible in the viewport */
	if($('#sec_'+audio).isInViewport()){	
		if(isWeb){		
			if(audio != audioSection){				
				audio_player.pause();
				audio_player.setSrc('audio/'+document.audioSrcArr[audio-1]+'.mp3');
				audio_player.play();				
				audioSection = audio;
				$(".audioControlHolder").css("visibility","visible"); /* display audio controls */
			}
		}else{		
		}
	}else{
		$(".audioControlHolder").css("visibility","hidden"); /* hide audio controls */
	}
}

function playDeviceAudio(audio){
	/* only play audio if section is visible in the viewport */	
	//alert('audio = ' + audio + ' viewport = ' +$('#sec_'+audio).isInViewport());
	//if($('#sec_'+audio).isInViewport()){	
		if(audio != null){		
			if(audio != audioSection){
				//alert('audio = ' + document.audioSrcArr[audio-1]);
				audio_player.pause();
				audio_player.setSrc('audio/'+document.audioSrcArr[audio-1]+'.mp3');
				audio_player.play();				
				audioSection = audio;
				$(".audioControlHolder").css("visibility","visible"); /* display audio controls */
			}
		}
	//}else{
	//	$(".audioControlHolder").css("visibility","hidden"); /* hide audio controls */
	//}
}

var audioPlaying = null;
var audioSection = null;
var dragging = false;
/* touch functionality to trigger audio in mobile devices */
$("body").bind("touchmove", function(){
      dragging = true;
});

$("body").bind("touchend", function(){
      /*if (dragging){
          //return;
		  //alert('drag');		  
		  playDeviceAudio(audioPlaying);		  
	  }else{
		  return;
		  //alert('tap');
	  }
      // wasn't a drag, just a tap
      // more code here*/
	  //alert('touch end ' + audioPlaying);
	  playDeviceAudio(audioPlaying);
});

$("body").bind("touchstart", function(){
    dragging = false;
});

/* set up mediaelement.js audio player */
var audio_player;
function setUpAudio(){	
	/* set up transcript button */
	MediaElementPlayer.prototype.buildtranscript = function(player, controls, layers, media) {
		var
			// create the loop button
			transcript =
			//$('<div class="mejs-button mejs-transcript-button ' + ((player.options.loop) ? 'mejs-transcript-on' : 'mejs-transcript-off') + '">' +
			$('<div class="mejs-button mejs-transcript-button mejs-transcript-on">' +
				'<span>Transcript</span>' +
			'</div>')
			// append it to the toolbar
			.appendTo(controls)
			// add a click toggle event
			.click(function() {
				/* show transcript here */
			});
	}
	audio_player = new MediaElementPlayer('#audio_player',{
		// if the <video width> is not specified, this is the default
		defaultVideoWidth: 480,
		// if the <video height> is not specified, this is the default
		defaultVideoHeight: 270,
		// if set, overrides <video width>
		videoWidth: -1,
		// if set, overrides <video height>
		videoHeight: -1,
		// width of audio player
		audioWidth: 300,
		// height of audio player
		audioHeight: 30,
		// initial volume when the player starts
		startVolume: 0.5,
		// useful for <audio> player loops
		loop: false,
		// enables Flash and Silverlight to resize to content size
		enableAutosize: true,
		// the order of controls you want on the control bar (and other plugins below)
		features: ['playpause','progress','current','duration','tracks','fullscreen','volume'],
		// Hide controls when playing and mouse is not over the video
		alwaysShowControls: true,
		// force iPad's native controls
		iPadUseNativeControls: false,
		// force iPhone's native controls
		iPhoneUseNativeControls: false, 
		// force Android's native controls
		AndroidUseNativeControls: false,
		// forces the hour marker (##:00:00)
		alwaysShowHours: false,
		// show framecount in timecode (##:00:00:00)
		showTimecodeFrameCount: false,
		// used when showTimecodeFrameCount is set to true
		framesPerSecond: 25,
		// turns keyboard support on and off for this instance
		enableKeyboard: false,
		// when this player starts, it will pause other players
		pauseOtherPlayers: true,
		// path to Flash and Silverlight plugins
		pluginPath: '',
		// name of flash file
		flashName: 'mejs/flashmediaelement.swf',
		// name of silverlight file
		silverlightName: 'mejs/silverlightmediaelement.xap',		
		// array of keyboard commands
		keyActions: [],		
		success: function (mediaElement, domObject) {	 
		// add ended event listener
		mediaElement.addEventListener('ended', function(e) {			 
			audioComplete();			 
			}, false);			 
		},
		// fires when a problem is detected
		error: function () { 		 
		}	 
	});
}

/* audioComplete function called when audio finishes and displays the next section */
function audioComplete(){
	//var nextSection = Number(audioPlaying) + 1;
	//$('#sec_'+ nextSection).css('display','block');
	document.intComplete = true;
	document.audioComplete = true; /* mark audio complete */	
	//isPageComplete(document.currentSection); /* check if the page is complete */
	isPageComplete(audioSection);
	//audioSection = null;
	audioPlaying = null;
	$(".audioControlHolder").css("visibility","hidden"); /* hide audio controls */
}

/* function to check element is in the viewport */
$.fn.isInViewport = function() {
    var elementTop = $(this).offset().top;
    var elementBottom = elementTop + $(this).outerHeight();
    var viewportTop = $(window).scrollTop();
    var viewportBottom = viewportTop + $(window).height();
    return elementBottom > viewportTop && elementTop < viewportBottom;
};

/* not currently used */
/*function checkforNextSectionAudio(){
	//alert($(window).height());	
	if($(window).height() > 768){		
		if(document.audioArr[document.currentSection] == 1){			
			document.intComplete = true;
			var nextSection = document.currentSection + 1;
			document.currentSection = nextSection;
			playAudio(document.currentSection);
		}		
	}
}*/

function deviceAudio(){
	/*audio_player.pause();
	audio_player.setSrc('audio/test.mp3');
	audio_player.play();*/
}