/*
 * http://trac.openlayers.org/attachment/ticket/1484/ticket1484.patch
 * added by bartvde
 */ 

//-------------------------------------------------------------------
//Handler.js
OpenLayers.Handler.implement({
    /**  
    * APIProperty: cursors  
    * {Array} an array specifying the mouse cursors to use on the map div   
    */  
    cursor: null,
    
    /**  
    * Method: setCursor  
    * Set the mouse cursor on the map div  
    *  
    * Parameters:  
    * functionName - {String} the name of the function (e.g. mousedown)  
    */  
    setCursor: function(functionName) { 
        if (this.cursors && this.cursors[functionName]) { 
            this.map.div.style.cursor = this.cursors[functionName]; 
        } 
    }
});

 
//-------------------------------------------------------------------
//Control.js
OpenLayers.Control.implement({
    /** 
     * APIProperty: cursor 
     * {String} cursor which will be set on the map div upon activation. 
    */
    cursor: null,
    activate: function() {
        if (this.cursor) {
            this.map.div.style.cursor = this.cursor;
        }
        //else{
        //    this.map.div.style.cursor = 'default';
        //}
        
        if (this.active) {
            return false;
        }
        if (this.handler) {
            this.handler.activate();
        }
        this.active = true;
        this.events.triggerEvent("activate");
        return true;
    }
});


//-------------------------------------------------------------------
// Control/DragPan.js
OpenLayers.Control.DragPan.implement({
    draw: function() {
        this.handler = new OpenLayers.Handler.Drag(this,
                        {"move": this.panMap, "done": this.panMapDone}, this.handlerOptions);
    }
});


//-------------------------------------------------------------------
// Handler/Box.js
OpenLayers.Handler.Box.implement({
    startBox: function(xy) {
        this.zoomBox = OpenLayers.Util.createDiv('zoomBox',this.dragHandler.start);
        this.zoomBox.style.position = 'absolute';                                                 
        this.zoomBox.style.display = 'block';                                         
        this.zoomBox.style.width = '2px';                                         
        this.zoomBox.style.fontSize = '0px';                                        
        this.zoomBox.style.height = '2px';                                         
        this.zoomBox.style.border = '2px solid red';                                    
        this.zoomBox.style.zIndex = 9999999999;
        $('visualisation').appendChild(this.zoomBox);
    },
    endBox: function(end) {
        var result;
        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);
            var bottom = Math.max(start.y, end.y);
            var left = Math.min(start.x, end.x);
            var right = Math.max(start.x, end.x);
            result = new OpenLayers.Bounds(left, bottom, right, top);
        } else {
            result = this.dragHandler.start.clone(); // i.e. OL.Pixel
        } 
        $('zoomBox').remove() ;
        this.callback("done", [result]);
    }
});

 
//-------------------------------------------------------------------
// Handler/Drag.js
OpenLayers.Handler.Drag.implement({
    mousedown: function(evt) {
        this.setCursor('mousedown') ;
        var propagate = true;
        this.dragging = false;
        if (this.checkModifiers(evt) && OpenLayers.Event.isLeftClick(evt)) {
            this.started = true;
            this.start = evt.xy;
            this.last = evt.xy;
            this.down(evt);
            this.callback("down", [evt.xy]);
            OpenLayers.Event.stop(evt);
            
            if(!this.oldOnselectstart) {
                this.oldOnselectstart = (document.onselectstart) ? document.onselectstart : function() { return true; };
                document.onselectstart = function() {return false;};
            }
            
            propagate = !this.stopDown;
        } else {
            this.started = false;
            this.start = null;
            this.last = null;
        }
        return propagate;
    },
    mouseup: function(evt) {
        this.setCursor('mouseup') ;
        if (this.started) {
            var dragged = (this.start != this.last);
            this.started = false;
            this.dragging = false;
            this.up(evt);
            this.callback("up", [evt.xy]);
            if(dragged) {
                this.callback("done", [evt.xy]);
            }
            document.onselectstart = this.oldOnselectstart;
        }
        return true;
    },
    mouseout:  function (evt) {
        if (this.started && OpenLayers.Util.mouseLeft(evt, this.map.div)) {
            var dragged = (this.start != this.last);
            this.started = false; 
            this.dragging = false;
            this.out(evt);
            this.callback("out", []);
            if(dragged) {
                this.callback("done", [evt.xy]);
            }
            if(document.onselectstart) {
                document.onselectstart = this.oldOnselectstart;
            }
        }
        return true;
    }
});  

