/* copyright Harpreet Riat (http://hs.riat.in) */
var RPlayer = {
	SWF_LOCATION: '/rplayer/player_mp3_js.swf',/*original swf file - http://flash-mp3-player.net*/
	PLAYER_CLASS_NAME: 'track_player',
	flashPlayer: false,
	tracks: Array(),
	listener: {
		url: '',
		onInit: function() {
			this.position = 0;
		},
		onUpdate: function() {}
	},
	init: function() {	
		var objectDiv = document.createElement('div');
		objectDiv.innerHTML = '<object type="application/x-shockwave-flash" data="'+this.SWF_LOCATION+'" width="1" height="1" style="position:absolute"><param name="movie" value="'+this.SWF_LOCATION+'" /><param name="AllowScriptAccess" value="always" /><param name="FlashVars" value="listener=RPlayer.listener&amp;interval=500" /></object>';
		document.body.appendChild(objectDiv);
		this.flashPlayer = objectDiv.getElementsByTagName('object')[0];
		var divs = document.getElementsByTagName('div');
		for (var i = 0; i < divs.length; i++)
		if (divs[i].className.match(this.PLAYER_CLASS_NAME))
		this.addMP3(divs[i]);
	},
	addMP3: function(div) {
		this.tracks.push(new MP3Track(div, this));
	},
	stop: function() {
		for (var i in this.tracks) {
			this.tracks[i].stop();
		}
	}
}
function MP3Track(div, parent) {
	this.parent = parent;
	this.div = div;
	var link = this.div.getElementsByTagName('a')[0];
	link.style.display = 'none';
	this.url = link.href;
	this.playButton = document.createElement('div');
	this.playButton.className = 'play_button';
	this.div.appendChild(this.playButton);
	this.stopButton = document.createElement('div');
	this.stopButton.className = 'stop_button';
	this.div.appendChild(this.stopButton);
	this.playStatus = 'stopped';
	this.play = function() {
		switch (this.playStatus) {
			case 'playing':
				this.playButton.className = 'play_button';
				this.parent.flashPlayer.SetVariable("method:pause", "");
				this.playStatus = 'paused';
				return;
			case 'stopped':
				for (var i = 0; i < this.parent.tracks.length; i++)
				this.parent.tracks[i].stop();
				try {
					this.parent.flashPlayer.SetVariable("method:setUrl", this.url);
				}
				catch (e) {
					alert('You need Adobe Flash Player installed on your browser to run this player.');
					return;
				}
				this.parent.flashPlayer.SetVariable("enabled", "true");
				this.stopButton.style.display = 'block';
			case 'paused':
				this.playButton.className = 'pause_button';
				this.parent.flashPlayer.SetVariable("method:play", "");
				this.playStatus = 'playing';
				return;
		}
	}
	this.stop = function() {
		if (this.playStatus != 'stopped') {
			this.parent.flashPlayer.SetVariable("method:stop", "");
			this.playButton.className = 'play_button';
			this.stopButton.style.display = 'none';
			this.playStatus = 'stopped';
		}
	}
	var track = this;
	this.playButton.onclick = function() {
		track.play();
	}
	this.stopButton.onclick = function() {
		track.stop();
	}
}
