/** Settings Banner Rotator**/

var settings = {
 'force_size': 1,										// Se impostato a 1 tutti i banner saranno ridimensionati alle dimensioni impostate successivamente
 'img_width': 60,										// larghezza di ridimensionamento per tutti i banner a (attiva SOLO se force_size=1)
 'img_height': 75,									// altezza di ridimensionamento per tutti i banner a (attiva SOLO se force_size=1)
 'refresh_time': 20000,							// secondi per il refresh dei banner (0 per disabilitare)
 'refresh_max': 100,								// numero massimo di refreshs per ogni caricamento della pagina
 'duplicate_banners': 0,						// Impostare a 0 per accertarsi che lo stess0 banner non verrà visualizzato sulla stessa pagina
																		// ha effetto solo se show_banners (); è usato più di una volta.
																		// È necessario assicurarsi di avere banner sufficienti per riempire tutti gli slot altrimenti il browser
																		// potrebbe bloccarsi o dare un errore di stack overflow
 'location_prefix': 'adLocation-',	// Il prefisso degli ID per i <div> che contengono i banner - questo <div> viene generato dinamicamente.
																		// Verrà aggiunto un numero alla fine di questa stringa.
																		// adLocation- è stato utilizzato di default prima versione 1.4.x
 'location_class': 'swb',						// Classe da aggiungere a tutte i <div> che contengono i banner, ideale per CSS - usare .swb img nel CSS
 'window': '_self',									// Finestra destinazione dei link, _self=corrente, _blank=new, _top usare se in un frame
 'default_ad_loc': 'default'				// L'impostazione predefinita adLocation-. Viene assegnato ad ogni banner non dotato di un adLocation nella
																		// lista dei banner. Non è necessario modificare questo valore
}

/** Banners **/
// Sintassi lista banner: nuovo banner "website_name, website_url, banner_url, show_until_date, adlocation", (formato data: gg/mm/aaaa)
// Se non si sta usando adlocations basta lasciare vuoto il campo '' come nell'ultimo esempio
// Per assicurarsi che un banner rimanga sempre in rotazione, è sufficiente impostare una data lontana nel tempo, es anno 3000

var banners = [
 new banner('Benessere e Cambiamento in Azienda', 'http://www.e-consultant.it/collana_editoriale/libro_benessere_cambiamento_azienda.php', '../_file/image/book/book_benessere.jpg', '31/12/3000', ''),
 new banner('Relazione e Comunicazione Efficace', 'http://www.e-consultant.it/collana_editoriale/libro_relazione_comunicazione_efficace.php', '../_file/image/book/book_comunicazione_efficace.jpg', '31/12/3000', ''),
 new banner('Creativit&agrave; e Sviluppo Personale', 'http://www.e-consultant.it/collana_editoriale/libro_creativita_sviluppo_personale.php', '../_file/image/book/book_creativa_sviluppo.jpg', '31/12/3000', ''),
 new banner('Il Manager del Cambiamento', 'http://www.e-consultant.it/collana_editoriale/libro_manager_cambiamento.php', '../_file/image/book/book_manager_cambiamento.jpg', '31/12/3000', ''),
 new banner('Marketing e Innovazione', 'http://www.e-consultant.it/collana_editoriale/libro_marketing_innovazione.php', '../_file/image/book/book_marketing_innovativo.jpg', '31/12/3000', ''),
 new banner('Performance e Saving in Produzione', 'http://www.e-consultant.it/collana_editoriale/libro_performance_saving_produzione.php', '../_file/image/book/book_performance.jpg', '31/12/3000', ''),
 new banner('Potenziare la Propria Professionalit&agrave;', 'http://www.e-consultant.it/collana_editoriale/libro_potenziare_propria_professionalita.php', '../_file/image/book/book_potenziare.jpg', '31/12/3000', ''),
 new banner('Problem Solving Relazionale', 'http://www.e-consultant.it/collana_editoriale/libro_problem_solving_relazionale.php', '../_file/image/book/book_problem_solving.jpg', '31/12/3000', ''),
 new banner('Sviluppo e Gestione della Qualit&agrave; in Azienda', 'http://www.e-consultant.it/collana_editoriale/libro_sviluppo_gestione_qualita_azienda.php', '../_file/image/book/book_sviluppo_qualita.jpg', '31/12/3000', ''),
 new banner('Tecniche di Vendita e Negoziazione', 'http://www.e-consultant.it/collana_editoriale/libro_tecniche_vendita_negoziazione.php', '../_file/image/book/book_tec_vendita.jpg', '31/12/3000', ''),
 new banner('Sviluppo e Gestione della Qualit&agrave; in Azienda', 'http://www.e-consultant.it/collana_editoriale/libro_sviluppo_gestione_qualita_azienda.php', '../_file/image/book/book_sviluppo_qualita.jpg', '31/12/3000', ''),
 new banner('Libro Windows 7 la tua prima guida', 'http://www.e-consultant.it/collana_editoriale/libro_windows_7_la_tua_prima_guida.php', '../_file/image/book/book_windows7.jpg', '31/12/3000', ''),
 new banner('Meditazione e autoipnosi contemplativa', 'http://www.e-consultant.it/collana_editoriale/libro_meditazione_autoipnosi_contemplativa.php', '../_file/image/book/book_meditazione.jpg', '31/12/3000', '')
]

// Scripting - There is no need to edit below here
//         				There is no need to edit below here
///////////////////////////////////////////////////////////////////////////////////

/** "global" vars **/
var used = 0;
var location_counter = 0;
var refresh_counter = 1;
var map = new Array();


/*** function banner() creates a banner object **/
function banner(name, url, image, date, loc) {
	this.name	= name;
	this.url = url;
	this.image = image;
	this.date	= date;
	this.active = 1;
	this.oid = 0;
	
	// if no adlocation is given use the default a adlocation setting
	// this is used if adlocations aren't being used or using pre-1.4.x code
	if(loc != '') {
		this.loc = loc;
	} else {
		this.loc = settings.default_ad_loc;
	}
}


/** function show_banners() writes banner <div> HTML and maps ad locations to <div> ID tags **/
function show_banners(banner_location) {
	// increase the counter ready for further calls
	location_counter = location_counter + 1;
	// this part maps the adlocation name supplied by the user to the adlocation ID used by the script
	if(banner_location != '' && banner_location != undefined) {
		map[location_counter] = banner_location;
	} else {
		map[location_counter] = settings.default_ad_loc;
	}
	// writes banner html
	var html = '<div id="' + settings.location_prefix + location_counter + '" class="' + settings.location_class + '"></div>';
	document.write(html);
	// calls the display banners script to fill this ad location
	display_banners(location_counter);
}

/*************
	function display_banners()
	displays banners for a given location number
*************/
function display_banners(location)
{
	// used in this function to hold tempoary copy of banners array
	var location_banners	= new Array();
	
	// if no location is given, do nothing
	if(location == '' || !location || location < 0)
	{
		return;
	}
	
	// get total banners
	var am	= banners.length;
	
	// all banners have been displayed in this pass and the user doesnt
	// want to have duplicate banners showing
	if((am == used) && settings.duplicate_banners == 0) {
		return;
	}

	// new for 1.4.x, this takes the list of banners and creates a tempoary list
	// with only the banners for the current adlocation in
	for(i = 0; i < (banners.length); i++)
	{
		banners[i].oid = i;
		if((banners[i].loc == map[location]) && (banners[i].active == 1))
		{
			location_banners.push(banners[i]);
		}
	}

	// same as 1.2.x - finds the banner randomly
	var rand	= Math.floor(Math.random()*location_banners.length);	
	var bn 		= location_banners[rand];
	
	// creates html
	var image_size 	= (settings.force_size == 1) ? ' width="' + settings.img_width + '" height="' + settings.img_height + '"' : '';
	var html 		= '<a href="' + bn.url + '" title="' + bn.name + '" target="' + settings.window + '"><img border="0" hspace="5" vspace="0" align="left" src="' + bn.image + '"' + image_size + ' alt="' + bn.name + '" /><b>' + bn.name + '</b></a>';
	
	// calculates the date from inputted string, expected formate is DD/MM/YYYY
	var now		= new Date(); 
	var input	= bn.date;
	input		= input.split('/', 3);
	
	// creates a date object with info
	var end_date	= new Date();
	end_date.setFullYear(parseInt(input[2]), parseInt(input[1]) - 1, parseInt(input[0]));
	
	// compares curent date with banner end date
	if((now < end_date) && bn.active == 1) 
	{
		// attempt to find adlocation div
		var location_element = document.getElementById(settings.location_prefix + location);
		
		// couldn't find it, if this message shows there is a problem with show_banners
		if(location_element == null)
		{
			alert('GervaWeb - Webmaster banner rotator\nError: adLocation doesn\'t exist!');
		}
		// output banner HTML
		else
		{
			location_element.innerHTML = html;
			
			// if the user doesn't want the same banner to show again deactive it and increase
			// the users banners counter
			if(settings.duplicate_banners == 0)
			{
				banners[bn.oid].active = 0;
				used++;
			}
			return;
		}
	}
	else
	{
		// inactive banner, find another
		// if no banners fit this adlocation you'll have an endless loop !
		display_banners(location);
	}
	return;
}

/**	function refresh_banners() resets counters and active settings **/
function refresh_banners() {
	if((refresh_counter == settings.refresh_max) || settings.refresh_time < 1) {
		clearInterval(banner_refresh);
	}
	used = 0;
	for(j = 0; j < (banners.length); j++) {
		banners[j].active = 1;
	}
	for(j = 1; j < (location_counter+1); j++) {
		display_banners(j);
	}
	refresh_counter++;
}

/** set timeout **/
var banner_refresh = window.setInterval(refresh_banners, settings.refresh_time);
