// 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';
	
if(window.location.host == 'www.chietiweb.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)
	{
		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;
		}
		
		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 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 scrollBanner()
{
	var scrollB = document.getElementById('scrollBanner'),
		documentH = document.body.clientHeight,
		footerH = document.getElementById('footer').offsetHeight;
	
	if(scrollB)
	{		
		var topPosition = 0,
			bannerPosition = scrollB.offsetTop;
		
		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);
}

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);
	
}

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 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';
}
