var Audio = {
	
	audio: null,
	tid: null,
	
	play: function() {
		try {
			this.audio.play();
		} catch(e) {}
	},
	
	pause: function() {
		try {
			this.audio.pause();
		} catch(e) {}
	},
	
	rewind: function() {
		try {
			this.audio.currentTime = 0;
		} catch(e) {}
	},
	
	addTrack: function(id, url) {
		if ($('#a'+ id).length == 0) {
			$('#audiobox').append('<audio id="a'+ id +'" src="'+ url +'"></audio>')
		}
	},
	
	setTrack: function(id) {
		this.audio = document.getElementById('a'+ id);
		$(this.audio).bind('play',function() {
			$('#player .controls .play').hide();
			$('#player .controls .pause').show();
		}).bind('pause ended', function() {
			$('#player .controls .pause').hide();
			$('#player .controls .play').show(); 
		});
	},
	
	getLoadedPercent: function() {
		try {
			return Audio.audio.buffered.end(0) / Audio.audio.duration;
		} catch(e) {}
		return 0;
	},
	
	getPlayedPercent: function() {
		try {
			return Audio.audio.currentTime / Audio.audio.duration;
		} catch(e) {}
		return 0;
	},
	
	setPlayedPercent: function(val) {
		if (typeof(this.audio.duration) != undefined) {
			this.audio.currentTime = Math.round(val * this.audio.duration / 100);
			return true;
		}
		return false;
	},
	
	skipToPercent: function(val) {
		try {
			this.audio.currentTime = this.audio.duration * val;
		} catch(e) {}
	},
	
	startListener: function() {
		Audio.tid = window.setTimeout(Audio.startListener, 100);
		Player.updateUI(
			Audio.getLoadedPercent(),
			Audio.getPlayedPercent()
		);
	},
	
	killListener: function() {
		window.clearTimeout(this.tid);
	}
}  

$('#player .controls .play').live('click', function(){
	Audio.play();	
});

$('#player .controls .pause').live('click', function(){
	Audio.pause();
});