
var audioSmall = function(id, soundFile) {
	this.id = id.replace('audio_', '');
	this.soundFile = soundFile;
	this.status = 'no_init';
	
	var self = this;
	
	this.el = {
		'statusBar' : $('#' + id + ' div.audio-statusbar').click(function(e) {
			self.setPosition.call(self, e);
		}),
		'progressMain' : $('#' + id + ' div.audio-statusbar div.audio-position').css('width', 0),
		'progressLoad' : $('#' + id + ' div.audio-statusbar div.audio-loading'),
		'button' : $('#' + id + ' div.audio-button img').click(function(e) {
			self.toggle.call(self, e);
		})
	};

	return true;
};

audioSmall.prototype = {
	
	toggle : function(e) {
		var soundObj;
		if (this.status == 'no_init') {
			soundObj = soundManager.createSound({
				"id" : this.id,
				"url" : this.soundFile,
				"whileplaying" : this.event_whilePlaying,
				"whileloading" : this.event_whileLoading,
				"onfinish" : this.event_onFinish
			});
			soundObj.parent = this;
		} else {
			soundObj = soundManager.getSoundById(this.id);
		}

		if (this.status == 'stopped' || this.status == 'no_init') {
			this.el.button.attr('src', '/images/v3/button_pause2.png');
			soundManager.play(this.id);
			this.status = 'playing';
			return;
		} else if (this.status == 'playing') {
			this.el.button.attr('src', '/images/v3/button_play2.png');
			soundObj.pause();
			this.status = 'paused';
			return;
		} else if (this.status == 'paused') {
			this.el.button.attr('src', '/images/v3/button_pause2.png');
			soundObj.play();
			this.status = 'playing'; 
		}
	},
	
	setPosition : function(e) {
		if (this.status == 'no_init') {
			return;
		}
		var soundObj = soundManager.getSoundById(this.id);
		var pos = e.pageX - this.el.statusBar.offset().left;
		pos = (pos/this.el.statusBar.width()) * soundObj.durationEstimate;
		soundObj.setPosition(pos);
		if (this.status == 'stopped') {
			this.el.progressMain.stop().animate({'width':pos + 'px'}, 100);
		}
	},
	
	event_whilePlaying : function() {
		this.parent.el.progressMain.stop().animate({'width':Math.ceil(this.position/this.durationEstimate*this.parent.el.statusBar.width()) + 'px'}, 100);
	},
	
	event_whileLoading : function() {
		this.parent.el.progressLoad.stop().animate({'width':Math.ceil(this.bytesLoaded/this.bytesTotal*this.parent.el.statusBar.width()) + 'px'}, 100);
	},
	
	event_onFinish : function() {
		this.parent.status = 'stopped';
		this.parent.el.button.attr('src', '/images/v3/button_play2.png');
	}
	
};
