
//Dragable
Dragable = function(config){
    //Default Config
    this.config = {
        id: 'id',
        dragId: 'dragId',
        eventId: 'eventId'
    };
    Object.extend(this.config, config || {});

    this.init();
}

Dragable.prototype = {
    init: function()
    {
        this._startX = 0;
        this._startY = 0;
        this._offsetX = 0;
        this._offsetY = 0;
        this._oldZIndex = 0;

        this.id = this.config.id;
        this.dragId = this.config.dragId;
        this.eventId = this.config.eventId;
        
        this.dragControl = getControlById(this.config.dragId);
        this.eventControl = getControlById(this.config.eventId);

        var _this = this;
        this.eventControl.onmousedown = function(e)
        {
            _this.onMouseDown(e, _this);
        }
        this.eventControl.onmouseup = function(e)
        {
            _this.onMouseUp(e, _this);
        }
        
        dragManager.addItem(this);
    },
    onMouseDown: function(e, scope)
    {
         if (e == null) 
            e = window.event; 
    	
        if ((e.button == 1 && window.event != null || e.button == 0))
        {
            scope._startX = e.clientX;
            scope._startY = e.clientY;
    		
            scope._offsetX = ExtractNumber(scope.dragControl.style.left);
            scope._offsetY = ExtractNumber(scope.dragControl.style.top);
    		
            scope._oldZIndex = scope.dragControl.style.zIndex;
            scope.dragControl.style.zIndex = 10000;
            
            document.onmousemove = function(e)
            {
                scope.onMouseMove(e, scope);
            }
            document.body.focus();
            document.onselectstart = function () { return false; };
    		
            return false;
        }
    },
    onMouseMove: function(e, scope)
    {
        if (e == null) 
            var e = window.event; 

        scope.dragControl.style.left = (scope._offsetX + e.clientX - scope._startX) + 'px';
        scope.dragControl.style.top = (scope._offsetY + e.clientY - scope._startY) + 'px';
    },
    onMouseUp: function(e, scope)
    {
        if (scope.dragControl != null)
        {
            scope.dragControl.style.zIndex = scope._oldZIndex;

            document.onmousemove = null;
            document.onselectstart = null;
        }
    }
}

//Drag Manager
DragManager = function(){
    var list = [];

    return {
        addItem: function(item)
        {
            list.push(item);
        },
        getItem: function(id)
        {
            for (var i = 0; i < list.length; i++)
            {
                if (list[i].id == id) return list[i];
            }
            return null;        
        },
        removeItem: function(item)
        {
            if (typeof item == 'string') item = this.getItem(item);
            
            for (var i = 0; i < list.length; i++)
            {
                if (list[i].id == t.id)
                {
                    list.splice(i, 1);
                }
            }
        },
        getAll: function()
        {
            return list;
        },
        getDragItem: function(eventId)
        {
            for (var i = 0; i < list.length; i++)
            {
                if (list[i].eventId == eventId) return getControlById(list[i].id);
            }
            return null;
        }
    }
}

DragManager.prototype = {
}

dragManager = DragManager();

