/*
 * (c)2006 Jesse Skinner/Dean Edwards/Matthias Miller/John Resig
 * Special thanks to Dan Webb's domready.js Prototype extension
 * and Simon Willison's addLoadEvent
 */

var spirWidgetHostname = 'widget.spir.fr';

spirAddDOMLoadEvent = (function(){
    // create event function stack
    var load_events = [],safari_timer,done,exec,script;

 	var init = function () {
 			done = true;

	        // kill the timer
	        clearInterval(safari_timer);

	        // execute each function in the stack in the order they were added
	        while (exec = load_events.shift())
	        exec();

	        if (script) script.onreadystatechange = '';

        };

    return function (func) {
        // if the init function was already ran, just run this function now and stop
        if (done) return func();

        if (!load_events[0]) {
            // for Mozilla/Opera9
            if (document.addEventListener)
                document.addEventListener("DOMContentLoaded", init, false);

   	/*@cc_on @*/
	/*@if (@_win32)
	document.write("<script id='__ie_onload' defer src='javascript:void(0)'><\/script>");
	var script = document.getElementById("__ie_onload");
	script.onreadystatechange = function() {
  		if (this.readyState == "complete") {
    		init(); // call the onload handler
  		}
	};
	/*@end @*/

            if (/WebKit/i.test(navigator.userAgent)) { // sniff
                safari_timer = setInterval(function() {
                    if (/loaded|complete/.test(document.readyState))
                        init(); // call the onload handler
                }, 10);
            }

            var old_onload = window.onload;
            window.onload = function() {
                init();
                if (old_onload) old_onload();
            };
        }
        load_events.push(func);
    }
})();

var spirLoadWidgets = function(spirWidgets)
{
	var spirGetWidget = function(widget,wname)
	{
		var link = new Array();
		for(var key in widget)
		{
			link.push(key+'/'+widget[key]);
		}
		var script = document.createElement('script');
		script.src = "http://"+wname+"."+spirWidgetHostname+"/get/"+link.join('/');
		script.type = 'text/javascript';
		document.getElementsByTagName('head')[0].appendChild(script);
	};
	spirAddDOMLoadEvent(function(){
		if(spirWidgets !== undefined)
		{
			for(var widgetName in spirWidgets)
			{
				var widget = spirWidgets[widgetName];
				if(Object.prototype.toString.call(widget) === "[object Array]")
				{
					for (var w in widget)
					{
						spirGetWidget(widget[w], widgetName);
					}
				}
				else
				{
					spirGetWidget(widget, widgetName);
				}
			}
		}
		else
		{
			return false;
		}
	});
};
var spirLoadWidget = function(name)
{
	spirAddDOMLoadEvent(function(){
		var functionParent = spirJQuery('script:contains("spirLoadWidget(\''+name+'\')")');
		if(window.spirWidgets !== undefined && window.spirWidgets[name] !== undefined)
		{
			var widget = spirWidgets[name];
			var link = new Array();
			for(var key in widget)
			{
				link.push(key+'/'+widget[key]);
			}
			script.src = "http://"+name+"."+spirWidgetHostname+"/get/"+link.join('/');
			script.type = 'text/javascript';
			document.getElementsByTagName('head')[0].appendChild(script);
		}
		else
		{
			return false;
		}
	});
};
