﻿Type.registerNamespace('Custom.UI');

///////////////////////////////////////////////////////////////////////
// DragSourceBehavior class

Custom.UI.DragSourceBehavior = function(element, data) {
    Custom.UI.DragSourceBehavior.initializeBase(this, [element]);
    this._mouseDownHandler = Function.createDelegate(this, this.mouseDownHandler);
    this._title = '';
    this._groupid = '';
    this._grouplevel = '';
    this._dataType = '';
    if (data) {
        this._dataType = 'Draggable';

        var d = data.split(':');
        if (d.length > 0) {
            this._groupid = d[0];
        }
        if (d.length > 1) {
            this._grouplevel = d[1];
        }
        if (d.length > 2) {
            this._title = d[2];
        }
    }
    this._visual = null;
}

Custom.UI.DragSourceBehavior.prototype =
{
    // IDragSource methods
    get_dragDataType: function() { return this._dataType; },

    getDragData: function(context) { return this._groupid + ':' + this._grouplevel + ':' + this._title; },

    get_dragMode: function() {
        return Sys.Preview.UI.DragMode.Copy;
    },

    onDragStart: function() { },

    onDrag: function() { },

    onDragEnd: function(canceled) {
        if (this._visual) {
            this.get_element().parentNode.removeChild(this._visual);
            UnGlowDropArea();
        }
    },

    // Other methods
    initialize: function() {
        Custom.UI.DragSourceBehavior.callBaseMethod(this, 'initialize');
        $addHandler(this.get_element(), 'mousedown', this._mouseDownHandler)
    },

    mouseDownHandler: function(ev) {
        window._event = ev; // Needed internally by _DragDropManager

        HideTag('exTable_wrapper');
        GlowDropArea();
        this._visual = this.get_element().cloneNode(true);
        this._visual.style.opacity = '0.4';
        this._visual.style.filter = 'progid:DXImageTransform.Microsoft.BasicImage(opacity=0.4)';
        this._visual.style.zIndex = 99999;
        this._visual.style.width = '300px';
        this.get_element().parentNode.appendChild(this._visual);
        var location = Sys.UI.DomElement.getLocation(this.get_element());
        Sys.UI.DomElement.setLocation(this._visual, location.x, location.y + 15);
        Sys.Preview.UI.DragDropManager.startDragDrop(this, this._visual, null);
    },

    dispose: function() {
        if (this._mouseDownHandler)
            $removeHandler(this.get_element(), 'mousedown', this._mouseDownHandler);
        this._mouseDownHandler = null;
        Custom.UI.DragSourceBehavior.callBaseMethod(this, 'dispose');
    }
}

Custom.UI.DragSourceBehavior.registerClass('Custom.UI.DragSourceBehavior', Sys.UI.Behavior, Sys.Preview.UI.IDragSource);

///////////////////////////////////////////////////////////////////////
// DropTargetBehavior class

Custom.UI.DropTargetBehavior = function(element) {
    Custom.UI.DropTargetBehavior.initializeBase(this, [element]);
    this._data = null;
}

Custom.UI.DropTargetBehavior.prototype =
{
    // IDropTarget methods
    get_dropTargetElement: function() { return this.get_element(); },

    canDrop: function(dragMode, dataType, data) { return (dataType == 'Draggable' && data); },

    drop: function(dragMode, dataType, data) {
        if (dataType == 'Draggable' && data) {
            var d = data.split(':');
            if (d.length > 1) {
                var arguments = "";
                arguments = "groupid=" + d[0];

                // Do Selection Code
                if (prevGroupId && prevGLevel) {
                    UnSelectNav(prevGroupId, prevGLevel);
                }
                SelectNav(d[0], d[1]);

                // Update Previous GroupId & Glevel
                prevGroupId = d[0];
                prevGLevel = d[1];

                // Handle Loading
                HandleLoading('groupBody', '1', '');

                // Ajax Call
                CallBack('BGP.aspx', 'getdatasets', arguments, 'GetDataSetsTriggered');
            }
            if (d.length > 2) {
                this.get_element().innerHTML = "<p>&nbsp;" + d[2] + "</p>";
            }
        }
    },

    onDragEnterTarget: function(dragMode, dataType, data) {
        // Highlight the drop zone by changing its background
        // color to light gray
        if (dataType == 'Draggable' && data) {
            this._data = this.get_element().innerHTML;
            this.get_element().innerHTML = '';
        }
    },

    onDragLeaveTarget: function(dragMode, dataType, data) {
        // Unhighlight the drop zone by restoring its original
        // background color
        if (dataType == 'Draggable' && data) {
            this.get_element().innerHTML = this._data;
        }
    },

    onDragInTarget: function(dragMode, dataType, data) { },

    // Other methods
    initialize: function() {
        Custom.UI.DropTargetBehavior.callBaseMethod(this, 'initialize');
        Sys.Preview.UI.DragDropManager.registerDropTarget(this);
    },

    dispose: function() {
        Sys.Preview.UI.DragDropManager.unregisterDropTarget(this);
        Custom.UI.DropTargetBehavior.callBaseMethod(this, 'dispose');
    }
}

Custom.UI.DropTargetBehavior.registerClass ('Custom.UI.DropTargetBehavior', Sys.UI.Behavior, Sys.Preview.UI.IDropTarget);

///////////////////////////////////////////////////////////////////////
// Script registration
// Notify ScriptManager that this is the end of the script.
if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
