cms.loader =
{
	queue : new Array(),
	queueInvoked : false,

	enqueue : function()
	{
		if (this.queueInvoked)
		{
			this.runQueueItem(this.enqueue.arguments);
		}
		else
		{
			this.queue.push(this.enqueue.arguments);
		}
	},
	
	runQueue : function()
	{
		if (!this.queueInvoked)
		{
			this.queueInvoked = true;

			var queueItem;
			while(queueItem = this.queue.pop())
			{
				this.runQueueItem(queueItem);
			}
		}
	},
	
	runQueueItem : function(queueItem)
	{
		var arguments = new Array();
		for (iQueueItem = 2 ; iQueueItem < queueItem.length ; iQueueItem++)
		{
			arguments.push(queueItem[iQueueItem]);
		}
		thisKeyword = queueItem[1];
		if (thisKeyword == undefined)
		{
			thisKeyword = this;
		}
		queueItem[0].apply(thisKeyword, arguments);
	},
	
	initialize : function()
	{
		cms.event.attach(window, "load", this.runQueue, this);
		switch (cms.browser.getBrowser())
		{
			case cms.browser.MSIE:
				document.write("<script type=\"text/javascript\" id=\"contentLoaded\" defer=\"defer\" src=\"javascript:void(0)\"></script>");
				cms.event.attach(document.getElementById("contentLoaded"), "readystatechange", function() {
					if (document.readyState == "complete")
					{
						this.runQueue();
					}
				}, this);
				break;
			case cms.browser.SAFARI:
				 cms.interval.start(function() {
					if (/loaded|complete/.test(document.readyState))
					{
						this.runQueue();
						return false;
					}
					return true;
				}, this, 10);
				break;
			default:
				cms.event.attach(document, "DOMContentLoaded", this.runQueue, this);
				break;
		}
	}
}
cms.loader.initialize();