var __DDrop = DefineClass({

    init: function(type, direction) {
        if (type) {
            this.direction = direction;
            this.Ghost = ce('li', null, { id: 'GhostItem' });
            this.list = true;
        }
        this.Containers = {};
        this.Items = {};
        this.OnStopDrag = {};
        this.OnStartDrag = {};
    },

    AddItem: function(id, header, onstopdrag, onstartdrag) {
        var oThis = this;
        var item = $(id);
        this.Items[id] = item;
        if (header)
            $(header).onmousedown = function(e) { return(oThis.StartMove(e, this.parentNode)) }; else
                item.onmousedown = function(e) { return(oThis.StartMove(e, this)) }
        if (onstopdrag) this.OnStopDrag[id] = onstopdrag;
        if (onstartdrag) this.OnStartDrag[id] = onstartdrag;

        },

    AddContainer: function(id) {
        this.Containers[id] = $(id);
    },

    StartMove: function(e, item) {
        e = e || window.event;
        var target = Event.element(e);
        if (target.tagName == 'IMG') return;
        var oThis = this;
        this.CurrentItem = item;
        this.Reposition = true;
        document.onmousemove = function(e) { return(oThis.DoMove(e)); }
        document.onmouseup = function(e) { return(oThis.StopMove(e)); }
        var id = item.id.split('.');
        var t = getBounds1(this.CurrentItem);
        var scroll = Desktop.State[Modules[id[1]].State.cName].scrollTop || 0;
        if (Prototype.Browser.IE) scroll = 0;
        this.off = { x: Event.pointerX(e) - t.left, y: Event.pointerY(e) - t.top + scroll };
        return(false);
    },

    RedrawDragItem: function() {
        this.CurrentItem.style.left = this.DragItemCoordinates.x  - this.off.x + 'px';
        this.CurrentItem.style.top = this.DragItemCoordinates.y - this.off.y + 'px';
        this.FindContainer(this.DragItemCoordinates);
    },

    DoMove: function(e, item) {
        e = e || window.event;
        this.DragItemCoordinates = {x: Event.pointerX(e), y: Event.pointerY(e) };
        if (this.Reposition) {
            $(this.CurrentItem).setStyle({opacity: 0.8});
            if (this.OnStartDrag[this.CurrentItem.id]) this.OnStartDrag[this.CurrentItem.id]();
            var bounds = getBounds1(this.CurrentItem);
            var oThis = this;
            Position.absolutize(this.CurrentItem);
            this.scrollTop = this.CurrentItem.parentNode.parentNode.scrollTop;
            this.BackupContainer = this.CurrentItem.parentNode;
            document.body.appendChild(this.CurrentItem);
            this.Reposition = false;
            this.UpdateTimer =  setInterval(function() { oThis.RedrawDragItem(); }, 4);
            if (this.list) {
                this.CurrentItem.parentNode.insertBefore(this.Ghost, this.CurrentItem);
                this.Ghost.style.height = bounds.height + 'px';
            }
            this.CurrentItem.style.height = bounds.height + 'px';
            this.CurrentItem.style.width = bounds.width + 'px';
            this.RedrawDragItem();
        }
        return(false);
    },

    StopMove: function(e, item) {
        document.onmousemove = null;
        document.onmouseup = null;
        if (!this.Reposition) {
            if (this.Ghost && this.Ghost.parentNode) {
                this.Ghost.parentNode.insertBefore(this.CurrentItem, this.Ghost);
            } else {
                if (this.CurrentContainer) {
                    this.CurrentContainer.appendChild(this.CurrentItem);
                } else {
                    if (this.BackupContainer) this.BackupContainer.appendChild(this.CurrentItem);
                }
            }
        }

        if (this.OnStopDrag[this.CurrentItem.id]) var DragResult = this.OnStopDrag[this.CurrentItem.id](this.CurrentContainer);
        if (this.Ghost.parentNode) this.Ghost.parentNode.removeChild(this.Ghost);



            $(this.CurrentItem).setStyle({opacity: 1});
            this.CurrentItem.style.position = '';
            this.CurrentItem.style.height = '';
            this.CurrentItem.style.width = '';
            delete this.CurrentContainer;
            delete this.BackupContainer;
            delete this.DragItemCoordinates;
            delete this.offset;
            clearInterval(this.UpdateTimer);


            if (Desktop.SelectedTabID) {
                //echo(Desktop.SelectedTabID);
                /*var ul = Desktop.SelectedTabID.split('.');
                var ul = $('Content.'+ul[0]+'.'+ul[2]);
                this.CurrentItem.parentNode.removeChild(this.CurrentItem);
                ul.appendChild(this.CurrentItem);
                Desktop.SetActiveByItem($(Desktop.SelectedTabID));*/
            }


            delete this.CurrentItem;

        this.Reposition = true;
        return(false);
    },

    FindContainer: function(Cursor) {
        delete this.CurrentContainer;
        for (var i in this.Containers) {
            var Container = getBounds1(this.Containers[i])
            if (Cursor.x>Container.left && Cursor.x<Container.left+Container.width && Cursor.y>Container.top && Cursor.y<Container.top+Container.height) {
                this.CurrentContainer = this.Containers[i];
            }
       }

       if (this.list) {
            for (var i in this.Items) {
                if (this.CurrentItem.id != this.Items[i].id) {
                    //var Container = getBounds1(this.Items[i])

                    var Container = Position.page(this.Items[i]);
                    var Container = { left: Container[0], top: Container[1], width: this.Items[i].getWidth(), height: this.Items[i].getHeight() }



                    if (Cursor.x>Container.left && Cursor.x<Container.left+Container.width && Cursor.y>Container.top && Cursor.y<Container.top+Container.height) {
                        if (this.direction == 'vertical') {
                            var SubContainer = getBounds1(this.Items[i]);
                            if (Cursor.y-SubContainer.top>Math.floor(SubContainer.height/2))
                                var target = this.Items[i].nextSibling; else
                                    var target = this.Items[i];
                            } else {
                                var SubContainer = getBounds1(this.Items[i]);
                                if (Cursor.x-SubContainer.left>Math.floor(SubContainer.width/2))
                                    var target = this.Items[i].nextSibling; else
                                        var target = this.Items[i];
                            }


                            if (!this.CurrentSubContainer || this.CurrentSubContainer.id != this.Items[i]) {
                                this.Items[i].parentNode.insertBefore(this.Ghost, target);
                            }
                            this.CurrentSubContainer = this.Items[i];
                    }
                }
            }

            if (!target && this.CurrentContainer && this.Ghost.parentNode !=this.CurrentContainer) {
                this.CurrentContainer.appendChild(this.Ghost);

            }



        }
    }

});
