// Tabscriptaculous
// (C) PC Help 2006

// Requires: Prototype, Scriptaculous
// Last update: 12/12/2006

var Tabscriptaculous = Class.create();
Tabscriptaculous.prototype = {
	initialize: function(container,id) {
		this.id = id;
		this.container = container;
		var allTabs = document.getElementsByClassName("tab");
		var labels = document.createElement('div');
//		labels.setAttribute('class','tab-labels-container');
		Element.addClassName(labels,'tab-labels-container');

		var selected = document.location.href.split('#')[1];

		for(i = 0; i < allTabs.length; i++) {
			if (allTabs[i].parentNode == container) {
				if (typeof this.labels == "undefined") {
					container.insertBefore(labels,allTabs[i]);
					this.labels = labels;
				}
				var label = null;
				var a = null;
				label = document.createElement('div');
//				label.setAttribute('class','label');
				Element.addClassName(label,'label');
				labels.appendChild(label);
				a = document.createElement('a');
				a.setAttribute('href',"#" + allTabs[i].getAttribute('id'));
				a.tabgroup = this;
				label.tabgroup=this;
				a.tab = allTabs[i];
				Element.hide(a.tab);
				a.onclick = function() {this.tabgroup.swap(this); return false;};
				txt = document.createTextNode(allTabs[i].getAttribute('title'));
				label.appendChild(a);
				allTabs[i].label = label;
				allTabs[i].a = a;
				allTabs[i].tabgroup = this;
				a.appendChild(txt);
				a.label = label;
				label.onmouseover= function() {
					if (this.tabgroup.selected.label != this) {
						Element.addClassName(this,'over');
					}
				};
				label.onmouseout= function() {
					if (this.tabgroup.selected.label != this) {
					Element.removeClassName(this,'over');
					}
				};

				if (typeof selected != "undefined" && selected == allTabs[i].getAttribute('id')) {
					this.selected = allTabs[i];
				}
			}
		}
		if (typeof this.selected == "undefined") {
			this.selected = allTabs[0];
		}
		Element.show(this.selected);
		Element.addClassName(this.selected.label,'selected');
		if (typeof this.selected.getAttribute('beforeswap') != "undefined") { eval (this.selected.getAttribute('beforeswap')); };
		labels=null;
	},
	swap: function(a) {
		if (a.tab != this.selected) {
			if (typeof this.selected.getAttribute('afterswap') != "undefined") { eval (this.selected.getAttribute('afterswap')); };
			new Effect.Parallel([
				new Effect.BlindUp(this.selected, {sync:true}),
				new Effect.BlindDown(a.tab, {sync:true})
			],{
				duration:1,
				transition: Effect.Transitions.SwingTo,
				queue:{
					position:'end',
					scope:('tabscriptaculous' + this.id)
				},
				beforeStart: function(effect) {
					effect.effects[1].element.tabgroup.swapped(effect.effects[1].element);
				}
			  }
			);
			if (typeof a.tab.getAttribute('beforeswap') != "undefined") { eval (a.tab.getAttribute('beforeswap')); };
		}
	},
	swapped: function(tab) {
		Element.removeClassName(this.selected.label,'selected');
		this.selected=tab;
		Element.removeClassName(this.selected.label,'over');
		Element.addClassName(this.selected.label,'selected');
	}
}
function initTabscriptaculous() {
	allTabContainers = document.getElementsByClassName("tab-container");
	for(i = 0; i < allTabContainers.length; i++) {
		new Tabscriptaculous(allTabContainers[i],i);
	}
}
Event.observe(window, 'load', initTabscriptaculous, false);
//new domFunction(initTabscriptaculous);