var lastImageID = " ";
var isPlaying = false;
var currentlyNumber = -1;
var clips = new Array();
var product = new Array();
var currentlyPlayingClip = '';
var currentlyPlayingProductID = '';
var allowPlayListDetect = 0;
var objectCreated = 0;
var clipCounter = 0;
var firstPlay = true;

if (window.ActiveXObject && navigator.userAgent.indexOf('Windows') != -1)
{
  var allowPlayListDetect = 1;
}
/* 
Makes use of the Windows Media Player embedded within the major web browsers, IE, Firefox, Opera etc
to play 30 sound clips from a MMS stream.  This Media Player, unlike the ActiveX Media Player that isn't
supported by Firefox, doesn't support script access to it's controls.  The only way to launch the player
is to dynamically write the necessary HTML to the browsers DOM in response to an event on the web page.  
*/ 
function playAClip(clip) {
  var mediaPlayerHtml
  if(isPlaying && currentlyPlayingClip == clip) {
     	try {
     		if (window.ActiveXObject && navigator.userAgent.indexOf('Windows') != -1)
			{
				document.getElementById('MediaPlayerObject').Play();
			}
		}	
		catch(err)
		{
		}
  }
  else
  {
		// Opera will only play the clip if the <embed> tag is nested within the <object> tag.
	    mediaPlayerHtml = "<object ID=\"MediaPlayerObject\" type='audio/x-ms-wma' width='0' height='0' >";
	    mediaPlayerHtml += "<param name='fileName' value='" + clip + "' />";
	    mediaPlayerHtml += "<embed  ID=\"MediaPlayerEmbeded\" type='audio/x-ms-wma' width='0' height='0' ";
	    mediaPlayerHtml += "src='" + clip + "'></object>"+clip;
	    isPlaying = true; 
	    currentlyPlayingClip = clip;   
	    // Assign the value of the variable to the innerHTML of the 'mediaPlayer' div
	    if (document.getElementById('mediaPlayer') != null) {
  			document.getElementById('mediaPlayer').innerHTML = mediaPlayerHtml;	
  			objectCreated = 1;
	    }
		else {
			alert("Please add the media player div to place the player");
		}	  	
  }
  
}

function  demoPlay(clip) {
	 document.getElementById('mediaPlayer').innerHTML = clip;	
}

function playClip() {
	if (clips.length > 0) {
		/*
		 * Check the length of the clip
		 */
		if (currentlyNumber < 0) {
			currentlyNumber = 0;
		}
		if (currentlyNumber > (clips.length-1)) {
			currentlyNumber = (clips.length-1);
		}
		try {
			if(currentlyPlayingClip != clips[currentlyNumber]) {
				eventNowPlayingProduct(product[currentlyNumber]);	
			}
		}
		catch (err) {
			// function does not exists
		}
		try {
			if (firstPlay) {
				firstPlay = false;
				firstTimeWePlayerd();	
			}
		}
		catch (err) {
			// function does not exists
		}
		playAClip(clips[currentlyNumber]);
		currentlyPlayingProductID = product[currentlyNumber];
		
		buildList();
		if (!allowPlayListDetect) {
			clipCounter++;
			window.setTimeout('timerSkipCount('+clipCounter+')',20000);
		}
		
	}
}

function prevClip() {
	if (currentlyNumber > 0) {
		currentlyNumber--;
		playClip();
	}
	
}

function timerSkipCount(inCount) {
	if (inCount == clipCounter) {
		timerSkip();
	}
	
}

function timerSkip() {
	if(isPlaying) {
		stopClip();
		nextClip();
	}
}
function pauseClip() {
	if(isPlaying) {
		try {
			if (window.ActiveXObject && navigator.userAgent.indexOf('Windows') != -1)
			{
			  document.getElementById('MediaPlayerObject').Pause();
			} 
			else {
				mediaPlayerHtml = "<object type='audio/x-ms-wma' width='0' height='0' />";	
				document.getElementById('mediaPlayer').innerHTML = mediaPlayerHtml;	
				isPlaying = false;
				currentlyPlayingClip = "";
			}
		}	
		catch(err)
		{
		}
	}
}

function stopClip() {
	if (isPlaying) {
		try {
			if (window.ActiveXObject && navigator.userAgent.indexOf('Windows') != -1)
			{
			  document.getElementById('MediaPlayerObject').Stop();
			} 
			else {
				mediaPlayerHtml = "<object type='audio/x-ms-wma' width='0' height='0' />";	
				document.getElementById('mediaPlayer').innerHTML = mediaPlayerHtml;	
				currentlyPlayingClip = "";
			}
			isPlaying = false;
		}	
		catch(err)
		{
			alert(err.message);
		}
	}
}

function nextClip() {
	if (currentlyNumber < (clips.length-1)) {
		currentlyNumber++;
		playClip();
	}
}
function addClipNoPlay(inClip,inProductId) {
	pos = clips.length;
	clips[pos]=inClip;
	product[pos]=inProductId;
	buildList();
	return pos;
}
function addClip(inClip,inProductId) {
	pos = clips.length;
	clips[pos]=inClip;
	product[pos]=inProductId;
	if(!isPlaying) {
		currentlyNumber = pos;
		playClip();
	}
	buildList();
	return pos;
}

function clearClips() {
	stopClip();
	isPlaying = false;
			currentlyPlayingClip = "";
	currentlyNumber = -1;
	clips = new Array();
	product = new Array();
	buildList();
	if (document.getElementById('mediaPlayerProductView') != null) {
		document.getElementById('mediaPlayerProductView').innerHTML = '';	
	}
}

function buildList() {
//	list = '';
//	for (k = 0; k < clips.length ; k++) {
//		if (clips[k] == currentlyPlayingClip) {
//			list += "<b>"+clips[k]+"</b><br>";
//		}
//		else {
//			list += clips[k]+"<br>";
//		}
//	}
//	document.getElementById('inlist').innerHTML = list;	
}

function changeStateClip(inState) {
	
	allowPlayListDetect = 1;
	if (inState == 3) {
		
	} else if (inState == 0) {
		allowPlayListDetect = 1;
		timerSkip();
	}
	changeStateClip2(inState);
}

function changeStateClip2(inState) {
	allowPlayListDetect = 1;
	try {
		myText = document.getElementById('mediaPlayerStatus');	
		switch (inState){
		    case 1:
				myText.innerHTML = "Stopped";
		        break;
		    case 2:
		        myText.innerHTML = "Paused";
		        break;
		    case 3:
		        myText.innerHTML = "Playing";
		        break;
		    case 4:
		        myText.innerHTML = "ScanForward";
		        break;
			case 5:
				myText.innerHTML = "ScanReverse";
				break;   
			case 6:
				myText.innerHTML = "Buffering";
				break;   
			case 7:
				myText.innerHTML = "Waiting";
				break;   
			case 8:
				myText.innerHTML = "MediaEnded";
				break;   
			case 9:
				myText.innerHTML = "Transitioning";
				break;   
			case 10:
				myText.innerHTML = "Ready";
				break;   
			case 11:
				myText.innerHTML = "Reconnecting";
				break;   
		    default:
		        myText.innerHTML = "";
		}
	}	
	catch(err)
	{
		//alert(err);
	}
	
}



