function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

sM = {
  init: function() {
    var uls = document.getElementsByTagName('ul');
    for (var u = 0; u < uls.length; u++) {
      if (uls[u].className.search(/\bslidingmenu\b/) == -1) continue;
      var lis = uls[u].getElementsByTagName('li');
      for (var i = 0; i < lis.length; i++) {
        var node = lis[i];
        if (node.nodeName.toLowerCase() == 'li' &&
            node.getElementsByTagName('ul').length > 0) {
          sM.addEvent(node, 'mouseover', sM.getMoverFor(node), false);
          sM.addEvent(node, 'mouseout', sM.getMoutFor(node), false);
          node.getElementsByTagName('a')[0].className += ' subheader';
          node.isIn = false;
        }
      }
    }
  },
  
  getMoverFor: function(node) {
    return function(e) { sM.mover(e, node); };
  },
  
  getMoutFor: function(node) {
    return function(e) { sM.mout(e, node); };
  },

  mover: function(e, targetElement) {
    var el = window.event ? targetElement : e ? e.currentTarget : null;
    if (!el) return;
    clearTimeout(el.outTimeout);
    if (!el.isIn) {
      for (var i = 0; i < el.childNodes.length; i++) {
        var node = el.childNodes[i];
        if (node.nodeName.toLowerCase() == 'ul') {
          // Stop current animation
          clearInterval(node.intervalID);
          // Assign initial visible area
          node.clippingRectangle = [0, 0, 4, 0];
          // Save full width and height
          node.style.display = 'block';
          node.savedOW = node.offsetWidth;
          node.savedOH = node.offsetHeight;
          node.style.display = 'none';
          // Start animation
          node.intervalID = setInterval(function() { sM.showMenu(node); }, 10);
          break;
        }
      }
    }
    el.isIn = true;
    el.className += '';   // Force IE to recompute styles
  },

  mout: function(e, targetElement) {
    var el = window.event ? targetElement : e ? e.currentTarget : null;
    if (!el) return;
    el.outTimeout = setTimeout(function() { sM.mout2(el); }, 300);
  },

  mout2: function(el) {
    for (var i = 0; i < el.childNodes.length; i++) {
      var node = el.childNodes[i];
      if (node.nodeName.toLowerCase() == 'ul') {
        // Stop current animation
        clearInterval(node.intervalID);
        // Start animation
        node.intervalID = setInterval(function() { sM.hideMenu(node); }, 10);
        break;
      }
    }
    el.isIn = false;
  },
  
  showMenu: function(el) {
    el.clippingRectangle[1] += 20;
    if (el.clippingRectangle[1] >= el.savedOW) {
      el.clippingRectangle[1] = el.savedOW;
      el.clippingRectangle[2] += 20;
      if (el.clippingRectangle[2] >= el.savedOH) {
        el.clippingRectangle[2] = el.savedOH;
        clearInterval(el.intervalID);
        // reset the clip: browser-specific
        if (document.all && !window.opera) {
          el.style.clip = 'rect(auto)';
        } else {
          el.style.clip = '';
        }
        return;
      }
    }
    el.style.clip = 'rect(' + el.clippingRectangle.join('px ') + 'px)';
    el.style.display = 'block';
  },

  hideMenu: function(el) {
    el.clippingRectangle[2] -= 20;
    if (el.clippingRectangle[2] <= 4) {
      el.clippingRectangle[2] = 4;
      el.clippingRectangle[1] -= 20;
      if (el.clippingRectangle[1] <= 0) {
        clearInterval(el.intervalID);
        // reset the clip: browser-specific
        if (document.all && !window.opera) {
          el.style.clip = 'rect(auto)';
        } else {
          el.style.clip = '';
        }
        el.style.display = 'none';
        return;
      }
    }
    el.style.clip = 'rect(' + el.clippingRectangle.join('px ') + 'px)';
  },

  addEvent: function(elm, evType, fn, useCapture) {
    // cross-browser event handling for IE5+, NS6 and Mozilla
    // By Scott Andrew
    if (elm.addEventListener) {
      elm.addEventListener(evType, fn, useCapture);
      return true;
    } else if (elm.attachEvent) {
      var r = elm.attachEvent('on' + evType, fn);
      EventCache.add(elm, evType, fn);
      return r;
    } else {
      elm['on' + evType] = fn;
    }
  }
};

sM.addEvent(window, 'load', sM.init, false);
sM.addEvent(window, 'unload', EventCache.flush, false);
