/** 
* Copyright 2007 massimocorner.com
* @author      Massimo Foti (massimo@massimocorner.com)
* @version     2.0.1, 2007-02-20
* @require     tmt_core.js
* @require     tmt_css.js
*/

if(typeof(tmt) == "undefined"){
	alert("Error: tmt.core JavaScript library missing");
}

if(typeof(tmt.css) == "undefined"){
	alert("Error: tmt.css JavaScript library missing");
}

if(typeof(tmt.widget) == "undefined"){
	tmt.widget = {};
}

tmt.widget.tab = {};

tmt.widget.tab.SELECTED_CLASS = "tmtTabselected";

tmt.widget.tab.groupFactory = function(rootNode){
	var obj = {};
	obj.rootNode = rootNode;
	obj.currentPosition = 0;
	obj.tabs = [];
	var childDivs = obj.rootNode.getElementsByTagName("div");
	// First <div>, containing all <a> labels
	obj.labelsContainer = childDivs[0];
	var labelNodes = childDivs[0].getElementsByTagName("a");
	// Second <div>, containing all the panels
	obj.tabsContainer = childDivs[1];
	var innerDivs = obj.tabsContainer.getElementsByTagName("div");
	var panelDivs = tmt.filterNodesByAttributeValue("tmt:tabpanel", "true", innerDivs);
	// Be sure labels match panels
	if(labelNodes.length != panelDivs.length){
		alert("Error in tmt.widget.tab: the amount of labels doesn't match the amount of panels");
		return;
	}
	// Create panel objects
	for(var i=0; i<panelDivs.length; i++){
		var childPanel = tmt.widget.tab.panelFactory(obj, panelDivs[i], labelNodes[i], i);
		obj.tabs.push(childPanel);
	}

	// Open the tab in the given position (zero based)
	obj.go = function(position){
		if(obj.checkPositionRange(position)){
			for(var j=0; j<obj.tabs.length; j++){
				obj.tabs[j].hide();
			}
			obj.tabs[position].show();
			obj.currentPosition = position;
		}
	}

	// Open next tab
	obj.next = function(){
		var nextPosition = parseInt(obj.currentPosition) + 1;
		// If we reached the final tab, go to first tab
		if(nextPosition == (obj.tabs.length)){
			nextPosition = 0;
		}
		obj.go(nextPosition);
	}

	// Open previous tab
	obj.previous = function(){
		var prevPosition = parseInt(obj.currentPosition) - 1;
		// If we reached the first tab, go to last tab
		if(prevPosition < 0){
			prevPosition = obj.tabs.length -1;
		}
		obj.go(prevPosition);
	}

	// Check if we have a tab inside the given position. Alert otherwise
	obj.checkPositionRange = function(position){
		if((position < 0) || (position > obj.tabs.length)){
			alert("Error: tab position " + position + " is out of range");
			return false;
		}
		return true;
	}

	return obj;
}

tmt.widget.tab.panelFactory = function(containerObj, divNode, aNode, index){
	var obj = {};
	obj.container = containerObj;
	obj.panelNode = divNode;
	obj.labelNode = aNode;
	obj.position = index;
	obj.show = function(){
		tmt.css.displayBlock(obj.panelNode);
		tmt.css.addClass(obj.labelNode, tmt.widget.tab.SELECTED_CLASS);
	}
	obj.hide = function(){
		tmt.css.displayNone(obj.panelNode);
		tmt.css.removeClass(obj.labelNode, tmt.widget.tab.SELECTED_CLASS);
	}
	obj.labelNode.onclick = function(){
		// Controllo aggiunto per supportate il paramentro HREF nei tab per l'apertura di un altra pagina
		if(obj.labelNode.href == 'javascript:;'){
			obj.container.go(obj.position);
		}
	}
	return obj;
}

tmt.widget.tab.init = function(){
	var divNodes = document.getElementsByTagName("div");
	var panelNodes = tmt.filterNodesByAttributeValue("tmt:tabgroup", "true", divNodes);
	for(var i=0; i<panelNodes.length; i++){
		panelNodes[i].tmtTabGroup = tmt.widget.tab.groupFactory(panelNodes[i]);
	}
}

// Global object storing utility methods
tmt.widget.tab.util = {};

/**
* Private method. Given a <div> node or its id, return its tabs object
*/
tmt.widget.tab.util.getObjFromNode = function(containerNode){
	var targetNode = tmt.get(containerNode);
	if(!targetNode){
		alert("Error: unable to find the requested tab group");
		return null;
	}
	var targetObj = targetNode.tmtTabGroup;
	if(!targetObj){
		alert("Error: the request element is not a tab group. Verify it contains the tmt:tabgroup attribute");
		return null;
	}
	return targetObj;
}

/**
* Given a <div> node or its id, open the required tab (tab position is zero based)
*/
tmt.widget.tab.util.goTo = function(containerNode, position){
	var tabGroupObj = tmt.widget.tab.util.getObjFromNode(containerNode);
	tabGroupObj.go(position);
}

/**
* Given a <div> node or its id, open the next available tab
*/
tmt.widget.tab.util.next = function(containerNode){
	var tabGroupObj = tmt.widget.tab.util.getObjFromNode(containerNode);
	tabGroupObj.next();
}

/**
* Given a <div> node or its id, open the previous available tab
*/
tmt.widget.tab.util.previous = function(containerNode){
	var tabGroupObj = tmt.widget.tab.util.getObjFromNode(containerNode);
	tabGroupObj.previous();
}

tmt.addEvent(window, "load", tmt.widget.tab.init);
