(function($) {
	$(document).ready(function () {
		
		// config variables
		var SWF_OBJECT_ID					= "swf0";
		var PLAY_ALL_SELECTOR				= ".NPP_PlayAll";
		var PLAY_SEGMENT_SELECTOR_PREFIX	= ".NPP_PlaySegment";
		var ANCHOR_REGEX					= /#segment(\d+)/;
		
		// create JS player object
		var podcastPlayer = new PodcastPlayer( findSWF(SWF_OBJECT_ID) );
		
		// go to segment if specified in URL
		if( ANCHOR_REGEX.test(window.location.hash) ) {
			podcastPlayer.playSegment( ANCHOR_REGEX.exec(window.location.hash)[1]-1 );
		}
		// add events
		$(PLAY_ALL_SELECTOR).click(function(){ podcastPlayer.seekAndPlay(0); });
		
		var i = 0;
		var objects;
		
		var results = "";
		
		while( (objects = $(PLAY_SEGMENT_SELECTOR_PREFIX+i)).length > 0 ) {
			for( var j = 0; j < objects.length; j++ ) {
				results+=objects[j]+"\n";
				objects[j].segmentID = i;
				
				$(objects[j]).click(function(){ podcastPlayer.playSegment( this.segmentID ); });
			}
			i++;
		}
		
		return false;
	});
	
	function findSWF( id )
	{
		// IE should target the outer object tag, Fx should target the inner embed tag. Odd, I know.
		if( jQuery.browser.mozilla ) {
			id += " > object";
		}
		return $("#"+id)[0];
	}
	
}) (jQuery);