/*! SWFSound v1.0 <http://code.google.com/p/swfsound/>
	Copyright (c) 2009 Frank Baumgartner, www.b-nm.at
	This software is released under the MIT License <http://www.opensource.org/licenses/mit-license.php>

	*** Requires SWFObject 2.1 or higher ! ***
	*** Requires Flash Player 8 or higher! ***
*/

var swfsound = function()
{
		// Public API
		return {

		embedSWF: function( path )
		{
				if ( path == undefined ) path = "swfsound/swfsound.swf";
				
				var flashvars = false;
				
				var attributes = 
				{
						id								: 'swfSound_Flash'
				}

				var params = 
				{
      			menu							: 'false',
       	   	wmode							: 'transparent',
       	   	swLiveConnect			: 'true',
       	   	allowScriptAccess	: 'always'
				};

				document.write( '<div id="swfSound_Flash_div" style="position:absolute; left:0; top:0;"></div>' );

				try
				{
					 	swfobject.embedSWF( path, 'swfSound_Flash_div', '1', '1', '8.0.0', 'swfsound/expressInstall.swf', flashvars, params, attributes);
       	}
       	catch ( e )
       	{
       			alert( 'Seems like you are missing swfobject! - Please include the swfobject javascript into your HTML!' );
      	}
		},


		/*
				loadSound( mp3URL, streamingMode )
				=========================================
				mp3URL: String ... relative or absolute URL path to a sound
				streamingMode: Boolean ... true streams the mp3 progressively and automatically starts playback,
																	false just loads the sound for later event based triggering with "startSound" 
				onLoadCallbackFunctionName ... STRING of the JavaScript function that should be called when the sound is fully loaded

				return value: id of loaded sound
		*/
		loadSound: function( mp3URL, streamingMode, onLoadCallbackFunctionName )
		{
				if ( streamingMode == undefined ) streamingMode = false;
				if ( onLoadCallbackFunctionName == undefined ) onLoadCallbackFunctionName = null;

				var obj = document.getElementById( 'swfSound_Flash' );
				return obj.loadSound( mp3URL, streamingMode, onLoadCallbackFunctionName );
		},
		

		/*
				startSound( id_Sound, offsetSecondsFloat )
				=========================================
				mp3URL: String ... relative or absolute URL path to a sound
				offsetSecondsFloat: Number ... offset in seconds (float values possible)
				loopCount ... number of loops the sound should be played
				onSoundCompleteCallbackFunctionName ... the name of the function (as String!) that should be called when the sound playback has been completed 
		*/
		startSound: function( id_sound, offsetSecondsFloat, loopCount, onSoundCompleteCallbackFunctionName )
		{
				if ( offsetSecondsFloat == undefined ) offsetSecondsFloat = 0.0;
				if ( onSoundCompleteCallbackFunctionName == undefined ) onSoundCompleteCallbackFunctionName = null;
				if ( loopCount == undefined ) loopCount = 1;

				var obj = document.getElementById( 'swfSound_Flash' );
				obj.startSound( id_sound, offsetSecondsFloat, loopCount, onSoundCompleteCallbackFunctionName );

				return true;
		},
		
		
		stopSound: function( id_sound )
		{
				var obj = document.getElementById( 'swfSound_Flash' );
				obj.stopSound( id_sound );
				
				return true;
		},
		
		
		/*
				Set Volume:
				==================================				
				valid values: 0 (= silent) ... 100 (= maximum volume)
		*/
		setVolume: function( id_sound, newVolume )
		{
				var obj = document.getElementById( 'swfSound_Flash' );
				obj.setVolume( id_sound, newVolume );
				
				return true;
		},
		
		
		getVolume: function( id_sound )
		{
				var obj = document.getElementById( 'swfSound_Flash' );
				return obj.getVolume( id_sound );
		},

		
		/*
				Returns the duration of a sound, in milliseconds
		*/
		getDuration: function( id_sound )
		{
				var obj = document.getElementById( 'swfSound_Flash' );
				return obj.getDuration( id_sound );
		},


		/*
				Returns the current playback position of a sound, in milliseconds
		*/
		getPosition: function( id_sound )
		{
				var obj = document.getElementById( 'swfSound_Flash' );
				return obj.getPosition( id_sound );
		},


		/*
				Set left/right panning:
				==================================
				-100 	= left
				0 		= center
				+100 	= right
		*/
		setPan: function( id_sound, newPan )
		{
				var obj = document.getElementById( 'swfSound_Flash' );
				obj.setPan( id_sound, newPan );
				
				return true;
		},
		
		
		getPan: function( id_sound )
		{
				var obj = document.getElementById( 'swfSound_Flash' );
				return obj.getPan( id_sound );
		},


		/*
				Returns the number of bytes of a sound that have already been loaded
		*/
		getBytesLoaded: function( id_sound )
		{
				var obj = document.getElementById( 'swfSound_Flash' );
				return obj.getBytesLoaded( id_sound );
		},


		/*
				Returns the total number of bytes of a sound file
		*/
		getBytesTotal: function( id_sound )
		{
				var obj = document.getElementById( 'swfSound_Flash' );
				return obj.getBytesTotal( id_sound );
		}

	};

}();

