/**
 * The tabpane controller
 * 
 * @author Internet Architects BVBA
 * @copyight __copyight__
 * @version __version__
 * @requires IAJS.CONTROLLER.BaseController
 */ 
 /**
  * @constructor
  * @base IAJS.CONTROLLER.BaseController
  * @param {HTMLelement} node The eltype element to create the controller for
  */
IAJS.CONTROLLER.Tabpane = function(node) {
	this.node = node;
	this.tabView;
	this.objectType = "Tabpane";
	
	this.events = new IAJS.EventList(this);
	/**
	 * Fires after the value is changed.
	 * @name onChange
	 * @memberOf IAJS.CONTROLLER.Tabpane
	 */	
	this.events.add("onChange");
}
YAHOO.extend(IAJS.CONTROLLER.Tabpane, IAJS.CONTROLLER.BaseController);
/**
 * Initialization of the controller
 */
IAJS.CONTROLLER.Tabpane.prototype.init = function(){	

	var nav = $YD.getElementsByClassName('tabNav', "ul", this.node);
	if(nav.length == 0){ return; }
	//
	//tabHeading
	this._hideTabHead();
	//
	this.navElm = $YD.getElementsByClassName("tabHandler", "a", nav[0]);
	var navActive = $YD.getChildrenBy(nav[0], function(elm){ return $YD.hasClass(elm, "active" ); });
	
	if(navActive.length > 0){
		this._setTabs( $YD.getElementsByClassName("tabHandler", "a", navActive[0])[0]  );
	} else {
		this._setTabs( this.navElm[0]  );
	}
	//
	for(var i=0, l =this.navElm.length ; i < l ; i++){
		var tabElmid = this.navElm[i].getAttribute('href').substring(1);
		var tabElm = document.getElementById(tabElmid);
		$YE.on(this.navElm[i], "click", this._tabChangeHandler, this, true);
	}
}
IAJS.CONTROLLER.Tabpane.prototype._hideTabHead = function(){
	var main = $YD.getElementsByClassName('tabMain', "div", this.node);
	for(var i=0; i < main.length; i++){
		$YD.getElementsByClassName('tabHeading', null, main[i], function(el){el.style.display = "none";});
	}
	
}
/**
 * Handler for tab changes
 * @private
 */
IAJS.CONTROLLER.Tabpane.prototype._tabChangeHandler = function(e){
	$YE.preventDefault(e);
	var target = $YE.getTarget(e);
	var targetNav = $YD.getAncestorByClassName(target, "tabHandler");
	this._setTabs(targetNav);
	this.events.fire('onChange', this );
}

IAJS.CONTROLLER.Tabpane.prototype._setTabs = function(activeNav){
	var targetTabId = activeNav.getAttribute('href').substring(1);
	targetTabId = targetTabId.substring(targetTabId.lastIndexOf("#") + 1); // FF = "#the_href_value", IE = "fullURL/#the_href_value" (is this only with file://url/#the_href_value ?)
	
	for(var i=0, l=this.navElm.length ; i < l ; i++){
		var tabId = this.navElm[i].getAttribute('href').substring(1);
		tabId = tabId.substring(tabId.lastIndexOf("#") + 1); // FF = "#the_href_value", IE = "fullURL/#the_href_value" (is this only with file://url/#the_href_value ?)
		var paneElm = document.getElementById(tabId);
		var navElmLi =  $YD.getAncestorByTagName(this.navElm[i], "li");
		
		if( targetTabId == tabId ){
			$YD.addClass(navElmLi, "active");
			paneElm.style.display = "block";
		} else {
			$YD.removeClass(navElmLi, "active");
			paneElm.style.display = "none";
		}
	}
}
