
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;
}

function GetHtmlImageGeneric(imgWidth, imgHeight) {
    var htmlImage = "<div class=\"ImgAlternative\" style=\""
    try {
        if (imgWidth != null) {            
            htmlImage += " width:" + parseFloat(imgWidth) + "px;";
        }
        if (imgHeight != null) {
            htmlImage += " height:" + parseFloat(imgHeight) + "px;";
        }
    }
    catch (e) { }
    return htmlImage + "\">&nbsp;</div>";
}

// banner aggregate element class constructor
function bannerElement(Id, aggId, Order, strHtml, width, height)
{
    this.Id = Id;
    this.Width = width;
    this.Height = height;
    if (!FlashDetect.installed && (strHtml.match(/clsid:D27CDB6E-AE6D-11cf-96B8-444553540000/i) != null)) {
        this.strHtml = GetHtmlImageGeneric(width, height);
    }
    else {
        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)
{
    var result;
	if (a1.Order == a2.Order) {
		result = 0;
	}
	else if(a1.Order < a2.Order) {
		result = -1;
	}
	else {
	    result = 1;
	}
	
	return result;
}
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 OpenXSellingLink(aggId, contentId) {
    var oAgg = GetAggregate(aggId);
    var index = contentId.substring(3,4)-1;    
    window.location = oAgg.ItemList[index].Link;	    
}

function onMouseOverXSellingLink(aggId, contentId) {
    var oAgg = GetAggregate(aggId);
    var index = contentId.substring(3,4)-1;    
    window.status = oAgg.ItemList[index].Link;	    
}

function CancelAllEvents(e) {
    e.cancelBubble=true;
    if (e.stopPropagation) {
        e.stopPropagation();   
    }
}

function DrawXSelling(aggId) {
    
	if (document.all != null && (document.body.readyState!=null && document.body.readyState != 'complete')) 
	{
		setTimeout('DrawXSelling(' + aggId + ');', 1000 , 'JavaScript');
		return;
	}
	
	var oAgg = GetAggregate(aggId);

	if (oAgg.ItemList != null && oAgg.ItemList.length > 0) {
		        
	    for (var f = 0; f < oAgg.maxItems; f++)
	    {	    
		    var aObj = document.getElementById('PH_' + (f+1) + '_' + oAgg.Id);	    
	        if (aObj != null) {			
		        var AggItem = oAgg.ItemList[f];
		        if (AggItem.Link+''!='') {			                    	            
			        aObj.onclick = function(){OpenXSellingLink(aggId, this.id);};			                							
			        aObj.onmouseover = function(){onMouseOverXSellingLink(aggId, this.id);};
			        aObj.onmouseout = function(){window.status='';};
			        aObj.style.cursor = 'pointer';
    			    
		        }			
	        }
    		
		    aObj = document.getElementById('A_' + (f+1) + '_' + oAgg.Id);
		    if (aObj != null) {
			    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';
			    }
		    }
		    aObj = document.getElementById('DESC_' + (f+1) + '_' + oAgg.Id);
		    if (aObj != null)
		    {
			    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';
			    }
		    }
    				
		    aObj = document.getElementById('PH_' + (f+1) + '_' + oAgg.Id);	    
	        if (aObj != null) {
	            var aObjects = aObj.getElementsByTagName('A');
	            for (var i = 0; i < aObjects.length; i++) {	            	            
	                if (aObjects[i].addEventListener) {
                        aObjects[i].addEventListener ("click",CancelAllEvents,false);                    
                    } else if (aObjects[i].attachEvent) {
                        aObjects[i].attachEvent ("onclick",CancelAllEvents);                    
                    } else {
                        aObjects[i].onclick = CancelAllEvents;                    
                    }	            	            
	            }
	        }
	    }
    	
	    for (f = 0; f < 1000; f++)
	    {
		    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 ? 'block':'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) 
{

	var oAgg = GetAggregate(aggId);
	
	if (oAgg.ItemList != null && oAgg.ItemList.length > 0) {
		
	    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');
	    }
	}
}

