﻿/// <reference name="MicrosoftAjax.debug.js" />
/// <reference name="MicrosoftAjaxTimer.debug.js" />
/// <reference name="MicrosoftAjaxWebForms.debug.js" />
/// <reference name="AjaxControlToolkit.ExtenderBase.BaseScripts.js" assembly="AjaxControlToolkit" />


Type.registerNamespace('Infocaster.Web.Ajax');

Infocaster.Web.Ajax.DragSourceBehavior = function(element) {
    Infocaster.Web.Ajax.DragSourceBehavior.initializeBase(this, [element]);

    // TODO : (Step 1) Add your property variables here
    var _dragHandleID;
    var _dragHandle;
    var _dragClass;
    var _dragStartLocation;
    var _mouseDownHandler;
    var _dragGroup = null;
    var _tag;
}
Infocaster.Web.Ajax.DragSourceBehavior.prototype = {
    initialize : function() {
        Infocaster.Web.Ajax.DragSourceBehavior.callBaseMethod(this, 'initialize');
        //Resolve draghandle
        var handle = $get(this.get_DragHandleID());
        if (handle == null) //Set this element
            handle = this.get_element();
        //Set the handle which will also register mouse downs
        this._mouseDownHandler = Function.createDelegate(this, this.onHandleClicked)
        this.set_DragHandle(handle);       
    },
    
    //Ripped from AjaxControlToolkit because some types are just incompatible with beeing dragged
    checkCanDrag: function(element) {
        var undraggableTagNames = ["input", "button", "select", "textarea", "label"];
        var tagName = element.tagName;
        
        if ((tagName.toLowerCase() == "a") && (element.href != null) && (element.href.length > 0)) {
            return false;
        }
        if (Array.indexOf(undraggableTagNames, tagName.toLowerCase()) > -1) {
            return false;
        }
        return true;
    },

    dispose : function() {
        Infocaster.Web.Ajax.DragSourceBehavior.callBaseMethod(this, 'dispose');
    },

    get_DragHandleID : function() {
        return this._dragHandleID;
    },
    set_DragHandleID : function(value) {
        this._dragHandleID = value;
    },
    get_DragHandle : function() {
        return this._dragHandle;
    },
    set_Tag: function(value) {
        this._tag = value;
    },
    get_Tag : function() {
        return this._tag;
    },
    set_DragHandle : function(value) {
        if (this._dragHandle != null)
            $removeHandler(this._dragHandle, "mousedown", this._mouseDownHandler);
        this._dragHandle = value;
        $addHandler(value, "mousedown", this._mouseDownHandler);
    },
    get_DragClass : function() {
        return this._dragClass;
    },
    set_DragClass : function(value) {
        this._dragClass = value;
    },
    get_DropGroup: function(){
        return this._dropGroup;
    },
    set_DropGroup: function(val){
        this._dropGroup = val;
    },
    // Type get_dragDataType()
    get_dragDataType: function() { 
        //TODO: Instead of returning string, return name of this class
        return 'Infocaster.Web.Ajax.DragSourceBehavior';
    },
    // Object getDragData(Context)
    getDragData: function() { return this; },
    // DragMode get_dragMode()
    get_dragMode: function() { 
        //In this case we only want to move not, copy
        return AjaxControlToolkit.DragMode.Move;
    },
    // helps in raising events
    _raiseEvent : function( eventName, eventArgs ) {
        var handler = this.get_events().getHandler(eventName);
        if( handler ) {
            if( !eventArgs ) eventArgs = Sys.EventArgs.Empty;
            handler(this, eventArgs);
        }
    },
    // void onDragStart()
    onDragStart: function() { 
        var dragClass = this.get_DragClass();
        if (dragClass != null){
            Sys.UI.DomElement.addCssClass(this.get_element(), dragClass);
        }
        this._raiseEvent("onDragStart", new Infocaster.Web.Ajax.DragEventArgs(this));
     },
    // void onDrag()
    onDrag: function() { 
    },
    // void onDragEnd(Cancelled)
    onDragEnd: function(canceled) {
        var dragClass = this.get_DragClass();
        if (dragClass != null){
            Sys.UI.DomElement.removeCssClass(this.get_element(), dragClass);
        }
        if (!canceled) {
            var handler = this.get_events().getHandler('move');
            if(handler) {
                var cancelArgs = new Sys.CancelEventArgs();
                handler(this, cancelArgs);
                canceled = cancelArgs.get_cancel();
            }            
        }
        var el = this.get_element();
        el.style.width = el.style.height = el.style.left = el.style.top = "";
        el.style.zIndex = el.originalZIndex;
        this._raiseEvent("onDragEnd", new Infocaster.Web.Ajax.DragEventArgs(this)); 
    },
    startDragDrop: function(dragVisual) {
        AjaxControlToolkit.DragDropManager.startDragDrop(this, dragVisual, null);
    },
    //Called when the handle got clicked
    onHandleClicked: function(args){
        window._event = args;
        var el = this.get_element();
        
        //Only if this is draggable
        if (this.checkCanDrag(args.target)){                             
            // Get the location before making the element absolute
            var _location = Sys.UI.DomElement.getLocation(el);
            // Make the element absolute 
            el.style.width = el.offsetWidth + "px";
            el.style.height = el.offsetHeight + "px";    
            
            Sys.UI.DomElement.setLocation(el, _location.x, _location.y);        
            this._dragStartLocation = Sys.UI.DomElement.getLocation(el);
            
            args.preventDefault();  
            this.startDragDrop(el);
            
            // Hack for restoring position to static
            el.originalPosition = "static";
            el.originalZIndex = el.style.zIndex;
            el.style.zIndex = "9999";
        }
    }
}
Infocaster.Web.Ajax.DragSourceBehavior.registerClass('Infocaster.Web.Ajax.DragSourceBehavior', 
AjaxControlToolkit.BehaviorBase,
AjaxControlToolkit.IDragSource);

//Event args used by the DragSourceBehaviour
Infocaster.Web.Ajax.DragEventArgs = function(item) {
    Infocaster.Web.Ajax.DragEventArgs.initializeBase(this);
    //Assign the item    
    this._item = item;
}

Infocaster.Web.Ajax.DragEventArgs.prototype = {
    get_Item: function(){
        return this._item;
    }
}
Infocaster.Web.Ajax.DragEventArgs.registerClass("Infocaster.Web.Ajax.DragEventArgs", Sys.EventArgs);
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();