/* -------------------------------------------------------------------------- */
/** 
 *    @fileoverview
 *       make an object to be observable.
 *
 *    @version rev001.2007-02-04
 *    @requires interactive.js
 */
/* -------------------------------------------------------------------------- */


function tabItem() {
	this.callBackChains = {};
	this.id = "";
	this.className = "active";
	this.available = false;
	this.imageSuffix = "_s";
	this.node = null;
	this.images = [];
}
tabItem.prototype = new interactiveItem;
tabItem.prototype.enable = function() {
	this.available = true;
	if (this.node) {
		this.node.appendClassNameBA(this.className);
		for (var i = 0, n = this.images.length; i < n; i++) {
			this.images[i].node.src = this.images[i].active;
		}
	}
}
tabItem.prototype.disable = function() {
	this.available = false;
	if (this.node) {
		this.node.removeClassNameBA(this.className);
		var reg = new RegExp(this.activeImage + ".");
		for (var i = 0, n = this.images.length; i < n; i++) {
			this.images[i].node.src = this.images[i].normal;
		}
	}
}

/**
 * tabPanel
 * $Id$
 *
 * @constructor
 */
function tabPanel() {
	this.callBackChains = {};
	this.id = "tab";
	this.nextClass = "next";
	this.lastClass = "last";
	this.specialNextClass = "cart-next";
	this.specialLastClass = "cart-last";
	this.tabs = [];
	this.items = [];
	this.switchers = [];
}
tabPanel.prototype = new interactiveManager;
tabPanel.prototype.addItem = function(tab, item, switcher) {
	var id = this.items.length;

	item.setId(id);
	item.disable();
	this.items[id] = item;

	tab.setId(id);
	tab.disable();
	this.tabs[id] = tab;

	switcher.setId(id);
	switcher.disable();
	switcher.setCallBack("change", this.callBack, this);
	switcher.node.addEventListenerBA("click", switcher.callBack, switcher);
	switcher.node.addEventListenerBA("click", function(){this.blur()}, switcher.node);
	this.switchers[id] = switcher;

	if (item.node.hasClassNameBA("current")) {
		this.select(id);
	}
}
tabPanel.prototype.select = function(id) {
	if (this.items[id].isAvailable()) {
		return;
	}
	var next = (this.items[id] && this.items[id].className == "cart-active") ? this.specialNextClass : this.nextClass;
	var last = (this.items[id] && this.items[id].className == "cart-active") ? this.specialLastClass : this.lastClass;
	for (var i = 0, n = this.items.length; i < n; i++) {
		this.tabs[i].node.removeClassNameBA(this.nextClass);
		this.tabs[i].node.removeClassNameBA(this.specialNextClass);
		if (this.items[i].getId() == id) {
			this.items[i].enable();
			this.tabs[i].enable();
		} else if (this.items[i].getId() == id+1) {
			this.items[i].disable();
			this.tabs[i].disable();
			this.tabs[i].node.appendClassNameBA(next);
		} else {
			this.items[i].disable();
			this.tabs[i].disable();
		}
	}
	this.tabContainer.removeClassNameBA(this.lastClass);
	this.tabContainer.removeClassNameBA(this.specialLastClass);
	if (!this.items[id+1]) {
		this.tabContainer.appendClassNameBA(last);
	}
}

BAAppendCSS("/shared/css/mod_tabpanel.css", "", "screen");
BAAddOnload(function () {
	var tabClass = "tabs";
	var nodes = document.getElementsByClassNameBA(tabClass, "ul");

	for (var i = 0, n = nodes.length; i < n; i++) {
		var tabNodes = nodes[i].getElementsByTagNameBA("li");
		var manager = new tabPanel;
		manager.tabContainer = nodes[i];
		for (var j = 0, m = tabNodes.length; j < m; j++) {
			var link = tabNodes[j].getElementsByTagNameBA("a")[0];
			var hash = link.getAttributeBA("href");
			if (!hash) {
				continue;
			}
			hash = (hash.split("#"))[1];
			var panelNode = document.getElementById(hash);
			if (!panelNode) {
				continue;
			}
			var tab = new tabItem;
			tab.attachNode(tabNodes[j]);
			if (tab.node.hasClassNameBA("cart")) {
				tab.className = "cart-active";
				var images = tab.node.getElementsByTagNameBA("img");
				for (var i = 0, n = images.length; i < n; i++) {
					var normalImage = new Image;
					normalImage.src = images[i].src;
					var activeImage = new Image;
					activeImage.src = images[i].src.replace(/\./, tab.imageSuffix + ".");
					tab.images.push({node : images[i], normal : normalImage.src, active : activeImage.src});
				}
			}

			var panel = new interactiveItem;
			panel.attachNode(panelNode);

			var switcher = new interactiveItem;
			switcher.attachNode(link);

			manager.addItem(tab, panel, switcher);
		}
		manager.select(0);
	}
});


