


// Copyright (c) NimbleCat, 2008

/*
* this object implements a tabview
* 
* */
// make sure that the required includes are there
 if (typeof NC == 'undefined') {
       alert("TabView.js requires the NC JavaScript framework");
 }
 
 if (typeof NC.widget == 'undefined') {
       alert("TabView.js requires the NC JavaScript framework");
 }
 
 /*
  * constructor
  */
  
  NC.widget.TabView = function( id, contentContainer) {
		var fnSuperClass = NC.widget.TabView.superclass.constructor;
		this.contentContainer = contentContainer;
		fnSuperClass.call( this, id);
  }
  
  NC.widget.TabView.SelectedTabClass = "selected";
  
  NC.widget.TabView.selectTab = function( tabContainerId, tab) {
  	/*
  	 * Some of this code is duplicated in tabMouseUp - remove from there to here eventually
  	 */
		if (tab == null) {
			return;
		}
		var tabcontainer = document.getElementById( tabContainerId); 
		if (tabcontainer != null) {
	       	var tabs = tabcontainer.getElementsByTagName("li");
	       	for ( var i = 0; i < tabs.length; i++) {
	       		if (tabs[i] != tab) {
	       			NC.util.Dom.removeClass( tabs[i], NC.widget.TabView.SelectedTabClass);
	       		}
	       	}
			NC.util.Dom.addClass( tab, NC.widget.TabView.SelectedTabClass);
		}
  	
  }
  	
  NC.widget.TabView.tabMouseUp = function( event) {
		var target  = NC.core.Event.getEventTarget(event);
		// find the enclosing tab(li) and then the enclosing tabview id by marching up the tree
		var tabcontainer = NC.util.Dom.getParentByType(target,"ul");
		if (tabcontainer != null) {
	       	var tabs = tabcontainer.getElementsByTagName("li");
			var tab = NC.util.Dom.getParentByType(target,"li");
	       	for ( var i = 0; i < tabs.length; i++) {
	       		if (tabs[i] != tab) {
	       			NC.util.Dom.removeClass( tabs[i], NC.widget.TabView.SelectedTabClass);
	       		}
	       	}
			NC.util.Dom.addClass( tab, NC.widget.TabView.SelectedTabClass);
			var tabview = NC.util.Dom.getParentByType(tabcontainer,"div");
			var oTabView = NC.widget.ElementManager.lookup( tabview.id);
			if (oTabView != null) {
				oTabView.onTabSelected( event, tab);
			}
		}
  }
  
  NC.widget.TabView.tabMouseOver = function( event) {
		var target  = NC.core.Event.getEventTarget(event);
		// find the enclosing tab(li) and then the enclosing tabview id by marching up the tree
		var tabcontainer = NC.util.Dom.getParentByType(target,"ul");
		if (tabcontainer != null) {
	       	var tabs = tabcontainer.getElementsByTagName("li");
			var tab = NC.util.Dom.getParentByType(target,"li");
			var tabview = NC.util.Dom.getParentByType(tabcontainer,"div");
			var oTabView = NC.widget.ElementManager.lookup( tabview.id);
			if (oTabView != null) {
				oTabView.onTabMouseOver( event, tab);
			}
		}
  }
		
  NC.widget.TabView.tabMouseOut = function( event) {
		var target  = NC.core.Event.getEventTarget(event);
		// find the enclosing tab(li) and then the enclosing tabview id by marching up the tree
		var tabcontainer = NC.util.Dom.getParentByType(target,"ul");
		if (tabcontainer != null) {
	       	var tabs = tabcontainer.getElementsByTagName("li");
			var tab = NC.util.Dom.getParentByType(target,"li");
			var tabview = NC.util.Dom.getParentByType(tabcontainer,"div");
			var oTabView = NC.widget.ElementManager.lookup( tabview.id);
			if( oTabView != null) {
				oTabView.onTabMouseOut( event, tab);
			}
		}
  }
  
  NC.lang.extend(NC.widget.TabView, NC.widget.Element, 
  	{


	  	init: function() {
            NC.widget.TabView.superclass.init.call(this);
            var tabDiv = document.getElementById( this.domId);
			NC.widget.Element.addElementDomEvent( tabDiv, NC.core.EventType.Mouseup, NC.widget.TabView.tabMouseUp, false);
			NC.widget.Element.addElementDomEvent(tabDiv, NC.core.EventType.Mouseover, NC.widget.TabView.tabMouseOver, false);
			NC.widget.Element.addElementDomEvent(tabDiv, NC.core.EventType.Mouseout, NC.widget.TabView.tabMouseOut, false);
			NC.widget.ElementManager.register( this.domId, this);
	  		return "init TabView function";
	  	},
	  	/*
	  	 * this function must be overridden by an subclass
	  	 */
	  	onTabSelected: function( event, element) {
	  	},

	  	onTabMouseOver: function( event, element) {
	  	},

	  	onTabMouseOut: function( event, element) {
	  	},


 		render:function() {
 			return "render";
 		}
	  	
  	});
