// Enum: The available heater models.
XiHeaterModels = new Object();
XiHeaterModels.AOSGoldXi = 'aosgoldxi';
XiHeaterModels.AOSCycloneXi = 'aoscyclonexi';
XiHeaterModels.AOSmithCustomXi = 'aosmithcustomxi';
XiHeaterModels.AmericanHighEfficiency = 'americanhighefficiency';
XiHeaterModels.AmericanHeavyDutyElectric = 'americanheavydutyelectric';
XiHeaterModels.StateUltraForce = 'stateultraforce';
XiHeaterModels.StateSandblaster = 'statesandblaster';
XiHeaterModels.StateDSE = 'statedse';

// Class: XiSimulator
XiSimulator = function(settings) {
    // Source file for the application.
    //this.SourceFile = 'ClientBin/Debug/Shell.xap';
        this.SourceFile = 'ClientBin/Release/Shell.xap'

    // Plug-in Not Installed Template
    this.PluginNotInstalledTemplate =
                '<a href="Silverlight.2.0.exe" style="text-decoration: none;">' +
                    '<img src="App_Themes/Default/InstallSilverlight.png" alt="Get Microsoft Silverlight"' +
                        'style="border-style: none" />' +
                '</a>';

    // Configuration Settings.
    this.GetConfigurationSettings = function() {
        if (settings["width"] == undefined) settings["width"] = '500';
        if (settings["height"] == undefined) settings["height"] = '350';
        var args =
        {
            // ID of the plug-in object.
            pluginID: settings["pluginID"],
            parentElement: document.getElementById(settings["parentElementID"]),
            width: settings["width"],
            height: settings["height"],
            background: 'transparent',
            initParams:
            {
                // ID of the application object
                // under the plug-in object.
                // TODO: Make this a parameter.
                applicationID: settings["applicationID"],

                // Heater Model. Available options:
                // - GoldXi
                // - CycloneXi
                model: settings["model"]
            }
        };
        return args;
    }

    this.onSilverlightError = function(sender, args) {

        var appSource = "";
        if (sender != null && sender != 0) {
            appSource = sender.getHost().Source;
        }
        var errorType = args.ErrorType;
        var iErrorCode = args.ErrorCode;

        var errMsg = "Unhandled Error in Silverlight 2 Application " + appSource + "\n";

        errMsg += "Code: " + iErrorCode + "    \n";
        errMsg += "Category: " + errorType + "       \n";
        errMsg += "Message: " + args.ErrorMessage + "     \n";

        if (errorType == "ParserError") {
            errMsg += "File: " + args.xamlFile + "     \n";
            errMsg += "Line: " + args.lineNumber + "     \n";
            errMsg += "Position: " + args.charPosition + "     \n";
        }
        else if (errorType == "RuntimeError") {
            if (args.lineNumber != 0) {
                errMsg += "Line: " + args.lineNumber + "     \n";
                errMsg += "Position: " + args.charPosition + "     \n";
            }
            errMsg += "MethodName: " + args.methodName + "     \n";
        }

        throw new Error(errMsg);
    }

    // The fault check box changed event handler.
    this.onFaultCheckboxChanged = function() {

        if (settings["faultCheckBoxID"] != undefined) {
            var faultCheckBox = document.getElementById(settings["faultCheckBoxID"]);
            if (faultCheckBox != null && faultCheckBox != undefined) {
                var plugin = document[settings.pluginID];
                if (plugin != null)
                    if (plugin.Content[settings.applicationID] != null)
                    plugin.Content[settings.applicationID].NotifyAlert(faultCheckBox.checked);
            }
        }
    }

    // The event handler for on plugin loaded.
    this.onPluginLoaded = function(sender) {

        // Enable the check box.
        if (settings["faultCheckBoxID"] != undefined) {
            var faultCheckBox = document.getElementById(settings["faultCheckBoxID"]);
            if (faultCheckBox != null && faultCheckBox != undefined)
                faultCheckBox.disabled = "";
        }
    }

    // Run the silverlight instance.
    this.Run = function() {

        // Settings as arguments.
        var args = this.GetConfigurationSettings();

        // Build the Init Params String.
        var initParamsString = null;
        if (args["initParams"] != null) {

            // Buld the initParams string.
            initParamsString = '';
            for (var name in args["initParams"]) {
                if (initParamsString != '')
                    initParamsString += ',';
                initParamsString += name + '=' + args["initParams"][name];
            }
        };

        // Set the fault check box event handler and disable the checkbox.
        if (
            (
                settings["model"] == XiHeaterModels.AOSGoldXi   ||
                settings["model"] == XiHeaterModels.AmericanHeavyDutyElectric ||
                settings["model"] == XiHeaterModels.StateSandblaster    ||
                settings["model"] == XiHeaterModels.AOSmithCustomXi
            )
            &&
            settings["faultCheckBoxID"] != undefined) {
            var faultCheckBox = document.getElementById(settings["faultCheckBoxID"]);
            if (faultCheckBox != null) faultCheckBox.onclick = this.onFaultCheckboxChanged;
            faultCheckBox.disabled = 'disabled';
        }

        Silverlight.createObjectEx(
            {
                source: this.SourceFile,
                parentElement: args["parentElement"],   // Parent element of the silverlight object.
                id: args["pluginID"],                   // Unique plug-in ID value.
                properties:
                {                                           // Plug-in properties.
                    width: args["width"],                   // Width of rectangular region of plug-in, in pixels.
                    height: args["height"],                 // Height of rectangular region of plug-in, in pixels.
                    background: args["background"],         // Background color of plug-in.
                    isWindowless: 'true',                   // Is Plug-in windowless?
                    version: '2.0',                         // Plug-in version to use.
                    alt: this.PluginNotInstalledTemplate    // Plugin Not Installed template.
                },
                events:
                {
                    onError: this.onSilverlightError,        // OnError property value -- event-handler function name.
                    onLoad: this.onPluginLoaded              // OnLoad property value -- event-handler function name.
                },
                initParams: initParamsString,           // initParams property value -- user-settable string for information passing.
                context: null                           // Context value -- passed to Silverlight.js onLoad event handlers.
            });
    }

}

