/* AUDIO PLAYER*/

var flashPlayer;
var isPlaying = false;
var isLoadingSong = false;
var currentlyPlaying = "";

var isInternetExplorer = navigator.appName.indexOf("Microsoft") != -1;

function thisMovie(movieName){
    // IE and Netscape refer to the movie object differently.
    // This function returns the appropriate syntax depending on the browser.	
    if (isInternetExplorer) {
        //return window.document.getElementById(movieName);
        return window[movieName];
    }
    else {
        return document[movieName];
    }
}

function playSample(name){
	aplayer(name, "f");
	isPlaying = true;
}

function pauseSample(){
	aplayer("", "t");
	isPlaying = false;
}

/*
 * control the playing state of the mp3 sample.
 * click to play
 * click other song, play it and stop the last one
 * stop the song
 */
function previewPressed(button, songName){
	if (!isPlaying) {
		playSample(songName);
		currentlyPlaying = songName;
		button.id = "pauseAudio";
	}else{
		if (currentlyPlaying != songName) {
			if (isLoadingSong) {
				finishLoading();
			}
			pauseSample();
			playSample(songName);
			currentlyPlaying = songName;
			document.getElementById("pauseAudio").id = "";
			button.id = "pauseAudio";
		}else{
			if (isLoadingSong) {
				finishLoading();
			}
			pauseSample();
			document.getElementById("pauseAudio").id = "";
		}
	}
}

/* 
 * add the flash object inside the with ID = aplayer
 * mp3: can have the absolute path or the relative path
 * songState: is a true or false var, false for not playing and true for playing
 */
function aplayer(mp3, songState){
    var audioplayer = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="" width="0" height="0" id="player" align="middle">' +
    '<param name="allowScriptAccess" value="always" />' +
    '<param name="allowFullScreen" value="false" />' +
    '<param name="movie" value="store/20081002001/assets/flash/player.swf" />' +
    '<param name="swLiveConnect" value="true" />' +
    '<param name="flashVars" value="song=' +
    mp3 + '&sState=' +
    songState + '&ie=' +
    isInternetExplorer + '" />' + '<embed src="store/20081002001/assets/flash/player.swf" FlashVars="song=' +  mp3 + '&sState=' +   songState +  '&ie=' + isInternetExplorer +
    '" quality="high"  width="0" height="0" name="player" swLiveConnect=true allowScriptAccess="always" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />' +
    '</object>';
    
    document.getElementById("aplayer").innerHTML = audioplayer;
    
    if (isInternetExplorer) {
        document.getElementById("aplayer").innerTEXT = audioplayer;
        //sCompleteInt = setInterval(isSongCompleted, 1000);
    }
}

/* add the loading gif to the button */
function loadingSong(){
    isLoadingSong = true;
    document.getElementById("pauseAudio").innerHTML = '<img src="store/20081002001/assets/images5/store_layout/songloader.gif" style="padding-left: 7px; padding-top: 3px;"/>';
    //alert('loading');
}

/* remove the loading gif when the mp3 sample finish loading */
function finishLoading(){
    isLoadingSong = false;
    if (document.getElementById("pauseAudio") != null) 
        document.getElementById("pauseAudio").innerHTML = "";
    //alert('finish');
}

/* when the song ends, change the icon back to Play state*/
function songComplete(){
	pauseSample();
	document.getElementById("pauseAudio").id = "";
	//alert('songComplete');
}

/* receive data from the flash object */
function player_DoFSCommand(command, args){
    //var playerObj = isInternetExplorer ? document.all.player : document.player;
    
    if (command == "songComplete") {
        songComplete();
        //alert("complete"+args);
        pauseSample();
    }
    else 
        if (command == "finishLoading") {
            if (isLoadingSong) {
                finishLoading();
            }
        //alert("load"+args);
        }
        else {
            loadingSong();
        //alert("is loading"+args);
        }
}
