﻿$(function() {
    $(".editwidget").livequery('click', function(event) {
        // Finds parent div with class wh and then finds sibling with class we.
        $(this).parent().parent().parent().siblings(".we").toggle();
        return false;
    });
    $(".collapsewidget").livequery('click', function(event) {
        // Finds parent div with class wh and then finds sibling with class we and wc.
        $(this).parent().parent().parent().siblings(".we").hide();
        $(this).parent().parent().parent().siblings(".wc").toggle();
        return false;
    });

    if (document.all) {
        document.body.onmouseup = Widget_OnDrop;
        document.body.onmousemove = Widget_OnDragging;
        $(".column").livequery('mouseover', function(event) {
            if (m_objDragContainer != null) {
                $(this).addClass("columnhighlight");
            }
        });
        $(".column").livequery('mouseout', function(event) {
            $(this).removeClass("columnhighlight");
        });
    }
    else {
        $(".widget-thumb").livequery(function() {
            m_blnWidgetDragging = true;
            $(this).draggable({
                helper: 'clone'
            });
        });
        $(".column").livequery(function() {
            $(this).droppable({
                accept: ".widget-thumb",
                hoverClass: 'columnhighlight',
                drop: function(ev, ui) {
                    __doPostBack($(this).attr('id'), 'addwidget:' + $(ui.draggable).attr('widgetid') + ':' + $(this).attr('zoneid'));
                }
            });
        });
    }
});

// Sets the draggable handle in the widget to the WidgetHeadZone control.
function SetHandleDock(dock, args) {
    dock.set_handle(document.getElementById("Handle_" + dock.get_id()));
}

var m_objDragContainer = null;
var m_intYModifier = 0;
var m_intXModifier = 0;

function Widget_OnDrag(objThis, e) {
    if (document.all) {
        m_intYModifier = -10;
        m_intXModifier = -10;

        m_objDragContainer = document.getElementById('widgetDragContainer');
        m_objDragContainer.setAttribute('widgetid', objThis.getAttribute('widgetid'))
        m_objDragContainer.innerHTML = objThis.innerHTML;
        m_objDragContainer.style.display = '';
        m_objDragContainer.style.top = e.clientY - m_intYModifier;
        m_objDragContainer.style.left = e.clientX - m_intXModifier;
        bubbleEvent(event);
    }
    return false;
}

function Widget_OnDragging() {
    if (m_objDragContainer != null) {
        m_objDragContainer.style.top = event.clientY - m_intYModifier;
        m_objDragContainer.style.left = event.clientX - m_intXModifier;
        return false;
    }

}

function Widget_OnDrop() {
    if (m_objDragContainer != null) {
        var objDropZone = Widget_FindDropZone(event.srcElement);
        if (objDropZone != null) {
            __doPostBack(objDropZone.getAttribute('id'), 'addwidget:' + m_objDragContainer.getAttribute('widgetid') + ':' + objDropZone.getAttribute('zoneid'));
        }
        m_objDragContainer.style.display = 'none';
        m_objDragContainer = null;

    }
}

function Widget_FindDropZone(objCurrent) {
    if (objCurrent.nodeName.toLowerCase() == 'html') {
        return null;
    }
    if (objCurrent.getAttribute("zoneid") != null) {
        return objCurrent;
    }
    else {
        if (objCurrent.parentNode != null) {
            return Widget_FindDropZone(objCurrent.parentNode);
        }
    }
}

