
function ADrager(divDown, divMove, target) {

    var lastX = undefined;
    var self = this;
    this.delta = 0;

    this.startDrag = function(event) {
        self.delta = 0;
        lastX = event.screenX;
        event.preventDefault();
    };

    this.drag = function(event) {

        if (lastX != undefined) {
            // Delta

            self.delta = (event.screenX - lastX);
            lastX = event.screenX;

            // Moving
            target.onDrag(self.delta);
        }
        event.preventDefault();
    };

    this.stopDrag = function(event) {
        lastX = undefined;
        event.preventDefault();
    }

    divDown.mousedown(this.startDrag);
    divMove.mousemove(this.drag);
    $("body").mouseup(this.stopDrag);
    $("body").mouseleave(this.stopDrag);

    var cnt = 0;


    this.touchDrag = function(event) {

        if (lastX != undefined) {
          //if (event.targetTouches.length == 1){

            event.preventDefault();

            // Delta

            var delta = (event.originalEvent.touches[0].pageX - lastX);
            lastX = event.originalEvent.touches[0].pageX;

            self.delta = (event.screenX - lastX);

            // Moving
            target.onDrag(delta);

            //cnt++;
            //if (cnt==5) {
            //  alert(cnt);
            //}

          //}
        }
    };


    this.startTouchDrag = function(event) {
        self.delta = 0;
        event.preventDefault();
        lastX = event.originalEvent.touches[0].pageX;
        event.preventDefault();
    };




    // iPhone iPad
    divDown.bind('touchstart', this.startTouchDrag);
    divMove.bind('touchmove',   this.touchDrag);
    $("body").bind('touchend',    this.stopDrag);
    $("body").bind('touchcancel', this.stopDrag);

}

