// JavaScript Document

dojo.require("dojo.cookie");

//  mainMenuPopup (2009-11-03)
//  main menu popup controll object


function mainMenuPopup() {
  var self = this;
  this.button = null;
  this.popup = null;
  this.timerID = null;
  this.showed = false;
  this.fullHeight = 0;
  //
  this.init = function() {
    //
    self.popup = dojo.byId('mainmenu');
    self.button = dojo.byId('mainmenubutton');
    //
    var weballElem = dojo.byId('weball');
    var webbottomElem = dojo.byId('webbottom');
    //
    // transform nojs to js version
    dojo.removeClass(self.popup,"main-menu-nojs");
    dojo.addClass(self.popup,"main-menu-js");
    dojo.removeClass(weballElem,"web-all-nojs");
    dojo.addClass(weballElem,"web-all-js");
    dojo.removeClass(webbottomElem,"web-bottom-nojs");
    dojo.addClass(webbottomElem,"web-bottom-js");
    //
    dojo.query("a",self.popup).forEach(function(elem,index){ elem.innerHTML = '<span>'+elem.innerHTML+'</span>'; });
    //
    // get fullHeight (height of popup menu)
    dojo.style(self.popup,'height','auto');
    self.fullHeight = dojo.style(self.popup,'height');
    dojo.style(self.popup,'height','0px');
    //
    // show/hide menu on menu element
    dojo.connect(self.popup,"onmouseover",self.stop);
    dojo.connect(self.popup,"onmouseout",self.wait);
    dojo.connect(self.popup,"onfocus",self.stop);
    dojo.connect(self.popup,"onblur",self.wait);
    //
    // show/hide menu on bottom logo
    dojo.connect(self.button,"onmouseover",self.show);
    dojo.connect(self.button,"onmouseout",self.wait);
    dojo.connect(self.button,"onfocus",self.show);
    dojo.connect(self.button,"onblur",self.wait);
    //
    // show/hide menu on whole bottom line
    dojo.connect(self.button.parentNode,"onmouseover",self.show);
    dojo.connect(self.button.parentNode,"onmouseout",self.wait);
    dojo.connect(self.button.parentNode,"onfocus",self.show);
    dojo.connect(self.button.parentNode,"onblur",self.wait);
    //
    // hide menu on div.bottom-menu
    var bottommenuElem = dojo.query('div.bottom-menu',webbottomElem)[0];
    dojo.connect(bottommenuElem,"onmouseover",function(e){
      dojo.stopEvent(e);
      self.wait();
    });
    dojo.connect(bottommenuElem,"onfocus",function(e){
      dojo.stopEvent(e);
      self.wait();
    });
    //
    // show menu on first page
    if ((typeof((dojo.cookie))!='undefined') && (dojo.cookie.isSupported()) && !(dojo.cookie('77cm_menu_shown'))) {
      dojo.cookie('77cm_menu_shown','shown',{path:'/'});
      setTimeout(self.show,500);
      setTimeout(self.wait,2500);
    }
  };
  //
  this.swap = function() {
    if (self.showed) {
      self.wait();
    } else {
      self.show();
    }
  };
  //
  this.show = function() {
    clearTimeout(self.timerID);
    dojo.animateProperty({node:self.popup,properties:{height:self.fullHeight}}).play();
    dojo.addClass(self.button,'active');
    self.showed = true;
  };
  //
  this.stop = function() {
    clearTimeout(self.timerID);
  };
  //
  this.wait = function() {
    clearTimeout(self.timerID);
    self.timerID = setTimeout(self.hide,100);
  };
  //
  this.hide = function() {
    dojo.animateProperty({node:self.popup,properties:{height:0}}).play();
    dojo.removeClass(self.button,'active');
    self.showed = false;
  };
  //
  return self.init();
}

// end of document
