// Michael Washington
// Silverlight Tutorials
// http://www.adefwebserver.com/DotNetNukeHELP/Misc/Silverlight/

// Adapted from Justin-Josef Angel's
// "Silverlight Controls - The path to reusable XAML"
// http://blogs.microsoft.co.il/blogs/justinangel/archive/2007/08/14/Silverlight-Controls-_2D00_-The-path-to-reusable-XAML.aspx

// This JavaScript file defines the object "box"


box = function(ID, Parent, XLocation)
{

    this._ID = ID + "_";
    this._parent = Parent; 
    this._XLocation = XLocation;
    this._host = this._parent.getHost();
    this.Element;
   
    // The first step is to retrive the XAML content for the "box"
    this.StartXamlDownload();
}

box.prototype = 
{   

    _findNameByXamlID : function(nameInXamlFile)
    {
        return this._parent.findName(this._getIdFor(nameInXamlFile)); 
    },
    
    _getIdFor : function(nameInXamlFile)
    {
        return this._ID + nameInXamlFile;        
    },
    
    StartXamlDownload : function()
    {
        // A Silverlight "downloader" object is used to retrieve the "box.xaml" file that contains
        // the XAML for the "box"
        // A delegate is created that will call the "XamlDownloadCompleted" method when the 
        // download is completed
        var xamlDownloader = this._host.createObject("downloader");
        xamlDownloader.open("GET", "box.xaml");
        xamlDownloader.addEventListener("completed", Silverlight.createDelegate(this, this.XamlDownloadCompleted));
        xamlDownloader.send();
        
    },
    
    XamlDownloadCompleted : function(sender, eventArgs)
    {
        // The download of "box.xaml" has been completed
        // "sender.ResponseText" contains the contents of "box.xaml"
        var originalXaml = sender.ResponseText;

        // In order to avoid name collisions, the name of each "box" object will be replaced
        // with a name that begins with the ID that was passed in the object constructor
        originalXaml = originalXaml.replace(/Name="/g, "Name=\"" + this._ID);
                
        // The altered "box.xaml" is used to create a XAML object
	var plugin = sender.getHost(); 
        var newElement = plugin.content.createFromXaml(originalXaml)

	// Set Element to the XAML object so that it can be manipulated 
	this.Element = newElement;

        // The "box" will now be added to the main Canvas
        
        // The XML object is added to the element passed in the constructor
        this._parent.children.add(newElement);

        // Now that the "box" has been added to the main Canvas
        // the "BoxTitle" will be altered and the "box" position will be set
        this._setControlReferences(); 
    }, 
    
    _setControlReferences : function()
    {   
        // This method sets the "BoxTitle" and the "box" position
        this._BoxTitle = this._findNameByXamlID("BoxTitle");
        this._BoxTitle.text = this._XLocation.toString();   
       
        this._box = this._findNameByXamlID("box");
        this._box["Canvas.Left"] = this._XLocation;
        this._box["Canvas.Top"] = this._XLocation + 10;

	this._box.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleMouseDown));
    },
    
    handleMouseDown: function(sender, eventArgs)
    {     
	alert("Box clicked = " + this._BoxTitle.text);   
    } 
       
}

