//
//  Coco's Dress Up Kit
//

var status;
var isMouseDown = false;
var beginX;
var beginY;

function root_Loaded(sender, args)
{
    status = sender.findName("Status");
}

// Start drag and drop operation.
function onMouseDown(sender, mouseEventArgs)
{
    // Set the beginning position of the mouse.
    beginX = mouseEventArgs.x;
    beginY = mouseEventArgs.y;

    isMouseDown = true;
    updateStatus(sender.name);

    // Ensure this object is the only one receiving mouse events.
    sender.captureMouse();
}

// Stop drag and drop operation.
function onMouseUp(sender, mouseEventArgs)
{
    isMouseDown = false;
    updateStatus("");

    // All all objects to receive mouse events.
    sender.releaseMouseCapture();
}

// Reposition object during drag and drop operation.
function onMouseMove(sender, mouseEventArgs)
{
    // Determine whether the mouse button is down.
    // If so, move the object.
    if (isMouseDown == true)
    {
        // Retrieve the current position of the mouse.
        var currX = mouseEventArgs.x;
        var currY = mouseEventArgs.y;

        // Reset the location of the object.
        sender["Canvas.Left"] += currX - beginX;
        sender["Canvas.Top"] += currY - beginY;

        // Update the beginning position of the mouse.
        beginX = currX;
        beginY = currY;
    }
}

// Display the select object during the drag and drop operation.
function updateStatus(msgString)
{
    var tmp = msgString.split("_");
    status.text = "Item selected: " + tmp[0];
}

// Create the petals of the flower.
function onFlowerLoaded(sender, eventArgs)
{
    var control = sender.getHost();

    for (i = 0; i < 50; i++)
    {
        var left = 10 - Math.round(20 * Math.random());
        var top = 20 - Math.round(40 * Math.random());

        // Create a XAML fragment containing an Ellipse object.
        var xamlFragment = '<Ellipse Height="10" Width="10" Stroke="Maroon" StrokeThickness="0.5" Fill="Pink" ';
            xamlFragment += 'Canvas.Left="' + left + '" Canvas.Top="' + top + '" />';
        var ellipse = control.createFromXaml(xamlFragment);

        // Add the Ellipse object to the Flower object.
        sender.children.add(ellipse);
    }
}