﻿// JScript File



ie = (document.all && !window.opera);
isSafari = navigator.userAgent.indexOf('Safari') != -1;
isFireFox = navigator.userAgent.indexOf('Firefox') != -1;
ie6 = ie && navigator.userAgent.indexOf('MSIE 6') != -1;
isMac = navigator.userAgent.indexOf('Mac OS X') != -1;
isMacFireFox = isFireFox && isMac;

defaultMetro = 'nyc';



function getClientSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [myWidth, myHeight];
}


function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [scrOfX, scrOfY];
}


function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curleft += obj.offsetLeft;
            obj = obj.offsetParent;
        }
    } else if (obj.x) {
        curleft += obj.x;
    }
    return curleft;
}

function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curtop += obj.offsetTop;
            obj = obj.offsetParent;
        }
    } else if (obj.y) {
        curtop += obj.y;
    }

    return curtop;
}

function findMouseX(event) {
    if (ie) event = window.event;
    if (event.pageX || event.pageY) return event.pageX;
    else if (event.clientX || event.clientY) return event.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
    return -1;
}

function findMouseY(event) {
    if (ie) event = window.event;
    if (event.pageX || event.pageY) return event.pageY;
    else if (event.clientX || event.clientY) return event.clientY + document.body.scrollTop + document.documentElement.scrollTop;
    return -1;
}

// Useful DOM stuff

function childWithID(obj, id) {
    var nodes = obj.childNodes;
    for (var i = 0; i < nodes.length; i++) {
        var node = nodes[i];
        if (node.id == id) {
            return node;
        } else {
            var subNode = childWithID(node, id);
            if (subNode) return subNode;
        }
    }
    return null;
}

function parentWithID(obj, id) {
    if (obj.parentNode) {
        if (obj.parentNode.id == id) {
            return obj.parentNode;
        } else {
            return parentWithID(obj.parentNode, id);
        }
    } else {
        return null;
    }
}

  function toggle_tab(baseId, tabNum) {
    i=0;
    while (true) {
      var el = $(baseId + "_tab_" + i);
      if (el) el.className = remove_class(el.className, "selected");
      else break;
      i++;
    }

    i=0;
    while (true) {
      var el = $(baseId + "_content_" + i);
      if (el) el.style.display = "none";
      else break;
      i++;
    }
    var el = $(baseId + "_tab_" + tabNum);
    if (el) el.className = add_class(el.className, "selected");
    var el = $(baseId + "_content_" + tabNum);
    if (el) {
        el.style.display = "";
        if (el.show) el.show();
        fireImageOnload(el.id, function(image) {image.style.width="0px"; image.style.visibility="hidden";});
    }
  }

  function add_class(className, name) {
      if (className && className.indexOf(name) == -1) {
          className += ' ' + name;
      } else {
          className = name;
      }
      return className;
  }

  function remove_class(className, name) {
      if (className) {
          var index = className.indexOf(name);
          if (index != -1) {
              className = className.substr(0, index) + className.substr(index+name.length);
          }
      }
      return className;
  }
  
  
  
    function array_contains(array, value) {
      if (array == null || array == undefined || array.length == 0) return false;

      for (var i=0; i<array.length; i++) {
          if (array[i] == value) return true;
      }
      return false;
  }

  function array_remove(array, value) {
      if (array == null || array == undefined || array.length == 0) return;

      for (var i=0; i<array.length; i++) {
          if (array[i] == value) array.splice(i, 1);
      }
  }

  var menu_watch = new Array();

  function startMenuWatch(baseId) {
      if (menu_watch.length == 0) {
          menu_watch.push(baseId);
          if (ie) {
              document.attachEvent("onmousemove", checkMenuWatch);
          } else {
              document.addEventListener("mousemove", checkMenuWatch, false);
          }
      } else if (!array_contains(menu_watch, baseId)) {
          menu_watch.push(baseId);
      }
  }

  function checkMenuWatch(event) {
      if (ie) {
          x = window.event.clientX + document.documentElement.scrollLeft
              + document.body.scrollLeft;
          y = window.event.clientY + document.documentElement.scrollTop
              + document.body.scrollTop;
      } else {
          x = event.clientX + window.scrollX;
          y = event.clientY + window.scrollY;
      }

      // find the current open tab
      var tmp_menu_watch = menu_watch.slice(0);
      for (var j=0; j<tmp_menu_watch.length; j++) {
          var i=0;
          while (true) {
              var tab = document.getElementById(tmp_menu_watch[j] + '_tab_' + i);
              if (!tab) {
                  stopMenuWatch();
                  break;
              }
              if (tab.className.indexOf('open') != -1) {
                  var content = document.getElementById(tmp_menu_watch[j] + '_content_' + i);
                  if (!is_in_area(x, y, tab.id) && (content == undefined || !is_in_area(x, y, content.id))) {
                      tab.className = remove_class(tab.className, 'open');
                      if (content) content.style.display = 'none';
                      array_remove(menu_watch, tmp_menu_watch[j]);
                      stopMenuWatch();
                      break;
                  }
              }
              i++;
          }
      }
  }

  function stopMenuWatch() {
      if (menu_watch.length ==0) {
          if (ie) {
              document.detachEvent("onmousemove", checkMenuWatch);
          } else {
              document.removeEventListener("mousemove", checkMenuWatch, false);
          }
      }
  }

function is_in_area(x, y, id) {
    var node = document.getElementById(id);

    if (node) {
        posx = findPosX(node);
        posy = findPosY(node);

        if ((x >= posx && x <= (0+posx+node.offsetWidth)) &&
            (y >= posy && y <= (0+posy+node.offsetHeight))) {
            return true;
        }
    }
    return false;
}