soundManager.url = 'http://www.discord.co.uk/js/soundmanager2.swf';
soundManager.onload = function(){DISCO.tracks.init();};

DISCO = {};
DISCO.tracks = function() {
	var track;
	var loadpos = 0;

	function getTarget(t) {
		var t = t || window.event;
		return t.target || t.srcElement;
	}

	function click(e) {
		//get parent TD
		var node = getTarget(e);
		while(node.nodeName != 'TD') {
			node = node.parentNode;
		}
		//check playing sounds
		for(var i in soundManager.soundIDs) {
			var soundID = soundManager.soundIDs[i];
			var sound = soundManager.getSoundById(soundID);
			//stop if playing
			if(sound.playState == 1) {
				soundManager.stop(soundID);
				stop();
				//don't play (again) if were already
				if(soundID == node.id) {
					node.blur();
					return false;
				}
			}
		}
		track = node;
		start();
		soundManager.play(node.id);
		node.blur();
		return false;
	}

	function loading() {
		var percentage = parseInt(100 * (this.bytesLoaded / this.bytesTotal));
		track.childNodes[0].firstChild.firstChild.style.width = percentage + '%';
		track.childNodes[0].firstChild.firstChild.style.backgroundPosition = '0px ' + loadpos + 'px';
		loadpos++;
	}

	function loaded() {
		track.childNodes[0].firstChild.firstChild.style.width =  '0';
		track.childNodes[0].firstChild.firstChild.style.backgroundImage =  'none';
	}

	function playing() {
		var percentage = parseInt(100 * (this.position / this.duration));
		track.childNodes[0].firstChild.firstChild.style.width = percentage + '%';
	}

	function start() {
		track.childNodes[0].childNodes[1].style.backgroundPosition =  '0 -321px';
	}

	function stop() {
		track.childNodes[0].childNodes[1].style.backgroundPosition =  '0 -181px';
		track.childNodes[0].firstChild.firstChild.style.width =  '0';
	}

	return {
		init: function() {
			var list = document.getElementById('tracks');
			list.onclick = click;
			var tds = list.getElementsByTagName('TD');
			for(var i = 0; i < tds.length; i++) {
				var td = tds[i];
				//skip track numbers and empty tds
				if(!td.firstChild || td.firstChild.nodeType == 3) {
					continue;
				}
				var a = td.firstChild;
				var test = a.href.toLowerCase();
				if(a.nodeName == 'A' && test.indexOf('.mp3') == (test.length - 4)) {
					//wrapper div
					var content = td.removeChild(a);
					var trw = document.createElement('DIV');
					trw.className = 'trw';
					trw.appendChild(content);
					td.appendChild(trw);

					//remove speaker images
					if(content.lastChild.nodeName == 'IMG') {
						content.removeChild(a.lastChild);
					}

					//button
					var trb = document.createElement('DIV');
					trb.className = 'trb';
					trw.insertBefore(trb, trw.firstChild);
					//progressbar & container
					var tri = document.createElement('DIV');
					tri.className = 'tri';
					var trc = document.createElement('DIV');
					trc.className = 'trc';
					trc.appendChild(tri);
					trw.insertBefore(trc, trw.firstChild);

					//make sound
					var sID = 'sound' + i;
					soundManager.createSound({	id:sID,
									url:a.href,
									whileloading:loading,
									onload:loaded,
									whileplaying:playing,
									onfinish:stop
									});
					td.id = sID;
					td.style.cursor = 'pointer';
				}
			}
		}
	};
} ();