
if (!window.Designer)
	window.Designer = {};

Designer.Scene = function() {}

Designer.Scene.prototype = 
{
    handleLoad: function(control, userContext, rootElement) 
    {
        Designer.Scene.root = rootElement;
        Designer.Scene.control = control;

    }
}   

// Define state variables for drag and drop operation.
var beginX;
var beginY;
var isMouseDown = false;

// Start drag and drop operation.
function onMouseDown(sender, mouseEventArgs)
{
    // Set the beginning position of the mouse.
    beginX = mouseEventArgs.getPosition(null).x;
    beginY = mouseEventArgs.getPosition(null).y;

    isMouseDown = true;

    // Ensure this object is the only one receiving mouse events.
    sender.captureMouse();
}

// 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.getPosition(null).x;
        var currY = mouseEventArgs.getPosition(null).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;
        Connect(sender);
    }
}

// Stop drag and drop operation.
function onMouseUp(sender, mouseEventArgs)
{
    isMouseDown = false;

    // Allow all objects to receive mouse events.
    sender.releaseMouseCapture();
}



// Replaces all instances of the given substring.
String.prototype.replaceAll = function(strTarget, strSubString)
{
    var strText = this;
    var intIndexOfMatch = strText.indexOf( strTarget );

    // Keep looping while an instance of the target string
    // still exists in the string.
    while (intIndexOfMatch != -1)
    {
        // Relace out the current instance.
        strText = strText.replace( strTarget, strSubString )

        // Get the index of any next matching substring.
        intIndexOfMatch = strText.indexOf( strTarget );
    }

    // Return the updated string with ALL the target strings
    // replaced out with the new substring.
    return( strText );
}

function createSilverlight()
{
	var scene = new Designer.Scene();
	Sys.Silverlight.createObjectEx({
		source: "xaml/Scene.xaml",
		parentElement: document.getElementById("SilverlightControlHost"),
		id: "SilverlightControl",
		properties: {
			width: "800",
			height: "625",
			version: "0.8",
			background: "#FF333333",			
		    isWindowless: "true"
		},
		events: {
			onLoad: Sys.Silverlight.createDelegate(scene, scene.handleLoad)
		}
	});
}

if (!window.Sys)
	window.Sys = {};
	
if (!window.Silverlight) 
	window.Silverlight = {};

Sys.Silverlight.createDelegate = function(instance, method) {
	return function() {
        return method.apply(instance, arguments);
    }
}

function Connect(sender)
{
    var a1=sender.findName("a1");
    var a2=sender.findName("a2");
    var c1=sender.findName("a1a2conn1");
    var c2=sender.findName("a1a2conn2");
    var c3=sender.findName("a1a2conn3");
    if (c1==null)
    {        
        c1 = Designer.Scene.control.content.createFromXaml("<Rectangle xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' x:Name='a1a2conn1' Fill='blue' Height='1' Width='1' />");
        Designer.Scene.root.children.add(c1);        
    }
    if (c2==null)
    {
        c2 = Designer.Scene.control.content.createFromXaml("<Rectangle xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' x:Name='a1a2conn2' Fill='blue' Height='1' Width='1' />");
        Designer.Scene.root.children.add(c2);  
    }
    if (c3==null)
    {
        c3 = Designer.Scene.control.content.createFromXaml("<Rectangle xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' x:Name='a1a2conn3' Fill='blue' Height='1' Width='1' />");
        Designer.Scene.root.children.add(c3);    
    }	

    var fromX = a1["Canvas.Left"];
    var fromY = a1["Canvas.Top"]+a1["Height"]/2;
    var toX = a2["Canvas.Left"];
    var toY = a2["Canvas.Top"]+a2["Height"]/2;

    if (fromX > toX)
    {
        var intX=toX;
        toX = fromX;
        fromX = intX;
        var intY=toY;
        toY = fromY;
        fromY = intY;
        
    }
    if (fromX+a1.Width < toX) fromX=fromX+a1.Width;
    var wpe = (toX-fromX)/2;	

    c1["Canvas.Left"]=fromX;
    c1["Canvas.Top"]=fromY;
    c1["Width"]=wpe;
    c1["Height"]=2;

    c2["Canvas.Left"]=(toX-wpe); 
    c2["Canvas.Top"]=toY;
    c2["Width"]=wpe;
    c2["Height"]=2;
    
    if (fromY<toY)
    {
        c3["Canvas.Left"]=(fromX+wpe-1);
        c3["Canvas.Top"]=fromY;
        c3["Width"]=2;
        c3["Height"]=(toY-fromY+1);
    }
    else
    {
        c3["Canvas.Left"]=(toX-wpe-1);
        c3["Canvas.Top"]=toY;
        c3["Width"]=2;
        c3["Height"]=(fromY-toY+1);
    }
}
