/*
	jplayer-functions to handle all prelistenings with only one Flash-Object
	interface provided by one of many proxy-objects (.prelistening)
*/

var internal_player_id = 0;	
// in case of .prelistening nodes without id



var jPlayerObject = null;
var proxyObject = null


function start_prelistening() 
{
	if( jPlayerObject == null ) {	// we were called before document ready -> don't care,
		alert( "called early" );
		return;				// we will be called again on document.ready
	}						// that time with a working jPlayerObject!
	
	var url = $(this).attr( "link" );
	if( !url ) {
		url = $(this).attr( "href" );
	}
	
//	store proxy data in player-div
//	jQuery.data( div, $("#jpId"), $(this) ); ??
	
	if( !this.id ) {	//	ad unique id to all .prelistening without one
		this.id = "jp_internal_" + internal_player_id++;
	}
	
	jPlayerObject.attr( "href", url );

	/*	//	Does not work yet
	//	progress-bar function sets percentage player to percentage
	$(this).find( ".progress-bar" ).click(
		function( evt ) { // Handles clicks on the progress-bar
			var elem = $(this);
			var offset = elem.offset();
			var x = evt.pageX - offset.left;
			var w = elem.width();
			var percentage = 100 * x/w;
			jPlayerObject.jPlayer( "playHead", percentage );
			evt.stopPropagation();
		}
	);
	*/			
	if( jPlayerObject.attr( "player" ) ) {	//	jPlayer already instanciated?
		
		if( this.id == jPlayerObject.attr( "proxy_id" ) ) {	// 	click on currently active proxy => toggle playing
			
			if( jPlayerObject.jPlayer( "getData", "diag.isPlaying" ) ) {
				jPlayerObject.jPlayer( "pause" );
				$(this).removeClass( "playing" ).addClass( "paused" );	
			} else { // else continue to play
				$(this).removeClass( "paused" ).addClass( "playing" );	
				jPlayerObject.jPlayer( "play" );
			}
			return false;
		}
		
		//	other proxy clicked => stop playback, set new file,
		//	restore percentage from width of progress-bar, if possible
		//	and start playing 
		
		if( jPlayerObject.jPlayer( "getData", "diag.isPlaying" ) ) {
			jPlayerObject.jPlayer( "stop" );
		}
		
		var proxy;
		if( jPlayerObject.attr( "proxy_id" ) ) {
			proxy = $( "#" + jPlayerObject.attr( "proxy_id" ));
			proxy.removeClass( "playing" ).addClass( "paused" );
		}
		
		var percentage = parseInt( $(this).find( ".play-bar" ).css( "width" ) );
		
		jPlayerObject.attr( "proxy_id", this.id );
		jPlayerObject.attr( "no_update", 1 );
		
		jPlayerObject.jPlayer( "setFile", url ).jPlayer("play");
		
		$(this).removeClass( "paused" ).addClass( "playing" );	
	
		setTimeout( function() {	// does not work without delay...
			jPlayerObject.jPlayer( "playHead", percentage );
			jPlayerObject.removeAttr( "no_update" );
		}, 200 );
		
	} else {				
		jPlayerObject.attr( "proxy_id", this.id );
		$(this).removeClass( "paused" ).addClass( "playing" );	
		
// 		if( window.console ) console.log( "Start!");
		
		jPlayerObject.jPlayer( {
			ready: function () {
// 				if( window.console ) console.log( "READY!");
				
				jPlayerObject.attr( "player", "1" );
				var url =  this.element.attr( "href" );
				
				jPlayerObject.jPlayer( "onProgressChange", function( lp, ppr, ppa, pt, tt ) {
					if( jPlayerObject.attr( "no_update" ) || jPlayerObject.attr( "proxy_id" ) == "" ) {
						return;
					}
					var proxy = $( "#" + jPlayerObject.attr( "proxy_id" ));
					if( proxy.length ) {	// proxy found -> update
						proxy.find( ".load-bar" ).width( lp + "%" ); // percentage loaded
						proxy.find( ".play-bar" ).width( ppa + "%" ); // percentage played
						proxy.find( ".play-time").text( $.jPlayer.convertTime( pt ) );
					} else {	// proxy not found (perhaps unload of page part). stop player and remove proxy
						jPlayerObject.jPlayer( "stop" );
						jPlayerObject.attr( "proxy_id", "" );
					}
				} );
				
				setTimeout( function() {	//	occ. timing Probs in FF 3.6
					jPlayerObject.jPlayer( "setFile", url ).jPlayer("play"); // Auto-Plays the file
				}, 50 );
			
				jPlayerObject.jPlayer( "onSoundComplete", function() {
					var proxy = $( "#" + jPlayerObject.attr( "proxy_id" ));
					proxy.removeClass( "playing" ).addClass( "paused" );	
				} );
// 				if( window.console ) console.log( "READY done!");
			},
			swfPath: "/swf",
			errorAlerts: false
			, oggSupport: false
			, nativeSupport: false
		} );
	}
	
// 	if( window.console ) console.log( "Start done!");
	return false;
}

$(document).ready(
	
	function() { 
	// install player div if it is is missing
		
		if( !$("#jpId").length ) {
			$("body").append( $('<div id="jpId"></div>' ) );
		}
		
		jPlayerObject = $("#jpId");
		
		$.jPlayer.timeFormat.padMin = false;
		
		$("#kill").click( 
			function() {
				jPlayerObject.jPlayer( "stop" );
				return false;					
			}
		)
		
		$(".prelistening").click( start_prelistening );
	}
);


