var mootabs = new Class({
                Implements: [Options, Events],
                options: {
                        width:                          '300px',
                        height:                         '300px',
                        changeTransition:       Fx.Transitions.linear,
                        duration:                       500,
                        mouseOverClass:         'active',
                        activateOnLoad:         'first',
                        useAjax:                        false,
                        ajaxUrl:                        '',
                        ajaxOptions:            { method:'get' },
                        ajaxLoadingText:        'Loading...'
                },
               
                initialize: function(element, options) {
                        this.setOptions(options);
                        this.el = $(element);
                        this.elid = element;
                        this.el.setStyles({
                                // height: this.options.height, //jwib change
                                width: this.options.width
                        });
               
                        this.titles = $$('#' + this.elid + ' ul.mootabs_title li');
                        this.panelHeight = this.el.getSize().y - (this.titles[0].getSize().y + 4);
                        this.panels = $$('#' + this.elid + ' .mootabs_panel');
               
                        // this.panels.setStyle('height', this.panelHeight); //jwib change
               
                        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); //jwib change
                                        this.activate(item); //jwib change
                                }.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 = $get('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').filter('[title='+tab+']')[0];
                        tab = myTab;
                }
                if($type(tab) == 'element'){
                        var newTab = tab.getProperty('title');
                        this.panels.removeClass('active');
                        this.activePanel = $(newTab);
                       
                        this.activePanel.addClass('active');
                        if(this.options.changeTransition != 'none' && skipAnim==false){
                        	//updated 01/04/2008 j wiberley to change transition to fade
                        		$each(this.panels,function(p,idx,obj){obj[idx].setStyles({opacity:0});},this.panels);
                                //this.activePanel.setStyle('height', 0);
                                this.activePanel.setStyle('opacity', 0);
                                var changeEffect = new Fx.Elements(this.activePanel, {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.set('text', this.options.ajaxLoadingText);
                var newOptions = { url: this.options.ajaxUrl + '?tab=' + this.activeTitle.get('title'), update: this.activePanel.get('id') } ;
                this.options.ajaxOptions = $extend(this.options.ajaxOptions, newOptions);
                var req = new Request.HTML(
                        this.options.ajaxOptions
                ).send();
        },
       
        addTab: function(title, label, content){
                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));

                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').filter('[title='+title+']')[0].dispose();
                $$('#' + this.elid + ' .mootabs_panel').filter('[id='+title+']')[0].dispose();
        },
       
        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);
        }
});

/*
Function: $get
	This function provides access to the "get" variable scope + the element anchor

Version: 1.3

Arguments:
	key - string; optional; the parameter key to search for in the url's query string (can also be "#" for the element anchor)
	url - url; optional; the url to check for "key" in, location.href is default

Example:
	>$get("foo","http://example.com/?foo=bar"); //returns "bar"
	>$get("foo"); //returns the value of the "foo" variable if it's present in the current url(location.href)
	>$get("#","http://example.com/#moo"); //returns "moo"
	>$get("#"); //returns the element anchor if any, but from the current url (location.href)
	>$get(,"http://example.com/?foo=bar&bar=foo"); //returns {foo:'bar',bar:'foo'}
	>$get(,"http://example.com/?foo=bar&bar=foo#moo"); //returns {foo:'bar',bar:'foo',hash:'moo'}
	>$get(); //returns same as above, but from the current url (location.href)
	>$get("?"); //returns the query string (without ? and element anchor) from the current url (location.href)

Returns:
	Returns the value of the variable form the provided key, or an object with the current GET variables plus the element anchor (if any)
	Returns "" if the variable is not present in the given query string

Credits:
		Regex from [url=http://www.netlobo.com/url_query_string_javascript.html]http://www.netlobo.com/url_query_string_javascript.html[/url]
		Function by Jens Anders Bakke, webfreak.no
*/
function $get(key,url){
	if(arguments.length < 2) url =location.href;
	if(arguments.length > 0 && key != ""){
		if(key == "#"){
			var regex = new RegExp("[#]([^$]*)");
		} else if(key == "?"){
			var regex = new RegExp("[?]([^#$]*)");
		} else {
			var regex = new RegExp("[?&]"+key+"=([^&#]*)");
		}
		var results = regex.exec(url);
		return (results == null )? "" : results[1];
	} else {
		url = url.split("?");
		var results = {};
			if(url.length > 1){
				url = url[1].split("#");
				if(url.length > 1) results["hash"] = url[1];
				url[0].split("&").each(function(item,index){
					item = item.split("=");
					results[item[0]] = item[1];
				});
			}
		return results;
	}
}