if(typeof(Mesh) == 'undefined')
  var Mesh = new Object();

Mesh.Control = {
  domLoaded       : false,
  lvl0Scripts     : new Array(),
  lvl2Scripts     : new Array(),
  loadScripts     : new Array(),
  loadStart       : null,
  _options        : { windowParameters: { width: 250, height: 100, showEffect: Element.show, hideEffect: Element.hide },
                      buttonClass:      'button' },
  dialogOptions   : function(){ return Mesh.ObjectClone(this._options); },
  init            : function(){
                      if(arguments.length > 0)
                        var loadLevel = arguments[0];
                      else
                        loadLevel = 1;

                      if(!this.domLoaded){
                        this.loadStart    = new Date();
                        for(var i = 0; i < this.lvl0Scripts.length; i++){
                          if(typeof(this.lvl0Scripts[i]) == 'function')
                            this.lvl0Scripts[i]();
                        }
                      }
                      if(loadLevel > 0){
                        for(var i = 0; i < this.loadScripts.length; i++){
                          if(typeof(this.loadScripts[i]) == 'function')
                            this.loadScripts[i]();
                        }
                        if(Mesh.StartTime){
                          var time = new Date();
                          if(typeof console != 'undefined'){
                            console.log("LOAD TIME: " + parseFloat((time.getTime() - Mesh.StartTime) / 1000) + " Sec\n");
                            console.log("LOAD SCRIPTS TIME: " + parseFloat((time.getTime() - this.loadStart.getTime()) / 1000) + " Sec\n");
                          }
                        }
                      }
                      this.domLoaded = true;
                      window.setTimeout(this.initDeferred.bind(this), 1000); //run deferred scripts 1 second after load
                    },
  initDeferred    : function(){
                      for(var i = 0; i < this.lvl2Scripts.length; i++){
                        if(typeof(this.lvl2Scripts[i]) == 'function')
                          this.lvl2Scripts[i]();
                      }
                    },
  openPDF:         function(href){
                     window.open(href, "");
                   },
  checkEnter      : function(func){
                      return function(){
                        if(arguments.length > 0){
                          var code = Event.getKeyCode(arguments[0]);
                          if(code == Event.KEY_RETURN){
                            func();
                          }
                        }
                      }
                    },
  setupKeyListener: function(func, keyCode, ctrlKey){
                      return function(){
                        if(arguments.length > 0){
                          var code = Event.getKeyCode(arguments[0]);
                          if(code == keyCode && ((ctrlKey && arguments[0].ctrlKey) || (!ctrlKey && !arguments[0].ctrlKey)))
                            return func();
                        }
                        return true;
                      }
                    }
};

Event.observe(window, "DOMContentLoaded", Mesh.Control.init.bind(Mesh.Control, 0));
Event.observe(window, 'load', function(){ Mesh.Control.init(); });

// for Internet Explorer (using conditional comments)
/*@cc_on @*/
/*@if (@_win32)
document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
var script = document.getElementById("__ie_onload");
script.onreadystatechange = function() {
  if (this.readyState == "complete") {
    Mesh.Control.init(0); // call the onload handler
  }
};
/*@end @*/
