/****************************************************************
*  Copyright notice
*  
*  (c) 2006 Sintek Technology s.r.l.
*  All rights reserved
*  
*****************************************************************/

// Array globale per la gestione delle News
var rssNewsHome = new Array();

/***************************************************
	Cambia al volo il Feed RSS
***************************************************/
function cambiaFeed(idFeed) {
	clearTimeout(idTimer);
	document.getElementById('newsFromRSS').innerHTML = '';
	document.getElementById('listaFeedRSS').style.display = 'none';

	// Setta un cookie con il feed selezionato
	setCookie('feedRSS', idFeed, 'Thu, 01-Jan-2020 00:00:01 GMT');

	// Carica il nuovo Feed
	caricaFeed(idFeed, titoloFeed);
}

function closeListaFeed() {
	document.getElementById('listaFeedRSS').style.display = 'none';
}

/***************************************************
	Carica l'Array con le News dal Feed RSS
***************************************************/
function caricaFeed(idFeed) {
	// Verifica se è settato un cookie per il feed RSS selezionato in precedenza
	if ( cookievalue = getCookie('feedRSS') ) {
		idFeed = cookievalue;
	}

	// Visualizza il titolo del Feed
	titoloFeed = '';
	switch ( idFeed ) {
		case '1':
			titoloFeed = 'Punto Informatico';
		break;
		case '2':
			titoloFeed = 'Corriere.it';
		break;
		case '3':
			titoloFeed = 'Slashdot';
		break;
		case '4':
			titoloFeed = 'Repubblica.it';
		break;
		case '5':
			titoloFeed = 'Il Sole 24 Ore';
		break;
		case '6':
			titoloFeed = 'Google News';
		break;
		case '7':
			titoloFeed = 'GoMarche.it';
		break;

		default:
			titoloFeed = 'Punto Informatico';
		break;
	}
	document.getElementById('titoloFeedRSS').innerHTML = titoloFeed + ': ';

	// oggetto per la richiesta
	req = false;

	// branch for native XMLHttpRequest object
	if ( window.XMLHttpRequest ) {
		try {
			req = new XMLHttpRequest();
		} catch(e) {
			req = false;
		}
		// branch for IE/Windows ActiveX version
	} else if ( window.ActiveXObject ) {
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				req = false;
			}
		}
	}

	// Carica il Feed RSS
	if ( req ) {
		req.open("GET", "../feed_rss.php?idFeed=" + idFeed, true);

		req.onreadystatechange = function() {
			if ( req.readyState == 4 && req.status == 200 ) {

				var xmlDoc = req.responseXML;
				var root_node;
				var i = 0;

				// Visualizza il titolo del Feed
//				document.getElementById('titoloFeedRSS').innerHTML = xmlDoc.getElementsByTagName('channel').item(0).getElementsByTagName('title').item(0).firstChild.data + ':';

				// Reinizializza l'array
				rssNewsHome = new Array();

				while ( root_node = xmlDoc.getElementsByTagName('item').item(i) ) {
					rssNewsHome[i]  = '<a href="' + root_node.getElementsByTagName('link').item(0).firstChild.data + '" target="_blank">';
					rssNewsHome[i] += root_node.getElementsByTagName('title').item(0).firstChild.data;
					rssNewsHome[i] += '</a>';

					i++;
				}

				// Visualizza la prima news
				flash(-1, 50);
			}
		}
		req.send(null);
	}
}


// Variabili globali per immagazzinare l'indice della news e l'idTimer
var indexNewsSave = 0;
var idTimer;

/***************************************************
	Cicla le News dall'Array news
***************************************************/
function changeNews(idx) {
	if ( (idx + 2) > rssNewsHome.length ) {
		idx = -1;
	}
	indexNewsSave = idx;
	clearTimeout(idTimer);
	idTimer = setTimeout("flash(" + idx + ", 100)", 3000);
}

function mouseOverRSSNews() {
	clearTimeout(idTimer);
	cambiaOpacita(100, 'newsFromRSS');
}

function mouseOutRSSNews() {
	changeNews(indexNewsSave);
}

function flash(idx, opac) {
	if (opac > 0) {
		opac -= 10;
		cambiaOpacita(opac, 'newsFromRSS');
		clearTimeout(idTimer);
		idTimer = setTimeout("flash(" + idx + ", " + opac + ")", 10);
	} else {
		document.getElementById('newsFromRSS').innerHTML = rssNewsHome[idx + 1];
		fadeIn(0, idx);
	}
}

function fadeIn(opac, idx) {
	if (opac < 100) {
		opac += 10;
		cambiaOpacita(opac, 'newsFromRSS');
		clearTimeout(idTimer);
		idTimer = setTimeout("fadeIn(" + opac + ", " + idx + ")", 10);
	} else {
		changeNews(idx + 1);
	}
}

// Cambia l'opacità del layer
function cambiaOpacita(opac, layer) {
	var obj = document.getElementById(layer).style;

	obj.opacity = (opac / 100);
	obj.filter = "alpha(opacity=" + opac + ")";
	obj.MozOpacity = (opac / 100);
	obj.KhtmlOpacity = (opac / 100);
}


function apriListaFeed() {
	document.getElementById('listaFeedRSS').style.display = '';
}




/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, expires, path, domain, secure) {
	document.cookie= name + "=" + escape(value) +
		((expires) ? "; expires=" + expires : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name) {
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	} else {
		begin += 2;
	}
	var end = document.cookie.indexOf(";", begin);
	if (end == -1) {
		end = dc.length;
	}
	return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain) {
	if (getCookie(name)) {
		document.cookie = name + "=" +
			((path) ? "; path=" + path : "") +
			((domain) ? "; domain=" + domain : "") +
			"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}
