/*
	File to manager banner rotation
	Author: Daniele Radogna
	Date:		08/04/2008
*/

var dstNum = 3; 					// number of the placeholders to manage
var srcNum = 13; 					// number of the banners to get
var i ;										// the major cycle index (along source)
var j = 0;								// the minor cycle index  (along destination)
var interval = 3000;			// time interval in msecs.
var lista;
var dstprefix = "drlink";	// the id prefix for destination link
var srcprefix = "drsrc";	// the id prefix for the source link

// Here starts the time sequence updating the top page banners
// Being the first 3 banners already placed atop on the page the cycle must start with the index 3

var msg 		= document.getElementById ("msg");				// DEBUG: getting the message area id
msg.innerHTML = " ";

var check = checkId();		// all the required ID are here?		
i = 5 ;										// the major cycle index (along source)
if (check.length > 0)
{	
	alert("ERRORE: id not found: " + check + " skipping banner animation");	
}
else
{	
	timeoutid = setTimeout("swapBanners()", interval);	// imposto l'intervallo 
}


/* 
	-----------------------------------------------------------------
	The core function: swap the banners
	-----------------------------------------------------------------
*/

function swapBanners()
{																				
	if (i >= srcNum) 
	{	
		i = 0;
	}							// restarting from 0 if running out of banners
	j = i % 	dstNum;												// compute the destination banner index
	
	var srcId = srcprefix + i;							// build the expected id for source banner
	var dstId	= dstprefix + j;							// build the expected id for destination

	var srs 		= document.getElementById (srcId);					// getting the source id
	var dest 		= document.getElementById (dstId);					// getting the destination  id

// Copying the source element attributes to the destination element attributes

	// msg.innerHTML	= srcId + " -- " + dstId + (srs.childNodes[0].src ? " definito" : " NON definito" );
	// dest.childNodes[0].src	= srs.childNodes[0].src;

	dest.href 			= srs.href;
	dest.title			= srs.title;	
	dest.getElementsByTagName("img")[0].src = srs.getElementsByTagName("img")[0].src;

	i++;
	timeoutid = setTimeout("swapBanners()", interval);	// setting interval
}

/* 
	-----------------------------------------------------------------
	Check if the required Id are found
	-----------------------------------------------------------------
*/
function checkId()
{		
	var output = ""
	for (i = 0; i < srcNum; i++)
	{	
		var elementId = srcprefix + i;
		var theElement 	= document.getElementById (elementId);	
		if (! objectExists(theElement)) {	output += elementId + "; ";}
	}	
	for (i = 0; i < dstNum; i++)
	{	
		var elementId = dstprefix + i;
		var theElement 	= document.getElementById (elementId);	
		if (! objectExists(theElement))  {output += elementId + "; ";}
	}	
	return output;
}

/* 
	-----------------------------------------------------------------
	Check for existence of a given object 
	-----------------------------------------------------------------
*/
function objectExists(arg)
{	
	if (arg && typeof arg != "undefined")
	{	
		return true;	
	}
	else
	{	
		return false;	
	}
}