﻿

//This function is called from the silverlight menu application.
//The XAP file and long description are passed from the silverlight
//app to this javascript funtion.

function loadXAP(xapFile, longDesc) {

    //MS recently announced it will include jQuery in future release.
    //So to get up to speed let's jQuery.
    //Switch between the initial screen to the screen that displays then
    //silverlight applications. This can be done in a number of ways, I
    //decide to swap CSS classes using jQuery API.
    if ($("div.welcome").length > 0) {
        $("#xap").removeClass("xap").addClass("xapafter");
        $("#welcome").removeClass("welcome").addClass("welcomeafter").html("");
        $("#xapdesc").removeClass("xapdesc").addClass("xapdescafter");
        $("#outerxapdesc").css('height', '90px');
        $("#menu").css('margin-top', '10px');
        $("#content").css('margin-top', '10px');
    }
    $("#xapdesc").hide().html(longDesc).fadeIn(1000);
    
    //Loading the XAP file is trivial just set the Source to the Uri of the XAP file.
    //What is a little more difficult is making sure the user does not change the XAP
    //while the browser is in the process of loading a XAP. This happens if the user
    //quickly navigates through the menu.
    //To ensure that the user does not change the XAP we will implement a simple
    //hand shake. Take a look at the XamlContent silverlight control and notice
    //that the OnPluginLoaded property is set to the onXAPCompleted. What this does
    //is when the XAP is completed load it will invoke the onXAPCompleted javascript
    //function. This function will call a silverlight scriptable member which will
    //enable the menu. 
    try {
        var slcontent = document.getElementById("XamlContent");
        slcontent.Source = xapFile;
    } catch (e) {
        alert(e);
        //Just in case something went wrong then force refresh of browser.
        alert("An error loading the silverlight control has occurred. This happens primarily when you navigate too quickly in the menu. The browser will now refresh.");
        window.location.reload(true);
    }
}

//This is called when the silverlight control has loaded completely. This
//ensures that a XAP is completed loaded before the user can set a new XAP. 
function onXAPCompleted() {
    $get("XamlMenu").content.SilverlightObj.WaitUntilSLLoaded();
}
