﻿Type.registerNamespace('Custom.UI');

///////////////////////////////////////////////////////////////////////
// MultiDragSourceBehavior class

Custom.UI.MultiDragSourceBehavior = function(element, data) {
    Custom.UI.MultiDragSourceBehavior.initializeBase(this, [element]);
    this._mouseDownHandler = Function.createDelegate(this, this.mouseDownHandler);
    this._subjectid = '';
    this._varnumber = '';
    this._title = '';

    if (data) {
        var d = data.split('|');
        if (d.length > 0) {
            this._subjectid = d[0];
        }
        if (d.length > 1) {
            this._varnumber = d[1];
        }
        if (d.length > 2) {
            this._title = d[2];
        }
    }
    this._visual = null;
}

Custom.UI.MultiDragSourceBehavior.prototype =
{
    // IDragSource methods
    get_dragDataType: function() { return 'Draggable'; },

    getDragData: function(context) { return this._subjectid + '|' + this._varnumber + '|' + 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);
            UnGlowDropAreas();
        }
    },

    // Other methods
    initialize: function() {
        Custom.UI.MultiDragSourceBehavior.callBaseMethod(this, 'initialize');
        $addHandler(this.get_element(), 'mousedown', this._mouseDownHandler)
    },

    mouseDownHandler: function(ev) {
        window._event = ev; // Needed internally by _DragDropManager

        // Make sure Variable is not selected
        if (prevVariableIds.indexOf(','+this._subjectid + '_' + this._varnumber+',') == -1) {
            HideTag('dv_var_desc_pointer');
            HideTag('dv_var_description');
            HideTag('dv_Row_Options_Cont');
            HideTag('dv_Row_Options_Disc');
            HideTag('dv_Column_Options_Disc');
            HideTag('dv_Column_Options_Cont');
            HidePercentExample();
            GlowDropAreas();
            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.MultiDragSourceBehavior.callBaseMethod(this, 'dispose');
    }
}

Custom.UI.MultiDragSourceBehavior.registerClass('Custom.UI.MultiDragSourceBehavior', Sys.UI.Behavior, Sys.Preview.UI.IDragSource);

///////////////////////////////////////////////////////////////////////
// MultiDropTargetBehavior class

Custom.UI.MultiDropTargetBehavior = function(element) {
    Custom.UI.MultiDropTargetBehavior.initializeBase(this, [element]);
    this._data = null;
}

Custom.UI.MultiDropTargetBehavior.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 > 2) {
                DropVar(this.get_element(), d[0], d[1], d[2]);
            }
        }
    },

    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 = '<p>&nbsp;</p>';
        }
    },

    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.MultiDropTargetBehavior.callBaseMethod(this, 'initialize');
        Sys.Preview.UI.DragDropManager.registerDropTarget(this);
    },

    dispose: function() {
        Sys.Preview.UI.DragDropManager.unregisterDropTarget(this);
        Custom.UI.MultiDropTargetBehavior.callBaseMethod(this, 'dispose');
    }
}

Custom.UI.MultiDropTargetBehavior.registerClass('Custom.UI.MultiDropTargetBehavior', 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();
