/*
 * Fabtabulous! Simple tabs using Prototype
 * http://tetlaw.id.au/view/blog/fabtabulous-simple-tabs-using-prototype/
 * Andrew Tetlaw
 * version 1.1 2006-05-06
 * http://creativecommons.org/licenses/by-sa/2.5/
 * heavily customized by Scott Gillam
 * http://pushdesign.ca
 */
var Fabtabs = Class.create();
var ek=new String();
var sindex = new Number();
var elmCon = new String();
var convar = 'content';
var subvar = 'sidenav';
var itemCount = Number(0);
var locindex = Number(0);
rightCorner = function(m){
	var setImage = 'url(../images/bkgd/rightcorner/'+picTitles[m]+'.gif)';
	if(picTitles[m]==undefined){
			setImage='url(../images/bkgd/rightcorner/'+picTitles[itemCount]+'.gif)';
	}
	document.getElementById('bigwrapper').style.backgroundImage = setImage;
	if( picTitles[m] =='minibooks'){
		$(document.getElementById('bigwrapper')).addClassName('minibooks');
	}else{
		$(document.getElementById('bigwrapper')).removeClassName('minibooks');
	}
}
rightCornerOver = function(m){
	var setImage = 'url(../images/bkgd/rightcorner/'+toplevelcat[m]+'.gif)';
	document.getElementById('bigwrapper').style.backgroundImage = setImage;
	document.getElementById('bigwrapper').style.backgroundImage = setImage;
	if( toplevelcat[m] =='minibooks'){
		$(document.getElementById('bigwrapper')).addClassName('minibooks');
	}else{
		$(document.getElementById('bigwrapper')).removeClassName('minibooks');
	}
}
allExcept = function(){
	$(ek).removeClassName('hide');
}
Fabtabs.prototype = {

	initialize : function(element) {
		this.element = $(element);
		var options = Object.extend({}, arguments[1] || {});
		this.menu = $A(this.element.getElementsByTagName('a'));
		this.show(this.getInitialTab());
		this.menu.each(this.setupTab.bind(this));
		$(convar).setStyle({
			overflow: 'auto',
			height: 'auto'
		});
		$(subvar).setStyle({
			overflow: 'auto',
			height: 'auto'
		});	
	},
	setupTab : function(elm) {
		$(this.tabID(elm)).index= itemCount;
		Event.observe(elm,'click',this.activate.bindAsEventListener(this),false)
		if (elm+'\/' != document.location.href){
			
			$(this.tabID(elm)).addClassName('hide');
		} else {
			moveDiv(0,30*itemCount+1*itemCount);
			var sectNum = Number($(elm).parentNode.parentNode.className.replace(' hide', ''));
			goAccordion('tabs', sectNum);
			document.title = pageTitleMain+catTitles[itemCount];
			rightCorner(itemCount);
			Effect.Fade('bkgd_rightcorner', { duration:.75, from:2.0, to:0.1 });
		}
		itemCount+=1;

	},
	activate :  function(ev) {
		var elm = Event.findElement(ev, "a");
		Event.stop(ev);
		var aindex = $(this.tabID(elm)).index;
		this.show(elm);
		this.menu.without(elm).each(this.hide.bind(this));
		moveDiv(0,30*aindex+1*aindex);
	},
	hide : function(elm) {
		$(elm).removeClassName('active-tab');
		$(this.tabID(elm)).addClassName('hide');
	},
	show : function(elm) {
		elmCon=elm;
		ek = this.tabID(elm);
		sindex = $(this.tabID(elm)).index;
		if( sindex == undefined ){sindex=locindex-1;if( sindex == -1 ){ sindex = 0;} }
		if($(elm).className != 'active-tab'){Effect.Fade($(ek), { duration:.75, from:-1.0, to:1.0 });Effect.Fade('bkgd_rightcorner', { duration:.75, from:2.0, to:0.1 });document.getElementById('bigwrapper').style.backgroundImage = 'none';setTimeout('allExcept()',100);rightCorner(sindex);}
		$(elm).addClassName('active-tab');
		//body.addClassName(catTitles[sindex]);
		document.location = elm+'\/';
		document.title ='The Lab Works : '+catTitles[sindex];
		
	},
	tabID : function(elm) {
		return elm.href.match(/#(\w.+)/)[1];
	},
	getInitialTab : function() {
		if(document.location.href.match(/#(\w.+)/)) {
			var loc = RegExp.$1;
			loc = (loc.replace('\/', ''));
			locindex = Number(loc.replace('tab',''));
			locindex = toplevelcat.indexOf(loc);
			var elm = this.menu.find(function(value) { return value.href.match(/#(\w.+)/)[1] == loc; });
			return elm || this.menu.first();

		} else {
			return this.menu.first();
		}
	}
}

Event.observe(window,'load',function(){ new Fabtabs('tabs'); },false);