var mootabs = new Class({
	
	initialize: function(element, options) {
		this.options = Object.extend({
			width:				'300px',
			height:				'200px',
			changeTransition:	Fx.Transitions.linear,
			duration:			500,
			mouseOverClass:		'active',
			activateOnLoad:		'first',
			useAjax: 			false,
			ajaxUrl: 			'',
			ajaxOptions: 		{method:'get'},
			ajaxLoadingText: 	'Loading...'
		}, options || {});
		
		this.el = $(element);
		this.elid = element;
		this.ranonce = false;
		
		this.el.setStyles({
			//height: this.options.height, //removed to allow auto height
			
			width: this.options.width
		});
		
		this.titles = $$('#' + this.elid + ' ul.mootabs_title li');
		this.panelHeight = this.el.getSize().size.y - (this.titles[0].getSize().size.y + 4);
		this.panels = $$('#' + this.elid + ' .mootabs_panel');

		
			
		
		//this.panels.setStyle('height', this.panelHeight);//removed to allow auto height
		
		

		this.titles.each(function(item) {
			item.addEvent('click', function(){
				var newTab = item.getProperty('class');
				if(newTab == 'active') return false; //added 01/04/2008 by j.wiberley to stop it reloading current tab if clicked again
					item.removeClass(this.options.mouseOverClass);
					this.activate(item);
				}.bind(this)
			);
			
			item.addEvent('mouseover', function() {
				if(item != this.activeTitle)
				{
					item.addClass(this.options.mouseOverClass);
				}
			}.bind(this));
			
			item.addEvent('mouseout', function() {
				if(item != this.activeTitle)
				{
					item.removeClass(this.options.mouseOverClass);
				}
			}.bind(this));
		}.bind(this));
		
		
		if(this.options.activateOnLoad != 'none')
		{
			//added 01/04/2008 to allow active tab to come from the url
			var getTab = param('tab');
			if(getTab)
			{
				//alert(getTab);
				//replacedTab = getTab.replace(/-/," ");
				//alert(replacedTab);
				//alert(this.titles.length);
				
				for(f=0;f<this.titles.length; f++)
				{
					//alert(this.titles[f].getProperty('title'));
					if(this.titles[f].getProperty('title') == getTab)
					{
						//alert(f);
						this.activate(this.titles[f], true);
						this.ranonce = true;
					}
					
				}
				//if more than one tab section per page set the rest to first tab, 
				//could update it to allow for all to be set vis url at some point
				//01/04/2008 jwiberley
				if(this.ranonce==false)
				{
					this.activate(this.titles[0], true);
				}
			} else 
			{
			
					if(this.options.activateOnLoad == 'first')
					{
						this.activate(this.titles[0], true);
					}
					else
					{
						this.activate(this.options.activateOnLoad, true);	
					}

					

			}



			/*===============14/04/2008 open tan with first class*/
			for(f=0;f<this.titles.length; f++)
				{
					//alert(this.titles[f].getProperty('title'));
					if(this.titles[f].getProperty('class') == 'open')
					{
						//alert(f);
						this.activate(this.titles[f], true);
					//	alert("ACTIVE "+this.titles[f].getProperty('title'));
					}
					
				}
		}
	},
	
	activate: function(tab, skipAnim){
		if(! $defined(skipAnim))
		{
			skipAnim = false;
		}
		if($type(tab) == 'string') 
		{
			myTab = $$('#' + this.elid + ' ul li').filterByAttribute('title', '=', tab)[0];
			tab = myTab;
		}
		
		if($type(tab) == 'element')
		{
			var newTab = tab.getProperty('title');
			this.panels.removeClass('active');
			
			this.activePanel = this.panels.filterById(newTab)[0];
			
			this.activePanel.addClass('active');
			
			if(this.options.changeTransition != 'none' && skipAnim==false)
			{
				
				//updated 01/04/2008 j wiberley to change transition to fade
				this.panels.filterById(newTab).setStyle('opacity', 0);
				var changeEffect = new Fx.Elements(this.panels.filterById(newTab), {duration: this.options.duration, transition: this.options.changeTransition});
				changeEffect.start({
					'0': {
						//'height': [0, this.panelHeight]
						'opacity': 1
					}
				});
			}
			
			this.titles.removeClass('active');
			
			tab.addClass('active');
			
			this.activeTitle = tab;
			
			if(this.options.useAjax)
			{
				this._getContent();
			}
		}
	},
	
	_getContent: function(){
		this.activePanel.setHTML(this.options.ajaxLoadingText);
		var newOptions = {update: this.activePanel.getProperty('id')};
		this.options.ajaxOptions = Object.extend(this.options.ajaxOptions, newOptions || {});
		var tabRequest = new Ajax(this.options.ajaxUrl + '?tab=' + this.activeTitle.getProperty('title'), this.options.ajaxOptions);
		tabRequest.request();
	},
	
	addTab: function(title, label, content){
		//the new title
		var newTitle = new Element('li', {
			'title': title
		});
		newTitle.appendText(label);
		this.titles.include(newTitle);
		$$('#' + this.elid + ' ul').adopt(newTitle);
		newTitle.addEvent('click', function() {
			
			this.activate(newTitle);
		}.bind(this));
		
		newTitle.addEvent('mouseover', function() {
			if(newTitle != this.activeTitle)
			{
				newTitle.addClass(this.options.mouseOverClass);
			}
		}.bind(this));
		newTitle.addEvent('mouseout', function() {
			if(newTitle != this.activeTitle)
			{
				newTitle.removeClass(this.options.mouseOverClass);
			}
		}.bind(this));
		//the new panel
		var newPanel = new Element('div', {
			'style': {'height': this.options.panelHeight},
			'id': title,
			'class': 'mootabs_panel'
		});
		if(!this.options.useAjax)
		{
			newPanel.setHTML(content);
		}
		this.panels.include(newPanel);
		this.el.adopt(newPanel);
	},
	
	removeTab: function(title){
		if(this.activeTitle.title == title)
		{
			this.activate(this.titles[0]);
		}
		$$('#' + this.elid + ' ul li').filterByAttribute('title', '=', title)[0].remove();
		
		$$('#' + this.elid + ' .mootabs_panel').filterById(title)[0].remove();
	},
	
	next: function(){
		var nextTab = this.activeTitle.getNext();
		if(!nextTab) {
			nextTab = this.titles[0];
		}
		this.activate(nextTab);
	},
	
	previous: function(){
		var previousTab = this.activeTitle.getPrevious();
		if(!previousTab) {
			previousTab = this.titles[this.titles.length - 1];
		}
		this.activate(previousTab);
	}
});

Array.extend({
	foldl: function(binaryOperator, startingValue){
		var reduction = startingValue;
		this.each( function(item){
			reduction = binaryOperator(reduction, item);
		});
		return reduction;
	}
});
 
var params = window.location.search.substring(1).split('&').map(function(x){return x.split('=')}).foldl(function(h,a){
	return h.set(a[0], a[1]);
}, new Hash());
 
function param(key, defaultValue){
	return params.get(key) || defaultValue;
}