if (!window.MS_Dynamics) window.MS_Dynamics = {};

MS_Dynamics.Base = function() {
	this.getATrialLink = "/poland/dynamics/everyonegetsit/default.mspx";
    this.fileAssets = new Array();
    this.fileAssets.push({ name: "fontSegoe", file: "silverlight/HeroWidget/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();
		this.registerButton(this.rootCanvas.findName("click_shape"), this.onTrial_Over,
			this.onTrial_Out, this.onTrial_Down, this.onTrial_Release);
		//this.registerButton(this.rootCanvas.findName("ctlButtonCanvas"), this.onTrial_Over,
			//this.onTrial_Out, this.onTrial_Down, this.onTrial_Release);
    },
	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("Get_shadow"), null, "fontSegoe", "Segoe", true);
		this.attachFont(this.rootCanvas.findName("a_shadow"), null, "fontSegoe", "Segoe", true);
		this.attachFont(this.rootCanvas.findName("trial_shadow"), null, "fontSegoe", "Segoe", true);
		this.attachFont(this.rootCanvas.findName("Text1_Area"), null, "fontSegoe", "Segoe", true);
		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("Text3_gets"), null, "fontSegoe", "Segoe", true);

		this.attachFont(this.rootCanvas.findName("GetTrial_roll"), null, "fontSegoe", "Segoe", true);
		this.attachFont(this.rootCanvas.findName("GetTrial_white"), null, "fontSegoe", "Segoe", true);
		this.attachFont(this.rootCanvas.findName("GetTrial_click"), null, "fontSegoe", "Segoe", true);

		// begin animations
		this.rootCanvas.findName("animation_wmv").play();
        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();
    },
    registerButton : function(button, overAction, outAction, downAction, releaseAction) {
        button.addEventListener("MouseEnter", Silverlight.createDelegate(this, overAction));
        button.addEventListener("MouseLeave", Silverlight.createDelegate(this, outAction));
        button.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, downAction));
        button.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, releaseAction));
    },
    onTrial_Over : function(sender, eventArgs) { this.rootCanvas.findName("RollOver").begin(); },
    onTrial_Out : function(sender, eventArgs) { this.rootCanvas.findName("RollOut").begin(); },
    onTrial_Down : function(sender, eventArgs) { this.rootCanvas.findName("Click").begin(); },
	onTrial_Release : function(sender, eventArgs) {
		this.rootCanvas.findName("Up").begin();
		document.location = this.getATrialLink;
	},
	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;
	}
}