/*
	(c) 2007, Innoetics

	First thing to do, is to place the hidden player at any place in your page using embedPlayer().

	Then, for each object where you weant to embed audio, define its unique id and set its onclick() 
	event handler to call pressAudioButton(), e.g.:
	  onclick="javascript:pressAudioButton(this, '2d54387cba1526c82842b2198e778fec.mp3');
	
	That's it!

	When toggling from play to stop mode, the script sets the innerHTML of the audioButton(s) to 'Play'
	and 'Stop'. If you want to change that, e.g. also include a graphic button to indicate the current
	state, call setPlayCaption() and setStopCaption() providing a string with the desired innerHTML, e.g.
	  setPlayCaption('<img src="images/wsplay.bmp">Play');	  
*/

var captionPlay = 'Play';
var captionStop = 'Stop';

var playing = false;
var currentElementId = '';

//Define the innerHTML that will be used when the audioButton is waiting for a click to start playback
function setPlayCaption(caption) {
	captionPlay = caption;
}

//Define the innerHTML that will be used when the audioButton is waiting for a click to stop playback
function setStopCaption(caption) {
	captionStop = caption;
}

//Use this function at any position where you want to insert the hidden player. It is not impotrant where you put it
//since its position is absolute and outside the visible area of the page.
function embedPlayer() {
	//document.write('<div id="hiddenPlayer" name="hiddenPlayer" STYLE="position:absolute;top:-100px;left:0px"></div>')
    var newdiv = document.createElement('div');
    newdiv.setAttribute('id','hiddenPlayer');
    newdiv.setAttribute('name','hiddenPlayer');
    newdiv.setAttribute('STYLE','position:absolute;top:-100px;left:-100px;');
    document.body.appendChild(newdiv);
}

//performs initialization
function initialize() {    
    if(document.getElementById('hiddenPlayer')){
        //alert('found');
    }else{
        embedPlayer();
        setPlayCaption('<img src="images/play32.gif"/>');
        setStopCaption('<img src="images/stop32.gif"/>');
        
        //Image preloading
        image1 = new Image(); image1.src = "images/stop32.gif";
    }
	initialized = true;
}

//Just play
function startPlayback(sound) {   
    initialize(); 
	window.status = sound; //SPY
	
	//If something is playing, stop it
	if (playing && currentElementId!='') {
		alert(playing);
		alert(currentElementId);
		playing = false;
		togglePlayButtonViewState(currentElementId);
	}

	document.getElementById('hiddenPlayer').innerHTML = getPlayerScript(sound);
	playing = true;
	currentElementId = '';
}

//This is the main function that needs to be called at the onclick handler of all audioButtons.
//It toggles playing and stopping.
function pressAudioButton(element, sound) {
    initialize();
    
	//If something is playing, stop it
	if (playing) {
		playing = false;
		togglePlayButtonViewState(currentElementId);
		if (element.id==currentElementId) {
			document.getElementById('hiddenPlayer').innerHTML = "&nbsp;";
			return;
		}
	}
	document.getElementById('hiddenPlayer').innerHTML = getPlayerScript(sound);
	playing = true;
	currentElementId = element.id;
	togglePlayButtonViewState(currentElementId);
}

//Stop anything that might be playing
function stopPlayback() {    
	//If something is playing, stop it
	if (playing) {
		playing = false;
		togglePlayButtonViewState(currentElementId);
		document.getElementById('hiddenPlayer').innerHTML = "&nbsp;";
		currentElementId = '';
	}
}

//This is called internally and is responsible for appropriately setting the innerHTML
//of the repsective audioBuuton
function togglePlayButtonViewState(elementId) {
	if (elementId=='')
		return;

	if (playing)
		document.getElementById(elementId).innerHTML = captionStop;
	else
		document.getElementById(elementId).innerHTML = captionPlay;
}

function getPlayerScript(sound) {
	var src='<OBJECT class="player" ID="objMediaPlayer" CLASSID="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95" STANDBY="Loading Windows Media Player components..." TYPE="application/x-oleobject" width="1" height="1"> '
		+ '<PARAM NAME="FileName" VALUE="' + sound + '"> '
		+ '<PARAM NAME="Hidden" Value="false"> '
		+ '<embed  '
			+ 'type="application/x-mplayer2" '
			+ 'NAME="objMediaPlayer"  '
			+ 'src="' + sound + '" '
			+ 'SHOWGOTOBAR="0"  '
			+ 'AUTOREWIND="0"  '
			+ 'SHOWCAPTIONING="0"  '
			+ 'ShowTracker="0"  '
			+ 'CurrentPosition="0"  '
			+ 'AUTOSIZE="0"  '
			+ 'SHOWPOSITIONCONTROLS="0"  '
			+ 'SHOWDISPLAY="0"  '
			+ 'SHOWTRACKER="0"  '
			+ 'SHOWCONTROLS="0"  '
			+ 'SHOWSTATUSBAR="0"  '
			+ 'AUTOSTART="1"  '
			+ 'windowlessvideo="false"  '
			+ 'showfullscreencontrols="false"  '
			+ 'width="1"  '
			+ 'height="1"  '
			+ 'name="player">  '
		+ '</embed>  '
		+ '</OBJECT> '
	;	
	//alert("getplayersound:"+src);
	return src;
	/*
	var src = '<embed name="speech" AllowScriptAccess="always" HEIGHT=60 WIDTH=144 src="' 
		+ sound 
		+ '" autostart="true" loop="false" volume="60"></embed>';
	return src;
	*/
}
