/* Tabas Script 
 * Requires Prototype and Script.aculo.us Libraries
 * By: Henry YP Ho @ monKiKi.st
 * http://www.monkiki.st
 * This work is licensed under the Creative Commons Attribution-Share Alike 3.0
 * http://creativecommons.org/licenses/by-sa/3.0/us/
 */
 
 if (typeof Effect == 'undefined')
  throw("You must have the script.aculo.us library to use this Tabas");


var Tabas = Class.create({
 
 
 
 	initialize:function(id, options) {
		
		if (!$(id)) throw("id does not exist!");
		//set the default holding div
		this.taba = $(id);
		//set the extra options
		this.setOptions(options);
		//set the tabs and tab content
		this.controls = this.taba.select('div#'+this.options.tabBut);
		
		//get the extra controls
		this.extraControls = this.taba.select('a.'+this.options.extraBut);
		
		//get the content array
		this.contentDiv = this.taba.select('div#'+this.options.tabContent);
		this.tabContents = this.contentDiv[0].select('div.'+this.options.contentClass);
		
		

		// initialise the click handler
        var clickHandler =  this.clickHandler.bindAsEventListener(this);
        this.controls[0].observe('click', clickHandler);		
		//get all the extra controls
		this.extraControls.each(function (eH) {
			
			eH.observe ('click',clickHandler);
										  
		});
		
		//setting the default content
		this.toggleTab (this.tabContents[this.options.defaultDisplay]);
		
	},
	
	//set the extra options for the class
  	setOptions: function(options) {
		this.options = {
			//tab buttons
			tabBut: 'tabs',
			//extra button within the tabs
			extraBut: 'extratab',
			//content
			tabContent: 'tabscontent',
			//class for the content
			contentClass:'tabscontentclass',
			//attribute used to identify the target content at the tab buttons
			targetContent: 'targetcontent',
			//set the default to
			defaultDisplay: 0
    	};
		Object.extend(this.options, options || {});
	},
	
	
	//handle the clicking of the tab buttons
    clickHandler: function(e) {
        var el = e.element();
		var aLink = el.up();
		
        if(aLink.readAttribute(this.options.targetContent)) {
            
			// toggle content
			this.toggleTab (aLink.readAttribute(this.options.targetContent));
        }
    },
	
	//toggle the content
	toggleTab:function(targetC)  {
		//throw error message if target does not exist
		if (!$(targetC)) throw('The target does not exist');
		//
		this.tabContents.each (function(s){
			if ($(targetC) == $(s)) {
				s.show();
			}
			else {
				s.hide();
				
			}
		}.bind(this));
	}
	
});




