if (!window.MS_Dynamics) window.MS_Dynamics = {};

MS_Dynamics.Base = function() {
	this.getATrialLink = "/dynamics/everyonegetsit/default.mspx?WT.mc_id=dynamicshero";
    this.fileAssets = new Array();
    this.fileAssets.push({ name: "fontSegoe", file: "/hk/dynamics/images/silverlight0708/Segoe.deploy", resource: null, type: "font" });
}
MS_Dynamics.Base.prototype = {
    dispose : function() { this.root = null; },
    initialize : function(control, userContext, rootElement) {
		this.control = control;
		this.rootCanvas = this.control.content.findName("rootCanvas");
		this.currentAsset = -1;
		this.assetRetrieval();		

    },
	assetRetrieval : function(sender, eventArgs) {
		if (++this.currentAsset < this.fileAssets.length) {
	        this.fileDownloader(this.fileAssets[this.currentAsset].file, this.onAssetRetrieved);
		} else this.endAssetRetrieval();
    },
	onAssetRetrieved : function(sender, eventArgs) {
		var thisAsset = this.fileAssets[this.currentAsset];
		this.fileAssets[this.currentAsset].resource = thisAsset.type == "xaml" ? sender.getResponseText("") : sender;
		this.assetRetrieval();
    },
	endAssetRetrieval : function() {
		// set fonts

		this.attachFont(this.rootCanvas.findName("Text2_Discover"), null, "fontSegoe", "Segoe", true);
		//this.attachFont(this.rootCanvas.findName("Text2_people"), null, "fontSegoe", "Segoe", true);
		//this.attachFont(this.rootCanvas.findName("Text3_Everyone"), null, "fontSegoe", "Segoe", true);
		//this.attachFont(this.rootCanvas.findName("GetTrial_white"), null, "fontSegoe", "Segoe", true);

        this.rootCanvas.findName("mainAnimation").Begin();
	},
    onFontLoad : function(sender, eventArgs) {
		this.segoeFont = sender;
		this.attachFont(this.rootCanvas.findName("Title"));
		this.attachFont(this.rootCanvas.findName("ColHdr_1a"));
		this.attachFont(this.rootCanvas.findName("ColHdr_1b"));
		this.attachFont(this.rootCanvas.findName("ColHdr_2a"));
		this.attachFont(this.rootCanvas.findName("ColHdr_2b"));
		this.attachFont(this.rootCanvas.findName("ColHdr_3a"));
		this.attachFont(this.rootCanvas.findName("ColHdr_3b"));
		this.animateBack.begin();
		this.animateIntro.begin();
    },

	attachFont : function(textObject, textValue, fontSource, fontName, setFontStyle) {
		if (setFontStyle) {
			//textObject.setFontSource(this.getAsset(fontSource));
			textObject["FontFamily"] = fontName;
		}
		if (textValue != null) textObject["Text"] = textValue;
	},
	fileDownloader : function(fileName, callback, progress) {
		var downloader = this.control.createObject("downloader");
		downloader.addEventListener("Completed", Silverlight.createDelegate(this, callback));
		if (progress != null)
			downloader.addEventListener("DownloadProgressChanged", Silverlight.createDelegate(this, progress));
		downloader.open("GET", fileName);
		downloader.send();
	},
	getAsset : function(assetName) {
		for (var i = 0; i < this.fileAssets.length; i++)
			if (this.fileAssets[i].name == assetName) return this.fileAssets[i].resource;
		return null;
	}
}