﻿/// <reference path="wz_dragdrop.js" />
/// <reference path="DDUtils.js" />
/// <reference path="DesignHelper.js" />
var Class_DROPCUE = 'Dropcue';
var Class_Element = 'Elem';

var _dropCue;
var _targetContainer;
var _dropCueBounds;

function RepositionDropCue(container, item, x, y)
{
    if (!container)
    {
        return;
    }
    if (_targetContainer != container)
    {
        AdjustElements(_targetContainer.id, item, true);
        HideDropCue();
        var bounds = GetElementBounds(item);
        ShowDropCue(container, item, bounds);    
    }
    if (_targetContainer == container)
    {
        var holder = FindChildByClass(container, Class_HOLDER);
        var nearestChild = _findItemAt(container, item, x, y);
        
        if (nearestChild == null)
        {
            holder.removeChild(_dropCue);
            holder.appendChild(_dropCue);
        }
        else
        {
            window.status = nearestChild.id;
            holder.removeChild(_dropCue);
            holder.insertBefore(_dropCue, nearestChild);
        }
        AdjustElements(container.id, item, false);
    }
}


function ShowDropCue(container, item, bounds)
{
    if(container != null && _dropCue == null && _targetContainer == null)
    {
        var dropCue = FindChildByClass(container, Class_DROPCUE);
        _targetContainer    = container;
        _dropCue            = dropCue;
        _dropCueBounds      = bounds;
        
        if (dropCue != null)
        {
			Sys.UI.DomElement.setLocation(dropCue, bounds.x, bounds.y);
        }
        dropCue.style.width         = bounds.width +'px';
        dropCue.style.height        = (bounds.height > -1 ? bounds.height : 100) +'px';
        dropCue.style.display       = 'block';
        dropCue.style.visibility    = 'visible';
        RepositionDropCue(_targetContainer, item, bounds.x, bounds.y);
        
        _targetContainer    = container;
        _dropCue            = dropCue;
        _dropCueBounds      = bounds;
    }
}


function HideDropCue()
{
    if(_dropCue)
    {
        _dropCue.style.display      = 'none';
        _dropCue.style.visibility   = 'hidden';
        _dropCue            = null;
        _targetContainer    = null;
        _dropCueBounds      = null;
    }
}

function _findItemAt(container, item, x, y)
{
    var child;
    if (container != null && item != null)
    {
        var holder = FindChildByClass(container, Class_HOLDER);
        for (var i = 0 ; i < holder.childNodes.length; i++)
        {
            child = holder.childNodes.item(i);
            if (child != null)
            {
                if (child.className != null 
                    &&  child.className.indexOf(Class_Component) > -1
                    && child != item)
                {
                    var pos = Sys.UI.DomElement.getLocation(child);
                    var bounds = Sys.UI.DomElement.getBounds(child);
                    if( y <= pos.y + bounds.height/2)
                    {
                        return child;
                    }
                }
            }
        }
    }
    return child;
}

function GetDropCueBounds()
{
    var bounds = null;
    if (_dropCue != null)
    {
         bounds = GetElementBounds(_dropCue);
//         bounds = Sys.UI.DomElement.getBounds(_dropCue);
    }
    return bounds;
}