﻿//register namespace BanditDndNamespace
Type.registerNamespace("BanditDndNamespace");

//Define the constructor of class DraggableProductBehavior and define some related fields
BanditDndNamespace.DraggableProductBehavior = function(element) {

    // initialize the base class
    BanditDndNamespace.DraggableProductBehavior.initializeBase(this, [element]);
    
    //the event handler for mouse down
    this._mouseDownHandler = Function.createDelegate(this, this._handleMouseDown);
    
    //the pet represented by the draggable object  
    this._pet = null;
    
    this._mealid = null;
    this._day = null;
    
    //the translucent element(s) dragged with the mouse moving
    this._visual = null;
    
    this._startLocation = null;
}

function inrange(v1, v2) {

 if (v1 > v2 - 4 && v1 < v2+4)
    return true;
 return false;
 }

//Prototypes definition
BanditDndNamespace.DraggableProductBehavior.prototype = {
    // methods within interface IDragSource
    // return the data type of the draggable object---"MealEntry"
    get_dragDataType: function() {
        return "MealEntry" + this._day;
    },
 
    //Obtain data from the draggable object
    getDragData: function(context) {
        return this._mealid;
    },
 
    //Set the mode of the draggable object
    get_dragMode: function() {
        return Sys.Preview.UI.DragMode.Copy;
    },
 
    onDragStart: function() { },
 
    onDrag: function() { },
 
    //when the dragging finishes
    onDragEnd: function(canceled) {
        if (this._visual) {
            if (this._startLocation) {
                
                var location = Sys.UI.DomElement.getLocation(this._visual);

                //alert("start=" + this._startLocation.x + "," + this._startLocation.y + "; " + location.x + "," + location.y);
                if (inrange(location.x,this._startLocation.x) && inrange(location.y, this._startLocation.y)) {
                
                    window.location = "http://mealoutlaw.com/view_meal.aspx?mealId=" + this._mealid;
                }
            }
            
            this.get_element().parentNode.removeChild(this._visual);
        }
    },
   
    // property pet
    get_pet: function(pet) {
        return this._pet;
    },
    
    set_pet: function(pet) {
        this._pet = pet;
    },
    
    // property mealid
    get_mealid: function(mealid) {
        return this._pet;
    },
    
    set_mealid: function(mealid) {
        this._mealid = mealid;
    },

    // property day
    get_day: function(day) {
        return this._pet;
    },
    
    set_day: function(day) {
        this._day = day;
    },
    
    //initialize
    initialize: function() {
        $addHandler(this.get_element(), "mousedown", this._mouseDownHandler);
    },
 
    // mousedown event handler
    _handleMouseDown: function(ev) {
        // DragDropManager requires this step
        window._event = ev; 

        //alert("day=" + this._day);

        //set the style of the translucent element(s) dragged with the mouse moving
        this._visual = this.get_element().cloneNode(true);
        //this._visual.style.opacity = "0.6";
        //this._visual.style.filter = 
        //    "progid:DXImageTransform.Microsoft.BasicImage(opacity=0.6)";
        this._visual.style.zIndex = 99999;
        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);
        this._startLocation = location;
 
        //notify the DragDropManager that the dragging has started
        Sys.Preview.UI.DragDropManager.startDragDrop(this, this._visual, null);
    },
    
    _handleMouseUp: function(ev) {
    },
     
    //destructor
    dispose: function() {
        if (this._mouseDownHandler)
            $removeHandler(this.get_element(), "mousedown", this._mouseDownHandler);
        this._mouseDownHandler = null;
        
        BanditDndNamespace.DraggableProductBehavior.callBaseMethod(this, 'dispose');
    }
}

//Register the customed behavior into the ASP.NET AJAX client side framework
BanditDndNamespace.DraggableProductBehavior.registerClass(
    "BanditDndNamespace.DraggableProductBehavior",
    Sys.UI.Behavior, 
    Sys.Preview.UI.IDragSource
);

//Define a new behavior to be attached to the HTML DOM elements describling the shopping cart
//and let it accept the draggable object--here it must implement interface Sys.Preview.UI.IDropTarget
BanditDndNamespace.CalendarDndBehavior = function(element) {

    BanditDndNamespace.CalendarDndBehavior.initializeBase(this, [element]);
    
    // pet list in the cart
    this._pets = new Object();

    this._day = null;    
    
    this._bgcolor = null;
}

function onItemMoveComplete(msg)
{
    if (msg != "success")
    {
        alert(msg);
    }
    __doPostBack('ctl00$ContentPlaceHolder1$refreshButton','');
}

//prototypes
BanditDndNamespace.CalendarDndBehavior.prototype = {
    // methods inside interface IDropTarget
    //get the target to drop ( in this case—the shopping cart)
    get_dropTargetElement: function() {
        return this.get_element();
    },
 
    // property day
    get_day: function(day) {
        return this._day;
    },
    
    set_day: function(day) {
        this._day = day;
    },

    //Judge whether the draggble element can be dropped onto the target
    canDrop: function(dragMode, dataType, data) {
        return (data && (dataType != ("MealEntry" + this._day)));
        //return false;
    },
 
    //Drop the draggable element onto the related target
    drop : function(dragMode, dataType, data) {
        if (/*dataType == "MealEntry" && */data) {

            //
            // data is a string of the meal id
            //
            
            CalendarWebService.MoveMealItem( data, this._day, onItemMoveComplete );
            var image = document.createElement('img');
            /*
            image.setAttribute('src', 'img/progress_large.gif');
            image.setAttribute('width', '37');
            image.setAttribute('height', '37');
            */
            image.setAttribute('src', 'img/progress-small-black.gif');
            image.setAttribute('width', '16');
            image.setAttribute('height', '16');
            image.setAttribute('alt', 'in progress...');

            var firstElem = this.get_element().firstChild;
            
            this.get_element().insertBefore(image, firstElem.nextSibling);
        }
    },
 
    //call this function when the draggable element is located on the drop target 
    onDragEnterTarget : function(dragMode, dataType, data) {
        if (data) {
            // set the backgroundColor of the day gray
            //this._bgcolor = this.get_element().getAttribute('style');
            //this.get_element().setAttribute('style', 'background-color: Gainsboro;');
            //alert(this.get_element().style.backgroundColor);
            this._bgcolor = this.get_element().style.backgroundColor;
            this.get_element().style.backgroundColor = "Gainsboro";
        }
    },
   
    // call this function when the draggable element is away from the drop target
    onDragLeaveTarget : function(dragMode, dataType, data) {
        if (data) {
            //set back the backgroundColor of the cart white
            //this.get_element().setAttribute('style', this._bgcolor);
            this.get_element().style.backgroundColor = this._bgcolor;
        }
    },
 
    // call this function when the draggable element is being dragged on the drop target
    onDragInTarget : function(dragMode, dataType, data) {
    },
   
    //refresh the pet list in current shopping carts
    _refreshShoppingCart: function() {
        var cartBuilder = new Sys.StringBuilder();
        for (var id in this._pets) {
            cartBuilder.append("<div>");
            cartBuilder.append(this._pets[id].Pet.Name);
            cartBuilder.append(" * ");
            cartBuilder.append(this._pets[id].Quantity);
            cartBuilder.append("</div>");
        }
        
        this.get_element().innerHTML = cartBuilder.toString();
    },
    
    //Return the object holding the required product id and quanlity
    getProductsToBeOrdered: function() {
        var productsToBeOrdered = new Object();
        
        for (var id in this._pets) {
            productsToBeOrdered[id] = this._pets[id].Quantity;
        }
        
        return productsToBeOrdered;
    },
    
    //initialize
    initialize: function() {
        // initialize the base class
        BanditDndNamespace.CalendarDndBehavior.callBaseMethod(this, "initialize");
        
        //register the droppable object into DragDropManager
        Sys.Preview.UI.DragDropManager.registerDropTarget(this);
    },
   
    //destructor
    dispose: function() {
        //unregister the droppable object into DragDropManager
        //
        Sys.Preview.UI.DragDropManager.unregisterDropTarget(this);
        
        BanditDndNamespace.CalendarDndBehavior.callBaseMethod(this, "dispose");
    }
}
BanditDndNamespace.CalendarDndBehavior.registerClass("BanditDndNamespace.CalendarDndBehavior",
    Sys.UI.Behavior, Sys.Preview.UI.IDropTarget);
    
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();    
