//Author: Joshua Cottrell
//Purpose: allow skipping to chapters of the video
//requires:
//	flash video player plugin for Wordpress (with jw player)
//	jquery
//	XML file to load videos from
//	<div id="chapters"></div>

//this should allow us to monitor whether the div id="chapters" AND the player are loaded
//	the div loads through jquery's ready function (S(function() {...
//	the player loads through the playerReady(thePlayer) and playerWait functions

function playerReady(thePlayer) {
	player = window.document[thePlayer.id];
	playerWait();
}

//set up jw player listener
function playerWait() {
	if ( ( player == null ) || ( player === undefined ) ) {
		setTimeout("playerWait()",100);
	}
	else {
		player.addModelListener("STATE", "stateListener");
		check_for_chapters();
	}
}

//jw api listener for state changes: buffering, playing, completed, etc.
function stateListener(obj) { //IDLE, BUFFERING, PLAYING, PAUSED, COMPLETED
	if ( ( obj.newstate == "COMPLETED" ) && ( player.getConfig().item < player.getPlaylist().length ) )
		player.sendEvent( 'NEXT' );
}

//expects a string of seconds, one minute and 15 seconds would be "75"
//	returns a string in "digital" clock time, one minute 15 seconds would be 1:15
function parseDuration (duration) {
	
	var time = '';
	
	if ( parseInt( duration / 3600 ) > 0 ) {
		time = parseInt( duration / 3600 ) + ":";
		duration = duration - ( parseInt( duration / 3600 ) * 3600 );
	}
	if ( parseInt( duration / 60 ) > 0 ) {
		var minutes = parseInt( duration / 60 );
		if ( ( 10 > minutes ) && ( time.length > 0 ) )//add a zero to the front only if there are hours
			time += "0";
		time += minutes + ":";
		duration = duration - ( parseInt( duration / 60 ) * 60 );
	}
	else { //no minutes means duration / 60 == 0
		if ( time.length > 0 ) //if there are hours
			time += "00:";
		else
			time += "0:";
	}
	if ( 10 > duration )
		time += "0";
	time += duration;
	
	return time;
}

//jquery to run after everything is loaded
function check_for_chapters() {
	//make sure there is only one chapters id
	if ( $('div#chapters').length > 0 ) {
		if ( $('div#chapters').html() );
		else {
			$('div#chapters').html( 'loading chapters<img style="border:0px;padding: 0px;margin: 0 0 0 4px;" border="0" src="/_img/_icons/loading.gif">' );
		
		}

		var playlist = player.getPlaylist();
		
		if ( ( playlist === undefined ) || ( playlist == null ) )
			setTimeout("check_for_chapters()",100);
		else {
		
			var add_html = '<ol>\n';
			var total_time = 0;
			
			for ( var i = 0; i < playlist.length; i++ ) {
				var duration = playlist[i].duration;
				if ( i != 0 )
					total_time += parseInt( duration );
				
				add_html += '<li><a  id="' + i + '-chapter" href="#">' + playlist[i].title + '</a> (' + parseDuration( duration ) + ')</li>' +'\n';
			}
			add_html += '</ol>\n';
			add_html += 'Total Time: ' + parseDuration( total_time ) + '<br />\n';
		
			$('div#chapters').html(add_html);
			$('div#chapters li a').click(function() {
				var video_index = parseInt( $(this).attr( 'id' ) );
			
				player.sendEvent( 'ITEM', video_index );
			
				return false;
			});
		}
	}
}

