// JavaScript Document
if(window.location.host == 'www.termoliweb.it')
	var template = 't';
	
if(window.location.host == 'www.termolionline.it')
	var template = 'to';

if(window.location.host == 'www.campobassoweb.it')
	var template = 'c';
	
if(window.location.host == 'www.vastoweb.com')
	var template = 'v';
	
function core(pageId)
{
	
	if(pageId == 1)
	{
		//getFlashNews();
		slideCorriere();
		//setInterval('updateNews()', 300000);
	}
	else
	{
		fontSizeTransform('testo');
		//scrollBanner();
	}
	//colsHeight();
	var wsmsi_;
	wsmsi_ = setTimeout('menuScrollWS()',2000);
	rollover();
	footerBanner();
}

function hideSondaggio()
{
	var sondaggio = document.getElementById('formSondaggio');
	
	sondaggio.style.display = 'none';
	//sondaggio.style.height = '33px';
	
	var votaSondaggio = document.getElementById('votaSondaggio');
	
	votaSondaggio.onclick = function ()
	{
		sondaggio.style.display = '';
		sondaggio.parentNode.style.backgroundColor = '#f5f5f5';
		//votaSondaggio.style.color = '#900';
		var votaback = document.createTextNode('VOTA IL SONDAGGIO');		
		votaSondaggio.parentNode.replaceChild(votaback, votaSondaggio);
		return false;
	}
	
	votaSondaggio.onmouseover = function ()
	{
		sondaggio.parentNode.style.background = '#ffffff';
	}
	votaSondaggio.onmouseout = function ()
	{
		sondaggio.parentNode.style.background = '';
	}
	//sondaggio.formpollForm.style.display = 'none';
}


function hideSuegiu()
{
	var suegiu2 = document.getElementById('suegiu2'),
		suegiu = document.getElementById('suegiu');
	
	suegiu2.style.display = 'none';
	suegiu.style.height = '25px';
	
	var vediSuegiu = document.getElementById('vediSuegiu');
	
	vediSuegiu.onclick = function ()
	{
		suegiu2.style.display = '';
		/*
		var suegiuback = document.createTextNode("SU & GIU'"),
			vediA = vediSuegiu.getElementsByTagName('a');
			
		vediA[0].parentNode.replaceChild(suegiuback, vediA[0]);
		*/
		vediSuegiu.style.display = 'none';
		suegiu.style.height = '108px';
		return false;
	}
	//sondaggio.formpollForm.style.display = 'none';
}

function hideFermoPosta()
{
	var fermoposta = document.getElementById('fermoPosta');
	
	fermoposta.style.display = 'none';
	//fermoposta.style.height = '33px';
	
	var vediFermoPosta = document.getElementById('vediFermoPosta');
	
	vediFermoPosta.onclick = function ()
	{
		fermoposta.style.display = '';
		vediFermoPosta.style.display = 'none';
		//fermoposta.style.height = '168px';
		return false;
	}
	//sondaggio.formpollForm.style.display = 'none';
}


function colsHeight()
{
	var leftCol = document.getElementById('rightCol2'),
		rightCol = document.getElementById('rightCol'),
		centralCol = document.getElementById('mainCol'),
		maxHeight = Math.max(leftCol.offsetHeight, rightCol.offsetHeight);
		
	//alert(leftCol.offsetHeight);
	
	if(centralCol.offsetHeight < maxHeight)
	{
		centralCol.style.height = maxHeight + 'px';
	}
}

function popup(url, name, width, height)
{
settings=
"toolbar=no,location=no,directories=no,"+
"status=no,menubar=no,scrollbars=yes,"+
"resizable=yes,width="+width+",height="+height;

MyNewWindow=window.open(url,name,settings);

return false;
}

function printPreview()
{
	var htmlHead = document.getElementsByTagName('head'),
		newStyle = document.createElement('link'),
		bodyH = document.getElementById('prova');
	
	newStyle.setAttribute('rel', 'stylesheet');
	newStyle.setAttribute('type','text/css');
	newStyle.setAttribute('href', '/css/print.css');
	newStyle.setAttribute('id', 'printCss');
	
	htmlHead[0].appendChild(newStyle);
	
	window.print();

}

function cancelPrintPreview()
{
	var printCss = document.getElementById('printCss');
	
	printCss.parentNode.removeChild(printCss);
	
}


function fontSizeTransform(id)
{
	var testo = document.getElementById(id),
		aumenta = document.getElementById('aFont'),
		diminuisci = document.getElementById('dFont');
	
	//testo.style.fontSize = '14px';
	
	if(testo)
	{
	
		if(aumenta)
		{
			aumenta.onclick = function()
			{
				if(testo.style.fontSize == '') testo.style.fontSize = '13px';
				if(testo.style.fontSize.split('px')[0] < 18)
					testo.style.fontSize = Number(testo.style.fontSize.split('px')[0]) + Number(2) + 'px';
					
				return false;
			}
		}
		
		if(diminuisci)
		{
			diminuisci.onclick = function()
			{
				if(testo.style.fontSize == '') testo.style.fontSize = '13px';
				if(testo.style.fontSize.split('px')[0] > 13)
					testo.style.fontSize = Number(testo.style.fontSize.split('px')[0]) - Number(2) + 'px';
					
				return false;
			}
		}
	}
}


function rollover()
{
	var allLinks = document.links;
		rolloverImage = Array();
	
	for(a=0; a<allLinks.length; a++)
	{
		if(allLinks[a].getAttribute('class') == 'rollover')
		{
			rolloverImage[a] = allLinks[a].firstChild;
			
			allLinks[a].onmouseover = function()
			{
				if(this.childNodes[0].src)
					var image = this.childNodes[0];
				else if(this.childNodes[1].src)
					var image = this.childNodes[1];
					
				//alert(image.indexOf('stampa'));
				
				if(image.src.indexOf('stampa') > 0)
					image.src = '/template/'+template+'/images/stampa.png';
				else if(image.src.indexOf('mail') > 0)
					image.src = '/template/'+template+'/images/mail.png';
				
			}
			
			allLinks[a].onmouseout = function()
			{
				if(this.childNodes[0].src)
					var image = this.childNodes[0];
				else if(this.childNodes[1].src)
					var image = this.childNodes[1];
					
				//alert(image.indexOf('stampa'));
				
				if(image.src.indexOf('stampa') > 0)
					image.src = '/images/stampa.png';
				else if(image.src.indexOf('mail') > 0)
					image.src = '/images/mail.png';
			}
		}
	}
}

function countClick(id_banner)
{
	var xmlhttp;
	
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {
  // code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
/*
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4)
  {
  alert(xmlhttp.responseText);
  }
}*/
xmlhttp.open("GET",'/library/countbanner.php?id='+id_banner,true);
xmlhttp.send(null);

return true;
}

function aggiornamentoHome()
{
	var xmlhttp;
	
	if (window.XMLHttpRequest)
	  {
	  // code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }
	else if (window.ActiveXObject)
	  {
	  // code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	
	xmlhttp.onreadystatechange=function()
	{
	if(xmlhttp.readyState==4)
	  {
	  if(xmlhttp.responseText == 1) window.location = window.location;
	  }
	}
	xmlhttp.open("GET",'/newversion/library/lastupdate.php',true);
	xmlhttp.send(null);
	
	return true;
}

function banneraTempo(type, idBanner)
{
	var xmlhttp,
		response;
	
	if (window.XMLHttpRequest)
	{
		// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	xmlhttp.onreadystatechange=function()
	{
		if(xmlhttp.readyState==4)
		{
			response = xmlhttp.responseText;
			//alert(response);
			//return;
			
			var vPairs = new Array();
			
			vPairs = response.split('&');
			var value = new Array();
			
			
			for(var a = 0; a < vPairs.length; a++)
			{
				value[a] = vPairs[a].split('=');
			}
			
			
			if (navigator.appName == "Netscape") // vuoi mettere <br> in firefox
				var secondi = value[1][1] * 1500;
			else
				var secondi = value[1][1] * 1000;
				
			bannerType(type,idBanner);
			setTimeout('banneraTempo('+type+','+idBanner+')', secondi);
			
		}
	}
	xmlhttp.open("GET",'/newversion/library/bannertempo.php?id='+idBanner+'&type='+type,true);
	xmlhttp.send(null);
	
	return true;
}

function bannerType(type, idBanner)
{
	var xmlhttp,
		response = document.getElementById('bannerTempo-'+idBanner);
	
		
	if (window.XMLHttpRequest)
	{
		// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	xmlhttp.onreadystatechange=function()
	{
		if(xmlhttp.readyState==4)
		{
			response.innerHTML = xmlhttp.responseText;			
		}
	}
	xmlhttp.open("GET",'/newversion/library/bannertype.php?id='+type,true);
	xmlhttp.send(null);
	
	return true;
}

function footerBanner()
{
	var footB = document.getElementById('footerBanner'),
		incr,
		decr,
		a = -270;
	
	if(!footB)
		return;
	
	footB.onmouseover = function()
	{
		clearInterval(decr);
		incr = setInterval(increment, 10);
		//footB.style.bottom = '0';
	}
	
	footB.onmouseout = function()
	{
		clearInterval(incr);
		decr = setInterval(decrease, 10);
		//footB.style.bottom = '-270px';
	}
	
	function increment()
	{
		if(a < 0)
		{
			a += 30;
			footB.style.bottom = a + 'px';
		}
		else
		{
			clearInterval(incr);
		}
	}
	
	
	function decrease()
	{
		if(a > -270)
		{
			a -= 30;
			footB.style.bottom = a + 'px';
		}
		else
		{
			clearInterval(decr);
		}
	}

}

function updateNews()
{
	var xmlhttp,
		news = document.getElementById('news');
	
		
	if (window.XMLHttpRequest)
	{
		// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	xmlhttp.onreadystatechange=function()
	{
		if(xmlhttp.readyState==4)
		{
			news.innerHTML = xmlhttp.responseText;			
		}
	}
	xmlhttp.open("GET",'/newversion/library/update.php',true);
	xmlhttp.send(null);
	
	return true;
}

function DataUpdate(table, id, tag_id)
{
	switch(table)
	{
		case 'sindaci':
		var udLink = 'udsindaci.php';
		break;
		case 'lista':
		var udLink = '/elezioni-comunali-2010/udlista.php?id='+id;
		break;
		case 'sezioni':
		var udLink = '/elezioni-comunali-2010/udsezioni.php';
		break;
		case 'news':
		var udLink = '/elezioni-comunali-2010/udnews.php';
		break;
		case 'fotogallery':
		var udLink = '/elezioni-comunali-2010/udfotogallery.php';
		break;
	}
	
	if(tag_id == null)
		tag_id = 'DataUpdate';
	
	if(udLink)
	{
		var interval = setInterval('caricaTesto("'+udLink+'", "'+tag_id+'")', 30000);
	}
	
}

function caricaTesto(file, id)
{
	var xmlhttp,
		tag_id = document.getElementById(id);
	
		
	if (window.XMLHttpRequest)
	{
		// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	xmlhttp.onreadystatechange=function()
	{
		if(xmlhttp.readyState==4)
		{
			tag_id.innerHTML = xmlhttp.responseText;			
		}
	}
	xmlhttp.open("GET",file,true);
	xmlhttp.send(null);
	
	return true;
}

function updateBal()
{	
	caricaTesto('/elezioni-comunali-2010/ballottaggioud.php?t=v&s=b', 'nvotib');
	caricaTesto('/elezioni-comunali-2010/ballottaggioud.php?t=p&s=b', 'pvotib');
	caricaTesto('/elezioni-comunali-2010/ballottaggioud.php?t=v&s=m', 'nvotim');
	caricaTesto('/elezioni-comunali-2010/ballottaggioud.php?t=p&s=m', 'pvotim');
	caricaTesto('/elezioni-comunali-2010/ballottaggioud.php?t=p&s=t', 'totVoti');
	caricaTesto('/elezioni-comunali-2010/ballottaggioud.php?t=v&s=t', 'ptotvoti');
}


function scrollBanner()
{
	var scrollB = document.getElementById('scrollBanner'),
		documentH = document.body.clientHeight,
		footerH = document.getElementById('footer').offsetHeight;
	
	if(scrollB)
	{		
		var topPosition = 0,
			bannerPosition = scrollB.offsetTop;
			
		var browserH = window.innerHeight;
		if(browserH == undefined)
			browserH = document.documentElement.clientHeight;
		
		if(browserH > 300)
		{
			if(window.pageYOffset)
				topPosition = window.pageYOffset;
			else
				topPosition = document.body.parentElement.scrollTop;
				
			if(topPosition > bannerPosition)
			{
				if(documentH - topPosition - scrollB.offsetHeight > footerH)
					scrollB.style.marginTop = topPosition - bannerPosition + 'px';
			}
			else
			{
				scrollB.style.marginTop = '0';
			}
		}
		else
		{
			scrollB.style.marginTop = '0';
		}
		
		window.onscroll = function()
		{
			var browserH = window.innerHeight;
			if(browserH == undefined)
				browserH = document.documentElement.clientHeight;
			
			if(browserH > 300)
			{
				if(window.pageYOffset)
					topPosition = window.pageYOffset;
				else
					topPosition = document.body.parentElement.scrollTop;
				
				if(topPosition > bannerPosition)
				{
					if(documentH - topPosition - scrollB.offsetHeight > footerH)
						scrollB.style.marginTop = topPosition - bannerPosition + 'px';
				}
				else
				{
					scrollB.style.marginTop = '0';
				}	
			}//End If
			else
			{
				scrollB.style.marginTop = '0';
			}
		}//End onScrool Function
	}// End If
	
}



function menuScrollWS()
{
	var menuws = document.getElementById('wsmscroll');
	
	menuws.onmouseover = function()
	{
		clearTimeout(wsmsi_);
	}
	
	menuws.onmouseout = function()
	{
		wsmsi_ = setTimeout('menuScrollWS()',1000);
	}
		
	if(menuws)
	{
		var end = document.getElementById('wsms'),
			spostamento = end.offsetLeft - 975;
		
		if(menuws.style.left == '')
			menuws.style.left = '0px';

		var leftP = menuws.style.left.replace('px','');
				
		if(leftP > (spostamento*-1))
		{
			menuws.style.left = leftP -1 + 'px';
			wsmsi_ = setTimeout('menuScrollWS()',100);
		}
		else
		{
			wsmsi_ = setTimeout("resetMenuScroll();",2000);
		}
		
		//menu.style.left = '-361px';
		
		//alert(spostamento);
		
	}
}

function resetMenuScroll(id)
{
	var menuws = document.getElementById('wsmscroll');
	menuws.style.left = '';
	wsmsi_ = setTimeout("menuScrollWS()",2000);
}

function TncCountdown() {
  iniData= new Date(2010,7,23,11,0,0);
  /*
    Data dell'inizio dell'evento, espressa in: anno, mese, giorno, minuti, secondi, millisecondi
  */
  oggi= new Date();
  milDif=(iniData-oggi);
  giorni=parseInt(milDif/86400000);
  milDif=milDif-(giorni*86400000);
  ore=parseInt(milDif/3600000);
  milDif=milDif-(ore*3600000);
  minuti=parseInt(milDif/60000);
  milDif=milDif-(minuti*60000);
  secondi=parseInt(milDif/1000);
  milDif=milDif-(secondi*1000);
  if (giorni <= "0" && ore <= "0" && minuti <= "0" && secondi <= "0")
    /* se è già arrivata la data scrive che siamo arrivati, altrimenti dice i giorni mancanti */
    testo='';
  else
    testo= giorni+' giorni '+ore+' ore '+minuti+' min '+secondi+' sec';

    if (document.getElementById)
      document.getElementById("countdown").innerHTML=testo;
    else
      document.all.countdown.innerHTML=testo;
 
setTimeout("TncCountdown()",1000)
}



function TNCgetTime() {
now = new Date();
y2k = new Date("Aug 18 2010 22:00:00");
days = (y2k - now) / 1000 / 60 / 60 / 24;
daysRound = Math.floor(days);
hours = (y2k - now) / 1000 / 60 / 60 - (24 * daysRound);
hoursRound = Math.floor(hours);
minutes = (y2k - now) / 1000 /60 - (24 * 60 * daysRound) - (60 * hoursRound);
minutesRound = Math.floor(minutes);
seconds = (y2k - now) / 1000 - (24 * 60 * 60 * daysRound) - (60 * 60 * hoursRound) - (60 * minutesRound);
secondsRound = Math.round(seconds);
sec = "s ";
min = "m ";
hr = "h ";
dy = "g ";
if(now < y2k)
{
	countDown.innerHTML = '';
	if(daysRound < 10)
		daysRound = '0'+daysRound;
	
	countDown.innerHTML += '<strong>'+daysRound+'</strong>' + dy;
	
	if(hoursRound < 10)
		hoursRound = '0' + hoursRound;
	
	countDown.innerHTML += '<strong>'+hoursRound+'</strong>' + hr;
	
	if(minutesRound < 10)
		minutesRound = '0' + minutesRound;
	
	countDown.innerHTML += '<strong>'+minutesRound+'</strong>' + min;
	
	if(secondsRound < 10)
		secondsRound ='0'+secondsRound;
	
	countDown.innerHTML += '<strong>'+secondsRound+'</strong>' + sec;
	
	//countDown.innerHTML = daysRound  + dy + hoursRound + hr + minutesRound + min + secondsRound + sec;
	//countDown.innerHTML = output;
}
newtime = window.setTimeout("TNCgetTime();", 1000);
}

function chiudiPopup()
{
	var popupB = document.getElementById('popupBanner');
	var popupSfondo = document.getElementById('coprente');
	popupSfondo.style.display = 'none';
	popupB.style.display = 'none';
}

function showPopup()
{
	var popupB = document.getElementById('popupBanner');
	popupB.style.visibility = 'visible';
}

function cvda_showRisp(id)
{
	divToShow = document.getElementById('cvda_mess_'+id);
	divToShow.style.display = 'block';
}

var currentPic = 0;
var loaderDelay;

function gallery(pic, ppic)
{
	var Img = $('#newsG li#pic'+pic+' img'),
		Li = $('#newsG li#pic'+pic),
		Gallery = $('#newsG li .picContainer');
		
		
	function showPic() {		
		 Li.fadeIn('fast', 'swing');
		 $('#controller div').show();
		 clearTimeout(loaderDelay);
		 $('#fotoGloader').hide();
		 
		 var imgW = Img.width(),
		 	imgH = Img.height();
			
		var GalleryW = Gallery.width(),
			GalleryH = Gallery.height();
			
		if(imgW > GalleryW || imgH > GalleryH)
		{
			var newW = Math.round(imgW * GalleryH / imgH);
			Img.width(newW);
		}
		else if(imgH < (GalleryH+1))
		{
			if(imgH > 0)
			{
				var marginTop = Math.round((GalleryH - imgH) / 2);
				
				
				if(marginTop < Math.round((GalleryH/2)-1))
					Img.css('margin-top',  marginTop+'px');
			}
		}		
		
	 }
	 
//	 function resizeImg(width,height
	
	$('#controller div').hide();
	loaderDelay = setTimeout("$('#fotoGloader').show()", 1500);
	
	if(Img.attr('src') == '')
	{
		Img.attr('src', Img.attr('id')+'?trick='+Math.round(new Date().getTime() / 1000));
		
		Img.load(function() {$('#newsG li#pic'+ppic).fadeOut('fast', 'swing',showPic);});

	}
	else
	{
		Img.ready(function() {$('#newsG li#pic'+ppic).fadeOut('fast', 'swing',showPic);});
	}
	
		
	
	
	currentPic = pic;
}


function prevpic()
{
	if(currentPic > 1)
	{
		prevPic = currentPic;
		currentPic--;
	}
	else
	{
		currentPic = num_pics;
		prevPic = 1;
	}
	
	gallery(currentPic, prevPic);
	window.location.hash = '#'+(currentPic);
}

function nextpic()
{
	if(currentPic < num_pics)
	{
		prevPic = currentPic;
		currentPic++;
	}
	else
	{
		currentPic = 1;
		prevPic = num_pics;
	}
	
	gallery(currentPic, prevPic);
	window.location.hash = '#'+(currentPic);
	
}

var contatoretnc = 1; 

function galleryTnc()
{
	if (contatoretnc == 14) 
	{
		contatoretnc = 1;
		$("#bannertnc a img.fototnc").animate({marginLeft: '+=2418'}, 1000);	
		$("#bannertnc a img.cattnc").animate({marginLeft: '+=372'}, 1000);	
	}
	else
	{
		$("#bannertnc a img.fototnc").animate({marginLeft: '-=186'}, 1000);	
		if (contatoretnc == 5 || contatoretnc == 10)	
			$("#bannertnc a img.cattnc").animate({marginLeft: '-=186'}, 1000);
		contatoretnc++;
	}
}










