
var Aggregates = new Array();
function AddAggregate(oAgg)
{
	Aggregates[Aggregates.length] = oAgg;
	return oAgg;
}
function GetAggregate(aggId)
{
	for (var f = 0; f < Aggregates.length; f++)
	{
		if (Aggregates[f].Id == aggId)
			return Aggregates[f];
	}
	return null;
}
// xselling aggregate element class constructor
function xsElement(Id, aggId, Title, Desc, Order, Link)
{
	this.Id = Id;
	this.Title = Title;
	this.Desc = Desc;
	this.Order = Order;
	this.Link = Link;
	this.aggId = aggId;
}
// banner aggregate element class constructor
function bannerElement(Id, aggId, Order, strHtml)
{
	this.Id = Id;
	this.strHtml = strHtml;
	this.Order = Order;
	this.aggId = aggId;
}
// aggregate class 
function AddAggregateElement(oAggElement)
{
	this.ItemList[this.ItemList.length] = oAggElement;
	return oAggElement;
}
// constructor
// AggType: 1 - xselling; 2 - banner; 3 - cpage
function Aggregate(Id, AggType, RotationTime_ms, Algorithm, maxItems)
{
	this.Id = Id;
	this.AggType = AggType;
	this.CurrentIndex = -1;
	this.RotationTime_ms = RotationTime_ms;
	this.Algorithm = Algorithm;
	this.maxItems = maxItems;
	this.ItemList = new Array();	
	this.Add = AddAggregateElement;
}

// sort aux function
function sortAggElements(a1, a2)
{
	if (a1.Order == a2.Order)
		return 0;
	else 
		return (a1.Order < a2.Order ? -1 : 1);
}
function AggList_Render(oAgg)//ItemList, maxItems, Algorithm, aggId)
{
	if (oAgg.Algorithm == 2) // random
	{
		// shuffle array
		for (var f = 0; f < 100; f++)
		{
			var ix1 = Math.floor(Math.random()*(oAgg.ItemList.length));
			var ix2 = Math.floor(Math.random()*(oAgg.ItemList.length));
			var tmpItem = oAgg.ItemList[ix1];
			oAgg.ItemList[ix1] = oAgg.ItemList[ix2];
			oAgg.ItemList[ix2] = tmpItem;
		}
	}
	else // sequential or simple, therefore order array
	{
		oAgg.ItemList.sort(sortAggElements);
	}
	
	// for xSellings
	if (oAgg.AggType == 1)
	{
		DrawXSelling(oAgg.Id);
	}
	// for banners
	if (oAgg.AggType == 2)
	{
		DrawBanner(oAgg.Id);
	}
}

function DrawXSelling(aggId) 
{
	if (document.all != null && document.body.readyState != 'complete') 
	{
		setTimeout('DrawXSelling(' + aggId + ');', 1000 , 'JavaScript');
		return;
	}
	
	var oAgg = GetAggregate(aggId);

	if (oAgg.ItemList == null)
		return;
	if (oAgg.ItemList.length == 0)
		return;
		
	for (var f = 0; f < oAgg.maxItems; f++)
	{
		var aObj = document.getElementById('A_' + (f+1) + '_' + oAgg.Id);
		if (aObj != null)
		{
			var AggItem = oAgg.ItemList[f];
			if (AggItem.Link+''!='')
				aObj.href = AggItem.Link;
			aObj.innerHTML = AggItem.Title;
			if (document.all == null)
			{
				aObj.style.visibility = 'visible';
				aObj.style.position = 'relative';
				aObj.style.left = '0px';
				aObj.style.top = '0px';
			}
			else
			{
				aObj.style.display = 'inline';
			}
		}
		var aObj = document.getElementById('DESC_' + (f+1) + '_' + oAgg.Id);
		if (aObj != null)
		{
			var AggItem = oAgg.ItemList[f];
			aObj.innerHTML = AggItem.Desc;
			if (document.all == null)
			{
				aObj.style.visibility = 'visible';
				aObj.style.position = 'relative';
				aObj.style.left = '0px';
				aObj.style.top = '0px';
			}
			else
			{
				aObj.style.display = 'inline';
			}
		}
	}
	for (var f = 0; f < 1000; f++)
	{
		var aObj = document.getElementById('PH_' + (f+1) + '_' + oAgg.Id);
		if (aObj != null)
		{
			if (document.all == null)
			{
				aObj.style.visibility = (f < oAgg.maxItems ? 'visible':'hidden');
				aObj.style.position = (f < oAgg.maxItems ? 'relative':'absolute');
				aObj.style.left = '0px';
				aObj.style.top = '0px';
			}
			else
			{
				aObj.style.display = (f < oAgg.maxItems ? 'inline':'none');
			}
			
			aObj = document.getElementById('A_' + (f+1) + '_' + oAgg.Id);
			if (aObj != null)
			{
				if (document.all == null)
				{
					aObj.style.visibility = (f < oAgg.maxItems ? 'visible':'hidden');
					aObj.style.position = (f < oAgg.maxItems ? 'relative':'absolute');
					aObj.style.left = '0px';
					aObj.style.top = '0px';
				}
				else
				{
					aObj.style.display = (f < oAgg.maxItems ? 'inline':'none');
				}
			}
			
			aObj = document.getElementById('DESC_' + (f+1) + '_' + oAgg.Id);
			if (aObj != null)
			{
				if (document.all == null)
				{
					aObj.style.visibility = (f < oAgg.maxItems ? 'visible':'hidden');
					aObj.style.position = (f < oAgg.maxItems ? 'relative':'absolute');
					aObj.style.left = '0px';
					aObj.style.top = '0px';
				}
				else
				{
					aObj.style.display = (f < oAgg.maxItems ? 'inline':'none');
				}
			}
		}
		else
			break;
	}
}

function DrawBanner(aggId) 
{
/*
	if (document.all != null && document.body.readyState != 'complete') 
	{
		setTimeout('DrawBanner(' + aggId + ');', 1000 , 'JavaScript');
		return;
	}
*/	
	var oAgg = GetAggregate(aggId);
	if (oAgg.ItemList == null)
		return;
	if (oAgg.ItemList.length == 0)
		return;

	if (oAgg.CurrentIndex < 0)
		oAgg.CurrentIndex = oAgg.CurrentIndex + oAgg.ItemList.length;
	
	if (oAgg.CurrentIndex >= oAgg.ItemList.length)
		oAgg.CurrentIndex = oAgg.CurrentIndex - oAgg.ItemList.length;
	
	// hide previous banner
	if (oAgg.CurrentIndex >= 0)
	{
		var oBannerPH_Prev = document.getElementById('PH_' + oAgg.CurrentIndex + '_' + aggId);
		if (oBannerPH_Prev != null)
		{
			if (document.all == null)
			{
				oBannerPH_Prev.style.visibility = 'hidden';
				oBannerPH_Prev.style.position = 'absolute';
				oBannerPH_Prev.style.left = '0px';
				oBannerPH_Prev.style.top = '0px';
			}
			else
			{
				oBannerPH_Prev.style.display = 'none';
			}
		}
	}
	
	if (oAgg.Algorithm == 2) // random
	{
		if (oAgg.ItemList.length == 1)
			oAgg.CurrentIndex = 0;
		else
		{
			var tmpIx = oAgg.CurrentIndex;
			while (tmpIx == oAgg.CurrentIndex)
				oAgg.CurrentIndex = Math.floor(Math.random()*(oAgg.ItemList.length));
		}
	}
	else
		oAgg.CurrentIndex++;
		
	if (oAgg.CurrentIndex >= oAgg.ItemList.length)
		oAgg.CurrentIndex = oAgg.CurrentIndex- oAgg.ItemList.length;

	// show next banner
	var oBannerPH_Current = document.getElementById('PH_' + oAgg.CurrentIndex + '_' + aggId);
	if (oBannerPH_Current.innerHTML == '')
	{
		oBannerPH_Current.innerHTML = oAgg.ItemList[oAgg.CurrentIndex].strHtml;
	}
	else
	{
		if (oBannerPH_Current.childNodes.length > 0)
		{
			var oSWF = oBannerPH_Current.childNodes[0];
			if (oSWF.tagName.toLowerCase() == 'object')
			{
				try
				{
					oSWF.stop();
					oSWF.rewind();
					oSWF.play();
				}
				catch (e) {}
			}
		}
	}
	
	if (document.all == null)
	{
		oBannerPH_Current.style.visibility = 'visible';
		oBannerPH_Current.style.position = 'relative';
		oBannerPH_Current.style.left = '0px';
		oBannerPH_Current.style.top = '0px';
	}
	else
	{
		oBannerPH_Current.style.display = 'inline';
	}

	if (oAgg.RotationTime_ms > 0 && oAgg.Algorithm != 1) // don't rotate when Algorithm == "simple" or when the timer is 0
		setTimeout('DrawBanner(' + aggId + ');', oAgg.RotationTime_ms, 'JavaScript');
}

