/**
 * @fileoverview tabs.js
 * Widget.Tabs creates a tabview control from existing markup.<br />
 * Requires Prototype (http://www.prototypejs.org) 1.6 or later as well as Widget.Activator<br /><br />
 *
 * Copyright (c) 2008 Marc Heiligers (marc@eternal.co.za) http://www.eternal.co.za<br /><br />
 */

var Widget = window.Widget || {};
Widget.Tabs = Class.create({
	initialize: function(element, options) {
		this.element = $(element);

		this.activator = new Widget.Activator("li", {
			container: this.element
		});

		this.activator.getElements("*").each(function(e) {
			$$(e.down("a[href]").getAttribute("href")).invoke("hide");
		}.bind(this));

		this.activator.observe(this.activator.options.events.mouseover, this.show.bind(this));
		this.show(0);
	},
	show: function(e) {
		var tab;
		if(Object.isNumber(e) || Object.isString(e)) {
			tab = this.activator.getElement(e);
		} else {
			tab = this.activator.isElement(e.memo.element);
		}
		if(!tab) return;

		if(this.selected) {
			this.activator.setSelected(this.selected, false);
			$$(this.selected.down("a[href]").getAttribute("href")).invoke("hide");
		}
		this.selected = tab;
		this.activator.setSelected(this.selected, true);
		$$(this.selected.down("a[href]").getAttribute("href")).invoke("show");
	}
});
