player = {
	objs: [],
	curr_obj: 0,
	audio: {
		type: {
			OGG: 'audio/ogg',
			MP3: 'audio/mpeg',
			WAV: 'audio/x-wav'
		}
	},
	obj: {
		supports_audio: !!document.createElement('audio').canPlayType,
		basepath: '',
		set_basepath: function( path ) {
			this.basepath = path;
		},
		files: [],
		add_file: function( src,type ) {
			this.files[this.files.length] = {
				src: src,
				type: type
			}
		},
		loaded: false,
		audio: null,
		parts: {},
		manual_seek: false,
		init: function( id ) {
			if ( this.supports_audio ) {
				if ( this.files.length == 0 ) {
					alert('Please add files for audio player ' + id);
				}
				var html = '										<span class="player_sprite player_playtoggle"></span>\
										<span class="player_gutter">\
											<span class="player_sprite player_gutter_left"></span>\
											<span class="player_sprite player_gutter_right"></span>\
											<!--<div class="player_loading"></div>-->\
											<span class="player_sprite player_handle"></span>\
										</span>\
										<span class="player_timeleft"></span>\
										<audio preload="metadata">';
				for( var i=0;i < this.files.length;i++ ) {
					html += '										<source src="' + this.basepath + this.files[i].src + '" type="' + this.files[i].type + '"></source>';
				}
				html += '									</audio>';
				$('#' + id).html(html);
				this.audio = $('#' + id + ' audio').get(0);
				this.parts.playtoggle = $('#' + id + ' .player_playtoggle');
				this.parts.gutter = $('#' + id + ' .player_gutter');
				this.parts.loading = $('#' + id + ' .player_loading');
				this.parts.handle = $('#' + id + ' .player_handle');
				this.parts.time = $('#' + id + ' .player_timeleft');
				if ( ( this.audio.buffered != undefined ) && ( this.audio.buffered.length !== 0 ) ) {
					this.process();
				}
				else {
					this.parts.loading.remove();
				}
				this.timeupdate();
				this.play();
				this.pause();
				this.toggle();
			}
		},
		progress: function() {
			var parent = this;
			$(this.audio).bind('progress',function() {
				var loaded = parseInt( ( ( parent.audio.buffered.end(0) / parent.audio.duration ) * 100 ),10 );
				parent.parts.loading.css({ width: loaded + '%' });
			});
		},
		timeupdate: function() {
			var parent = this;
			$(this.audio).bind('timeupdate',function() {
				var rem  = parseInt( ( parent.audio.duration - parent.audio.currentTime ), 10);
				var pos  = ( ( parent.audio.currentTime / parent.audio.duration ) * 100 );
				var mins = Math.floor( ( rem / 60 ),10 );
				var secs = ( rem - ( mins * 60 ) );
				//set time left here, use: 
				parent.parts.time.text('-' + mins + ':' + ( secs < 10 ? '0' + secs : secs ));
				if ( parent.manual_seek == false ) {
					parent.parts.handle.css({ left: pos + '%' });
				}
				if ( parent.loaded == false ) {
					parent.loaded = true;
					parent.parts.gutter.slider({
						value: 0,
						step: 0.01,
						orientation: 'horizontal',
						range: 'min',
						max: parent.audio.duration,
						animate: true,
						slide: function() {
							parent.manual_seek = true;
						},
						stop: function( e,ui ) {
							parent.manual_seek = false;
							parent.audio.currentTime = ui.value;
						}
					});
				}
			});
		},
		play: function() {
			var parent = this;
			$(this.audio).bind('play',function() {
				parent.parts.playtoggle.addClass('player_playing');
			});
		},
		pause: function() {
			var parent = this;
			$(this.audio).bind('pause ended',function() {
				parent.parts.playtoggle.removeClass('player_playing');
			});
		},
		toggle: function() {
			var parent = this;
			this.parts.playtoggle.click(function() {
				if ( parent.audio.paused ) {
					parent.audio.play();
				}
				else {
					parent.audio.pause();
				}
			});
		}
	},
	basepath: '',
	set_basepath: function( path ) {
		this.basepath = path;
	},
	create: function() {
		this.curr_obj = this.objs.length;
		this.objs[this.curr_obj] = jQuery.extend(true,{},this.obj);
		this.objs[this.curr_obj].set_basepath( this.basepath );
		return this.objs[this.curr_obj];
	}
}
