﻿// simple compatability layer to allow Live.com web gadgets to run on Sidebar
//
// by Donavon West of LiveGadgets.net


//write our own version of registerNamespace
function registerNamespace(ns){
    var base=window;
    var nss=ns.split(".");
    for (var i=0; i < nss.length; i++){
	    if (!base[nss[i]]){
		    base[nss[i]]={};
	    }
	    base=base[nss[i]];
    }
}

//stub out the Altas inheritence functions
Function.prototype.registerBaseMethod=function(){};
Function.prototype.initializeBase=function(){};
Function.prototype.registerClass=function(){};
Function.prototype.getBaseMethod=function(){
  return function() {
    return;
  };
};

//create a singleton UpTheArse.WebGadgetEmulation class
//containing a single onPageLoad method
registerNamespace("UpTheArse");
UpTheArse.WebGadgetEmulation = new function() {

	this.onPageLoad = function() {

		var tmp, app, appEl, AppFn;

		//this is called as the page is unloading
		function onPageUnload() {
			//call the gadget's dispose() function
			try {app.dispose(true);} catch(ex){}
			window.detachEvent("onunload", onPageUnload);
			app = appEl = AppFn = tmp = null; //clean up
			UpTheArse.WebGadgetEmulation = null;
		}

		//this is where our gadget will "live"
		appEl = document.getElementById("application");

		//transfer the background image, height and width
		//from the application DIV to the body of the HTML
		tmp = appEl.currentStyle.backgroundImage;
		appEl.style.backgroundImage = "none";
		document.body.style.backgroundImage = tmp;
		tmp = appEl.currentStyle.height;
		document.body.style.height = tmp;
		tmp = appEl.currentStyle.width;
		document.body.style.width = tmp;
		
		//create an instance of the gadget
		AppFn=eval(appEl.className.replace(/_/g,"."));
		
		//app = new UpTheArse.SpursClock(appEl, {}, null);
		app = new AppFn(appEl, {}, null);
		
		//call the gadget's initialize() function
		app.initialize(null);	

		window.detachEvent("onload", UpTheArse.WebGadgetEmulation.onPageLoad);
		window.attachEvent("onunload", onPageUnload); //so we can tear down
	};
};

window.attachEvent("onload", UpTheArse.WebGadgetEmulation.onPageLoad);
