﻿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) {
			if (this.config.altcomponent != null) {
				this.config.altcomponent();
				return;
			}
			// 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);

			RFInstall.HtmlElement = 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();
				this.textBlock(textBlockName);
				//var desc = .replace(/\{1\}/g, version);
				//this.root.innerHTML = desc;
			};

			this.textBlock = function (name) {
				var message,
					xml,
					url;
				var isIE8 = window.XDomainRequest ? true : false;

				url = applicationPath + "RS/Freepage/TextBlock.aspx";
				try {
					xml = "<root name=\"" + name + "\" />";
					if (isIE8) {
						if (typeof (RFInstall.XDR) == "undefined") {
							RFInstall.XDR = new window.XDomainRequest();
							RFInstall.XDR.open("POST", url);
							RFInstall.XDR.send(xml);
							RFInstall.XDR.onload = this.messageLoaded;
						}
					}
					else {
						if (typeof (RFInstall.XDR) == "undefined") {
							if ((RFInstall.XDR = executeRequest(url, xml)) != null)
								this.displayHTML(getResponseMessage(RFInstall.XDR));
							else
								this.displayHTML("ExecuteRequest failed");
						}
					}
				}
				catch (error) {
					this.displayHTML(error.message);
				}
			};

			this.displayHTML = function (message) {
				RFInstall.HtmlElement.innerHTML = message;
			}

			this.messageLoaded = function () {
				if (RFInstall.XDR.responseText.length != 0)
					RFInstall.HtmlElement.innerHTML = RFInstall.XDR.responseText;
			}
		}
		else // No installation required, inject the control
			element.innerHTML = obj;
	}
}

function executeRequest(url, xmlRequest) {
	var httpRequest;

	if ((httpRequest = createXmlHttpObject()) != null) {
		httpRequest.open("POST", url, false);
		httpRequest.send(xmlRequest);
	}
	return httpRequest;
}

function createXmlHttpObject()
{
	
	var xmlHttp;
	try
	{
		xmlHttp = new XMLHttpRequest(); 
	}
	catch (e)
	{
		try
		{
			xmlHttp = new ActiveXObject("MSXML2.XmlHttp");
		}
		catch (e)
		{
			try
			{
				xmlHttp = new ActiveXObject("Microsoft.XmlHttp");
			}
			catch (e)
			{
				xmlHttp = null;
			}
		}
	}
	return xmlHttp;
}

function getResponseMessage(request) {
	var nsResolver,
		evaluator,
		message,
		results,
		xmlDoc;

	if (typeof (request.responseXML.text) != "undefined")
		message = request.responseXML.text;
	else {
		evaluator = new XPathEvaluator();
		xmlDoc = request.responseXML;
		nsResolver = evaluator.createNSResolver(xmlDoc.ownerDocument == null ? xmlDoc.documentElement : xmlDoc.ownerDocument.documentElement);
		//results = evaluator.evaluate("response/message", xmlDoc, nsResolver, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
		//message = results.singleNodeValue.firstChild.nodeValue;
		message = request.responseText;
	}
	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 = {};


