﻿window.RFInstall = {
	INSTALL_REQUIRED: "install",
	UPGRADE_REQUIRED: "upgrade",
	RESTART_REQUIRED: "restart",
	INSTALLING: "installing",
	NOT_SUPPORTED: "notSupported",

	WIDE: "Wide",
	NARROW: "Narrow",
	SMALL: "Small",
	ENCODER: "Encoder",

	IsIE6: typeof window.XMLHttpRequest == "undefined" && window.external,
	IsIE: /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent),

	DefaultConfig:
    {
    	title: "Silverlight Project",
    	picture: "images/wide/light.jpg",
    	component: "*"
    },

	// Iterate objects and copy contents where it is missing in the original
	Fill: function(f, t) {
		for (var k in f) {
			if (!t[k] || (typeof t[k] != typeof f[k]))
				t[k] = f[k];
			else if (typeof f[k] == "object")
				RFInstall.Fill(f[k], t[k]);
		}
	},

	Installer: function(params) {

		var NEED_INSTALL = 'install';
		this.config = params.installerUiConfig;

		// Iterate config and replace any missing parts with default
		if (!this.config)
			this.config = RFInstall.DefaultConfig;
		else
			RFInstall.Fill(RFInstall.DefaultConfig, this.config);

		params.properties.alt = NEED_INSTALL;
		var version = params.properties.version;

		var element = document.getElementById(params.parentElement);
		if (!element) {
			alert("Silverlight could not find a DOM element with id '" + params.parentElement + "'.");
			return;
		}

		// Perform the injection ourselves, so make sure Silverlight knows not to do it
		params.parentElement = null;
		var obj = Silverlight.createObjectEx(params);

		if (obj == NEED_INSTALL) {
			// Installation is required.
			var t = RFInstall.WIDE;

			var w = element.offsetWidth;
			var h = element.offsetHeight;
			if (w <= 350 || h <= 475)
				t = RFInstall.SMALL;
			else if ((w <= 600 && w >= 350) && (h <= 700 && h >= 475))
				t = RFInstall.NARROW;
			this.type = this.config.type ? this.config.type : t;
			if (!Silverlight.installControls)
				Silverlight.installControls = [];
			Silverlight.installControls.push(this);

			this.root = element;
			this.state = RFInstall.UPGRADE_REQUIRED;

			// Update the installer to a predefined state
			this.update = function(state) {
				// If no state was passed, just redraw with the current state
				if (!state)
					state = this.state;
				this.state = state;
				var textBlockName = "silverlight" + "." + this.config.component.toLowerCase() + "." + state + "." + this.type.toLowerCase();
				var desc = TextBlock(textBlockName).replace(/\{1\}/g, version);
				this.root.innerHTML = desc;
			};
		}
		else // No installation required, inject the control
			element.innerHTML = obj;
	}
}

function TextBlock(name) {
	var httpRequest,
        xmlRequest,
        message,
        xml;

	try {
		xmlRequest = new XmlRequest(applicationPath + "RS/Freepage/TextBlock.aspx", null);
		xml = "<root name=\"" + name + "\" />";
		if ((httpRequest = xmlRequest.ExecuteRequest(xml)) != null)
			message = responseMessage(httpRequest);
		else
			message = "ExecuteRequest failed";
	}
	catch (error) {
		message = error.message;
	}
	return message;
}

Silverlight.inject = function(params) {
	new RFInstall.Installer(params);
}

Silverlight.onGetSilverlight = function() {
	for (var i in Silverlight.installControls)
		Silverlight.installControls[i].update(RFInstall.INSTALLING);
};

Silverlight.onRestartRequired = function() {
	for (var i in Silverlight.installControls)
		Silverlight.installControls[i].update(RFInstall.RESTART_REQUIRED);
};

Silverlight.onUpgradeRequired = function() {
	for (var i in Silverlight.installControls)
		Silverlight.installControls[i].update(RFInstall.UPGRADE_REQUIRED);
};

Silverlight.onInstallRequired = function() {
	for (var i in Silverlight.installControls)
		Silverlight.installControls[i].update(RFInstall.INSTALL_REQUIRED);
};

function pageLoaded() {
	if (Silverlight.supportedUserAgent()) {
		// silverlight is supported, update any installers
		for (var i in Silverlight.installControls)
			Silverlight.installControls[i].update();
	}
	else {
		// silverlight is not supported, tell the installers to report this
		for (var i in Silverlight.installControls)
			Silverlight.installControls[i].update(RFInstall.NOT_SUPPORTED);
	}
}

if (!window.Silverlight)
	Silverlight = {};

