var name = getName(location.pathname);
//var pathPrefix = (name == "index") ? ("") : ("../");
var pathPrefix = (name == "home") ? ("../") : "";
var active;
var activeId;
var expanded;
var naturalHeight;
var currentHeight;
var expanderId;

function expand(id) {
 if (expanded == id) return;
 collapse();
 expanded = id;
 var menu = document.getElementById("menu" + id.substring(4));
 menu.style.zIndex = 3;
 menu.style.visibility = "visible";
 menu.style.display = "block";
 menu.style.top = (getRelativeBottom(document.getElementById(id), menu) + 4) + "px";
 menu.style.left = (getRelativeLeft(document.getElementById(id), menu) - 2) + "px";
 menu.style.overflow = "hidden";
 naturalHeight = menu.offsetHeight;
 currentHeight = 0;
 animate(menu, naturalHeight);
}

function animate(menu, naturalHeight) {
 if (currentHeight < naturalHeight) {
  menu.style.height = currentHeight + "px";
  currentHeight += 16;
  expanderId = setTimeout(function() { animate(menu, naturalHeight); }, 20);
 } else {
  currentHeight = naturalHeight;
  menu.style.height = currentHeight + "px";
 }
}

function collapse() {
 if (expanded) {
  var menu = document.getElementById("menu" + expanded.substring(4));
  currentHeight = naturalHeight;
  menu.style.height = currentHeight + "px";
  menu.style.visibility = "hidden";
  menu.style.display = "none";
  expanded = null;
 }
}

function activate(id) {
 var ele = document.getElementById(id);
 propUp(ele);
 var prefix = "";
 if (ele.className == "menuLink") {
  prefix = getCategory(id) + "-";
 }
 location.href = pathPrefix + prefix + id.substring(4,5).toLowerCase() + id.substring(5);
}

function propUp(ele) {
 if (!ele) return;
 propDown();
 activeId = ele.id;
 active = ele;
 ele.id = "activeLink";
 if (ele.cli) removeEvent(ele, "click", ele.cli.clickerize);
 if (navigator.appName == "Microsoft Internet Explorer" && ele.obj) {
  ele.detachEvent("onmouseover", ele.obj.biggerize);
  ele.detachEvent("onmouseout", ele.obj.smallerize);
 }
}

function propDown() {
 if (active) {
  if (active.click) {
   active.id = activeId;
   var cli = new clickifier(active);
   active.cli = cli;
   addEvent(active, "click", cli.clickerize);
   if (navigator.appName == "Microsoft Internet Explorer") {
    var obj = new objectifier(active);
    active.obj = obj;
    active.attachEvent("onmouseover", obj.biggerize);
    active.attachEvent("onmouseout", obj.smallerize);
   }
  }
 }
}

function getName(path) {
 var pageIndex = path.indexOf("page=");
 if (pageIndex != -1) {
  pageIndex = pageIndex + 6;
  var endPageIndex = path.indexOf("&", pageIndex);
  endPageIndex = endPageIndex > 0 ? endPageIndex : path.length;
  return path.substring(pageIndex, endPageIndex);
 }
 var ray = path.toLowerCase().split("/");
 var ar;
 if (ray[ray.length - 1] == "" || ray[ray.length - 1] == "index.html") ar = 2;
 else ar = 1;
 ar = ray[ray.length - ar].split("-");
 return ar[ar.length - 1];
}

function getCategory(id) {
 id = id.substring(4);
 if (id == "Arana" || id == "Arche"|| id == "Arsyn") return "products";
 if (id == "Overview" || id == "Partners"|| id == "Customers"|| id == "News"|| id == "Events") return "aboutUs";
 if (id == "Verification" || id == "Characterization" || id=="RuleChecking" || id == "Optimization" || id == "Migration" || id == "RobustDesign") return "solutions";
 else return false;
}

function getRelativeLeft(ele, other) {
 var left = ele.offsetLeft;
 while (ele.offsetParent != other.offsetParent && ele.offsetParent) {
  ele = ele.offsetParent;
  left += ele.offsetLeft;
 }
 return left;
}

function getRelativeBottom(ele, other) {
 var bottom = ele.offsetHeight + ele.offsetTop;
 while (ele.offsetParent != other.offsetParent) {
  ele = ele.offsetParent;
  bottom += ele.offsetTop;
 }
 return bottom;
}

function clientAdjust() {
 if (navigator.appName == "Microsoft Internet Explorer") {
  document.getElementById("sidebar").style.borderColor = "#BBBBFF";
  document.getElementById("content").style.borderColor = "#AAAAAA";
  var ar = document.getElementsByTagName("td");
  for (var i = ar.length - 1; i >= 0; i--) {
   if (ar[i].className == "link" || ar[i].className == "menuLink") {
    var obj = new objectifier(ar[i]);
    ar[i].obj = obj;
    ar[i].attachEvent("onmouseover", obj.biggerize);
    ar[i].attachEvent("onmouseout", obj.smallerize);
   }
  }
 }
}

function objectifier(ele) {
 function biggerize() {
  ele.style.fontSize = "11pt";
 }
 function smallerize() {
  ele.style.fontSize = "10pt";
 }
 this.ele = ele;
 this.biggerize = biggerize;
 this.smallerize = smallerize;
}

function clickifier(ele) {
 function clickerize() {
  activate(ele.id);
 }
 this.ele = ele;
 this.clickerize = clickerize;
}

function addEvent(ele, event, listener) {
 if (ele.addEventListener) {
  ele.addEventListener(event, listener, false);
 } else if (ele.attachEvent) {
  ele.attachEvent("on" + event, listener);
 }
}

function removeEvent(ele, event, listener) {
 if (ele.removeEventListener) {
  ele.removeEventListener(event, listener, false);
 } else if (ele.detachEvent) {
  ele.detachEvent("on" + event, listener);
 }
}

function setUp() { 
 ar = document.getElementsByTagName("td");
 for (i = ar.length - 1; i >= 0; i--) {
  if (ar[i].className == "link" || ar[i].className == "menuLink" || ar[i].className == "linkToMenu") {
   var cli = new clickifier(ar[i]);
   ar[i].cli = cli;
   addEvent(ar[i], "click", cli.clickerize);
  }
 }

 if (name != "" && name != "index.html") propUp(document.getElementById("link" + name.substring(0, 1).toUpperCase() + name.substring(1)));

 clientAdjust();
 if (name != "home") {
  moveNav();
  addEvent(window, "resize", moveNav);
 } else {
//  moveHome();
//  addEvent(window, "resize", moveHome);
 }
}

function showOffCustomers() {
 var customify = new customifier(document.getElementById("customers"));
 customify.recustomize();
 window.setInterval(customify.customize, 6000);
}

function customifier(ele) {
 function exitize() {
  position += 1;
  position += Math.sqrt(position) / 2;
  ele.style.backgroundPosition = "0px " + Math.round(position) + "px";
  if (position > 100) {
   window.clearInterval(intervalId);
   recustomize();
  }
 }
 function enterize() {
  position += 1;
  position += Math.sqrt(Math.abs(position)) / 2;
  if (position > 0) {
   window.clearInterval(intervalId);
   position = 0;
  }
  ele.style.backgroundPosition = "0px " + Math.round(position) + "px";
 }
 total = 16;
 rotation = Math.round(Math.random() * total);
 position = 0;
 this.enterize = enterize;
 this.exitize = exitize;
 this.ele = ele;
 function customize() {
  position = 0;
  intervalId = window.setInterval(exitize, 29);
 }
 function recustomize() {
  rotation++;
  ele.style.backgroundImage = "url(" + pathPrefix + "aboutUs-customers/" + (rotation % total) + ".png)";
  ele.style.backgroundPosition = "0px -100px";
  position = -100;
  intervalId = window.setInterval(enterize, 29);
 }
 this.customize = customize;
 this.recustomize = recustomize;
}

function moveNav() {
  // placeholder
}

var prevTab;

function clickifyTab(tab) {
 if (tab == prevTab) return;
 if (prevTab) {
  if (prevTab.className == "active") prevTab.className = "";
  if (prevTab.className == "activeLarge") prevTab.className = "large";
  if (prevTab.className == "activeSmall") prevTab.className = "small";
  document.getElementById(prevTab.id.substring(0, prevTab.id.length - 3) + "Div").className = "";
 }
 if (tab.className == "") tab.className = "active";
 if (tab.className == "large") tab.className = "activeLarge";
 if (tab.className == "small") tab.className = "activeSmall";
 document.getElementById(tab.id.substring(0, tab.id.length - 3) + "Div").className = "active";
 prevTab = tab;
}
