// 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();
   
    // 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");
        
        // Get an instance of the DNN_Silverlight object that contains the current
        // information containing the location of the .xaml file
		var Module = new DNN_Silverlight_Object.Module();
		
        xamlDownloader.open("GET", Module.TemplateSourceDirectory + "/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 "box" will now be added to the main Canvas
        // "plugin" is a reference to the Silverlight control on the html page 
        var plugin = sender.getHost(); 
        
        // The altered "box.xaml" is used to create a XAML object
        var newElement = plugin.content.createFromXaml(originalXaml)

        // "rootCanvas" is a reference to the main Silverlight Canvas 
        var rootCanvas = plugin.content.findName("Page");
        
        // The XML object is added to the root Canvas
        rootCanvas.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;
    }        
}