﻿// Copyright (c) Omar AL Zabir. All rights reserved.
// For continued development and updates, visit http://msmvps.com/omar

Type.registerNamespace('CustomDragDrop');

CustomDragDrop.CustomFloatingBehavior = function(element) {
    CustomDragDrop.CustomFloatingBehavior.initializeBase(this,[element]);
    
    var _handle;
    var _location;
    var _dragStartLocation;
    var _DragHandleIDValue;
    
    var _mouseDownHandler = Function.createDelegate(this, mouseDownHandler);
    var divGhost=document.createElement("div");
    divGhost.id="divGhost";
    divGhost.style.border="1px dashed #00a ";
    var columnsObj={};
    var mousePos ; 
    var index=0;
    var colnum=0;
   
    this.get_DragHandleID = function()
    {
        return this._DragHandleIDValue;
    }
    
    this.set_DragHandleID = function(value)
    {
        this._DragHandleIDValue = value;
    }
    
    this.add_move = function(handler) {
        this.get_events().addHandler('move', handler);
    } 
    this.remove_move = function(handler) {
        this.get_events().removeHandler('move', handler);
    }
    
    this.get_handle = function() {
        return _handle;
    }
    this.set_handle = function(value) {
        if (_handle != null) {
            $removeHandler(_handle, "mousedown", _mouseDownHandler);                        
        }
    
        _handle = value;
        $addHandler(_handle, "mousedown", _mouseDownHandler);                
    }
    
    this.get_location = function() {
        return _location;
    }
    this.set_location = function(value) {
        if (_location != value) {
            _location = value;
            if (this.get_isInitialized()) {                
                
                Sys.UI.DomElement.setLocation(this.get_element(), _location.x, _location.y);
            }
            this.raisePropertyChanged('location');
        }
    }
    
    this.initialize = function() {
        CustomDragDrop.CustomFloatingBehavior.callBaseMethod(this, 'initialize');

        // Set the handle and initialize dragging
        this.set_handle( $get( this.get_DragHandleID() ) );
    }
    
    this.dispose = function() {
        if (_handle && _mouseDownHandler) {
            $removeHandler(_handle, "mousedown", _mouseDownHandler);
        }
        _mouseDownHandler = null;
        CustomDragDrop.CustomFloatingBehavior.callBaseMethod(this, 'dispose');
    }
    
    this.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;
    }

    
    function mouseDownHandler(ev) {
        window._event = ev;
        var el = this.get_element();
        
        if (!this.checkCanDrag(ev.target)) return;
        
        // Get the location before making the element absolute
        _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);               
        
        _dragStartLocation = Sys.UI.DomElement.getLocation(el);        
        
        ev.preventDefault();
        
        this.startDragDrop(el);
        
        // Hack for restoring position to static
        el.originalPosition = "static";
        el.originalZIndex = el.style.zIndex;
        el.style.zIndex = "60000";
	    el.style.position="absolute";
	    //el.style.zIndex ="999999999";
    }
    
    // Type get_dataType()
    this.get_dragDataType = function() {
        return "_CustomFloatingItem";
    }
    
    // Object get_data(Context)
    this.getDragData = function(context) {
        return { item : this.get_element(), handle : this.get_handle() } ;
    }
    
    // DragMode get_dragMode()
    this.get_dragMode = function() {
        //return Sys.Preview.UI.DragMode.Move;
        return AjaxControlToolkit.DragMode.Move;
    }
    function findPosX(obj)
    {
	    var curleft=0;
	    if(obj.offsetParent)
	    {
		    while(obj.offsetParent)
		    {
			    curleft+=obj.offsetLeft;
			    obj=obj.offsetParent;
		    }
	    }
	    else if(obj.x)
		    curleft+=obj.x;
	    return curleft;
    }
    function findPosY(obj)
    {
	    var curtop=0;
	    if(obj.offsetParent)
	    {
		    while(obj.offsetParent)
		    {
			    curtop+=obj.offsetTop;
			    obj=obj.offsetParent;
		    }
	    }
	    else if(obj.y)
		    curtop+=obj.y;
	    return curtop;
    }
    function getModuleArr(column)
    {
	    var arr=[];
	    var ln=column.childNodes.length;
	    for(var z=0;z<ln;z++)
	    {
		    if(!column.childNodes[z].isDragging)
		    {
			    arr.push(column.childNodes[z]);
		    }
	    }
	    return arr;
    }
    function getGhostPos(arr,y)
    {
	    var ln=arr.length;
	    for(var z=0;z<ln;z++)
	    {
		    if(arr[z]==divGhost)return z;
	    }
    }
    function getclientHeight()
    {
	    if(window.innerHeight<document.body.clientHeight)
	    {
		    return window.innerHeight
	    }
	    return document.body.clientHeight
    };
    function getscrollHeight()
    {
	    if(document.body.scrollHeight>document.documentElement.clientHeight)
	    {
		    return document.body.scrollHeight
	    }
	    else
	    {
		    return document.documentElement.clientHeight
	    }
    };
    // void onDragStart()
    this.onDragStart = function() {
//        var el = this.get_element();
//        //alert(el.id + "--XXXX--" + el.style.left);
//        	var left=findPosX(el);
//			var top=findPosY(el);
//			var w=el.offsetWidth;
//            // alert("_location.x:" + _location.x +"  _location.y:"+ _location.y + " left ");
//			el.style.width=(w)+"px";
//			el.style.left=(left-15)+"px";
//			el.style.top=(top)+"px";
//			divGhost.style.height=el.offsetHeight+"px";
////			if(Browser.isGecko)
////			{
////				divGhost.style.height-=parseInt(divGhost.style.borderTopWidth)*2;
////			}
//            //alert(divGhost.style.top)
//			//el.parentNode.insertBefore(divGhost,el);
//			el.style.position="absolute";
//			el.style.zIndex ="999999999";
//			el.isDragging=true;
//			divGhost.col=el.parentNode;
	}
    function CreateCol()
    {
        
    	var div=document.createElement("div");
		div.style.minHeight="1px";
		div.id="col1";
		div.className="widget_hover";
		div.colNum=1;
		return;
    }
    // void onDrag()
    this.onDrag = function() { 
    
//            var el = this.get_element();
//        	var left=findPosX(el);
//			var top=findPosY(el); 
//			el.style.left=(left-15)+"px";
//			el.style.top=(top)+"px";

    }
    
    // void onDragEnd(Canceled)
    this.onDragEnd = function(canceled) {
        //alert(canceled);
        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;
// 			var col=divGhost.col;
//			el.isDragging=false;
			//col.replaceChild(el,divGhost);
			el.style.position="static";
			el.style.width="auto";
    }
    
    this.startDragDrop = function(dragVisual) {
        AjaxControlToolkit.DragDropManager.startDragDrop(this, dragVisual, null);
        //Sys.Preview.UI.DragDropManager.startDragDrop(this, dragVisual, null);
    }
    
    this.get_dropTargetElement = function() {
        return document.body;
    }
    
    this.canDrop = function(dragMode, dataType, data) {
        return (dataType == "_CustomFloatingItem");
    }

}

CustomDragDrop.CustomFloatingBehavior.registerClass('CustomDragDrop.CustomFloatingBehavior', 
AjaxControlToolkit.BehaviorBase, 
AjaxControlToolkit.IDragSource, Sys.IDisposable);
//Sys.UI.Behavior, AjaxControlToolkit.IDragSource, Sys.IDisposable);
//);


if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();