﻿/*

拖动类

*/

function clsDrag(a,b,cursor,onStart,onDrag,onEnd, onScroll,isDrag, isDisposed,handler,self)
{
    if (typeof(a) == 'string')
        this._obj = document.getElementById ? document.getElementById(a) : null
    else if (typeof(a) == 'object')
        this._obj = a;
    else
        this._obj = null;
    
    if (typeof(b) == 'string')
        this.b_obj = document.getElementById ? document.getElementById(b) : null
    else if (typeof(b) == 'object')
        this.b_obj = a;
    else
        this.b_obj = null;
    this.handler=handler;
    this.self=self;
    
        
    if(this._obj == null)
        return;
    if (!isNaN(this._obj.__isCD) && this._obj.__isCD == true)
        return;
    this._obj.__isCD = true;/* 对象是否已经创建了拖动 */
    this.__cursor = this._obj.style.cursor;
    if (cursor)
        this._obj.style.cursor = cursor;
    this.__onmousedown = this._obj.onmousedown;
    this._obj.onmousedown = this.start();
    if (onStart)
        this.onDragStart = onStart;
    else
        this.onDragStart = new Function();
    if (onDrag)
        this.onDrag = onDrag;
    else
        this.onDrag = new Function();
    if(onEnd)
        this.onDragEnd = onEnd;
    else
        this.onDragEnd = new Function();
    if(onScroll)
        this.onScroll = onScroll;
    else
        this.onScroll = new Function();
    if (isNaN(isDrag) || typeof(isDrag) != 'boolean')
        this.isDrag = true;
    else
        this.isDrag = isDrag;
    if (this.isDrag == true)
        this._obj.style.position = 'absolute';
    if (isNaN(isDisposed) || typeof(isDisposed) != 'boolean')
        this._isDisposed = true;
    else
        this._isDisposed = isDisposed;
    this.lastMouseX = null;
    this.lastMouseY = null;
    this.interval = null;
}
clsDrag.prototype._fixE = function(evt) {
    if (typeof evt == "undefined") {
        evt = window.event;
    }
    if (typeof evt.layerX == "undefined") {
        evt.layerX = evt.offsetX;
    }
    if (typeof evt.layerY == "undefined") {
        evt.layerY = evt.offsetY;
    }
    if (typeof evt.which == "undefined") {
        evt.which = evt.button;
    }
    return evt;
};
clsDrag.prototype.start = function() {
    var a = this;
    return function(evt) {
        evt = a._fixE(evt);
        if (evt.which != 1) {
            return true;
        }
        //更改标题栏top left注释掉
//        if (isNaN(parseInt(a._obj.style.left))) {
//            a._obj.style.left = (evt.clientX - a._obj.offsetWidth / 2) + 'px';
//        }
//        if (isNaN(parseInt(a._obj.style.top))) {
//            a._obj.style.top = (evt.clientY - a._obj.offsetHeight / 2) + 'px';
//        }
        a.onDragStart(evt);
        a.lastMouseX = evt.clientX;
        a.lastMouseY = evt.clientY;
        a.interval = window.setInterval(a.scroll(), 10);
        document.onmouseup = a.end();
        document.onmousemove = a.drag();
        return false;
    };
};
clsDrag.prototype.drag = function() {
    var a = this;
    return function (evt) {
        evt = a._fixE(evt);
        if (evt.which == 0) {
            return (a.end())();
        }
        var element = a._obj;
        var _clientX = evt.clientX;
        var _clientY = evt.clientY;
        if (a.lastMouseX == _clientX && a.lastMouseY == _clientY) {
            return false;
        }
        var movedX, movedY;
        movedX = _clientX - a.lastMouseX;
        movedY = _clientY - a.lastMouseY;
        if (a.isDrag == true)
        {
            var l,t;
            l = parseInt(element.style.left) + movedX;
            t = parseInt(element.style.top) + movedY;
            element.style.left = l + "px";
            element.style.top = t + "px";
        }
        a.lastMouseX = _clientX;
        a.lastMouseY = _clientY;
        a.onDrag(evt,_clientX,_clientY, movedX, movedY,a.b_obj,a.handler,a.self);
        return false;
    };
};
clsDrag.prototype.dispose = function() {
    this._obj.onmousedown = this.__onmousedown;
    this._obj.style.cursor = this.__cursor;
    this.__onmousedown = null;
    this.__cursor = null;
    this.__position = null;
    this.onDragStart = new Function();
    this.onDragEnd = new Function();
    this.onDrag = new Function();
    this._obj.__isCD = false;
    this._obj = null;
};
clsDrag.prototype.end = function() {
    var a = this;
    return function (evt) {
        var _onDragEndFuc = a.onDragEnd;
        evt = a._fixE(evt);
        document.onmousemove = null;
        document.onmouseup = null;
        window.clearInterval(a.interval);
        a.interval = null;
        if (a._isDisposed == true)
            a.dispose();
        return _onDragEndFuc(evt);
    };
};
clsDrag.prototype.scroll = function() {
    var a = this;
    return function() {
        var obj = a._obj;
        if (obj != null)
        {
            var b;
            if(document.body.scrollHeight > document.documentElement.clientHeight)
                b = document.body.scrollHeight;
            else
                b = document.documentElement.clientHeight;
            var c = a.clientHeight();
            var d = a.pageOffset();
            var e = d[1];
            var f = 4;
            var g = 0.05 * c;
            var h = e;
            var i = obj.offsetTop;
            if(a.lastMouseY <= g) {
                h = e - f;
            } else if(a.lastMouseY >= c - g) {
                h = Math.min(b - c,e + f);
            }
            var j = h - e;
            if(j != 0) {
                window.scrollBy(0, j);
                i += j;
                if (i > 0)
                    obj.style.top = "0px"; //原始为obj.style.top = i + "px";
            }
        }
        a.onScroll();
    };
};
clsDrag.prototype.clientHeight = function() {
    var a;
    if(window.innerHeight) {
        a = window.innerHeight;
    } else if(document.documentElement && document.documentElement.clientHeight) {
        a = document.documentElement.clientHeight;
    } else {
        a = document.body.offsetHeight;
    }
    if( a < document.body.clientHeight) {
        return a;
    }
    return document.body.clientHeight;
};
clsDrag.prototype.pageOffset = function() {
    var a = 0;
    var b = 0;
    if(typeof window.pageYOffset == "number") {
        a = window.pageXOffset;
        b = window.pageYOffset;
    } else if(document.body && (document.body.scrollLeft || document.body.scrollTop)) {
        a = document.body.scrollLeft;
        b = document.body.scrollTop;
    } else if(document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
        a = document.documentElement.scrollLeft;
        b = document.documentElement.scrollTop;
    }
    return [a,b];
};


