
//var audio = new SWFObject(playerURL,'audio','100','50','8');
//Event.observe($(
var audio = Class.create();

audio.prototype = {
	initialize: function(element) {
		this.element = $(element);
		this.menu = $A(this.element.getElementsByTagName('a'));
		this.menu.each(this.setupAudio.bind(this));
		this.load(this.getInitialTab());
		this.autoload = 'true';
		
	},
	setupAudio: function(elm) {
		Event.observe(elm,'click',this.activate.bindAsEventListener(this),false);
	},
	activate: function(ev) {
		var elm = Event.findElement(ev,'a');
		this.menu.without(elm).each(this.unload.bind(this));
		this.load(elm);
	},
	load: function(elm) {
		//alert(location.hash.indexOf('autoload=')+9);
		if(location.hash.indexOf('autoload=true') > 0) {
			this.autoload = 'true';
		}
		else if(location.hash.indexOf('autoload=false') > 0) {
			this.autoload = 'false';
		}
		
		var filename = this.tabID(elm) + '.mp3';
		var ajaxRequestURL = 'script/php/audio.php';
		//alert(ajaxRequestURL);
		var self = this;
		var ajax = new Ajax.Request(ajaxRequestURL, {
			method: 'post',
			parameters: {'filename': filename},
			onSuccess: function(r) {
				if(r.responseText == '1') {
					//if(autoload == null) {autoload = 'true';}
					var playerURL = 'http://www.healthieryou.ws/flash/mediaplayer.swf';
					var obj = new SWFObject(playerURL,'audio','100','50','8');
					//alert(obj.toString());
					var file = 'http://www.healthieryou.ws/audio/performers/'+ filename;
					obj.addParam('allowscriptaccess','always');
					obj.addParam('allowfullscreen','false');
					obj.addVariable('width','100');
					obj.addVariable('height','20');
					obj.addVariable('file',file);
					obj.addVariable('javascriptid','audio');
					obj.addVariable('enablejs','true');
					obj.addVariable('backcolor','0x8FACDE');
					obj.addVariable('lightcolor','0xFFFFFF');
					obj.addVariable('autostart',self.autoload);
					obj.write(self.audioDiv(elm));
				}
				else {return false;}
			}
		});
		//alert(ajax.exists);
			
		
	},
	unload: function(elm) {
		elm = Element.firstDescendant($(this.tabID(elm)));
		if(elm.hasClassName('audioContainer')) {
			Element.remove(elm);
		}
	},
	tabID: function(elm) {
		return elm.href.match(/#(\w.+)/)[1];
	},
	audioDiv: function(elm) {
		var tab = this.tabID(elm);
		var div = new Element('div', { 'class': 'audioContainer'});
		Element.insert(tab,{top: div});
		return div;
	},
	getInitialTab : function() {
		if(document.location.href.match(/#(\w.+)/)) {
			var loc = RegExp.$1;
			var elm = this.menu.find(function(value) { return value.href.match(/#(\w.+)/)[1] == loc; });
			return elm || this.menu.first();
		} else {
			return this.menu.first();
		}
	}
}
function startPerformers() {
	var mainTabs = new Fabtabs('tabs');
	var peopleTabs = new Fabtabs('peopleTabs');
	var performerTabs = new Fabtabs('performerTabs');
	var performers = new audio('performerTabs');
	var optionsTab = new Fabtabs('optionsTabs');	
}
if(window.attachEvent) {
	Event.observe(window,'load',startPerformers,false);
}
else {
	Event.observe(window,'DOMContentLoaded',startPerformers,false);
}
