var rpPopUp = {
  js: {
    dragndrop : {
      ie: document.attachEvent != null,
      dragging: false,
      position: {},
      draggable: null,

      initialize: function() {

        this.move_x = this.ie ?
                      function(e) { return this.position.bx + event.clientX - this.position.x } :
                      function(e) { return this.position.bx + e.clientX - this.position.x }

        this.move_y = this.ie ?
                      function(e) { return this.position.by + event.clientY - this.position.y} :
                      function(e) { return this.position.by + e.clientY - this.position.y }

        var handler = this;

        document.onmousedown = function(e) { rpPopUp.js.dragndrop.on_mouse_start_drag.call(handler, e) };
        document.onmouseup = function() { rpPopUp.js.dragndrop.dragging = false }
      },

      on_mouse_move: function(e) {
        if (this.dragging) {
          var d = this.draggable

          var x  = d.style.left = (this.move_x.call(this, e) + 'px')
          var y  = d.style.top = (this.move_y.call(this, e) + 'px')

          //console.log(x + ':' + y)

          return false;
        }
      },

      on_mouse_start_drag : function(e) {
        var source  = this.ie ? event.srcElement : e.target;
        var root    = this.ie ? "body" : "html";
        var pattern = /\bdraggable\b/i

        while ((root != source.tagName.toLowerCase() && source.className.search(pattern) < 0)) {
          source = this.ie ? source.parentElement : source.parentNode;
        }

        if (source == null || source.className.search(pattern) < 0) return;

        this.dragging = true;
        this.draggable = source;

        this.position.bx = parseInt(this.draggable.offsetLeft + 0);
        this.position.by = parseInt(this.draggable.offsetTop + 0);

        this.position.x = this.ie ? event.clientX : e.clientX;
        this.position.y = this.ie ? event.clientY : e.clientY;

        var handler = this;
        document.onmousemove = function(e) { return rpPopUp.js.dragndrop.on_mouse_move.call(handler, e)};

        return false;
      }
    }
  }
}