// -*- javascript -*-
dojo.require("dojo.logging.ConsoleLogger");
dojo.require("dojo.event.*");
dojo.require("dojo.html.*");
dojo.require("dojo.lang.extras");

dojo.declare('menutimer', null, {
    initializer : function(node, interval) {
      this.node = node;
      this.hideInterval = interval;
      this.timer = null;
      this.connect(node);

      var sublist=node.getElementsByTagName("LI");

      for (i=0; i<sublist.length; i++) {
        this.connect(sublist[i]);
      }
    },

    connect : function(n) {
      dojo.event.connect(n, "onmouseover", this, "onMouseOver");
      dojo.event.connect(n, "onmouseout", this, "onMouseOut");
    },

    start : function() {
      var _this = this;
      var _node = this.node;

      var hidemenu = function () {
        dojo.html.removeClass(_node, 'over');
        //_node.lastChild.style.display="none";
        this.stop();
        //dojo.debug("fired");
      }

      this.timer = dojo.lang.setTimeout(hidemenu, _this.hideInterval);
      //dojo.debug("timer start: " + this.timer);
    },

    stop : function() {
      dojo.lang.clearTimeout(this.timer);
      //dojo.debug("timer stop: " + this.timer);
      this.timer = null;
    },

    isRunning : function() {
      return (this.timer != null);
    },

    onMouseOver: function() {
      if (this.isRunning()) {
        this.stop();
      }
      //if (!this.node.lastChild.style.display == "block") {
        dojo.html.addClass(this.node, 'over');
        //this.node.lastChild.style.display="block";
        //}
    },

    onMouseOut: function() {
      if (this.isRunning()) {
        this.stop();
      }
      this.start();
    }
  });

activateMenu = function(menulist) {
  //  if(document.all && document.getElementById(nav).currentStyle) {
    // only MSIE supports document.all
  var menuid = null;
  var ix = 0;

  for (ix in menulist) {
    menuid = menulist[ix];

    var menu = dojo.byId(menuid);
    if (menu != null) {
      //dojo.debug("Got menu: " + menuid);
      menu._timer = new menutimer(menu, 200);
    }
    else {
      dojo.debug("no menu: " + menuid);
    }
  }
}
  //}

initMenus = function() {
  activateMenu(['homemenu','aboutmenu', 'assistancemenu', 'collectorsmenu', 'signupmenu', 'cartmenu', 'newsmenu', 'fineartmenu']);
}

delayedScrollToElement = function(e, delay) {
  dojo.lang.setTimeout(function() {scrollToElement(e);}, delay);
}

scrollToElement = function(e) {
  var elt = dojo.byId(e);
  if (elt != null) {
    scrollTo(0, elt.offsetTop);
  }
}

galleryscroll = function() {
  scrollToElement('gallerynav');
  scrollToElement('gallerybox');
  delayedScrollToElement('gallerybox', 500);
  delayedScrollToElement('gallerybox', 1500);
}

dojo.addOnLoad(initMenus);
