/*
 * halfbricking.com in-page music player loader
 *
 * usage:
 * - add "swfobject.js" to the page's script.
 *   Note some wordpress plugins do this already and having two will cause a script error.
 * - add this to the page's script.
 * - add <SPAN CLASS="music" ID="playlistname" /> anywhere in the page content.
 *
 * When the page loads, any of these SPANs become an in-page music player
 * using the playlist "/music/playlistname.xml".
 * Supports multiple players per page, as long as they have different playlists(!)
 */


// Load a musicplayer
//
function loadMusic( sPlayerID, sPlaylist, sTitle ) {
	var flashvars = {};
	var params = {};

	var attributes = {
		id: "player-" + sPlayerID,
		name: "player-" + sPlayerID
	};

	var xpsfparams = [
		"volume_level=60",
		"autoresume=false",
		"autoplay=false",
		"shuffle=false",
		"repeat_playlist=false",
		"buffer=3",
		"crossFade=0",
		"player_title=" + escape( "Click to play: " + sTitle ),
		"playlist_url=" + escape( "/Player/" + sPlaylist ),
		"skin_url=" + escape( "/Player/playerskin.xml" ),
		"mainurl=" + escape( "http://halfbricking.com/" )
	];

	swfobject.embedSWF("/Player/xspf_jukebox.swf?" + xpsfparams.join("&"),
		sPlayerID,
		"420",
		"40", 
		"7.0",
		"/Player/expressInstall.swf", flashvars, params, attributes);
}


if(typeof(Halfbricking) == 'undefined') Halfbricking = {}

Halfbricking.Threw = {
	// Load music for all
	Go: function() {
		var all = document.getElementsByTagName('span');
		for( var i = all.length-1; i>=0; i-- ) {
			var o = all[i];
			if( o.className=="music" ) {
				loadMusic( o.id, o.id + ".xml", o.title );
			}
		}
	}
}

Halfbricking.addLoadEvent = function(f) {
	var old = window.onload;
	if (typeof old != 'function') window.onload = f;
	else { window.onload = function() { old(); f() }}
}


// Hook the page onload
Halfbricking.addLoadEvent( Halfbricking.Threw.Go );

