/* Configurazione */
var numero_notizie = 4;
var cambio_notizia = 3000;

var isOpen = false;
var notizie = null;
var ocInt = null;
var currentNews = null;
var nextNews = null;
var cnInt = null;
var gnInt = null;
var loop_notizie = null;
var arr_cat = null;

function ETN_init (loop, arrCat, maxNews, changeTime) {
	
	document.getElementById('cBig').style.position = 'absolute';
	
	if(document.all != undefined){
		// Hack IE Margine
		//document.getElementById('cxLittle').style.borderTop = '1px solid white';
		document.getElementById('sxBigDown').style.marginTop = '-1px';
		document.getElementById('dxBigDown').style.marginTop = '-1px';
		document.getElementById('cBig').style.left = '128px';
	}else{
		// Firefox
		document.getElementById('cBig').style.left = document.getElementById('sxLittle').offsetLeft + 'px';
	}
	
	numero_notizie = maxNews;
	cambio_notizia = changeTime;
	arr_cat = arrCat;
	loop_notizie = new Loop();
	for(var i = 0; i < numero_notizie; i++)
		loop_notizie.push(loop.getNext());


	
	document.getElementById('sxLittle').onmouseover = function() {document.getElementById('sxImg').src = 'modules/news/images/up_on.png'};
	document.getElementById('plLittle').onmouseover = function() {isOpen ? document.getElementById('plImg').src = 'modules/news/images/meno_on.png' : document.getElementById('plImg').src = 'modules/news/images/plus_on.png'};
	document.getElementById('dxLittle').onmouseover = function() {document.getElementById('dxImg').src = 'modules/news/images/down_on.png'};
	
	document.getElementById('sxLittle').onmouseout = function() {document.getElementById('sxImg').src = 'modules/news/images/up_off.png'};
	document.getElementById('plLittle').onmouseout = function() {isOpen ? document.getElementById('plImg').src = 'modules/news/images/meno_off.png' : document.getElementById('plImg').src = 'modules/news/images/plus_off.png'};
	document.getElementById('dxLittle').onmouseout = function() {document.getElementById('dxImg').src = 'modules/news/images/down_off.png'}; 
	
	document.getElementById('plLittle').onclick = function() {ocInt = window.setInterval(ETN_openClose, 5)};
	document.getElementById('sxLittle').onclick = ETN_showPrev;
	document.getElementById('dxLittle').onclick = ETN_showNext;
	// Creo DOM notizie
	var fNews = true;
	for(i = 0; i < numero_notizie; i++){
		var obj = loop_notizie.getNext();
		ETN_writeNews(obj, 'cNews', false, false, fNews);
		fNews = false;
	}
	gnInt = window.setInterval(ETN_changeNews, cambio_notizia);
}

function ETN_showPrev(){
	if(!isOpen){
		window.clearInterval(gnInt);
		var obj = loop_notizie.getPrev();
		child = ETN_writeNews(obj, 'cNews', true, false, true);
		child.nextSibling.style.display = 'none';
		gnInt = window.setInterval(ETN_changeNews, cambio_notizia);		
	}
}

function ETN_showNext(){
	if(!isOpen){
		window.clearInterval(gnInt);
		ETN_changeNews();
		gnInt = window.setInterval(ETN_changeNews, cambio_notizia);
	}
}

function ETN_openClose() {
	document.getElementById('plLittle').onclick = function() {return;}
	window.clearInterval(gnInt);
	var elem = document.getElementById('cBig');
	var value = Number(elem.style.height.match(/^[\-]{0,1}[0-9]{1,}/));
	// 181px
	if(!isOpen){
		if(elem.style.display == 'none'){
			elem.style.display = 'block';
		}
		if(value < 181)
			elem.style.height = (value + 10) + 'px';
		else{
			document.getElementById('plImg').src = 'modules/news/images/meno_off.png';
			window.clearInterval(ocInt);
			isOpen = true;
			ETN_notizieCentrali();
		}
	}else{
		if(value > 0)
			elem.style.height = (value - 10) + 'px';
		else{
			document.getElementById('plImg').src = 'modules/news/images/plus_off.png'
			window.clearInterval(ocInt);
			isOpen = false;
			elem.style.display = 'none';
			gnInt = window.setInterval(ETN_changeNews, cambio_notizia);
		}
	}
	document.getElementById('plLittle').onclick = function() {ocInt = window.setInterval(ETN_openClose, 5)};
}

function ETN_notizieCentrali() {
	var tc = document.getElementById('textBig');
	while(tc.firstChild)
		tc.removeChild(tc.firstChild);
	
	var newsArr = loop_notizie.getAll();
	for(var i = 0; i < newsArr.length; i++){
		ETN_writeNews(newsArr[i], 'textBig', false, true, true);
	}
	
}

function ETN_writeNews(obj, node, prepend, onBig, show){
	prepend = prepend || false;
	onBig = onBig || false;
	var div = document.createElement('div');
	div.onclick = function() {window.location.href = 'http://www.energethics.it/page/27/news.html';}
	if(onBig)
		div.id = 'newsBlock' + obj.id + 'BIG';
	else
		div.id = 'newsBlock' + obj.id;
	if(!onBig)
		removeNode('newsBlock' + obj.id);
	div.className = 'ETN_nNews';
	if(!show)
		div.style.display = 'none';
	// Creo la notizia formattata correttamente
	var newsText = obj.date + ' ' + obj.title;
	div.appendChild(document.createTextNode(newsText));
	var cat = obj.cat.toLowerCase().replace(' ', '_');
	try{
	if(arr_cat[cat] != null)
		div.style.color = arr_cat[cat];
	}catch(e){alert(cat);}
	var desc = obj.desc;
	var span = document.createElement('span');
	span.className = 'ETN_spanNews';
	span.appendChild(document.createTextNode(desc));
	div.appendChild(span);
	if(!prepend)
		document.getElementById(node).appendChild(div);
	else
		prependChild(document.getElementById(node), div);
	return div;
}

function removeNode(nodeId){
	try{
		document.getElementById(nodeId).parentNode.removeChild(document.getElementById(nodeId));
	}catch(e){}
}

function prependChild(parent, node) {
    parent.insertBefore(node, parent.firstChild);
}

function ETN_changeNews() {
	currentNews = document.getElementById('cNews').firstChild;
	nextNews = currentNews.nextSibling;
	cnInt = window.setInterval(ETN_moveNews, 20);
	
}

function ETN_moveNews(){
	var cTop = Number(currentNews.style.top.match(/^[\-]{0,1}[0-9]{1,}/));
	if(cTop >= -27){
		currentNews.style.top = (cTop - 5) + 'px';
	}else{
		window.clearInterval(cnInt);
		nextNews.style.display = 'block';
		document.getElementById('cNews').removeChild(currentNews);
		var obj = loop_notizie.getNext();
		ETN_writeNews(obj, 'cNews');
	}
}


/*UTILITY*/

function Loop(data){
	if(typeof(data) != 'object')
		data = [];
	this.index = 0;
	this.data = data;
}

Loop.prototype.push = function(sData){
	this.data.push(sData);
}

Loop.prototype.getNext = function() {
	if((this.data.length) == this.index)
		this.index = 0;
	return this.data[this.index++];
}

Loop.prototype.getPrev = function() {
	if(this.index == 0)
		this.index = this.data.length;
	return this.data[--this.index];
}

Loop.prototype.getAll = function() {
	var tmpIndex = this.index;
	var ret = [];
	do{
		ret.push(this.getNext());
	}while(this.index != tmpIndex);
	return ret;
}

function ETN_findPosX(obj) {
	var curLeft = 0;
	if(obj.offsetParent){
		while(obj.offsetParent){
			curLeft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	else if(obj.x)
		curLeft += obj.x;
	return curLeft;
}

