/*
 * $Id: crop.js 235 2006-08-08 22:20:27Z michi2 $
 */

var canvas, big_canvas, image, zoombox
var dx, dy;

function windowLoaded(evt) {
    canvas = document.getElementById("canvas");
    big_canvas = document.getElementById("big-canvas");
    image = document.getElementById("image");
    zoombox = document.getElementById("zoombox");
    DomUtils.addEventListener(image ,"mousedown",imageMouseDown, false);
    DomUtils.addEventListener(big_canvas ,"mousedown",imageMouseDown, false);
}

function imageMouseDown(evt) {
    evt = new Evt(evt);
    dx = evt.getX() - DomUtils.getX(image);
    dy = evt.getY() - DomUtils.getY(image);
    DomUtils.addEventListener(document.body,"mousemove",imageMouseMove,false);
    DomUtils.addEventListener(document.body,"mouseup",imageMouseUp,false);
    evt.consume();
}

function imageMouseMove(evt) {
    evt = new Evt(evt);
    var x = evt.getX() - dx;
    var y = evt.getY() - dy;
    var xz = -1 * x / 10 + 20;
    var yz = -1 * y / 10 + 350;
    DomUtils.setX(image,x);
    DomUtils.setY(image,y);
    DomUtils.setX(zoombox,xz);
    DomUtils.setY(zoombox,yz);
    evt.consume();
}

function imageMouseUp(evt) {
    DomUtils.removeEventListener(document.body,"mousemove",imageMouseMove,false);
    DomUtils.removeEventListener(document.body,"mouseup",imageMouseUp,false);
    evt.consume();
}

function handleMouseDown(evt) {
    evt = new Evt(evt);
    dx = evt.getX() - DomUtils.getX(evt.getSource());
    DomUtils.addEventListener(document.body,"mousemove",handleMouseMove,false);
    DomUtils.addEventListener(document.body,"mouseup",handleMouseUp,false);
    evt.consume();
}

function handleMouseMove(evt) {
    evt = new Evt(evt);
    var x = evt.getX() - dx;
    
    x = DomUtils.getX(image);
    var y = DomUtils.getY(image);
    var w = DomUtils.getWidth(image);
    var h = DomUtils.getHeight(image);
    DomUtils.setX(image,x);
    DomUtils.setY(image,y);
    DomUtils.setX(zoombox,x);
    DomUtils.setY(zoombox,y);
    evt.consume();
}

function handleMouseUp(evt) {
    DomUtils.removeEventListener(document.body,"mousemove",handleMouseMove,false);
    DomUtils.removeEventListener(document.body,"mouseup",handleMouseUp,false);
    evt.consume();
}
