// Author: Michael Gallagher
// Date:   03/06/2005

function TabContainer(id, width, tabContentsUrl, selTabClass, normTabClass, tabs)
{
  this.id = id;
  this.width = width;
  this.tabContentsUrl = tabContentsUrl;
  this.buttonClassOn = selTabClass;
  this.buttonClassOff = normTabClass;
  this.openTabPosition = -1;
  this.tabs = tabs;
  this.preloadingInProgress = false;
  
  for(var i = 0; i < this.tabs.length; i++)
  {
    this.tabs[i].setContainerContext(this, i + 1);
  }


  this.getOpenTabPosition =
    function()
    {
        return this.openTabPosition;
    }


  this.tabClick =
    function(position, event)
    {
      // Bail out if the tabs are still being preloaded
      if(this.preloadingInProgress)
      {
        return false;
      }
      
      var index = position - 1;
      // Bail out if the preTabClickEventMethodName returns false
      if( this.tabs[index].preTabClickEventMethodName != '' &&
          eval(this.tabs[index].preTabClickEventMethodName + '("' + this.tabs[index].contentDivId + '", ' + position + ', event)') == false )
      {
        return false;
      }

      var tabButton = document.getElementById(this.tabs[index].tabDivId);
      var contentDiv = document.getElementById(this.tabs[index].contentDivId);
      
      var current = 0;
      this.openTabPosition = position;
      
      
      if (contentDiv.style.visibility != "hidden")
      {
        return false;
      }
      
      // Hide all other tab's contents
      for (var i = 0; i < this.tabs.length; i++)
      {
        // Skip the tab to be displayed
        if(i != index)
        {
          // Hide content div
          currentDiv = document.getElementById(this.tabs[i].contentDivId);
          currentDiv.style.visibility = "hidden";

          // Change class of tab div
          tabDiv = document.getElementById(this.tabs[i].tabDivId);
          tabDiv.className = this.buttonClassOff;
        }
      }
      
      
      // Load the div contents if needed
      if (contentDiv.innerHTML.length == 0)
      {
        // The tab height will automatically be reset after it is loaded
        // the first time
        this.tabs[index].loadTabContent(this.tabContentsUrl);
      }
      else
      {
        // Reset the tab height
        this.tabs[index].resetTabHeight();
      }
      
      // Show div
      contentDiv.style.visibility = "visible";
      
      // Change the button's class
      tabButton.className = this.buttonClassOn;
    }


  
  this.preloadTabs =
    function()
    {
      this.preloadingInProgress = true;
      for (var i = 0; i < this.tabs.length; i++)
      {
        currentDiv = document.getElementById(this.tabs[i].contentDivId);
        // Load the div contents if needed
        if (currentDiv.innerHTML.length == 0)
        {
          this.tabs[i].loadTabContent(this.tabContentsUrl);
        }
      }
    }

  
  this.tabFinishedLoading =
    function()
    {
      // If all tabs have finished loading set the preloadingInProgress flag to
      // false
      for (var i = 0; i < this.tabs.length; i++)
      {
        currentDiv = document.getElementById(this.tabs[i].contentDivId);
        if (currentDiv.innerHTML.length == 0)
          return;
      }
      
      this.preloadingInProgress = false;
    }
    
}



function Tab(contentDivId, tabDivId, preTabClickEventMethodName, onLoadMethodName)
{
  this.container = null;
  this.tabPosition = -1; // 1 - based index (ie first is 1, second is 2, etc)
  this.preTabClickEventMethodName = preTabClickEventMethodName;
  this.onLoadMethodName = onLoadMethodName;
  this.contentDivId = contentDivId;
  this.tabDivId = tabDivId;
  this.heightNeedsReset = false;

  this.setContainerContext =
    function(container, tabPosition)
    {
      this.container = container;
      this.tabPosition = tabPosition;
    }
  
  this.loadTabContent =
    function(url)
    {
      var contentDiv = document.getElementById(this.contentDivId);
      this.loadFromURL(url + 'tab=' + this.tabPosition, contentDiv);
    }
  
  
  this.loadFromURL =
    function(url, div)
    {
      if (url == null) return;
      
      var xmlreq = null;
      if (window.XMLHttpRequest) // Non-IE browsers
      {
        xmlreq = new XMLHttpRequest();
        xmlreq.open("GET", url, false);
        xmlreq.send(null);
      }
      else if (window.ActiveXObject) // IE
      {
        var xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
        xmlreq.open("GET", url, false);
        xmlreq.send();
      }
      div.innerHTML = xmlreq.responseText;
      
      this.resetTabHeight();
      this.container.tabFinishedLoading();
      if(this.onLoadMethodName != null)
      {
        eval(this.onLoadMethodName + "('" + this.contentDivId + "')");
      }
    }
  
  
  this.resetTabHeight =
    function()
    {
      var contentDiv = document.getElementById(this.contentDivId);
      var placeHolder = document.getElementById(this.container.id + "_placeholder");
      
      // Adjust div height
      contentDiv.style.height = contentDiv.scrollHeight + "px";
      placeHolder.style.height = contentDiv.scrollHeight + "px";
      alignDivs(placeHolder, contentDiv);
    }
    
}
