/*
 * Author: Vertex Logic, Inc
 * Copyright 2005, 2006, 2007 - All rights reserved
 *
 * Vertex Logic, Inc., California, USA 
 *
 * Vertex Logic grants you ("Licensee") a non-exclusive license to use
 * and modify this source and recompile it in
 * accordance with the terms of the Agreement under which this software is bought
 * and provided that (i) this copyright notice appear on all copies of the Software; 
 * (ii) Licensee does not sale the software as is or with modification without
 * the prior consent of Vertex Logic. (iii) Licensee agrees that it does not have any 
 * title or ownership of the Software.
 *
 * The program is provided "as is" without any warranty express or
 * implied, including the warranty of non-infringement and the implied
 * warranties of merchantibility and fitness for a particular purpose.
 * Vertex Logic will not be liable for any damages suffered 
 * by you as a result of using the Program. 
 * In no event will Vertex Logic be liable for any
 * special, indirect or consequential damages or lost profits even if
 * Vertex Logic have been advised of the possibility of their occurrence. 
 * Vertex Logic will not be liable for any third party claims against you.
 */
 Af.HtmlTab = Class.create();

Af.HtmlTab.prototype = {
   initialize: function(thID, tabColumns, tabSheets, contentID){
      //debugA("Af.HtmlTab->initialize");
      this.currentTab = 0;
      this.tabSheets = new Array();
      this.tabColumns = new Array();
      this.listener = null;
      this.activeTabCLS = "TabSelected";
      this.normalTabCLS = "TabNormal";
      this._init(thID, tabColumns, tabSheets, contentID);
      this.tabSelectionListener = null;
   },
   _init: function(thID, tabColumns, tabSheets, contentID){
       //debugA("Af.HtmlTab->_init");
     
      // attach click event to all this.tabColumns elements
      var columnList = tabColumns.split(',') ;
      var tabList =  tabSheets.split(',');
      for (var i=0; i<columnList.length; i++) {
         columnList[i] = trim(columnList[i]);
      }
      this.columnList = columnList;
      this.validityFlags = Object();
      for (var i=0; i<tabList.length; i++) {
         tabList[i] = trim(tabList[i]);
         this.validityFlags[tabList[i]] = true;
      }
      this.header = $(thID);
      //var headerEle = this.header.childNodes[0];
      
      //removeAll(this.header);
      
      for(i=0;i<columnList.length;++i){
         //debugA(columnList[i]);
         var th = $(columnList[i]);//headerEle.cloneNode(true);
         var tc = $(tabList[i]);
         //alert(th);
         th.id = "tabH_"+i;
         //tc.id = "tabC_"+i;
        // th.innerText =  columnList[i]  ;
         this.tabColumns.push(th);
         this.tabSheets.push(tc);
         //this.header.appendChild(th);
         //  alert(th.onclick);
         th.onclick = this.tabSelected.bindAsEventListener(this);
         if(i==0) this._showTab(i);
         else this._hideTab(i);
      }
      
      this.tabList = tabList;

   },
   tabSelected: function(evt){
      //debugA("Af.HtmlTab->initialize");
      var e = evt.target ? evt.target : evt.srcElement;
	   //debugA("clicked:"+e.id);
      var tabId = e.id;
      if (tabId == this.currentTab) {
         return;
      }
      var prevTabIdx = this.currentTab;

      var sepIdx = tabId.lastIndexOf("_");
      this.currentTab = parseInt(tabId.substring(sepIdx+1));
      
      this._hideTab(prevTabIdx);
      this._showTab(this.currentTab);


      // call listener's tabSelected with prevIdx, currentTab
      return false;
      
   },
   _showTab: function(idx){
      //debugA("Af.HtmlTab->_showTab:"+idx);
      //debugA(this.tabSheets.length);
	  this.currentTab = idx;
      this.tabSheets[idx].style.display = "";
      //this.tabColumns[idx].className = this.activeTabCLS;

	  // CSS CLASS MUST HAVE SAME TAB NAME WITH _ON AT THE END  TO GET BETTER APPEARENCE
	  this.tabColumns[idx].className = this.columnList[idx]+"_on";				  			  
				
      if (this.tabSelectionListener != null && this.tabSelectionListener.tabSelected != null) {
         this.tabSelectionListener.tabSelected(this.currentTab, this.columnList[this.currentTab]);
      }

   },
   _hideTab: function(idx){
       //debugA("Af.HtmlTab->_hideTab:"+idx);
     
      this.tabSheets[idx].style.display = "none";
      //this.tabColumns[idx].className = this.normalTabCLS;
	  this.tabColumns[idx].className = this.columnList[idx]+"_off";

   },
   
   showTab: function(idx){
      this._hideTab(this.currentTab);
      this._showTab(idx);
   },
   
   next: function() {
      var n = this.currentTab + 1;
      for (var i=n; i<this.tabList.length; i++) {
         if (this.validityFlags[this.tabList[i]]) {
            break;
         } else {
            n++;
         }
      }
      if (n >= this.tabColumns.length) {
         return;
      }
      var prevTabIdx = this.currentTab;
      this.currentTab = n;
      this._hideTab(prevTabIdx);
      this._showTab(this.currentTab);
   },
   
   previous: function() {
      var n = this.currentTab - 1;
      for (var i=n; i>=0; n--,i--) {
         if (this.validityFlags[this.tabList[i]]) {
            break;
         }
      }
      if (n < 0) {
         return;
      }
      var prevTabIdx = this.currentTab;
      this.currentTab = n;
      this._hideTab(prevTabIdx);
      this._showTab(this.currentTab);
   },
   
   getSelectedTab: function() {
      if (this.currentTab < 0) {
         return null;
      }
      return this.columnList[this.currentTab];
   }
}