/* ********************************************************************
* Evertz.com main javascript file
*
* All Javascript which affects all, or a large proportion of all, pages
* at the site should be placed here.
*
* Onload, this file triggers an empty evertzOnload() function, which
* you can fill in the Javascript file of whatever sub page which
* requires commands to be executed onload.
*
******************************************************************** */

var IE5 = false;
var HOSTCODE = window.location.hostname.replace(/webdev/, "").replace(/evertz\.com/, "").replace(/\./, "") || "www";


window.onload = function() {
  if (navigator.userAgent) {
    if (navigator.userAgent.indexOf('MSIE 5.') > -1) IE5 = true;
  }


  // Fill the page-jump dropdown menu, if it exists
  var dd = document.getElementById("dropdown");
  if (dd) {
    dd.className = "active";
    dd.appendChild(compileDropdown());
  }


  // Modify links which have certain magic classNames
  var replinks = document.body.getElementsByTagName('a');
  for (var x = 0, lnk; lnk = replinks[x++];) {
    if (lnk.className == "tblank") lnk.target = "_blank";
    if (lnk.className == "vipoverview") lnk.href = "vip-overview";
  }


  // Pass execution off to another function
  evertzOnload();
}



/* ***** Empty function placeholder ******************************** */
function evertzOnload() {}



/* ***** Page-Jump Dropdown Creation Code - ************************ */
function compileDropdown() {
  var wlp = window.location.pathname;
  var found = false;
  var piece = "\x2f";
  if (document.location.protocol == "file:")
    piece = (wlp.indexOf("products\x2f") != -1) ? "..\x2f" : "";

  function createOption(value, text) {
    var opt = document.createElement("option");
        opt.value = (value != 'NULL') ? piece + value : 'NULL';
        opt.appendChild(document.createTextNode(text));

      var find = wlp.indexOf(value);
      if (find != -1 && find == wlp.length - value.length) {
          opt.setAttribute("selected", "selected");
        found = true;
      }

    return opt;
  }

  var ddsel = document.createElement("select");
      ddsel.size = "1";
      ddsel.onchange = function() {
        if (this.value != 'NULL') {
          window.location = this.value;
        } else this.form.reset();
      };


  if (HOSTCODE == "cn") {
    ddsel.appendChild(createOption("NULL", '请选择一个产品类'));
    ddsel.appendChild(createOption("products/new", 'Evertz新产品'));
    ddsel.appendChild(createOption("products/3g", '3Gb/s系列产品 - 完整列表'));
    ddsel.appendChild(createOption("products/hdtv", 'HDTV系列产品 - 完整列表'));
    ddsel.appendChild(createOption("products/vistalink", 'VistaLINK\xae兼容的产品'));
    ddsel.appendChild(createOption("products/fulllist", '所有产品列表(由型号检索)'));
    ddsel.appendChild(createOption("NULL", ""));

    var groupLabel = "产品分类";
    var options = [
      createOption("products/7700", "7700系列组"),
      createOption("products/500", "500系列组"),
      createOption("products/2400", "2400系列小型设备"),
      createOption("products/fiber", "光纤系列产品"),
      createOption("products/monitoring", "监测 - 视频/音频/数据"),
      createOption("products/rf", "RF 传送/分配"),
      createOption("products/audio", "音频/数据"),
      createOption("products/converters", "上与下转换器"),
      createOption("products/keyers", "键控器与台标插入器"),
      createOption("products/caption", "隐藏字幕"),
      createOption("products/dtv-mpeg", "DTV & MPEG Products"),
      createOption("products/routers", "路由器"),
      createOption("products/delay", "同步/延迟/测试发生器"),
      createOption("products/synch", "同步与测试发生"),
      createOption("products/timecode", "时间码与信号源"),
      createOption("products/production", "后期制作/影片类")
    ];

  } else {
    ddsel.appendChild(createOption("NULL", "Select a Category"));
    ddsel.appendChild(createOption("products/new", "New Evertz Products"));
    ddsel.appendChild(createOption("products/3g", "3Gb/s Products"));
    ddsel.appendChild(createOption("products/hdtv", "HDTV Products"));
    ddsel.appendChild(createOption("products/vistalink", "VistaLINK\xae Enabled"));
    ddsel.appendChild(createOption("products/fulllist", "All Products by Model #"));
    ddsel.appendChild(createOption("NULL", ""));

    var groupLabel = "Product Categories";
    var options = [
      createOption("products/7700", "7700 Series Modular"),
      createOption("products/500", "500 Series Modular"),
      createOption("products/2400", "2400 Series Miniature"),
      createOption("products/fiber", "Fiber Optics"),
      createOption("products/monitoring", "Signal Monitoring"),
      createOption("products/rf", "RF Band Transmission"),
      createOption("products/audio", "Audio / Data"),
      createOption("products/converters", "Up \x26 Down Converters"),
      createOption("products/keyers", "Keyers \x26 Logo Inserters"),
      createOption("products/caption", "Closed Caption"),
      createOption("products/dtv-mpeg", "DTV & MPEG Products"),
      createOption("products/routers", "Routers"),
      createOption("products/delay", "Video \x26 Audio Delay"),
      createOption("products/synch", "Sync \x26 Test Generation"),
      createOption("products/timecode", "Timecode / Source ID"),
      createOption("products/production", "Post Production / Film")
    ];
  }

  if (IE5) { // Broken behaviour for IE5 which doesn't understand <optgroup>
    for (var x = 0; x < options.length; x++)
        ddsel.appendChild(options[x]);

  } else { // Correct behaviour for everyone else
    var optgroup = document.createElement("optgroup");
        optgroup.label = groupLabel;
      for (var x = 0; x < options.length; x++)
          optgroup.appendChild(options[x]);
    ddsel.appendChild(optgroup);
  }

  if (!found) ddsel.options[0].setAttribute("selected", "selected");

  return ddsel;
}