﻿Type.registerNamespace("QuickPhotoClient");

QuickPhotoClient.DragDropList = function(e) {
    this._callback = null;
    this._newIndex = -1;
     this._childList = new Array();
    QuickPhotoClient.DragDropList.initializeBase(this, [e]);    
}

QuickPhotoClient.DragDropList.prototype = {   
    set_Callback :function(v){this._callback=v;}, 
    get_callback:function(){return this._callback;},
    dispose : function() {
        QuickPhotoClient.DragDropList.callBaseMethod(this, 'dispose');
    },
    
    initialize : function() {
        QuickPhotoClient.DragDropList.callBaseMethod(this, 'initialize');        
        this._saveChildOrder();
    },
    
     _getSavedChildIndex : function(childId) {
        if (this._childList && childId) {            
            for (var i = 0; i < this._childList.length; i++) {
                if (childId == this._childList[i]) {            
                    return i;
                }
            }
        }
        return -1;
    },
    
    _saveChildOrder : function() {
        var e = this.get_element();
        if (!e) return;
        var children = e.childNodes;
        this._childList = [];
        var childCount = 0;
       
        for (var i = 0; i < children.length; i++) {
            // note Safari is returning all children, not just direct ones
            //
            if (children[i] && children[i].parentNode === e ) {
                this._childList[childCount++] = children[i].id;                
            }
        }   
    },
    
     findChild : function(parent, childId) {
        
        // just walk through the list of children looking for the child
        //
        var childIndex = 0;
        var nodes = parent.childNodes;
        for (var i = 0; i < nodes.length; i++) {
            var item = nodes[i];
            // nodeName check is for Safari which enumerates LI contents as well
            if ((item != null) && (item.nodeName == "LI")) {
                if (item.id == childId) {
                    return childIndex;
                }
                childIndex++;
            }
        }
        return -1;
    },
    
    
   getItem : function(index) {
        if (!this._childList) {
            this._saveChildOrder();
        }
    
        return this._childList[index];
    },
    
    _findPotentialNextSibling:function(dragVisual) {
        var dragVisualRect = Sys.UI.DomElement.getBounds(dragVisual);
        var nodeRect;
        var i = 0;
        for (var node = this.get_element().firstChild; node !== null; node = node.nextSibling) {
            i++;

            nodeRect = Sys.UI.DomElement.getBounds(node);
            if ( dragVisualRect.x >= nodeRect.x && dragVisualRect.x <= (nodeRect.x+nodeRect.width)  && dragVisualRect.y >= nodeRect.y && dragVisualRect.y <= (nodeRect.y+nodeRect.height) ) {
               this._newIndex = i-1;
               return node;
            }
        }
       
        return null;
    },
            
    onDragEnd:function(cancelled) {
        if (this._floatContainerInstance) {
            if (this._dragMode === Sys.Preview.UI.DragMode.Copy) {
                this._floatContainerInstance.removeChild(this._dragVisual);
            }
            else {
                    this._dragVisual.style.opacity = "0.999";
                    this._dragVisual.style.filter = "";
                this._dragVisual.style.zIndex = this._originalZIndex ? this._originalZIndex : 0;

                if (cancelled) {
                    this._dragVisual.parentNode.removeChild(this._dragVisual);
                    if (this._originalNextSibling != null) {
                        this._originalParent.insertBefore(this._dragVisual, this._originalNextSibling);
                    }
                    else {
                        this._originalParent.appendChild(this._dragVisual);
                    }
                }
                else {
                    if (this._dragVisual.parentNode === this._floatContainerInstance) {
                        this._dragVisual.parentNode.removeChild(this._dragVisual);
                    }
                }
            }
  
            document.body.removeChild(this._floatContainerInstance);
        }
        else {
            this._dragVisual.parentNode.removeChild(this._dragVisual);
        }

        if (!cancelled && this._data && this._dragMode === Sys.Preview.UI.DragMode.Move) {
            var data = this.getDragData(this._currentContext);
            if (this._data && data) {
                if (Sys.Preview.Data.IData.isImplementedBy(this._data)) {
                    this._data.remove(data);
                }
                else if (this._data instanceof Array) {
                    Array.remove(this._data, data);
                }
            }
        }

        this._isDragging = false;
        this._validate();
    },
    
   _setDropCueVisible: function (visible, dragVisual) {
        if (this._dropCueTemplate) {
            if (visible) {
                if (!this._dropCueTemplateInstance) {
                    var documentContext = document.createDocumentFragment();
                    this._dropCueTemplateInstance = this._dropCueTemplate.createInstance(documentContext).instanceElement;
                }

                var potentialNextSibling = this._findPotentialNextSibling(dragVisual);

                if (!Sys.Preview.UI.DragDropManager._getInstance().hasParent(this._dropCueTemplateInstance)) {
                    if (potentialNextSibling) {
                        this.get_element().insertBefore(this._dropCueTemplateInstance, potentialNextSibling);
                    }
                    else {
                        this.get_element().appendChild(this._dropCueTemplateInstance);
                    }
                    
                }
                else {
                   if (Sys.Preview.UI.DragDropManager._getInstance().getNextSibling(this._dropCueTemplateInstance) !== potentialNextSibling) {
                         if(potentialNextSibling!=this._dropCueTemplateInstance)
                        this.get_element().removeChild(this._dropCueTemplateInstance);
                        if (potentialNextSibling) {
                            if(potentialNextSibling!=this._dropCueTemplateInstance)
                            this.get_element().insertBefore(this._dropCueTemplateInstance, potentialNextSibling);
                        }
                        else {
                            this.get_element().appendChild(this._dropCueTemplateInstance);
                        }
                    }
                }
            }
            else {
                if (this._dropCueTemplateInstance && Sys.Preview.UI.DragDropManager._getInstance().hasParent(this._dropCueTemplateInstance)) {
                    this.get_element().removeChild(this._dropCueTemplateInstance);
                }
            }
        }
    },
    
    drop : function(dragMode, dataType, data) {   
        if (dataType === "HTML" && dragMode === Sys.Preview.UI.DragMode.Move) {
                        dragVisual = data;

            var potentialNextSibling =  Sys.Preview.UI.DragDropManager._getInstance().getNextSibling(this._dropCueTemplateInstance);
            this._setDropCueVisible(false, dragVisual);
            dragVisual.parentNode.removeChild(dragVisual);

            if (potentialNextSibling) {
                this.get_element().insertBefore(dragVisual, potentialNextSibling);

                 var childId = data.id;
                 if (!this._callback || !childId) return;
    
                 var oldIndex = this._getSavedChildIndex(childId);
                 if (this._newIndex != -1 && this._newIndex != oldIndex) {
                        this._saveChildOrder();                             
                        eval(this._callback +"('"+childId+"',"+ this._newIndex+","+ oldIndex+")");              
                 }      
                
            }
            else {
               this.get_element().insertBefore(dragVisual, this._originalNextSibling);
            }
        }
        else {
            this._setDropCueVisible(false);
        }

        if (this._data && data) {
            var newRow = data;
            if (Sys.Preview.Data.DataRow.isInstanceOfType(data) && Sys.Preview.Data.DataTable.isInstanceOfType(this._data)) {
                var src = data.get_table();
                if (src) {
                    newRow = this._data.createRow(data);
                }
            }
            if (Sys.Preview.Data.IData.isImplementedBy(this._data)) {
                this._data.add(newRow);
            }
            else if (this._data instanceof Array) {
                Array.add(this._data, newRow);
            }
        }
    }   
}
QuickPhotoClient.DragDropList.registerClass('QuickPhotoClient.DragDropList', Sys.Preview.UI.DragDropList);

if(typeof(Sys) != "undefined") {Sys.Application.notifyScriptLoaded();}