/*
Client-Side UserAgent Sniffer
Written by Sing Chan
Grey Worldwide Northwest - www.greynorthwest.com, 604.687.1001
Version 4.0 - Last modified October 2, 2001
*/

function uaWinX () {
  return (window.innerWidth) ? window.innerWidth : (document.body) ? document.body.clientWidth : 0;
}

function uaWinY () {
  return (window.innerHeight) ? window.innerHeight : (document.body) ? document.body.clientHeight : 0;
}

function uaScrollX () {
  return (!ua.isNS3 && pageXOffset != 'undefined') ? pageXOffset : (document.body) ? document.body.scrollLeft : 0;
}

function uaScrollY () {
  return (!ua.isNS3 && pageYOffset != 'undefined') ? pageYOffset : (document.body) ? document.body.scrollTop : 0;
}

function uaDebug () {
  var key;
  var s = '';
  for (key in this) {
    s += key + ': ' + this[key] + '\n';
  }
  return (s);
}

function uaSniffer() {
  var s, i;
  this.sAgent   = navigator.userAgent.toLowerCase();
  this.os       = (this.sAgent.indexOf('win') >= 0) ? 'win' : (this.sAgent.indexOf('mac') >= 0) ? 'mac' : 'other';
  this.isIE     = false;
  this.isIE4mac = false;
  this.isIE5mac = false;
  this.isGecko  = false;
  this.isNS6    = false;
  this.isNS4    = false;
  this.isNS3    = false;
  this.isOther  = false;

  if (this.sAgent.indexOf('mozilla/') >= 0 && parseFloat(navigator.appVersion) < 4) {
    this.version = parseFloat(navigator.appVersion);
    this.isNS3 = true;
  } else if ((i = this.sAgent.indexOf(s = 'msie')) >= 0) { // check IE version
    this.version = parseFloat(this.sAgent.substr(i + s.length));
    this.isIE = true;
    this.isIE4mac = (this.os == 'mac' && this.version < 5) ? true : false;
    this.isIE5mac = (this.os == 'mac' && this.version >= 5) ? true : false;
  } else if ((i = this.sAgent.indexOf(s = 'netscape6/')) >= 0) { // check Netscape 6 version
    this.version = parseFloat(this.sAgent.substr(i + s.length));
    this.isNS6 = true;
  } else if ((i = this.sAgent.indexOf(s = 'mozilla/')) >= 0) { // check Gecko / Netscape 4 or 3 version
    this.version = parseFloat(this.sAgent.substr(i + s.length));
    if (this.version >= 5) { // if Gecko, check for revision version instead
      this.isGecko = true;
      this.version = this.sAgent.substr(this.sAgent.indexOf(s = 'rv:') + s.length);
      s = this.version.substr(0, this.version.indexOf('.') + 1);
      this.version = this.version.substr(this.version.indexOf('.'));
      for (i = 0; i < this.version.length; i++) {
        if (this.version.charAt(i) != '.') {
          s += this.version.charAt(i);
        }
      }
      this.version = parseFloat(s);
    } else if (this.version >= 4) {
      this.isNS4 = true;
	}
  } else {
    this.isOther = true;
  }
  
  this.isCool = ((this.isIE && this.version >= 5) || (this.isNS6 && this.version >= 6.1) || (this.isGecko && this.version >= 0.92)) ? true : false;

  this.flash    = false;
  this.qt       = false;
  this.screenX  = (this.isNS3) ? 0 : window.screen.width;
  this.screenY  = (this.isNS3) ? 0 : window.screen.height;
  this.winX     = uaWinX;
  this.winY     = uaWinY;
  this.scrollX  = uaScrollX;
  this.scrollY  = uaScrollY;
  this.debug    = uaDebug;
}

var ua = new uaSniffer();
var userAgent = new uaSniffer();

// end of user agent

// scmenu.js
// Simple/Compact Dynamic HTML Pulldown Menus for DOM and CSS Compliant Browsers
// Version 1.0 - September 20, 2001
// Written by Sing Chan and Edwin Ching
// Grey Worldwide Northwest - www.greynorthwest.com, 604.687.1001
// Based off of BrainJar Menu Bar Demo by Mike Hall - www.brainjar.com


// REQUIRED GLOBALS
var scActiveButton = null;
var scMenuList = new Array();
var scTimeout = new Array();
var scCompliantBrowser = (ua.isNS6 || ua.isGecko || (ua.isIE && ua.os != 'mac' && ua.version >= 5)) ? true : false;


// SET EVENT HANDLER
if (ua.isIE) {
  document.onmouseover = scMouseOver;
} else if (ua.isNS6) {
  document.addEventListener("mouseover", scMouseOver, true);
}


// FUNCTIONS
function scMouseOver(event) {
  var idName;
  var menuName;
  
  // check to see if browser supports menus
  if (scCompliantBrowser) {
    
    // get id name of target element
    if (ua.isIE) {
      idName = window.event.srcElement.id;
    } else if (ua.isNS6) {
      idName = (event.target.id) ? event.target.id : event.target.parentNode.id;
    }
        
    // if target element is part of scMenu system, hide all selectboxes
    if (idName.indexOf('scMenu') >= 0) {
      selectboxToggle('hidden');
    }
        
    // if target element is a scMenuItem, get parent scMenu's id
    if (idName.indexOf('scMenuItem') >= 0) {
      menuName = (ua.isIE) ? window.event.srcElement.parentNode.id : (ua.isNS6) ?  event.target.parentNode.id : '';
      
      // close any scMenus below current scMenu
      for (i = 0; i < scMenuList.length; i++) {
        if (scMenuList[i] == menuName) {
          for (j = i+1; j < scMenuList.length; j++) {
            if (scMenuList[j] != '') {
              document.getElementById(scMenuList[j]).style.visibility = 'hidden';
              scMenuList[j] = '';
            }
          }
          break;        
        }
      }
    }
    
    // if target element is not part of scMenu system, trigger delayed close
    if (idName.indexOf("scMenu") < 0) {
      if (scMenuList.length == 0) {
        scHideMenus();
      } else {
        scTimeout[scTimeout.length] = setTimeout('scHideMenus()',500);
      }
    } else {
      // otherwise clear all delayed close and keep scMenu open
      for (var i = 0; i < scTimeout.length; i++) {
        clearTimeout(scTimeout[i]);
      }
      scTimeout = new Array();
    }
  }  
}


function scElementOver (oElement, sMenuName, xOffset, yOffset) {
  if (scCompliantBrowser) {
    
    // Associate additional properties to element.
    oElement.isMenuButton = (oElement.id.indexOf('scMenuButton') >= 0) ? true : false;
    oElement.isMenuSub = (oElement.id.indexOf('scMenuSub') >= 0) ? true : false;
    oElement.xMenuOffset = !isNaN(xOffset) ? xOffset : 0;
    oElement.yMenuOffset = !isNaN(yOffset) ? yOffset : 0;

    if (sMenuName == null || sMenuName == '' || !document.getElementById('scMenu' + sMenuName)) {
      if (oElement.isMenuButton) {
        scHideMenus();
        scSetActiveButton(oElement);
      } else if (oElement.isMenuSub) {
        for (i = 0; i < scMenuList.length; i++) {
          if (scMenuList[i] == oElement.parentNode.id) {
            for (j = i+1; j < scMenuList.length; j++) {
              if (scMenuList[j] != '') {
                document.getElementById(scMenuList[j]).style.visibility = 'hidden';
                scMenuList[j] = '';
              }
            }
            break;        
          }
        }
      }
    } else {
      oElement.menu = document.getElementById('scMenu' + sMenuName);
  
      if (oElement.isMenuButton) {
        scResetMenuList();
        scResetActiveButton();
        scMenuList[0] = 'scMenu' + sMenuName;
        scDisplayMenu(oElement);
      } else if (oElement.isMenuSub) {
        // close any scMenus below current scMenu and show new scMenu
        for (i = 0; i < scMenuList.length; i++) {
          if (scMenuList[i] == oElement.parentNode.id) {
            for (j = i+1; j < scMenuList.length; j++) {
              if (scMenuList[j] != '') {
                document.getElementById(scMenuList[j]).style.visibility = 'hidden';
                scMenuList[j] = '';
              }
            }
            scMenuList[i+1] = 'scMenu' + sMenuName;
            scDisplayMenu(oElement);
            break;        
          }
        }
      }
    }
  }
}


function scDisplayMenu(oElement) {
  var w, dw, x, y;
  var reverse = false;

  // For IE, set an explicit width on the first menu item. This will
  // cause link hovers to work on all the menu's items even when the
  // cursor is not over the link's text.

  if (ua.isIE && !oElement.menu.firstChild.style.width && ua.os != 'mac') {  
    w = oElement.menu.firstChild.offsetWidth;
    oElement.menu.firstChild.style.width = w + "px";
    dw = oElement.menu.firstChild.offsetWidth - w;
    w -= dw;
    oElement.menu.firstChild.style.width = w + "px";
    oElement.menu.firstChild.style.width = '';
  }

  // For IE 5 on Macs, manually set scMenu width
  if (ua.isIE && ua.os == 'mac' && ua.version <= 5.1) {
    oElement.menu.style.width = '200px';
    oElement.menu.style.overflow = 'hidden';
  }
  
  // if scMenu activated by scMenuButton, display underneath button
  if (oElement.isMenuButton) {
    x = getPageOffsetLeft(oElement) + oElement.xMenuOffset;

    // if scMenu is going to be displayed off screen, display to left unless it appears off screen to left as well, then display to right
    if (x + oElement.menu.offsetWidth > ua.winX()) {
      x = getPageOffsetLeft(oElement) + oElement.offsetWidth - oElement.menu.offsetWidth + oElement.xMenuOffset;
      reverse = true;
      if (x < 0) {
        x = getPageOffsetLeft(oElement) + oElement.xMenuOffset;
        reverse = false;
      }
    }
    y = getPageOffsetTop(oElement) + oElement.offsetHeight + oElement.yMenuOffset;

  // if scMenu activated by scMenuSub, display slightly underneath and to right
  } else if (oElement.isMenuSub) {
    x = getPageOffsetLeft(oElement) + oElement.offsetWidth + oElement.xMenuOffset - 4;

    // if scMenu is going to be displayed off screen, display to left unless it appears off screen to left as well, then display to right
    if (x + oElement.menu.offsetWidth > ua.winX()) {
      x = getPageOffsetLeft(oElement) - oElement.menu.offsetWidth - oElement.xMenuOffset + 4 ;
      reverse = true;
      if (x < 0) {
        x = getPageOffsetLeft(oElement) + oElement.offsetWidth + oElement.xMenuOffset - 4;
        reverse = false;
      }
    }
    y = getPageOffsetTop(oElement) + oElement.yMenuOffset + 4 ;
  }
    
  // tweak positioning for different browsers
  if ((ua.isIE && ua.version >= 5.5 && ua.os != 'mac')) {
    x += (reverse) ? 1 : -1;
  } else if (ua.isIE && ua.version < 5.5 && oElement.isMenuSub) {
    x += (reverse) ? 6 : -6;
  } else if (ua.isNS6 && ua.version >= 6.1) {
    x += (reverse) ? 1 : -1;
  } else if (ua.isNS6 && ua.version < 6.1) {
    x += (reverse) ? 1 : -1;
  }

  // display scMenu
  oElement.menu.style.left = x + "px";
  oElement.menu.style.top  = y + "px";
  oElement.menu.style.visibility = "visible";
  
  // if element is scMenuButton, make element scActiveButton and switch to 'active' class
  scSetActiveButton (oElement);
}


function scSetActiveButton (oElement) {
  if (oElement.isMenuButton) {
    oElement.origClassName = oElement.className;
    oElement.className = (oElement.className != '' || oElement.className != null) ? oElement.className + 'Active' : '';
    scActiveButton = oElement;
  }
}


// Hide all scMenus and show all selectboxes
function scHideMenus() {
  scResetMenuList();
  scResetActiveButton();
  selectboxToggle('visible');
}


// Reset scActiveButton
function scResetActiveButton () {
  if (scActiveButton) {
    scActiveButton.className = scActiveButton.origClassName;
    scActiveButton = null;
  }
}


// Reset scMenuList
function scResetMenuList () {
  for (i = 0; i < scMenuList.length; i++) {
    if (scMenuList[i] != '') {
      document.getElementById(scMenuList[i]).style.visibility = 'hidden';
    }
  }
  scMenuList = new Array();
}





// OBJECT METHODS
function scMenuAddMenu (sName, sClass) {
  var o = new Object();
  o['name'] = sName;
  o['class'] = sClass;
  o['items'] = new Array ();
  this.menus[this.menus.length] = o;
}

function scMenuAddItem (sParentMenu, sURL, sText, sClass, sTargetMenu, sTargetFrame) {
  var oMenu = new Object();
  var o = new Object();
  o['url'] = sURL;
  o['text'] = sText;
  o['class'] = sClass;
  o['targetMenu'] = sTargetMenu;
  o['targetFrame'] = sTargetFrame;

  for (var i = 0; i < this.menus.length; i++) {
    oMenu = this.menus[i];
    if (oMenu['name'] == sParentMenu) {
      oMenu['items'][oMenu['items'].length] = o;
    }
  }  
}

function scMenu() {
  this.menus = new Array();
  this.addMenu = scMenuAddMenu;
  this.addItem = scMenuAddItem;
  this.renderHTML = scMenuRenderHTML;
  this.debug = scMenuDebug;
}

function loadpage(page){
parent.location=page;
//alert(page);
}

function scMenuRenderHTML() {
  var oMenu = new Object();
  var oItem = new Object();
  var s = '';

  for (var i = 0; i < this.menus.length; i++) {
    oMenu = this.menus[i];
    s += '<div id="scMenu' + oMenu['name'] + '" class="' + oMenu['class'] + '">';

    for (var j = 0; j < oMenu['items'].length; j++) {
      oItem = oMenu['items'][j];
      
      s += '<a id="scMenuSub' + oMenu['name'] + j;
      s += (oItem['targetMenu'] != null && oItem['targetMenu'] != '') ? '" onmouseover="scElementOver(this, \'' + oItem['targetMenu'] + '\');' : '';
      s += '" class="' + oItem['class'] + '" href="' + oItem['url'];
      s += (oItem['targetFrame'] != null && oItem['targetFrame'] != '') ? '" target="' + oItem['targetFrame'] : '';
      s += '" >' + oItem['text'] + '</a>';
    	}
    s += '</div>';
  }
document.writeln(s);
}

function scMenuDebug() {
  var oMenu = new Object();
  var oItem = new Object();
  for (var i = 0; i < this.menus.length; i++) {
    oMenu = this.menus[i];
    document.writeln(oMenu['name'] + ', ' + oMenu['class'] + '<br>');
    for (var j = 0; j < oMenu['items'].length; j++) {
      oItem = oMenu['items'][j];
      document.writeln('&nbsp;&nbsp;' + oItem['url'] + ', ' + oItem['text'] + ', ' + oItem['class'] + ', ' + oItem['targetMenu'] + '<br>');
    }
  }
}



// GENERAL FUNCTIONS
// Return the true x coordinate of an element relative to the page.
function getPageOffsetLeft(el) {
  return el.offsetLeft + (el.offsetParent ? getPageOffsetLeft(el.offsetParent) : 0);
}

// Return the true y coordinate of an element relative to the page.
function getPageOffsetTop(el) {
  return el.offsetTop + (el.offsetParent ? getPageOffsetTop(el.offsetParent) : 0);
}

// toggle form selectBox visibility
function selectboxToggle(state) {
  
  for(var i = 0; i < document.forms.length; i++) {
    for(var j = 0; j < document.forms[i].elements.length; j++) {
      if(document.forms[i].elements[j].type=="select-one") {
        document.forms[i].elements[j].style.visibility = state;
      }
    }
  }
}
