/*
  Satus Belyash JS menu dragging module.
  Made by danilissimus (http://danilissimus.habrahabr.ru)
*/
      //for usual browsers
      window.onload = dragFindElems;
      
      //for shitty IE
      /*@cc_on @*/
      /*@if (@_win32)
      document.write("<script id=__ie_onload defer src=javascript:void(0)>");
      document.write("<\/script>");
      var script = document.getElementById("__ie_onload");
      script.onreadystatechange = function() {
          if (this.readyState == "complete") {
              dragFindElems();
          }
      };
      /*@end @*/
      
      
      var dragElement = [];
      dragElement['drag'] = null;
      dragElement['acce'] = null;
      dragElement['tfex'] = false;
      
        Node.prototype.swapNode_a = function(swapNode) {
            var n = this.cloneNode(true);
            var nt = swapNode.cloneNode(true);
            this.parentNode.insertBefore(nt,this);
            this.parentNode.removeChild(this);
            swapNode.parentNode.insertBefore(n,swapNode);
            swapNode.parentNode.removeChild(swapNode);
        }
      
      function dragFindElems() {
          var divs = document.getElementsByTagName("div");
          var pattern = new RegExp("^iE-[a-zA-Z0-9]+$");
          for(var i = 0; i<divs.length; i++) {
              if(pattern.test(divs[i].id)) {
                  dragDoMovable(document.getElementById(divs[i].id));
                  document.getElementById(divs[i].id).style.border = "1px solid white";
              }
          }
      }


      function dragDoMovable(elem) {
              elem.onmousedown = mouseDown;
              elem.onmouseup   = mouseUp;
              elem.onmouseover = mouseOver;
              elem.onmouseout  = mouseOut;
              elem.style.cursor = "move";
              
      }
      
      function mouseDown() {
          document.body.ondragstart = function() { return false }
          document.body.onselectstart = function() { return false }
          dragElement['tfex'] = true;
          dragElement['drag'] = this;
          this.style.border = "1px solid red";
          return false;
      }
      
      function mouseUp() {
          if(!dragElement['acce'] || dragElement['drag'].id == this.id) {
              this.style.border = "1px solid white";
              mouseReset();
          } else {
              if(dragElement['drag'] != dragElement['acce']) {
                  dragReplace(dragElement['drag'], dragElement['acce']);
                  mouseReset();
                  
              } else {
                  mouseReset();                 
              }
          }
      }
      
      function mouseOver() {
          if(dragElement['tfex']) {
              dragElement['acce'] = this;
          }
          if(dragElement['drag'] && this.id != dragElement['drag'].id ) {
              this.style.border = "1px dotted blue";
          }          
      }
      
      function mouseOut() {
          if(dragElement['tfex'] && dragElement['acce']) {
              dragElement['acce'] = null;
              
          }
          if(dragElement['drag'] && this.id != dragElement['drag'].id) {
              this.style.border = "1px solid white";
          }
      }
      
      function mouseReset() {
                  document.body.ondragstart = null;
                  document.body.onselectstart = null;
                  dragElement['tfex'] = false;
                  dragElement['tfex'] = null;
                  dragElement['drag'] = null;
                  dragFindElems();   
      }
      
      function dragReplace(elem1, elem2) {
            if(navigator.appName == "Microsoft Internet Explorer") {
                //native method
                elem1.swapNode(elem2);
            } else {
                //fucking shit method :/
                elem1.swapNode_a(elem2);
            }
      }