  $.blockUI.defaults.pageMessage = "";
  $.extend($.blockUI.defaults.pageMessageCSS,{border: "1px #000 solid", padding: "5px"});
  window.MSG_WAIT = function(text) {
    return "<table align=center style='width: 220px;'><tr><td style='width: 40px; text-align: center; vertical-align: middle;'><img width=20 height=20 src=/images/loading.gif></td><td style='text-align: center; vertical-align: middle;'><b>Пожалуйста, подождите.</b><br /><small>"+text+"</small></td></tr></table>";
  };
//<script>//****************** JavaScript движок сайта *****************//
  (function() {

//************ Объект "Приложение" ************//
    enApp.prototype = {
      region: "russia",
      unit: "default",
      view: "default",
      action: "default",
      uri: "",
      options: "",
      blocked: 0,
      resources_url: "",
      block:
        function() {
          this.blocked++;
        },
      unblock:
        function() {
          if (this.blocked>0) this.blocked--;
          if (this.blocked==0) $.unblockUI();
        },
      showMessage:
        function(options) {
          if (this.dialogWindow==undefined) {
            this.dialogWindow = new enWindow();
          }
          if (options.title==undefined) options.title = "Сообщение";
          this.dialogWindow.text.html("<table><tbody><tr><td style='height:70px;text-align:center;vertical-align:middle;'>"+options.text+"</td></tr></tbody></table><center><input type='button' class='button' value='Ok' style='width: 80px;' enevent='type:\"click\",name:\"close"+this.dialogWindow.id+"\"'></center>");
          this.dialogWindow.show({title:options.title,width:350,height:150,style:"blue"});
          if (options.event!=undefined) this.dialogWindow.onclose = function() {window.enApp.events.add(options.event); return true;};
          else this.dialogWindow.onclose = null;
          this.events.activateLinks(this.dialogWindow.text);
          this.dialogWindow.text.find("input[type=button]").focus()
        },
    // Обработка событий
      events: {
        parent: null,
        first: null,
        last: null,
        blocked: false,
        add:
          function(e) {
            e.next = null;
            if (this.first==null) {
              this.first = e;
              this.last = e;
            }
            else {
              this.last.next = e;
              this.last = e;
            }
          },
        activateLinks:
          function (jQ) {
            jQ.find("[enevent]").click(function(ev) {
              e = eval("(function(){return{"+$(this).attr("enevent")+"}})();");
              e.author = $(this);
              if (e.url==undefined && $(this).attr("href")!=undefined) e.url = $(this).attr("href");
              e.eventInfo = {};
              for (var i in ev) e.eventInfo[i]=ev[i];
              window.enApp.events.add(e);
              return false;
            });
          },
        takeEvent:
          function() {
            var e = this.first;
            if (this.first!=null) {
              this.first = this.first.next;
              if (this.first==null) this.last = null;
            }
            return e;
          },
        process: //Старт обработки событий
          function() {
            if (this.eventCount==undefined) this.eventCount = 0;
            this.eventCount++;
            if (window.enPanelLog!=undefined && window.enPanelLog.text!=undefined) window.enPanelLog.text.find("#eventCount").html(this.eventCount);
            if (this.first==null) {
              if (this.blocked) {
                this.parent.unblock();
                this.blocked = false;
              }
              setTimeout("enApp.events.process();",250);
            }
            else {
              var enEvent = this.takeEvent();
              if (window.enPanelLog!=undefined) {
                var s = "";
                for (var n in enEvent) {
                  if (n!="type"&&n!="name"&&n!="next") s = s+(s!=""?",":"")+n+":"+enEvent[n];
                }
                window.enPanelLog.log("Событие: "+enEvent.type+"("+((enEvent.name==undefined)?"":enEvent.name)+")("+s+")");
              }
              if (enEvent.msg!=undefined) {
                $.blockUI({message: MSG_WAIT(enEvent.msg)});
                if (!this.blocked) {
                  this.parent.block();
                  this.blocked = true;
                }
              }
              var listeners;
              var l = null;
              switch (enEvent.type) {
                case "getScript": //Событие "Загрузка JavaScript"
                  listeners = this.listeners["getScript"];
                  if (listeners[enEvent.name]!=undefined) { //Обработка именованных событий
                    l = listeners[enEvent.name];
                    while (l!=null) {
                      l.listener.enEvent(enEvent);
                      l = l.next;
                    }
                  }
                  l = listeners["all"]; //Обработка событий по умолчанию
                  while (l!=null) {
                    l.listener.enEvent(enEvent);
                    l = l.next;
                  }
                  $.getScript(enEvent.url, function() {setTimeout("enApp.events.process();",50);});
                  break;
                case "getWindow": //Событие "Открыть окно и загрузить код"
                  listeners = this.listeners["getWindow"];
                  if (listeners[enEvent.name]!=undefined) { //Обработка именованных событий
                    l = listeners[enEvent.name];
                    while (l!=null) {
                      l.listener.enEvent(enEvent);
                      l = l.next;
                    }
                  }
                  l = listeners["all"]; //Обработка событий по умолчанию
                  while (l!=null) {
                    l.listener.enEvent(enEvent);
                    l = l.next;
                  }
                  if (enEvent.options==undefined) {enEvent.options = {title: "Сообщение"};}
                  enWin = new enWindow(enEvent.options);
                  enWin.text.html('<br><br><img src="'+this.parent.resources_url+'/img/loading.gif"><br><b><small>Идет загрузка...</small></b>');
                  enWin.show();
                  window.enApp.events.add({type: "getHTML", url: enEvent.url, forItem: enWin.text});
                  setTimeout("enApp.events.process();",50);
                  break;
                case "getHTML": //Событие "Загрузка HTML"
                  listeners = this.listeners["getHTML"];
                  if (listeners[enEvent.name]!=undefined) { //Обработка именованных событий
                    l = listeners[enEvent.name];
                    while (l!=null) {
                      l.listener.enEvent(enEvent);
                      l = l.next;
                    }
                  }
                  l = listeners["all"]; //Обработка событий по умолчанию
                  while (l!=null) {
                    l.listener.enEvent(enEvent);
                    l = l.next;
                  }
                  enEvent.forItem.load(enEvent.url, function() {window.enApp.events.activateLinks(enEvent.forItem); setTimeout("enApp.events.process();",50);});
                  break;
                case "function": //Событие "Вызов функции"
                  listeners = this.listeners["function"];
                  if (listeners[enEvent.name]!=undefined) { //Обработка именованных событий
                    l = listeners[enEvent.name];
                    while (l!=null) {
                      l.listener.enEvent(enEvent);
                      l = l.next;
                    }
                  }
                  l = listeners["all"]; //Обработка событий по умолчанию
                  while (l!=null) {
                    l.listener.enEvent(enEvent);
                    l = l.next;
                  }
                  enEvent.fn();
                  setTimeout("enApp.events.process();",50);
                  break;
                case "animate": //Событие "Анимация"
                  listeners = this.listeners["animate"];
                  if (listeners[enEvent.name]!=undefined) { //Обработка именованных событий
                    l = listeners[enEvent.name];
                    while (l!=null) {
                      l.listener.enEvent(enEvent);
                      l = l.next;
                    }
                  }
                  l = listeners["all"]; //Обработка событий по умолчанию
                  while (l!=null) {
                    l.listener.enEvent(enEvent);
                    l = l.next;
                  }
                  enEvent.forItem.animate(enEvent.effect,enEvent.speed,function() {setTimeout("enApp.events.process();",50);});
                  break;
                default:
                  if (this.listeners[enEvent.type]!=undefined) {
                    listeners = this.listeners[enEvent.type];
                    if (listeners[enEvent.name]!=undefined) { //обработка именованных событий
                      l = listeners[enEvent.name];
                      while (l!=null) {
                        l.listener.enEvent(enEvent);
                        l = l.next;
                      }
                    }
                    l = listeners["all"]; //обработка событий по умолчанию
                    while (l!=null) {
                      l.listener.enEvent(enEvent);
                      l = l.next;
                    }
                  }
                  setTimeout("enApp.events.process();",50);
              }
            }
          },
        listeners: {//Список обработчиков событий
          parent: null,
          "click": {
            all: null
          },
          "getScript": {
            all: null
          },
          "getWindow": {
            all: null
          },
          "getHTML": {
            all: null
          },
          "function": {
            all: null
          },
          "animate": {
            all: null
          },
          add:
            function(type,name,p) {
              if (this[type]==undefined) this[type] = {all: null};
              if (name=="") {
                var l = {listener: p, next: this[type].all};
                this[type].all = l;
              }
              else {
                if (this[type][name]==undefined) this[type][name] = null;
                var l = {listener: p, next: this[type][name]};
                this[type][name] = l;
              }
              return p;
            },
          remove:
            function(type,name,p) {
              if (name=="") {
                if (this[type]!=undefined) {
                  var l = this[type].all;
                  if (p==l.listener) this[type].all = l.next;
                  while (l.next!=null) {
                    if (p==l.next.listener) l.next = l.next.next;
                    else l = l.next;
                  }
                }
              }
              else {
                if (this[type]!=undefined && this[type][name]!=undefined) {
                  var l = this[type][name];
                  if (p==l.listener) {
                    this[type][name] = l.next;
                    if (this[type][name]==null) delete this[type][name];
                  }
                  while (l.next!=null) {
                    if (p==l.next.listener) l.next = l.next.next;
                    else l = l.next;
                  }
                }
              }
              return p;
            },
          makeFirst:
            function(type,name,p) {
              this.remove(type,name,p);
              this.add(type,name,p);
            }
        } //listeners
      }//events
    };
    function enApp() {
      this.events.parent = this;
      this.events.listeners.parent = this.events;
      setTimeout("enApp.events.process();",250);
    };
    window.enApp = new enApp();

//************ Объект "Диалоговое окно" ************//
    enWindow.prototype = {
      title: "",
      width: 400,
      height: 200,
      style: "green",
      visiable: false,
      win: null,
      text: null,
      id: 0,
      onclose: null,
      usercanclose: true,
      droponclose: true,
      init: function() {
        this.id = new Date();
        this.id = this.id.getTime();
        $("<table id='w"+this.id+"'><tbody><tr><td style='text-align: center; vertical-align: middle;'><center><div id='window"+this.id+"'><div style='text-align: right; padding-right: 5px; padding-bottom: 10px;'><small>&nbsp;<a href=# id='close"+this.id+"'><b>Закрыть окно</b></a></small></div><div id=t"+this.id+" class='title"+this.style+"'>"+this.title+"</div><div id=text"+this.id+"></div></div></center></td></tr></tbody></table>").find("div#text"+this.id).css({overflow: "auto"}).end().css({height: "100%", width: "100%", top: "0px", left: "0px", position: "fixed", "z-index": 2001}).find("div#window"+this.id).css({background: "#FFF", border: "2px #000 solid"}).end().find("a#close"+this.id).attr("winid",this.id).click(function() {window.enApp.events.add({type: "click", name: "close"+$(this).attr("winid")}); return false;}).end().appendTo("body").hide();
        this.win = $("table#w"+this.id);
        if ($.browser.msie) {
          this.win.css({position: "absolute"});
          document.getElementById("w"+this.id).style.setExpression("top","eval(document.body.scrollTop) + 'px'","JavaScript");
          document.getElementById("w"+this.id).style.setExpression("height","eval(document.body.clientHeight) + 'px'","JavaScript");
        }
        this.text = this.win.find("div#text"+this.id);
      },
      show: function(options) {
        if (options!=undefined) {
          if (options.title!=undefined) this.title = options.title;
          if (options.width!=undefined) this.width = options.width;
          if (options.height!=undefined) this.height = options.height;
          if (options.style!=undefined) this.style = options.style;
          if (options.onclose!=undefined) this.onclose = options.onclose;
        }
        if (!this.usercanclose) this.win.find("a#close"+this.id).css({display:"none"});
        else this.win.find("a#close"+this.id).css({display:"inline"});
        this.win.find("div#t"+this.id).attr("class","title"+this.style).html(this.title);
        this.text.css({height: (this.height - 50)+"px"});
        this.win.find("div#window"+this.id).css({height: this.height+"px", width: this.width+"px"});
        if (!this.visiable) {
          this.visiable = true;
          window.enApp.block();
          $.blockUI({message: null});
        }
        this.win.appendTo("body").show();
        if ($.browser.msie) {
          this.win.css({position: "absolute"});
          document.getElementById("w"+this.id).style.setExpression("top","eval(document.body.scrollTop) + 'px'","JavaScript");
          document.getElementById("w"+this.id).style.setExpression("height","eval(document.body.clientHeight) + 'px'","JavaScript");
        }
      },
      close: function() {
        if (this.onclose != null) {
          if (this.onclose()) {
            this.win.hide();
            this.visiable = false;
            window.enApp.unblock();
          };
        }
        else {
          $("table#w"+this.id).hide();
          this.visiable = false;
          window.enApp.unblock();
        }
        if (this.droponclose) this.free();
      },
      free: function() {
        this.win.remove();
        window.enApp.events.listeners.remove("click","close"+this.id,this);
        if (this.visiable) window.enApp.unblock();
      },
      enEvent: function() {
        this.close();
      }
    };
    function enWindow(options) {
      if (options!=undefined) {
        if (options.title!=undefined) this.title = options.title;
        if (options.width!=undefined) this.width = options.width;
        if (options.height!=undefined) this.height = options.height;
        if (options.style!=undefined) this.style = options.style;
        if (options.onclose!=undefined) this.onclose = options.onclose;
      }
      this.init();
      window.enApp.events.listeners.add("click","close"+this.id,this);
    };
    window.enWindow = enWindow;

//************ Объект для отображения древовидных структур ************//
    enTree.prototype = {
      map: null,
      create:
        function(base, fnConstructor) {
          function fc(b, fn) {
            var r = [];
            var k = 0;
            for (var i in b) {
              r[k] = [];
              r[k][0] = null;
              r[k][1] = fn(b[i]);
              if (b[i][0]!=null) {
                r[k][0] = fc(b[i][0],fn);
              }
              k++;
            }
            return r;
          };
          this.map = fc(base,fnConstructor);
        },
      show:
        function(inbox) {
          function sh(b,p,l) {
            var r = "";
            if (b[0]==null) {
              r = "<nobr>"+p+"<img width=13 height=18 align='absmiddle' src='"+window.enApp.resources_url+"/img/tree/"+(l?"L":"T")+".gif'> "+b[1]+"</nobr><br>";
            }
            else {
              var d = "tree"+Math.round(Math.random()*100000000+10000000);
              var c = b[0].length;
              r = "<nobr>"+p+"<a href='javascript: void(0);' onclick=\" document.getElementById('i"+d+"').src=(document.getElementById('"+d+"').style.display=='none'?'"+window.enApp.resources_url+"/img/tree/"+(l?"L":"T")+"minus.gif':'"+window.enApp.resources_url+"/img/tree/"+(l?"L":"T")+"plus.gif'); window.enApp.events.add({type: 'animate',forItem:$('#"+d+"'),effect:{height: 'toggle'},speed:'normal'});\"><img width=13 height=18 align='absmiddle' id=i"+d+" src="+window.enApp.resources_url+"/img/tree/"+(l?"L":"T")+"plus.gif></a> "+b[1]+"</nobr><br><div id="+d+" style='display: none;'>";
              for (var i in b[0]) {
                r = r+sh(b[0][i],p+(l?"<img width=13 height=18 align='absmiddle' src='"+window.enApp.resources_url+"/img/tree/blank.gif'>":"<img align='absmiddle' src='"+window.enApp.resources_url+"/img/tree/I.gif'>"),--c==0);
              }
              r = r+"</div>";
            }
            return r;
          }
          var r = "";
          var c = this.map.length;
          for (var i in this.map) {
            r = r+sh(this.map[i],"",--c==0);
          }
          window.enApp.events.activateLinks($(inbox).html(r));
        }
    };
    function enTree(base,fnConstructor) {
      if (base != undefined) {
        if (typeof(fnConstructor) == "function") {
          this.create(base, fnConstructor);
        }
        else {
          this.map = base;
        }
      }
    };
    window.enTree = enTree;
  })();

