//	-------------------------------------
//	Global variables
//	-------------------------------------


//	-------------------------------------
//	Swap songbook
//	-------------------------------------
//	swap_songbook() switches out the
//	currently playing audio for the newly
//	requested audio.
//	-------------------------------------

function swap_songbook( url, result, oldid, newid )
{
	var target		= $(result);
	var pars		= '';
	
	//	Show spinner
	Element.show( 'spinner' + newid );
	
	//	Change state of new container
	Element.addClassName( 'main_container' + newid, 'playing');
	
	//	Play buttons
	Element.hide( 'play' + newid + 'on' );
	Element.show( 'play' + newid + 'off' );
	
	var id	= $(oldid).value;
	
	if ( id != '' )
	{
		//	Old play buttons		
		Element.show( 'play' + id + 'on' );
		Element.hide( 'play' + id + 'off' );
	
		//	Change state of old container
		$( 'main_container' + id ).setAttribute( 'class', 'audio' );
		
		if ( (navigator.userAgent.indexOf("MSIE") != -1) && (navigator.userAgent.indexOf("Opera") == -1) )
		{
			$( 'main_container' + id ).style.setAttribute( 'cssText', 'background:#000;' );
		}
		
		//	Stop audio
		stop_player( oldid );
	
		//	Remove audio
		remove_video( 'song_container' + id );
	}
	
	//	Record which audio we're loading
	$(oldid).value	= newid;
	
	//	Fetch audio
	var	aj			= new Ajax.Request( url, { method: 'post', postBody: pars, onSuccess: put, onFailure: fail } );

	function put(t)
	{	
		//	Hide spinner
		Element.hide( 'spinner' + newid );
		
		//	Place audio
		var output				= t.responseText;
		target.innerHTML		= output;
	}
	
	function fail()
	{	
		//	Hide spinner
		Element.hide( 'spinner' + newid );
	}
}

//	End Swap Songbook


//	-------------------------------------
//	Hide song
//	-------------------------------------
//	hide_song() stops play of current
//	song and hides meta data.
//	-------------------------------------

function hide_song( result, oldid, newid )
{
	var target		= $(result);
	
	Element.show( 'play' + newid + 'on' );
	Element.hide( 'play' + newid + 'off' );
	
	//	Change state of old container		
	$( 'main_container' + newid ).setAttribute( 'class', 'audio' );
	
		
	if ( (navigator.userAgent.indexOf("MSIE") != -1) && (navigator.userAgent.indexOf("Opera") == -1) )
	{
		$( 'main_container' + newid ).style.setAttribute( 'cssText', 'background:#000;' );
	}
	
	//	Stop audio
	stop_player( oldid );

	//	Remove audio
	remove_video( 'song_container' + newid );
	
	//	Reset
	$(oldid).value	= '';
}

//	End hide song


//	-------------------------------------
//	Swap video
//	-------------------------------------
//	swap_video() switches out the
//	currently playing video for the newly
//	requested video.
//	-------------------------------------

function swap_video( url, result, oldid, newid, large_loader )
{
	var large		= $(large_loader);
	var target		= $(result);
	var pars		= '';
	
	//	Show loading
	Element.hide( large );
	Element.toggle( large );
	
	//	Show spinner
	Element.show( 'spinner' + newid );
	
	//	Play buttons
	play_buttons( oldid, newid );
	
	//	Stop video
	stop_player(oldid);
	
	//	Remove video
	remove_video( result );
	
	//	Fetch video
	var	aj			= new Ajax.Request( url, { method: 'post', postBody: pars, onSuccess: put, onFailure: fail } );

	function put(t)
	{
		//	Hide loader
		Element.toggle(large);
	
		//	Hide spinner
		Element.hide( 'spinner' + newid );
		
		//	Place video
		var output				= t.responseText;
		target.innerHTML		= output;
	}
	
	function fail()
	{
		//	Hide loader
		Element.toggle(large);
	
		//	Hide spinner
		Element.hide( 'spinner' + newid );
		
		//	Show no_load image
		Element.toggle('no_load');
	}
}

//	End Swap Video


//	-------------------------------------
//	Remove video
//	-------------------------------------
//	remove_video() removes the requested
//	video.
//	-------------------------------------

function remove_video(which)
{
	var video		= $(which);
	
	video.innerHTML	= '';
}

//	End remove video



//	-------------------------------------
//	Play Buttons
//	-------------------------------------
//	play_buttons() swap the on and off
//	play buttons.
//	-------------------------------------

function play_buttons( oldid, newid  )
{
	var id	= $(oldid).value;
	if ( id == '' ) { return; }
	
	Element.show( 'play' + id + 'on' );
	Element.hide( 'play' + id + 'off' );
	Element.toggle( 'play' + newid + 'on' );
	Element.toggle( 'play' + newid + 'off' );
}

//	End play buttons



//	-------------------------------------
//	Stop player
//	-------------------------------------
//	stop_player() stops the currently
//	playing video because just swapping
//	out the iner html is not necessarily
//	enough to shut 'er down. Safari in
//	particular.
//	-------------------------------------

function stop_player(which)
{
	var id	= $(which).value;
	if ( id == '' ) { return; }
	var id	= 'document.' + 'vid' + id;	
	var obj	= eval(id);
	// obj.Stop();
}

//	End stop player