/*
 * contentLoader.js
 *
 * Get the latest version from:
 *
 * http://www.jsclasses.org/fast-content-loader
 *
 * @(#) $Id: contentLoader.js,v 1.9 2010/10/21 09:48:38 mlemos Exp $
 *
 *
 * This LICENSE is in the BSD license style.
 * *
 * Copyright (c) 2010, Manuel Lemos
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 *   Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 *   Neither the name of Manuel Lemos nor the names of his contributors
 *   may be used to endorse or promote products derived from this software
 *   without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

if(typeof(ML) === 'undefined')
	var ML = {};
if(typeof(ML.content) === 'undefined')
	ML.content = {};
ML.content.contentLoader = function()
{
	var doNotRemoveThisGetTheLatestVersionFrom = 'http://www.jsclasses.org/fast-content-loader';
	var content = [];
	var update = null;

	this.debug = false;
	this.defaultInline = false;
	this.updateInterval = 10;
	this.contentPrefix = 'con';
	this.delayedPrefix = 'del';
	this.delayedContent = '';

	var outputDebug = function(o, message)
	{
		if(o.debug)
		{
			if(console
			&& console.log)
				console.log(message);
			else
				alert(message);
		}
		return false;
	}
	
	var replaceContent = function(o)
	{
		var remaining = 0;

		for(var c in content)
		{
			if(!content[c].loaded)
			{
				var delayed = document.getElementById(content[c].delayed);
				var place = document.getElementById(content[c].id);
				if(delayed
				&& place)
				{
					delayed.parentNode.removeChild(delayed);
					place.parentNode.replaceChild(delayed, place);
					delayed.style.display = (content[c].inline ? 'inline' : 'block');
					content[c].loaded = true;
				}
				else
					++remaining;
			}
		}
		if(remaining == 0)
		{
			window.clearInterval(update);
			update = null;
		}
		return remaining;
	}

	this.addContent = function(properties)
	{
		if(!properties.content)
			return outputDebug(this, 'Content properties are missing');
		properties.id = this.contentPrefix + content.length;
		properties.delayed = this.delayedPrefix + content.length;
		if(typeof(properties.inline) === 'undefined')
			properties.inline = this.defaultInline;
		if(window.opera
		|| navigator.userAgent.indexOf('MSIE') != -1)
		{
			properties.loaded = true;
			document.write(properties.content);
		}
		else
		{
			properties.loaded = false;
			document.write('<div id="' + properties.id + '" style="' + (properties.width ? 'width: ' + properties.width + 'px;' : '') + (properties.height ? ' height: ' + properties.height + 'px;' : '') + ' overflow: none; display: ' + (properties.inline ? 'inline' : 'block') + '">' +  this.delayedContent + '</div>');
		}
		content[content.length] = properties;
		return true;
	}

	this.loadContent = function()
	{
		for(var c in content)
		{
			if(!content[c].loaded)
				document.write('<div id="' + content[c].delayed + '" style="' + (content[c].width ? 'width: ' + content[c].width + 'px;' : '') + (content[c].height ? ' height: ' + content[c].height + 'px;' : '') + ' overflow: none; display: none">' + content[c].content + '</div>');
		}
		if(replaceContent(this)
		&& update == null)
		{
			var o = this;
			update = window.setInterval(function()
			{
				replaceContent(o);
			}, this.updateInterval);
		}
	}
}

