var mouseDownXPosition = 0;
var mouseDownYPosition = 0;
var mouseDownXValue = -1;
var mouseDownYValue = -1;


function myMouseButtonDown(sender, args) {
    sender.captureMouse();
    mouseDownXValue = dragGetXValue(sender);
    mouseDownYValue = dragGetYValue(sender);
    mouseDownXPosition = args.getPosition(null).x;
    mouseDownYPosition = args.getPosition(null).y;
}

function myMouseButtonUp(sender, args) {
    sender.releaseMouseCapture();
    mouseDownXValue = -1;
}

function myMouseMove(sender, args) {
    if (mouseDownXValue != -1) {
        var newXValue = mouseDownXValue + (args.getPosition(null).x - mouseDownXPosition);    
        var newYValue = mouseDownYValue + (args.getPosition(null).y - mouseDownYPosition);    
        dragSetValue(sender, newXValue, newYValue);
    }   
}

function dragGetXValue(sender) {
    return sender["Canvas.Left"]; 
}

function dragGetYValue(sender) {
    return sender["Canvas.Top"]; 
}

function dragSetValue(sender, newXValue, newYValue) {
	// if the mouse goes off the canvas, the mouse flag is reset to "up"
    if ((newXValue + sender.width) >= sender.getParent().width || (newYValue + sender.height) >= sender.getParent().height) {
       mouseDownXValue = -1;
    }
    else if (newXValue <= 1 || newYValue <=1) {
        mouseDownXValue = -1;
    }
	
	// check for the top wall of the canvas
	if (newYValue <= 0) {
		newYValue = 1;
	}
	// check for the left wall of the canvas
	if (newXValue <= sender.getParent() ["Canvas.Left"]) {
		newXValue = 1;
	}
	// check for the right wall of the canvas
    if (eval(newXValue + sender.width) >= (sender.getParent().width - 1)) {
		newXValue = sender.getParent().width - sender.width;
	}
	
	// check for the bottom wall of the canvas
	if (eval(newYValue + sender.height) >= (sender.getParent().height - 1)) {
		newYValue = sender.getParent().height - sender.height;
	}

    sender["Canvas.Left"] = newXValue;
	sender["Canvas.Top"] = newYValue;
}
