/*
* AdManager class
* @author: Marco Hoeck <marco.hoeck@tvi-services.de>
* @copyright	TV Information Services 2007
* @since		20.09.2007 - 10:00:00
*/

/*
* AdManager
*/
function AdManager()
{
	this.timeout = 0;
	this.lastRefreshDate = new Date(0);
	this.ads = new Array();
	this.adTypes = new Array();

	this.addType = function(type, code)
	{
		var adType = new AdManagerType();
		adType.type = type;
		adType.code = code;

		this.adTypes.push(adType);
	}

	this.addAd = function(type, node)
	{
		var ad = new AdManagerAd();
		ad.node = node;
		ad.type = type;

		this.ads.push(ad);
		return this.ads.length - 1;
	}

	this.removeAdByIndex = function(index)
	{
		if(this.ads.length < index)
			return false;

		this.ads.splice(index, 1);
		return true;
	}

	this.refresh = function(forced)
	{
		if(!forced)
			forced = false;

		var now = new Date();
		var timeDiff = now.getTime() - this.lastRefreshDate.getTime();
		if(forced == false && timeDiff < this.timeout)
			return;

		for(var i = 0; i < this.ads.length; i++)
		{
			if(this.ads[i].node)
			{
				var htmlCode = '&nbsp;';
				for(var j = 0; j < this.adTypes.length; j++)
				{
					if(this.adTypes[j].type == this.ads[i].type)
						htmlCode = this.adTypes[j].code;
				}
				this.ads[i].node.innerHTML = '<span class="ajax-scriptfix">&nbsp;</span>' + htmlCode;
				document.runScriptTags(this.ads[i].node);
			}
		}

		this.lastRefreshDate = now;
	}
}

/*
* AdManagerAd
*/
function AdManagerAd()
{
	this.node = null;
	this.type = null;
}

/*
* AdManagerType
*/
function AdManagerType()
{
	this.type = null;
	this.code = null;
}