// tabbedpanel.js

var divLocation = new Array(numLocations)
var newLocation = new Array(numLocations)

for(var i=0; i<numLocations; ++i) {
 divLocation[i] = i
 newLocation[i] = i
}

function getDiv(s,i) {
 var div
 if(navigator.appName == "Microsoft Internet Explorer"
   && navigator.appVersion.charAt(0) < 5)
  div = document.all.item(panelID+s+i)
 else div = document.getElementById(panelID+s+i)
 return div
}
function setZIndex(div, zIndex) {
 div.style.zIndex = zIndex
}
function getLocation(i) {
 return divLocation[i]
}
function setLocation(i, j) {
 divLocation[i] = j
}
function getNewLocation(i) {
 return newLocation[i]
}
function setNewLocation(i, j) {
 newLocation[i] = j
}
function updatePosition(div, newPos) {
 div.style.top = (numRows-(Math.floor(newPos/tabsPerRow) + 1)) * (tabHeight-vOffset)
 div.style.left = (newPos % tabsPerRow) * tabWidth +
  (hOffset * (Math.floor(newPos / tabsPerRow)))
}

function selectTab(n) {
 // n is the ID of the division that was clicked
 // firstTab is the location of the first tab in the selected row
 var firstTab = Math.floor(getLocation(n) / tabsPerRow) * tabsPerRow
 // newLoc is its new location
 for(var i=0; i<numDiv; ++i) {
   // loc is the current location of the tab
   var loc = getLocation(i)
   // If in the selected row
   if(loc >= firstTab && loc < (firstTab + tabsPerRow)) setNewLocation(i, loc - firstTab)
   else if(loc < tabsPerRow) setNewLocation(i,firstTab+(loc % tabsPerRow))
   else setNewLocation(i, loc)
 }
 // Set tab positions & zIndex
 // Update location
 for(var i=0; i<numDiv; ++i) {
  var div = getDiv("tab",i)
  var loc = getNewLocation(i)
  updatePosition(div, loc)
  if(i == n) setZIndex(div, numLocations +1)
  else setZIndex(div,numLocations - loc)
  div = getDiv("panel",i)
  if(i == n) setZIndex(div, numLocations +1)
  else setZIndex(div, numLocations - loc)
  setLocation(i, loc)
 }
}
