// JavaScript Document

/**** prototype JAvascript tab class****************************
 *
 * created by JW 1.2.08
 *
 * hoping to combine the process of my tab function with Lucas's tabber.js class
 * manipulation of tabs.  This might lead to more work than needed.
 *
 * Vars:
 * 
 * tab container div id
 * boxes container div id    
 * select tab div class 
 * standard tab div class
 * default starting tab 
 *    
 *******************************************************************/
 
 
function cnTabs(tabDiv, boxDiv, classTabSelected, classTabDefault, defaultTab) {
    
  //making sure the Divs are properly referanced for prototype
  this.tabDiv = $(tabDiv);
  this.boxDiv = $(boxDiv);
  
  //array containing the tabs and boxes
  this.tabs = this.tabDiv.childElements();
  this.boxes = this.boxDiv.childElements();
  
  //do the tabs have id?? Dynamically create some then
  this.tabs.each(function(name, n){
    if(!name.id) {
      name.id = "tab_"+n;
    }
  });
  
  //do the boxes have ids??  If not, dynamically create some
  this.boxes.each(function(name, n){
    if(!name.id) {
      name.id = "box_"+n;
    }
  });
  
  //setting the selected and nonselected tab classes
  this.selectedClass = classTabSelected;
  this.defaultClass = classTabDefault;
 
  //what tab to initialize first
  if(!defaultTab) { defaultTab=0; }
  if(defaultTab > this.tabs.size()-1 ) { defaultTab = this.tabs.size() -1; }
  this.defaultTab = defaultTab;

  
  /*alert(this.tabs.inspect());
  alert(this.boxes.inspect());
  alert(defaultTab);
  alert(this.defaultTab);
  */
  
  //alert(this.inspect());
}

/* function to take a class or ID and exclude that element from being in the  
 * tabs.  Currently used to allow a cleardiv element inside the tabs.
 **/
cnTabs.prototype.exclude = function(exclude, type){
  
  if(!(exclude)) return false;
  
  var remove;
  
  this.tabs.each(function(name, n) {
    if(type == 'class') {
      if(name.className == exclude) {
        remove = name;
      }
    }
    else if (type == 'id') {
      if(name.id == exclude ) {
        remove = name;
      }
    }
    
  });
  
  this.tabs = this.tabs.without(remove);

}
/* Function - starts or init the tabs */
cnTabs.prototype.init = function() {
  this.createLinks();
  
  this.update(this.defaultTab);
}

cnTabs.prototype.createLinks = function() {
  var i=0;
  var anchor

  for(i=0; i< this.tabs.size(); i++) {
    var t = this.tabs[i];
    var text = t.innerHTML.unescapeHTML();  //on 7.2.08 added the unescapteHTML for remove the &amp;
   
    //basic link information;
    anchor = document.createElement("a");
    anchor.appendChild(document.createTextNode(text));
    anchor.href = "javascript:void(null);";
    anchor.onclick = this.tabClick;
    
    //give the link an id, and store the index of the tab within the tab
    //anchor.id = "tabLink_".i;
    anchor.num = i;
    anchor.tabber = this;
    
    //update the tab <li> element with the new built anchor link.  NO JS DOM toString method
    //for my built <a> element, so I had to clear the <li> and then append the <a> element as
    //an object
    //damn AP has 2005 beta copy of prototype
    //$(t).update();
    $(t).innerHTML = '';
    //$(t).update(anchor);
    $(t).appendChild(anchor);
    
    
  }

}

/* Processes the user tab click, grabs the class information from the link and updates
 * the tabs according to which tab link was clicked
 */ 
cnTabs.prototype.tabClick = function(a_link){
 
  //if(!a_link) return false;
  
  var a;
  a = this;
  
  if(!a.tabber) return false;
 
  cnTabber = a.tabber;
  
  //clears the "highlighted box" from around the tab 
  a.blur();

  cnTabber.update(a.num); 

  return false;

}


/* main function to update the state of the tabs.  Only param is the index value
 * of the tab & box to show and highlight.  Hides all other boxes and changes the classes 
 * of all other tabs
 */  
cnTabs.prototype.update = function(index) {
  var tmp;
   
  if( !this.tabs[index] ) {return false;}
  
  /*temporarly hide all boxes and deselect all tabs*/
  this.unsetAll();
  
  this.tabs[index].className = this.selectedClass;
  
  $(this.boxes[index]).show();
 
}

/* Function - loop through all the tabs and call the "unset" routine for each element*/
cnTabs.prototype.unsetAll = function() {
  var i;
  
  for (i = 0; i < this.tabs.length; i++) {
    this.unset(i);
  }
 
}

/* Function - hides the box and restores the tab to default of the index value */
cnTabs.prototype.unset = function(index){
  this.tabs[index].className = this.defaultClass;
  $(this.boxes[index]).hide();  
}


