MainMenu.Inherits(PortalControl);
function MainMenu() {
    this.Inherits(PortalControl);
    this.id = "MainMenu";
};

Object.extendprototype(MainMenu.prototype, {
    OnLoad : function() {
        // remove non script elements
        this.riHandle = null;
        // attach events
        engine.AddEventWithCallback(engine.GetElement("MainMenu:reg"), "mouseover", this, this.RegionBtnOver);
        engine.AddEventWithCallback(engine.GetElement("MainMenu:reg"), "mouseout", this, this.RegionBtnOut);
        engine.AddEventWithCallback(engine.GetElement("MainMenu:regcont"), "mouseover", this, this.RegionOver);
        engine.AddEventWithCallback(engine.GetElement("MainMenu:regcont"), "mouseout", this, this.RegionOut);
        engine.AddEventWithCallback(engine.GetElement("LangSelect"), "change", this, this.LanguageChange);
    },
    
    LanguageChange : function(evt) {
        location.href = this.RedirectString(location.href, "lang", engine.GetElement("LangSelect").value);
    },
    
    RedirectString: function(url, id, val) {
      if (url.indexOf(".aspx") < 0)
        return url + "Default.aspx?" + id + "=" + val;
      if (url.indexOf("?") < 0)
        return url + "?" + id + "=" + val;

      var idLoc = url.indexOf("?" + id + "=");
      if (idLoc < 0)
        idLoc = url.indexOf("&" + id + "=")
      if (idLoc >= 0) {
        // find first &
        var idEnd = url.indexOf("&", idLoc + 1);
        if (idEnd > 0) {
            return url.substring(0, idLoc + 1) + id + "=" + val + url.substr(idEnd, url.length);
        } else {
            return url.substring(0, idLoc + 1) + id + "=" + val;
        }
      }
      return url + "&" + id + "=" + val;
    },

    RegionOver : function(evt) {
        if (this.riHandle != null)
            window.clearTimeout(this.riHandle);
    },

    RegionOut : function(evt) {
        var obj = null;
        if (evt.toElement)
            obj = evt.toElement;
        else if (evt.relatedTarget) {
            obj = evt.relatedTarget;
        }
        if (!engine.ElementContains(engine.GetElement("MainMenu:regcont"), obj)) {
            this.riHandle = window.setTimeout(eventCallback(this, this.HideRegion), 300);
        }
    },

    RegionBtnOver : function(evt) {
        if (this.riHandle != null)
            window.clearTimeout(this.riHandle);
        // set position
        var coords = engine.ElementCoords(engine.GetElement("MainMenu:reg"));
        var o = engine.GetElement("MainMenu:regcont2");
        engine.GetElement("MainMenu:regcont").style.display = "block";
    },

    RegionBtnOut : function(evt) {
        var obj = null;
        if (evt.toElement)
            obj = evt.toElement;
        else if (evt.relatedTarget) {
            obj = evt.relatedTarget;
        }
        if (!engine.ElementContains(engine.GetElement("MainMenu:reg"), obj)) {
            this.riHandle = window.setTimeout(eventCallback(this, this.HideRegion), 300);
        }
    },
    
    HideRegion : function() {
        engine.GetElement("MainMenu:regcont").style.display = "";
    }
});
engine.RegisterControl("MainMenu");