/*
	Tab class.  Takes an active class name, a container id.
	This class requires that prototype.js be included
*/
var tabs = Class.create({
	initialize: function(activeClass, container, tabElement){
		this.activeClassName = activeClass;
		this.container = container;
		if(tabElement)
			this.tabElement = tabElement;
		else
			this.tabElement = 'span';
		this.addEvent();
		this.showTab('');
   },
   /*
	Set up events.
   */
   addEvent: function(){
		$$('#' + this.container + ' > ' + this.tabElement).each(function(elem, index){          
			elem.observe('click',function(event){								
								event.stop();								
								this.toggleTab(event.element(), index);								
								}.bind(this)
						 );
		}.bind(this));
   },
   /*
	Toggle to a selected tab.
   */
   toggleTab: function(elem,i){	   
	$$('#' + this.container + ' > div').each(function(elem, index){
											if(index != i)
											   elem.setStyle({display: 'none'});
											else
											   elem.setStyle({display: 'inline'});
										});

   $$('#' + this.container + ' > '+ this.tabElement).each(function(elem, index){
										if(index != i)
				elem.removeClassName(this.activeClassName);
				
										else
											elem.addClassName(this.activeClassName);
									 }.bind(this)
									 );
   } ,
   /*
	This function searchs the tag elements for a specific class.  If it finds an element with that class it toggles that tab.
   */
   showTab: function(targetClass){	
	if(targetClass != ''){
		$$('#' + this.container + ' > ' + this.tabElement).each(function(elem, index){
			if (elem.hasClassName(targetClass)){
				this.toggleTab(elem, index);
				//window.location.hash = 'tab=' + index;
			}
		}.bind(this));
	}
	else{
		this.toggleTab($$('#' + this.container + ' > ' + this.tabElement)[0], 0);
	}
   }
});