//Version: 1.00, 04/01/2005
//Version: 1.01, 13/01/2005: Add transition support
//Version: 1.02, 03/07/2006: Added support for building TabControlHeirarchy from control
//Version: 1.03, 03/07/2006: Corrected TabControl.new adding itself twice

var Altamira_Web_TabControl_ScriptVersion = 104;

var Page_TabControls = new Array;



function TabControl(tabControlName, tabControlHeight, tabControlResizeHeight, strButtonCSSClass, strButtonCSSClassSelected, strButtonBorderWidth) {
  //Properties
  this.m_CurrentLayerShowing = null;
  this.m_PreviousLayerShowing = null;
  this.m_LayerBorder = null;
  this.m_Debug = null;
  this.m_TabControlName = tabControlName;
  this.m_TabControlHeight = tabControlHeight;
  this.m_TabControlResizeHeight = tabControlResizeHeight;
  this.m_ButtonCSSClass = strButtonCSSClass;
  this.m_ButtonCSSClassSelected = strButtonCSSClassSelected;
  this.m_ButtonBorderWidth = strButtonBorderWidth;
  this.m_TabPages = new Array();
  this.AltezzaBottoni = AltezzaBottoni;

  //Methods
  this.Initialize = Initialize;
  this.showLayerNumber = showLayerNumber;
  this.showLayerNumberCallback = showLayerNumberCallback;
  this.showLayer = showLayer;
  this.GetElement = GetElement;
  //this.doTransition = doTransition;

  //Add to TabControl collection. Find out if we have an 
  //existing tab control with the same ID already added
  var bAdded = false;
  for (j = 0; j <= Page_TabControls.length - 1; ++j) {
    if (Page_TabControls[j].m_TabControlName == tabControlName) {
      Page_TabControls[j] = this;
      bAdded = true;
    }
  }
  //If not found add a new control
  if (!bAdded)
    Page_TabControls[Page_TabControls.length] = this;

  //Initialize
  this.Initialize()
}

function BuildTabControlHeirarchy(objControl, arControls) {
  //Return if we are at the root
    if (objControl.parentNode == null)
    return arControls;
  //Set the parent variable
    var objParent = objControl.parentNode;
    if (objParent.nodeName == "#document")
        return arControls;
  //Create the array if necessary
  if (arControls == null)
    arControls = new Array;

  //Is it a tab control?
  var lPos = objParent.id.indexOf("TabControlLayer_");
  if (lPos != -1) {
    lPos += "TabControlLayer_".length;
    lEndPos = objParent.id.lastIndexOf("_");
    //Get the tab control name
    var strTabControlName = objParent.id.substr(lPos, lEndPos - lPos);
    var iTabPage = objParent.id.substr(lEndPos + 1, objParent.id.length - (lEndPos + 1));
    //Find in TabControl array
    for (j = 0; j <= Page_TabControls.length - 1; ++j) {
      if (Page_TabControls[j].m_TabControlName == strTabControlName)
        arControls[arControls.length] = new TabSelection(Page_TabControls[j], iTabPage);
    }
  }

  return BuildTabControlHeirarchy(objParent, arControls);

}

function TabControlRestart(tabControlName, tabControlHeight, tabControlResizeHeight, strButtonCSSClass, strButtonCSSClassSelected, strButtonBorderWidth) {
  eval(tabControlName + " = new TabControl(\"" + tabControlName + "\", " + tabControlHeight + ", " + tabControlResizeHeight + ", '" + strButtonCSSClass + "', '" + strButtonCSSClassSelected + "', '" + strButtonBorderWidth + "')");
}

//Tha startup function
function Initialize() {
  //Page will not be ready yet in IE

  if (this.m_TabControlResizeHeight && navigator.appName != "Netscape" && document.readyState != "complete") {
    setTimeout("TabControlRestart(\"" + this.m_TabControlName + "\", " + this.m_TabControlHeight + ", '" + this.m_TabControlResizeHeight + "', '" + this.m_ButtonCSSClass + "', '" + this.m_ButtonCSSClassSelected + "', '" + this.m_ButtonBorderWidth + "')", 10);
    return;
  }

  this.m_LayerBorder = parseInt(GetElement("LayerBorder_" + this.m_TabControlName).value);
  this.m_PreviousLayerShowing = GetElement("PreviousLayerShowing_" + this.m_TabControlName).value;

  //Set to defaults if necessary
  if (!this.m_PreviousLayerShowing) this.m_PreviousLayerShowing = 1;
  if (!this.m_LayerBorder) this.m_LayerBorder = 5;
  this.m_CurrentLayerShowing = this.m_PreviousLayerShowing;

  if (this.m_Debug) {
    alert(this.m_PreviousLayerShowing);
    alert(this.m_LayerBorder);
  }
  if (this.m_TabControlResizeHeight)
    this.showLayerNumber(this.m_PreviousLayerShowing);


  for (var i = 1; ; i++) {
    var objLayer = this.GetElement('TabControlLayer_' + this.m_TabControlName + '_' + i);
    if (objLayer == null)
      return i - 1;
    else {
      this.m_TabPages.push(new LayerElement(i, objLayer));
    }
  }
}

//Main layer switching function
function showLayerNumber(number) {
  var layerNumToShow = number;
  var i = 0;
  if (this.m_Debug) {
    alert(eval('"TabControlLayer_' + this.m_TabControlName + '_' + m_CurrentLayerShowing + '"'));
    alert(eval('"TabControlLayer_' + this.m_TabControlName + '_' + layerNumToShow + '"'));
  }

  //Hide the first layer if has been clicked back in browser  and show the new layer
  if(this.GetElement(eval('"TabControlLayer_' + this.m_TabControlName + '_1' + '"')) != null)
    this.showLayer(eval('"TabControlLayer_' + this.m_TabControlName + '_1' + '"'), false);
  var objFirstButton = this.GetElement("button_" + this.m_TabControlName + "_1");
  if (objFirstButton != null)
    if (this.m_ButtonCSSClass != "")
    SetClass(objFirstButton, this.m_ButtonCSSClass);
  else {
    if (this.m_ButtonBorderWidth != "0")
      objFirstButton.style.borderStyle = 'outset';
  }

  //Hide the old layer (if not just loaded) and the first one if has been clicked back in browser  and show the new layer
  if (this.m_CurrentLayerShowing != 0) 
		this.showLayer(eval('"TabControlLayer_' + this.m_TabControlName + '_' + this.m_CurrentLayerShowing + '"'), false);
  this.showLayer(eval('"TabControlLayer_' + this.m_TabControlName + '_' + layerNumToShow + '"'), true);

  //Change the button styles from inset to outset or apply the relevant style
  var objSelectedButton = this.GetElement("button_" + this.m_TabControlName + "_" + layerNumToShow);
  if (objSelectedButton != null) {
      if (this.m_ButtonCSSClassSelected != "")
          SetClass(objSelectedButton, this.m_ButtonCSSClassSelected); //objSelectedButton.className=this.m_ButtonCSSClassSelected;
      else {
          if (this.m_ButtonBorderWidth != "0")
              objSelectedButton.style.borderStyle = 'inset';
      }
  }
  
  if (layerNumToShow != this.m_CurrentLayerShowing) {
    var objDeselectedButton = this.GetElement("button_" + this.m_TabControlName + "_" + this.m_CurrentLayerShowing);
    if (objDeselectedButton != null)
      if (this.m_ButtonCSSClass != "")
      SetClass(objDeselectedButton, this.m_ButtonCSSClass); //objDeselectedButton.className=this.m_ButtonCSSClass;
    else {
      if (this.m_ButtonBorderWidth != "0")
        objDeselectedButton.style.borderStyle = 'outset';
    }
  }


  //Set the hidden field to the layer now showing
  this.m_CurrentLayerShowing = layerNumToShow;
  this.GetElement("PreviousLayerShowing_" + this.m_TabControlName).value = this.m_CurrentLayerShowing;
  //Call the callback


  if (window.pageYOffset)
    window.pageYOffset = 0;
  else
    document.scrollTop = 0;


  this.showLayerNumberCallback(number);
}

//Stub
function showLayerNumberCallback(number) {
}

function showLayer(layerName, bShow) {
	//Quit if the requested layer does not exist
	if (GetElement(layerName) == null)
		return;
  var strShowCmd;
  if (bShow)
    strShowCmd = "inherit";
  else
    strShowCmd = "hidden";
  //Change the style

  this.GetElement(layerName).style.visibility = strShowCmd;
  if (bShow) {
    this.GetElement(layerName).style.display = 'block';
  }
  else {
    this.GetElement(layerName).style.display = 'none';
  }
  if (bShow)
    this.GetElement(layerName).style.zIndex = 1;
  else
    this.GetElement(layerName).style.zIndex = 0;

}


function TabSelection(objControl, iPage) {
  this.TabControl = objControl;
  this.TabPage = iPage;
}



function LayerElement(numElement, objElement) {
  this.m_numElement = numElement;
  this.m_objElement = objElement;
}

function AltezzaBottoni(massimo) {
  var Tab;
  var MascheraTab;
  var v = 0;

  if (!!(window.attachEvent && !window.opera)) //IE
  {
    for (var i = 2; v == 0; ++i) {
      Tab = document.getElementById("buttons_" + this.m_TabControlName + "_" + i);
      MascheraTab = document.getElementById("buttons_" + this.m_TabControlName + "_" + i);
      if (Tab == null) {
        v = 1;
        return true;
      }
      if (i == 2) {
        if (massimo != null) {
          Tab.style.height = (massimo + "px");
        }
        else {
          Tab.style.height = (Tab.offsetHeight + "px");
          massimo = Tab.offsetHeight;
        }
      }
      else {

        if ((Tab.offsetHeight - Tab.offsetTop) > massimo) {
          massimo = (Tab.offsetHeight - Tab.offsetTop);
          v = 1;
          this.AltezzaBottoni(massimo);
        }
        else {
          Tab.style.height = (massimo + "px");
        }

      }

    }
  }
  else //FIREFOX
  {
    for (var i = 2; v == 0; ++i) {
      Tab = document.getElementById("buttonDrag_" + this.m_TabControlName + "_" + i);
      MascheraTab = document.getElementById("buttonDrag_" + this.m_TabControlName + "_" + i);
      if (Tab == null) {
        v = 1;
        return true;
      }
      if (i == 1) {
        if (massimo != null) {
          Tab.style.height = (massimo + "px");
        }
        else {
          Tab.style.height = (Tab.offsetHeight + "px");
          massimo = Tab.offsetHeight;
        }
      }
      else {

        if ((Tab.offsetHeight - Tab.offsetTop) > massimo) {
          massimo = (Tab.offsetHeight - Tab.offsetTop);
          AltezzaBottoni();
        }
        else {
          Tab.style.height = (massimo + "px");
        }

      }

    }
  }
}

