/*
	sky.drag.js
	2007-04-11 ~ 2007-05-08 

	Seo, Jaehan <daddyofsky@gmail.com>

    @dependence sky.base.js
*/

	var Drag = Class({
		init : function(obj, options) {
			this.object = Element.extend(obj);
			this.options = Class.extend({
				handler : [],
				area : null,
				onStart : null,
				onMove : null,
				onEnd : null,
				zIndex : 7777
			}, options);

			this.onmousedown = this.onMouseDown.bindForEvent(this);
			this.onmouseup = this.onMouseUp.bindForEvent(this);
			this.onmousemove = this.onMouseMove.bindForEvent(this);

			var opt = this.options;
			if (!opt.handler.length) {
				$l(this.object, 'mousedown', this.onmousedown);
			} else {
				for(var i=0; i<opt.handler.length; i++) {
					$l($(opt.handler[i]), 'mousedown', this.onmousedown);
				}
			}

            if (opt.area) {
                if (opt.area instanceof Array && opt.area.length == 4) {
                    this.area = {
                        left   : opt.area[0],
                        top    : opt.area[1],
                        right  : opt.area[2],
                        bottom : opt.area[3]
                    }
                } else if (typeof opt.area.left == 'number' && typeof opt.area.top == 'number' && typeof opt.area.right == 'number' && typeof opt.area.bottom == 'number') {
                    this.area = opt.area;
                } else {
                    var obj = Element.extend(opt.area);
                    if (obj) {
                        this.area = obj.getRect();
                    }
                }
            }
		},
		onMouseDown : function(evt) {
            Event.stop(evt);

            $l(document, 'mousemove', this.onmousemove);
            $l(document, 'mouseup', this.onmouseup);

            var opt = this.options;
            var epos = Event.pos(evt);
            var opos = this.object.pos();
            this.gapX = epos.x - opos.x;
            this.gapY = epos.y - opos.y;

            this.zIndex = this.object.getStyle('zIndex');

			// callback
            var ret;
			if (typeof this.options.onStart == 'function') {
				ret = this.options.onStart.call(this, evt);
            }
            if (ret !== false) {
	            this.object.setStyle('zIndex', this.options.zIndex);
			}
		},
		onMouseMove : function(evt) {
            Event.stop(evt);

            var epos = Event.pos(evt);
			var newX = epos.x - this.gapX;
			var newY = epos.y - this.gapY;

			if (this.area) {
				var size = this.object.getSize();
				if (newX > this.area.right - size.width) newX = this.area.right - size.width;
				if (newY > this.area.bottom - size.height) newY = this.area.bottom - size.height;
				if (newX < this.area.left) newX = this.area.left;
				if (newY < this.area.top) newY = this.area.top;
			}
			this.newX = newX;
			this.newY = newY;

			// callback
            var ret;
			if (typeof this.options.onMove == 'function') {
				ret = this.options.onMove.call(this, evt);
			}
            if (ret !== false) {
                this.object.setPosition(this.newX, this.newY);
            }            
		},
		onMouseUp : function(evt) {
            Event.stop(evt);

			$lx(document, 'mousemove', this.onmousemove);
            $lx(document, 'mouseup', this.onmouseup);
			this.object.setStyle('zIndex', this.zIndex);

			// callback
			if (typeof this.options.onEnd == 'function') {
				this.options.onEnd.call(this, evt);
			}
		}
	});