﻿/*
Nate Grover (www.nategrover.com)
This software is not submitted to the public domain. 
You are free to use it provided:

1) this notice remains intact.
2) you have read and agreed with the usage terms

For usage terms view http://www.nategrover.com/terms.html

Within those restrictions, you may use, incorporate or
extend this software. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/

function initSmartTabs(){
    if(SmartTabs != null){
    	for(var i=0;i<SmartTabs.length;i++){
	    	initSmartTabObject(SmartTabs[i]);
	    }
    }
}

function initSmartTabObject(group){
	group.makeTabs();
    group.activateTab(group.selectedTab)
}

function SmartTabObject(tabNames,root,style){
    this.root = root;
    if(typeof tabNames == "number"){
        this.totalTabs = tabNames;
        emptyArray = new Array();
		this.tabNames = emptyArray;
		for(var i=0;i<this.totalTabs;i++){
		        this.tabNames[i] = "&nbsp;";
	    }
    }
    else{
        this.tabNames = tabNames;
        this.totalTabs = tabNames.length;
    }

    this.style = style == null?"defaultST":style;
    this.containerDivStyle = this.style + "Container";
    this.selectedTab = 0;
    emptyArray = new Array();
    this.inactiveTabStyles = emptyArray;
    emptyArray = new Array();
    this.activeTabStyles = emptyArray;
    emptyArray = new Array();
    this.hoverTabStyles = emptyArray;
    emptyArray = new Array();
    this.contentDivStyles = emptyArray;
    for(var i =0;i<this.totalTabs;i++){
        this.inactiveTabStyles[i] = this.style + "Inactive";
        this.activeTabStyles[i] = this.style + "Active";
        this.hoverTabStyles[i] = this.style + "Hover";
        this.contentDivStyles[i] = this.style + "Content";
    }

    this.id = root + "Tabs";
    this.index = SmartTabs.length;
}
SmartTabObject.prototype.makeTabs = makeTabs;
SmartTabObject.prototype.makeTab = makeTab;
SmartTabObject.prototype.setMouseOverStyle = setMouseOverStyle;
SmartTabObject.prototype.setMouseOutStyle = setMouseOutStyle;
SmartTabObject.prototype.activateTab = activateTab;
SmartTabObject.prototype.showTabDiv = showTabDiv;
SmartTabObject.prototype.setTabsInactive = setTabsInactive;
SmartTabObject.prototype.setInactiveTabStyle = setInactiveTabStyle;
SmartTabObject.prototype.setActiveTabStyle = setActiveTabStyle;
SmartTabObject.prototype.setHoverTabStyle = setHoverTabStyle;
SmartTabObject.prototype.setTabStyle = setTabStyle;
SmartTabObject.prototype.setContentDivStyle = setContentDivStyle;
SmartTabObject.prototype.setStyleRoot = setStyleRoot;

function makeTabs(){
    var theDiv = document.getElementById(this.id);
    theDiv.className = this.containerDivStyle;
    var divArray = new Array();
	for(var i=0;i<this.tabNames.length;i++){
		theDiv.appendChild(this.makeTab(i));
        var div = document.getElementById(this.root + "TabDiv_" + i);
        div.className = this.contentDivStyles[i];
        divArray[divArray.length] = div;      
	}
}

function showTabDiv(id){
    for(var i=0;i<this.tabNames.length;i++){
        var tab = document.getElementById(this.root + "TabDiv_" + i);
        tab.style.display = "none";
    }
    tab = document.getElementById(id);
    tab.style.display = "block";
}



function activateTab(index){
    var obj = document.getElementById(this.root + "Span" + index);
    this.showTabDiv(this.root + "TabDiv_" + index);
    this.setTabsInactive();
    obj.onmouseover = ""
    obj.onmouseout = ""
    obj.onclick = ""
    obj.className = this.activeTabStyles[index];
}

function setTabsInactive(){
    for(var i=0;i<this.totalTabs;i++){
       var tabId = this.root + "Span" + i;
       var tab = document.getElementById(tabId);
       tab.className = this.inactiveTabStyles[i];
       tab.onclick = new Function("SmartTabs[" + this.index + "].activateTab(" + i + ")");
       tab.onmouseover = new Function("SmartTabs[" + this.index + "].setMouseOverStyle(" + i + ")");
       tab.onmouseout = new Function("SmartTabs[" + this.index + "].setMouseOutStyle(" + i + ")");
    }
 }
 

function makeTab(index){
        var theSpan = document.createElement("SPAN");
        theSpan.innerHTML = this.tabNames[index];
        theSpan.className = this.inactiveTabStyles[index];
        theSpan.onclick = new Function("SmartTabs[" + this.index + "].activateTab(" + index + ")");
        theSpan.onmouseover = new Function("SmartTabs[" + this.index + "].setMouseOverStyle(" + index + ")");
        theSpan.onmouseout = new Function("SmartTabs[" + this.index + "].setMouseOutStyle(" + index + ")");
        theSpan.id = this.root + "Span" + index;
        return theSpan;
}

function setMouseOverStyle(index){
    var obj = document.getElementById(this.root + "Span" + index);
    obj.className=this.hoverTabStyles[index];
}

function setMouseOutStyle(index){
    var obj = document.getElementById(this.root + "Span" + index);
    obj.className=this.inactiveTabStyles[index];
}

function setInactiveTabStyle(col,style){
   if(typeof col == "number"){
       this.inactiveTabStyles[col] = style;
   }
   else{
       for(var i=0;i<this.totalTabs;i++){
          if(this.tabNames[i] == col){
                 this.inactiveTabStyles[i] = style;
          }
       }
   }    
}

function setActiveTabStyle(col,style){
   if(typeof col == "number"){
       this.activeTabStyles[col] = style;
   }
   else{
       for(var i=0;i<this.totalTabs;i++){
          if(this.tabNames[i] == col){
                 this.activeTabStyles[i] = style;
          }
       }
   }    
}

function setHoverTabStyle(col,style){
   if(typeof col == "number"){
       this.hoverTabStyles[col] = style;
   }
   else{
       for(var i=0;i<this.totalTabs;i++){
          if(this.tabNames[i] == col){
                 this.hoverTabStyles[i] = style;
          }
       }
   }    
}

function setTabStyle(col,style){
   if(typeof col == "number"){
       this.activeTabStyles[col] = style;
       this.inactiveTabStyles[col] = style;
       this.hoverTabStyles[col] = style;
   }
   else{
       for(var i=0;i<this.totalTabs;i++){
          if(this.tabNames[i] == col){
                this.activeTabStyles[i] = style;
                this.inactiveTabStyles[i] = style;
                this.hoverTabStyles[i] = style;
          }
       }
   }    
}

function setContentDivStyle(col,style){
   if(typeof col == "number"){
       this.contentDivStyles[col] = style;
   }
   else{
       for(var i=0;i<this.totalTabs;i++){
          if(this.tabNames[i] == col){
                this.contentDivStyles[i] = style;
          }
       }
   }    
}

function setStyleRoot(style){
    for(var i =0;i<this.totalTabs;i++){
        this.inactiveTabStyles[i] = this.style + "Inactive";
        this.activeTabStyles[i] = this.style + "Active";
        this.hoverTabStyles[i] = this.style + "Hover";
        this.contentDivStyles[i] = this.style + "Content";
    }
    this.containerDivStyle = this.style + "Container";
}