function ge(name) {
    return document.getElementById(name);
}

function showError(e) {
    //alert("Error: " + e.name + ": " + e.message);
}

function playbackComplete(name, args) {
    try {
        songID = curPlayingElemID.substring(6);
	nextSongID = playerNextSongID[songID];
	if ( nextSongID != null ) {
            playSong(playerNextSongID[songID], true);
	} else {
	    hidePlayer();
	}
    } catch ( e ) {
        showError(e);
    }
}

var hasFlash = swfobject.hasFlashPlayerVersion("7.0");

var curPlayingElemID = null;

function hidePlayer() {
    if ( curPlayingElemID != null ) {
        swfobject.removeSWF(curPlayingElemID);
	curPlayingElemID = null;
    }
}

function playSong(songId, force) {
    try {
        if ( curPlayingElemID != null ) {
	    if ( ! force ) {
	        return;
	    }

	    hidePlayer();
	}

	var newPlayerID = "player" + songId;

	if ( ge(newPlayerID) == null ) {
	    var p = document.createElement("div");
	    p.setAttribute("id", newPlayerID);
	    ge("container-" + newPlayerID).appendChild(p);
        }

	var flashVars = { "songId" : songId, "autoStart" : "1" };
	var params = { "allowScriptAccess" : "sameDomain", "swliveconnect" : "true", "bgcolor" : "#000000" };
	var attributes = { "name" : newPlayerID };

	swfobject.embedSWF("miniplayer.swf", newPlayerID, "245", "15", "7.0.0", false, flashVars, params, attributes);
	curPlayingElemID = newPlayerID;
    } catch ( e ) {
        showError(e);
    }
}

var songToPlay = null;

function play() { 
    playSong(songToPlay, false); 
} 

function defaultSong(newSong) {
    if ( songToPlay == null )
        songToPlay = newSong;
}

function unloadPlayer() {
    try {
        if ( curPlayingElemID != null )
	    ge(curPlayingElemID).onUnload();
    } catch ( e ) {
        showError(e);
    }
}
