Type.registerNamespace('Skynetix.UI');

Skynetix.UI.ItemDragSourceBehavior = function(element, _content, _parent, _pageId, _widgetId, _nameOnPage)
{
    var s = '';
    Skynetix.UI.ItemDragSourceBehavior.initializeBase(this, [element]);
    this._mouseDownHandler = Function.createDelegate(this,
        this.mouseDownHandler);        
    this._data = this;
    this._visual = null;
    this._content = _content;
    this._parentHolder = _parent;
    this._pageId = _pageId;
    this._widgetId = _widgetId;
    this._nameOnPage = _nameOnPage;
    
}

Skynetix.UI.ItemDragSourceBehavior.prototype =
{
    // IDragSource methods
    get_dragDataType: function()
    {
        return 'DragDropItem';
    },

    getDragData: function(context)
    {
        return this._data;
    },
    
    get_dragMode: function()
    {
        return Sys.Preview.UI.DragMode.Move;
    },

    onDragStart: function() {},

    onDrag: function() {},

    onDragEnd: function(canceled)
    {
        if (this._visual && canceled)
        {
            $get(this.getParent()).removeChild(this._visual);
        }
    },
    
    // Other methods
    initialize: function()
    {
        Skynetix.UI.ItemDragSourceBehavior.callBaseMethod(this,
            'initialize');
        $addHandler(this.get_element(), 'mousedown',
            this._mouseDownHandler)
    },
    
    getContent: function()
    {
        return this._content;
    },
    
    getParent: function()
    {
        return this._parentHolder;
    },
    
    getVisual: function()
    {
        return this._visual;
    },
    
    setContent: function(new_content)
    {
        this._content = new_content;
    },
    
    setParent: function(new_parent)
    {
        this._parentHolder = new_parent;
    },
    
    getPageId: function()
    {
        return this._pageId;
    },
    
    getWidgetId: function()
    {
        return this._widgetId;
    },
    
    setWidgetId: function(new_widget)
    {
        this._widgetId = new_widget;
    },
    
    getNameOnPage: function()
    {
        return this._nameOnPage;
    },
    
    setNameOnPage: function(new_nameOnPage)
    {
        this._nameOnPage = new_nameOnPage;
    },   
    

    mouseDownHandler: function(ev)
    {
        
        //alert($get(this.getContent()).offsetWidth);
        window._event = ev; // Needed internally by _DragDropManager        
        this._visual = $get(this.getContent()).cloneNode(true);
        this._visual.style.width = $get(this.getContent()).offsetWidth;
        this._visual.style.opacity = '0.4';
        this._visual.style.filter =
          'progid:DXImageTransform.Microsoft.BasicImage(opacity=0.4)';
        this._visual.style.zIndex = 99999;
        var parentId = this._parentHolder;
        var parentCtl = $get(parentId);
        parentCtl.appendChild(this._visual);
        var location =
            Sys.UI.DomElement.getLocation(this.get_element());
        Sys.UI.DomElement.setLocation(this._visual, location.x,
            location.y);

        Sys.Preview.UI.DragDropManager.startDragDrop(this,
            this._visual, null);
        
    },

    dispose: function()
    {
        if (this._mouseDownHandler)
            $removeHandler(this.get_element(), 'mousedown',
                this._mouseDownHandler);
        this._mouseDownHandler = null;
        Skynetix.UI.ItemDragSourceBehavior.callBaseMethod(this,
            'dispose');
    }
}

Skynetix.UI.ItemDragSourceBehavior.registerClass
    ('Skynetix.UI.ItemDragSourceBehavior', Sys.UI.Behavior,
    Sys.Preview.UI.IDragSource);


///////////////////////////////////////////////////////////////////////
// ItemDropTargetBehavior class

Skynetix.UI.ItemDropTargetBehavior = function(element, content, _parent, _pageId, _widgetId, _nameOnPage, panelsList)
{
    Skynetix.UI.ItemDropTargetBehavior.initializeBase(this, [element]);
    this._content = content;    
    this._parentHolder = _parent;
    this._pageId = _pageId;
    this._widgetId = _widgetId;
    this._nameOnPage = _nameOnPage;
    this._panelsList = panelsList;
}
    
Skynetix.UI.ItemDropTargetBehavior.prototype =
{
    // IDropTarget methods
    get_dropTargetElement: function()
    {
        return this.get_element();
    },

    canDrop: function(dragMode, dataType, data)
    {
        return (dataType == 'DragDropItem' && data);
    },

    drop: function(dragMode, dataType, data)
    {
        if (dataType == 'DragDropItem' && data)
        {  
            $get(data.getParent()).removeChild(data.getVisual());
            if(data.get_element().id != this.get_element().id)
            {      
                var drag = $get(data.getContent());
                
                var target = $get(this.getContent());
                var targetParent = $get(this.getParent());
                
                var dragParent = $get(data.getParent());
                
                var droppedItem = new Object();
                droppedItem.TitleBlockId = data.get_element().id;
                droppedItem.ContentBlockId = data.getContent();
                droppedItem.ParentBlockId = data.getParent();
                droppedItem.WidgetId = data.getWidgetId();
                droppedItem.NameOnPage = data.getNameOnPage();
                
                var receivingItem = new Object();
                receivingItem.TitleBlockId = this.get_element().id;
                receivingItem.ContentBlockId = this.getContent();
                receivingItem.ParentBlockId = this.getParent();
                receivingItem.WidgetId = this.getWidgetId();
                receivingItem.NameOnPage = this.getNameOnPage();
                
                this.get_element().parentNode.style.border = "dashed 0px #000000";
                
                targetParent.appendChild(drag);            
                dragParent.appendChild(target);                
                
                this.updateCookies(droppedItem, receivingItem, data.getPageId());
                  
                __doPostBack(this.get_element().id, '');             
                __doPostBack(data.get_element().id, '');
            }                            
        }
    },
    
    // Adds both widgets to cookies.
    updateCookies: function(droppedItem, receivingItem, pageId)
    {        
        var dItem = new Object();     
        dItem.Id = droppedItem.WidgetId;
        dItem.NameOnPage = receivingItem.NameOnPage;
        var cookie = this.getWidgetCookie();
        cookie = this.setCookie(dItem, pageId, cookie);
        
        var tItem = new Object();  
        tItem.Id = receivingItem.WidgetId;
        tItem.NameOnPage = droppedItem.NameOnPage;
        cookie = this.setCookie(tItem, pageId, cookie);
        
        this.saveCookie(cookie);
    },
    
    // Sets a cookie.
    setCookie: function(widget, pageId, widgetCookie)
    {        
        var key = widget.NameOnPage + "_" + pageId;
        widgetCookie = this.deleteCookies(widget.Id, pageId, widgetCookie);          
        if(widgetCookie.length > 0)
        {
            widgetCookie += "&";
        }
        widgetCookie += key + "=" + widget.Id;
        //widgetCookie = 'widgets=' + widgetCookie;
        return widgetCookie;
        //this.saveCookie(widgetCookie);
    },
    
    // Updates or inserts cookie.
    saveCookie: function(widgetCookie)
    {      
        var expiration_date = new Date("January 1, 3000");
        expiration_date = expiration_date.toGMTString();        
        document.cookie = "widgets=" + widgetCookie + ";expires=" + expiration_date;        
    },
    
    // Gets the widget cookie.
    getWidgetCookie: function()
    {
        var cookies = document.cookie;
        if(cookies == '' || cookies == null)
        {
            return '';
        }
        var allCookies = cookies.split(';'); 
        for(var i = 0; i < allCookies.length; i++){
            var item = allCookies[i];
            if(item.indexOf('widgets=') != -1)
            {
                return item.replace('widgets=', '');                
            }         
        }
        return '';
    },
    
    // Deletes a cookie.
    deleteCookies: function(id, pageId, actualCookie)
    {        
//        //var dataToDelete;        
//        var actualCookie = this.getWidgetCookie();         
        if(actualCookie == '' || actualCookie == null)
        {
            return '';
        }
        var cookiesArray = actualCookie.split('&');
        for(var i = 0; i < cookiesArray.length; i++){
            if(cookiesArray[i].split('=')[0].split('_')[1] == pageId)
            {
                if(cookiesArray[i].split('=')[1] == id)
                {
                    var itemToReplace = cookiesArray[i];
                    if(i > 0){
                        itemToReplace = '&' + itemToReplace;
                    }
                    else if(i == 0 && cookiesArray.length > 1)
                    {
                        itemToReplace = itemToReplace + '&';
                    }
                    var widgetCookie = actualCookie.replace(itemToReplace, '');
                    return widgetCookie;
                    //widgetCookie = 'widgets=' + widgetCookie;
                    //this.saveCookie(widgetCookie);                    
                }
            }            
        } 
        return actualCookie;       
    },
    
    onDragEnterTarget: function(dragMode, dataType, data)
    {
        // Highlight the drop zone by changing its background
        // color to light gray
        if (dataType == 'DragDropItem' && data)
        {            
            this.get_element().parentNode.style.border = "dashed 3px #000000";            
        }
    },
    
    onDragLeaveTarget: function(dragMode, dataType, data)
    {
        // Unhighlight the drop zone by restoring its original
        // background color
        if (dataType == 'DragDropItem' && data)
        {
            this.get_element().parentNode.style.border = "dashed 0px #000000";            
        }
    },

    onDragInTarget: function(dragMode, dataType, data) {},
    
    // Other methods
    initialize: function()
    {
        Skynetix.UI.ItemDropTargetBehavior.callBaseMethod(this,
            'initialize');
        Sys.Preview.UI.DragDropManager.registerDropTarget(this);
    },
    
    getParent: function()
    {
        return this._parentHolder;
    },
    
    getContent: function()
    {
        return this._content;
    },
    
    setContent: function(new_content)
    {
        this._content = new_content;
    },
    
    setParent: function(new_parent)
    {
        this._parentHolder = new_parent;
    },
    
    getPageId: function()
    {
        return this._pageId;
    },
    
    getWidgetId: function()
    {
        return this._widgetId;
    },
    
    setWidgetId: function(new_widget)
    {
        this._widgetId = new_widget;
    },
    
    getNameOnPage: function()
    {
        return this._nameOnPage;
    },
    
    setNameOnPage: function(new_nameOnPage)
    {
        this._nameOnPage = new_nameOnPage;
    },
    
    dispose: function()
    {
        Sys.Preview.UI.DragDropManager.unregisterDropTarget(this);
        Skynetix.UI.ItemDropTargetBehavior.callBaseMethod(this,
            'dispose');
    }
}

Skynetix.UI.ItemDropTargetBehavior.registerClass
    ('Skynetix.UI.ItemDropTargetBehavior', Sys.UI.Behavior,
    Sys.Preview.UI.IDropTarget);

///////////////////////////////////////////////////////////////////////
// Script registration

Sys.Application.notifyScriptLoaded();

