/*  Javascript Bounding Box, version 0.0.1
 *  (c) 2007 Arthur Taylor, http://talkingcode.co.uk
 *  License: See http://talkingcode.co.uk/script/license.txt
 */
var BoundingBox = Class.create({
  initialize : function() {
    if (!$('__rubber_band'))
    {
      Element.insert(document.body, '<div id="__rubber_band"></div>');
    }
    this.box = $('__rubber_band');
  },
  getE : function(evt)
  {
    if (evt)
    {
      return Event.extend(evt);
    }
    return Event.extend(event);
  },
        startRubber: function(evt) {
          var e = this.getE(evt);
          this.starttime = (new Date()).valueOf();
          if (e.pointerX() < document.body.getWidth())
          {
            this.originY = e.pointerY();
            this.originX = e.pointerX();

            this.box.setStyle({
                        width: "0px",
                        height: "0px",
                        top: this.originY + "px",
                        left: this.originX + "px",
                        visibility: "visible",
                        position: "absolute",
                        border: "1px dashed #000000"
                      });
            document.onmousedown = null;
            if (this.drag)
            {
              document.onmouseup = this.stopRubber.bindAsEventListener(this);
            }
            else
            {
              document.onclick = this.stopRubber.bindAsEventListener(this);
              if (this.firstClickCallback)
              {
                this.firstClickCallback();
              }
            }
            document.onmousemove = this.moveRubber.bindAsEventListener(this);
          }
        },
        moveRubber: function(evt) {
          var e = this.getE(evt);
          if (e.pointerX() < document.body.getWidth())
          {
            width = e.pointerX() - this.originX;
            height = e.pointerY() - this.originY;
            newwidth = Math.abs(width);
            newheight = Math.abs(height);
            newleft = this.originX;
            newtop = this.originY;
            if (width == 0 || height == 0)
            {
              return;
            }
            if (width < 0)
            {
              newleft = e.pointerX();
            }
            if (height < 0)
            {
              newtop = e.pointerY();
            }
            this.box.setStyle({
                          left: (newleft + "px"),
                          top: (newtop + "px"),
                          width: (newwidth + "px"),
                          height: (newheight + "px")
                         });
          }
        },
        getBoundElements: function() {
          var items = $$("[bandable]");
          var selected = new Array();
          var band_offset = this.box.viewportOffset();
          var band_left = band_offset[0];
          var band_top = band_offset[1];
          var band_right = this.box.getWidth() + band_left;
          var band_base = this.box.getHeight() + band_top;
          for (var i=0; i<items.length; i++)
          {
            var item = items[i];
            item_offset = item.viewportOffset();
            item_left = item_offset[0];
            item_top = item_offset[1];
            item_right = item.getWidth() + item_left;
            item_base = item.getHeight() + item_top;
            if (band_left > item_right || band_top > item_base ||
                band_right < item_left || band_base < item_top)
            {
              continue;
            }
            if ((band_left < item_right && band_top < item_base) ||
                (band_right > item_left && band_base > item_top))
            {
              selected.push(item.id);
            }
          }
          return selected;
        },
        stopRubber : function(e) {
          var theDate = new Date();
          if (((theDate.valueOf() - this.starttime) < 200) && !this.drag)
          {
            return;
          }
          document.onmousemove = null;
          document.onclick = null;
          this.makeReservation();
        },
        startSelection_internal: function(drag) {
          this.drag = drag;
          document.onmousedown = this.startRubber.bindAsEventListener(this);
          document.body.style.cursor = "crosshair";
          this.box.setStyle({
            width: 0,
            height: 0
           });
        },
        startSelection: function(releaseCallback) {
          this.releaseCallback = releaseCallback;
          this.startSelection_internal(true);
        },
        startTwoClickSelection: function(firstClickCallback, secondClickCallback) {
          this.releaseCallback = releaseCallback;
          this.firstClickCallback = firstClickCallback;
          this.startSelection_internal(false);
        },
        makeReservation: function()
        {
          if (!(this.box.getWidth() != 0 || this.box.getHeight() != 0))
          {
            return;
          }
          document.onmouseup = null;
          this.box.setStyle({
                   visibility: "hidden"
                  });
          document.body.style.cursor = "default";
          if (this.releaseCallback)
          {
            this.releaseCallback(this.getBoundElements());
          }
        }
      });

var _pX = Event.pointerX;
Event.pointerX=function(event){
    if (!document || !document.documentElement || !document.body) return 0;
    return _pX(event);
};
var _pY = Event.pointerY; 
Event.pointerY=function(event){
    if (!document || !document.documentElement || !document.body) return 0;
    return _pY(event);
}; 


