/*  This is a hack of OpenLayers.Handler.Box
 * that works with a position:fixed map.div
 */
Mospace.BoxHandler=OpenLayers.Class(OpenLayers.Handler,{
		dragHandler:null,boxDivClassName:'olHandlerBoxZoomBox',
		boxCharacteristics:null,
		initialize:function(control,callbacks,options){
			OpenLayers.Handler.prototype.initialize.apply(this,arguments);
			var callbacks={"down":this.startBox,"move":this.moveBox,"out":this.removeBox,"up":this.endBox};
			this.dragHandler=new OpenLayers.Handler.Drag(this,callbacks,{keyMask:this.keyMask});
		},
		setMap:function(map){
			OpenLayers.Handler.prototype.setMap.apply(this,arguments);
			if(this.dragHandler){
				this.dragHandler.setMap(map);
			}
		},
		startBox:function(xy){
			start = this.dragHandler.start;
			start.x += this.map.div.offsetLeft -  getScrollXY()[0];
			start.y += this.map.div.offsetTop - getScrollXY()[1];
			this.zoomBox=OpenLayers.Util.createDiv('zoomBox',start);
			this.zoomBox.className=this.boxDivClassName;
			this.zoomBox.style.zIndex=this.map.Z_INDEX_BASE["Popup"]-1;
			this.zoomBox.style.position='fixed';
			this.map.viewPortDiv.appendChild(this.zoomBox);
			this.map.div.style.cursor="crosshair";
		},
		moveBox:function(xy){
			var startX=this.dragHandler.start.x;
			var startY=this.dragHandler.start.y; 
			xy.x = xy.x + this.map.div.offsetLeft  -  getScrollXY()[0];
			xy.y = xy.y + this.map.div.offsetTop  - getScrollXY()[1];
			var deltaX=Math.abs(startX-xy.x);
			var deltaY=Math.abs(startY-xy.y);
			this.zoomBox.style.width=Math.max(1,deltaX)+"px";
			this.zoomBox.style.height=Math.max(1,deltaY)+"px";
			this.zoomBox.style.left =  (xy.x < startX ? xy.x : startX) + "px";
			this.zoomBox.style.top=   (xy.y < startY ? xy.y : startY)  + "px";
			var box=this.getBoxCharacteristics(deltaX,deltaY);
			if(box.newBoxModel){
				if(xy.x>startX){
					this.zoomBox.style.width=Math.max(1,deltaX-box.xOffset)+"px";
				}
				if(xy.y>startY){
					this.zoomBox.style.height=Math.max(1,deltaY-box.yOffset)+"px";
				}
			}
		},
		endBox:function(end){
			var result;
			
			end.x += this.map.div.offsetLeft  -  getScrollXY()[0];
			end.y += this.map.div.offsetTop  - getScrollXY()[1];
			if(Math.abs(this.dragHandler.start.x-end.x)>5 ||
		            Math.abs(this.dragHandler.start.y-end.y)>5)
			{
				var start=this.dragHandler.start;
				var top=Math.min(start.y,end.y) - this.map.div.offsetTop ;
				var bottom=Math.max(start.y,end.y) - this.map.div.offsetTop ;
				var left=Math.min(start.x,end.x) - this.map.div.offsetLeft ;
				var right=Math.max(start.x,end.x) - this.map.div.offsetLeft ;
				result=new OpenLayers.Bounds(left,bottom,right,top);
			}else{
				result = this.dragHandler.start.clone();
			}
			this.removeBox();
			this.map.div.style.cursor="";
			this.callback("done",[result]);
		},
		removeBox:function(){
			this.map.viewPortDiv.removeChild(this.zoomBox);
			this.zoomBox=null;
			this.boxCharacteristics=null;
		},activate:function(){if(OpenLayers.Handler.prototype.activate.apply(this,arguments)){this.dragHandler.activate();
			return true;
			}else{return false;
		}},deactivate:function(){if(OpenLayers.Handler.prototype.deactivate.apply(this,arguments)){this.dragHandler.deactivate();
			return true;
			}else{return false;
		}},getBoxCharacteristics:function(dx,dy){if(!this.boxCharacteristics){var xOffset=parseInt(OpenLayers.Element.getStyle(this.zoomBox,"border-left-width"))+parseInt(OpenLayers.Element.getStyle(this.zoomBox,"border-right-width"))+1;
			var yOffset=parseInt(OpenLayers.Element.getStyle(this.zoomBox,"border-top-width"))+parseInt(OpenLayers.Element.getStyle(this.zoomBox,"border-bottom-width"))+1;
			var newBoxModel=OpenLayers.Util.getBrowserName()=="msie"?document.compatMode!="BackCompat":true;
			this.boxCharacteristics={xOffset:xOffset,yOffset:yOffset,newBoxModel:newBoxModel};
			}
			return this.boxCharacteristics;
},CLASS_NAME:"OpenLayers.Handler.Box"});

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
    if(navigator.appName == 'Opera'){
	    scrOfY = 2 * scrOfY;
    }
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

