/* dispone i tasti del menu uniformente distribuendo in modo omogeneo i pixel in eccesso tra i tasti.*/
function topmenu_equal (){
	// variabili temporanee per la misura del menu. normal_width contiene la larghezza del menu, elements il numero di elementi
	var normal_width=0;
	var elements=0;
	//valore del padding (2px a destra e 2 px a sinistra in questo caso)
	var padding=4;
	//larghezza dell'elemento contenitore
	var contenitorewidth=950;
	
	var ievs = (/MSIE (\d+\.\d+);/.test(navigator.userAgent));
	if (ievs){
		var iev=new Number(RegExp.$1);
		if (iev==6) {
			contenitorewidth=945;
		}
	}
	
	//trova la misura totale e il numero di elementi
	$('.Top_Menu li').each(function(){
		normal_width += $(this).width()+padding;
		elements ++;
	});
	
	//se il menu è disposto su una sola riga distribuisce lo spazio aggiuntivo tra i vari elementi
	if (normal_width<contenitorewidth){
		changing_width=Math.floor((contenitorewidth - normal_width)/elements);
		changing_width2=((contenitorewidth - normal_width) % elements);
		$('.Top_Menu strong').each(function(){
			$(this).css('width', ($(this).width())+changing_width);
		});
		$('.Top_Menu strong:lt('+(changing_width2)+')').each(function(){
			$(this).css('width', ($(this).width())+1);
		});
	}
	// se il menu è disposto su più di una fila si dispone su due file oltre due file non è contemplato
	else{
		$('.Top_Menu').css('height', 62);
		$pippo=$('.Top_Menu li').eq(0);
		normal_width=0;
		elements=0;
		while (normal_width<contenitorewidth){
			normal_width += $pippo.width()+padding;
			$pippo=$pippo.next();
			elements ++;
		}
		//quando passo la larghezza massima (contenitorewidth px) tolgo l'ultimo elemento inserito nella riga e eseguo l'operazione normale
		elements --;
		normal_width -= $('.Top_Menu li').eq(elements).width()+padding;
		
		changing_width=Math.floor((contenitorewidth-normal_width)/elements);
		changing_width2=((contenitorewidth-normal_width) % elements);
	
		$('.Top_Menu strong:lt('+(elements)+')').each(function(){
			$(this).css('width', ($(this).width())+changing_width);
		});
		$('.Top_Menu strong:lt('+(changing_width2)+')').each(function(){
			$(this).css('width', ($(this).width())+1);
		});
		
		//dopo di che ciclo gli elementi rimasti fuori e rieseguo l'operazione se la larghezza di questi elementi è suepriore a 500
		$pippo=$('.Top_Menu li').eq(elements);
		
		normal_width=0;
		elements2=elements;		
		elements=0;
		
		while ($pippo.next().length){
			normal_width += $pippo.width()+padding;
			$pippo=$pippo.next();
			elements ++;
		}
		normal_width += $pippo.width()+padding;
		elements ++;

		if (normal_width>500){
		changing_width=Math.floor((contenitorewidth-normal_width)/elements);
			$('.Top_Menu strong:gt('+(elements2-1)+')').each(function(){
				$(this).css('width', ($(this).width())+changing_width);
			});
			changing_width2=((contenitorewidth-normal_width) % elements);
			$('.Top_Menu strong:gt('+(elements2+elements-changing_width2-1)+')').each(function(){
				$(this).css('width', ($(this).width())+1);
			});
		}
	}
}

/*gallery di immagini 1 jack*/
//setta l'immagine corrente all'apertura della pagina
var immaginecorrente=1;

//funzione per cambiare immagine con fading
function cambia(num){
	$('.imgatt').fadeOut('slow').removeClass('imgatt');
	$('.didatt').removeClass('didatt');
	$('.gallery1jack img').eq(num).fadeIn('slow').addClass('imgatt');
	$('.gallery1jack .didascaliajackgallery').eq(num).addClass('didatt');
	$('.cursorjackgallery').animate({top:(70*(immaginecorrente-1)+2)+'px'}, 600);	
}
//funzione richiamata dall'intervallo epr scorrere tra le immagini
function scorri(){
	if (immaginecorrente==3){
		immaginecorrente=1
		cambia(0);
	}
	else{
		immaginecorrente++;
		cambia(immaginecorrente-1);
	}
}

//gallery3 su notizia
function makeScrollable($outer, $inner){

	var extra 			= 100;
	//Get menu width
	var divWidth = $outer.width();

	//Find last image in container
	var lastElem = $inner.find('img:last');
	$outer.scrollLeft(0);
	//When user move mouse over menu
	$outer.unbind('mousemove').bind('mousemove',function(e){
		var containerWidth = lastElem[0].offsetLeft + lastElem.outerWidth() + 2*extra;
		var left = (e.pageX - $outer.offset().left) * (containerWidth-divWidth) / divWidth - extra;
		$outer.scrollLeft(left);
	});

}

//funzione per la videogallery
function loadVideo(playerUrl, autoplay) {
  swfobject.embedSWF(
      playerUrl + '&rel=1&border=0&fs=1&autoplay=' + 
      (autoplay?1:0), 'player', '323', '200', '9.0.0', false, 
      false, {allowfullscreen: 'true'});
}
function showMyVideos2(data) {
  var feed = data.feed;
  var entries = feed.entry || [];
  var html = ['<ul>'];
  for (var i = 0; i < entries.length; i++) {
    var entry = entries[i];
    var title = entry.title.$t.substr(0);
    var thumbnailUrl = entries[i].media$group.media$thumbnail[0].url;
    var playerUrl = entries[i].media$group.media$content[0].url;
	if (i==0){
	html.push('<li onclick="loadVideo(\'', playerUrl, '\', true); $(\'#othervideos .actual\').removeClass(\'actual\'); $(this).children().eq(0).addClass(\'actual\');">',
              '<img src="', 
              thumbnailUrl, '" alt="" class="actual" /></li>');
	}else{
    html.push('<li onclick="loadVideo(\'', playerUrl, '\', true); $(\'#othervideos .actual\').removeClass(\'actual\'); $(this).children().eq(0).addClass(\'actual\')">',
              '<img src="', 
              thumbnailUrl, '" alt="" /></li>');
	}
  }
  html.push('</ul><br style="clear: left;"/>');
  document.getElementById('othervideos').innerHTML = html.join('');
  if (entries.length > 0) {
    loadVideo(entries[0].media$group.media$content[0].url, false);
  }
}

//funzione per scorrere lost & found, gallery2jack
function scorrimentogallery2(direzione){
	durata=1000;
	if (direzione==1){
		if (($('.coppiaattuale').next('.coppianews').length!=0)&&(fineanimazione)){
			fineanimazione=false;
			$('.coppiaattuale').animate({left: '-535px'}, durata, function(){
					$(this).removeClass('coppiaattuale').css('left', '45px');
					fineanimazione=true;
				}).next('.coppianews').css('left', '625px').addClass('coppiaattuale').animate({left:'45px'}, durata);
		}
		else if(fineanimazione){
			fineanimazione=false;
			$('.coppiaattuale').animate({left: '-535px'}, durata, function(){
					$(this).removeClass('coppiaattuale').css('left', '45px');
					fineanimazione=true;
				});
			$('.coppianews').eq(0).css('left', '625px').addClass('coppiaattuale').animate({left:'45px'}, durata);
		}
	}
	else if (direzione==-1){
		if (($('.coppiaattuale').prev('.coppianews').length!=0)&&(fineanimazione)){
			fineanimazione=false;
			$('.coppiaattuale').animate({left: '625px'}, durata, function(){
					$(this).removeClass('coppiaattuale').css('left', '45px');
					fineanimazione=true;
				}).prev('.coppianews').css('left', '-535px').addClass('coppiaattuale').animate({left:'45px'}, durata);
		}
		else if (fineanimazione){
			fineanimazione=false;
			$('.coppiaattuale').animate({left: '625px'}, durata, function(){
					$(this).removeClass('coppiaattuale').css('left', '45px');
					fineanimazione=true;
				});
			$('.coppianews').eq(($('.coppianews').length)-1).css('left', '-535px').addClass('coppiaattuale').animate({left:'45px'}, durata);
		}
	}
}
//funzione per scorrere appuntamenti in primo piano
function scorriappuntamenti(direzione){
	if (direzione==-1){
		if (($('.primoappuntamento').next().next().next('.appuntamentiSmall').length!=0)&&(fineanimazione)){
			fineanimazione=false;
			$('.primoappuntamento').removeClass('primoappuntamento').next('.appuntamentiSmall').addClass('primoappuntamento');
			$('.appuntamentiSmall').animate({top: '-=60'}, 500, function (){
				fineanimazione=true;
				$('.scorriappuntamenti').eq(1).show();
				if ($('.primoappuntamento').next().next().next('.appuntamentiSmall').length==0)
					$('.scorriappuntamenti').eq(0).hide();
			});
		}
	}
	else if (direzione==1){
		if (($('.primoappuntamento').prev('.appuntamentiSmall').length!=0)&&(fineanimazione)){
			fineanimazione=false;
			$('.primoappuntamento').removeClass('primoappuntamento').prev('.appuntamentiSmall').addClass('primoappuntamento');
			$('.appuntamentiSmall').animate({top: '+=60'}, 500, function (){
				fineanimazione=true;
				$('.scorriappuntamenti').eq(0).show();
				if ($('.primoappuntamento').prev('.appuntamentiSmall').length==0)
					$('.scorriappuntamenti').eq(1).hide();
			});
		}
	}
}


//funzione epr sostituire il campo password da type text a type password
function password_change(){
html = '<input type="password" class="login_input" name="password" id="password" value="" />';
	$('#password').after(html).remove();
	$('#password').focus();
	$('#password').blur(function(){
		if(this.value==''){
			html = '<input type="text" class="login_input" name="password" id="password" value="password" />';
			$(this).after(html).remove();
			$('#password').focus(function(){ 
				password_change();
			});
		}
	});
}

$(document).ready(function(){
	//distribuisce i menu su una linea distribuendo i pixel in eccesso.
	topmenu_equal();
	
	$('.Top_Menu li').each(function(){
		if ($(this).hasClass('actualpage')){
			$(this).css('backgroundImage', 'url("images/menu_regularHover.gif")').children().eq(0).children().eq(0).attr('src', 'images/menu_sxHover.gif');
			$(this).children().eq(0).children().eq(2).attr('src', 'images/menu_dxHover.gif');
		}
		if ($(this).children().eq(0).children().eq(1).hasClass('Top_MenuQz')){
				$(this).css('backgroundImage', 'url("images/menu_regularQz.gif")')
		}
		$(this).mouseover(function(){
			$(this).css('backgroundImage', 'url("images/menu_regularHover.gif")').children().eq(0).children().eq(0).attr('src', 'images/menu_sxHover.gif');
			$(this).children().eq(0).children().eq(2).attr('src', 'images/menu_dxHover.gif');
		});
		$(this).mouseout(function(){
			if ($(this).children().eq(0).children().eq(1).hasClass('Top_MenuQz')){
				$(this).css('backgroundImage', 'url("images/menu_regularQz.gif")').children().eq(0).children().eq(0).attr('src', 'images/menu_sxQz.gif');
				$(this).children().eq(0).children().eq(2).attr('src', 'images/menu_dxQz.gif');
			}
			else if ($(this).hasClass('actualpage')){}
			else {
				$(this).css('backgroundImage', 'url("images/menu_regular.gif")').children().eq(0).children().eq(0).attr('src', 'images/menu_sx.gif');
				$(this).children().eq(0).children().eq(2).attr('src', 'images/menu_dx.gif');
			}
		});
	});
	
	$('.SottoSezioni a').mouseover(function(){
		$(this).children().eq(1).css('backgroundImage', 'url("images/menu_regularHover.gif")');
		$(this).children().eq(0).attr('src', 'images/menu_sxHover.gif');
		$(this).children().eq(2).attr('src', 'images/menu_dxHover.gif');
	}).mouseout(function(){
		if ($(this).hasClass('actualpage2')){}
		else{
			$(this).children().eq(1).css('backgroundImage', 'url("images/menu_regular.gif")');
			$(this).children().eq(0).attr('src', 'images/menu_sx.gif');
			$(this).children().eq(2).attr('src', 'images/menu_dx.gif');
		}
	});
	
	/*mosaico*/
	$('.Mosaico div.MosaicoElement').mouseover(function(){
		$(this).children().eq(0).stop(true, true).fadeOut().next('.MosaicoNews').stop(true, true).fadeIn();
	}).mouseout(function(){
		$(this).children().eq(0).stop(true, true).fadeIn().next('.MosaicoNews').stop(true, true).fadeOut();
	});
	

	//intervallo per la gallery 1 jack
	intervallo1=setInterval(scorri, 10000);
	//click su notizia della gallery1 jack
	$('.didascaliajackgallery').click(function(){
		if (!$(this).hasClass('didatt')){
			immaginecorrente=$('.didascaliajackgallery').index($(this))+1;
			clearInterval(intervallo1);
			cambia($('.didascaliajackgallery').index($(this)));
			intervallo1=setInterval(scorri, 10000);
		}
	});
	
	//selettore multigallery
	$('.linguetta').click(function(){
		if (!$(this).hasClass('attuale')){
			$('.linguetta').removeClass('attuale');
			$(this).addClass('attuale');
			$('.multigalleryjack').hide().eq($('.linguetta').index($(this))).show();
			
		}
	});

	$('.mainview').mouseover(function(){
		$('.mainview .scorridx').show();
		$('.mainview .scorrisx').show();
	}).mouseout(function(){
		$('.mainview .scorridx').hide();
		$('.mainview .scorrisx').hide();
	});
	
	//scorrimento gallery
	$('#othergallery img').click(function(){
		$caller=$(this);
		$('#othergallery img.actual').removeClass('actual');
		$caller.addClass('actual');
		$('.mainview img').eq(0).fadeOut(500, function(){$('.mainview img').eq(0).fadeIn(300).attr('src', $caller.attr('alt'));});
		
		return false;
	});
	$('.mainview .scorridx').click(function(){
		if ($('#othergallery img').index($('#othergallery img.actual'))!=$('#othergallery img').length-1){
			$('#othergallery img.actual').removeClass('actual').parent().next().children().eq(0).addClass('actual');
			$caller=$('#othergallery img.actual');
		}else{
			$('#othergallery img.actual').removeClass('actual');
			$('#othergallery img').eq(0).addClass('actual');
			$caller=$('#othergallery img.actual');
		}
		$('.mainview img').eq(0).fadeOut(500, function(){$('.mainview img').eq(0).fadeIn(300).attr('src', $caller.attr('alt'));});
	});
	$('.mainview .scorrisx').click(function(){
		if ($('#othergallery img').index($('#othergallery img.actual'))!=0){
			$('#othergallery img.actual').removeClass('actual').parent().prev().children().eq(0).addClass('actual');
			$caller=$('#othergallery img.actual');
		}else{
			$('#othergallery img.actual').removeClass('actual');
			$('#othergallery img').eq($('#othergallery img').length-1).addClass('actual');
			$caller=$('#othergallery img.actual');
		}
		$('.mainview img').eq(0).fadeOut(500, function(){$('.mainview img').eq(0).fadeIn(300).attr('src', $caller.attr('alt'));});
	});
	

	//scorre gallery2jack Lost & found
	fineanimazione=true;
	$('.avantigallery2jack').click(function(){
		scorrimentogallery2(1);
	});
	$('.indietrogallery2jack').click(function(){
		scorrimentogallery2(-1);
	});
	
	//scorre tra gli appuntamenti
	$('.scorriappuntamenti').eq(1).hide();
	if ($('.appuntamentiSmall').length<3){
		$('.scorriappuntamenti').hide();
	}
	$('.scorriappuntamenti').click(function(){
		if ($(this).hasClass('avanti')){
		scorriappuntamenti(-1);
		}
		else{
		scorriappuntamenti(1);
		}
		return false;
	});
	
	//form nella parte destra della pagina
	$('#user').focus(function(){
			this.value='';
		}).blur(function(){
			if(this.value=='')
				this.value='Nome Utente';
	});
	/*
	$('#password').focus(function(){
			$(this).val('').attr('type', 'password');
		}).blur(function(){
			if(this.value=='')
			$(this).val('password').attr('type', 'text');
	});
	*/
	$('#password').focus(function(){ 
		password_change();
	});
	$('.login .loginbutton').click(function(){
		if ((location.href.indexOf('news')!=-1)&&(navigator.appName == 'Microsoft Internet Explorer')){
			Shadowbox.open({
				type:'iframe',
				content:'../../user.jsp?act=0&user='+$('#user').val()+'&password='+$('#password').val()+'&article='+$('#article').val(),
				height:410,
				width:520
			});
		} else {
			Shadowbox.open({
				type:'iframe',
				content:'user.jsp?act=0&user='+$('#user').val()+'&password='+$('#password').val()+'&article='+$('#article').val(),
				height:410,
				width:520
			});
		}
		return false;
	});

	
	
	//pagina delle notizie decollapse dei commenti
	$('.CommentsTrigger').click(function(){
		$('.Comments').show(600);
		return false;
	});
	
	// paginazione dei commenti (ajax?)
	// var numeropagine=4;
	// $('.paginazione a').eq(0).hide();
	// $('.paginazione a').eq(1).addClass('actual');
	// $('.paginazione a').eq(2).addClass('semiactual');
	
	// $('.paginazione a').click(function(){
		// if (($('.paginazione a').index($(this))!=0)&&($('.paginazione a').index($(this))!=(numeropagine+1))){
			// $('.paginazione a').removeClass('semiactual').removeClass('actual');
			// $(this).addClass('actual').prev().addClass('semiactual').next().next().addClass('semiactual');
		// }
		// else if(($('.paginazione a').index($(this))==0)&&(!$('.paginazione a').eq(1).hasClass('actual'))){
			// $('.paginazione a').removeClass('semiactual');
			// $('.paginazione .actual').removeClass('actual').addClass('semiactual').prev().addClass('actual').prev().addClass('semiactual');
		// }
		// else if(($('.paginazione a').index($(this))==numeropagine+1)&&(!$('.paginazione a').eq(numeropagine).hasClass('actual'))){
			// $('.paginazione a').removeClass('semiactual');
			// $('.paginazione .actual').removeClass('actual').addClass('semiactual').next().addClass('actual').next().addClass('semiactual');
		// }
		
	
	
		// if ($('.paginazione a').eq(0).hasClass('semiactual')){
			// $('.paginazione a').eq(0).hide();
		// }
		// else {
			// $('.paginazione a').eq(0).show();
		// }
		// if ($('.paginazione a').eq(numeropagine+1).hasClass('semiactual')){
			// $('.paginazione a').eq(numeropagine+1).hide();
		// }
		// else{
			// $('.paginazione a').eq(numeropagine+1).show();
		// }
		// return false;
	// });
	
	//galleria di immagini su notizia
	$('.jackgallery3').mouseenter(function (){
		if ($('.jackgallery3 .tbcont .slider img').length>1){
			$('.jackgallery3 .tbcont').stop(true, true).fadeIn();
		}
	}).mouseleave(function (){
		$('.jackgallery3 .tbcont').stop(true, true).fadeOut();
	});
	$('.jackgallery3 .tbcont img').mouseenter(function(){
		$(this).stop().animate({'opacity':'1'});
	}).mouseleave(function(){
		$(this).stop().animate({'opacity':'0.7'});
	}).click(function(){
		$questo=$(this);
		$('.jackgallery3 .main').fadeOut(500, function(){
			$('.jackgallery3 .main').attr('src', $questo.attr('src')).attr('alt', $questo.attr('alt')).load(function(){
				$('.jackgallery3 .main').show();
				$('.jackgallery3 .fade').hide();
			});
		});
		$('.jackgallery3 .didagallery').html($(this).attr('alt'));
		$('.jackgallery3 .fade').fadeIn(500).attr('src', $(this).attr('src')).attr('alt', $(this).attr('alt'));
		$('.jackgallery3 .tbcont img').removeClass('actual');
		$(this).addClass('actual');
		return false;
	});
	
	var widthsliderjackgallery3 = ($('.jackgallery3 .tbcont .slider img').length)*132;
	$('.jackgallery3 .tbcont .slider').css('width', widthsliderjackgallery3+'px')
	var sinistramassima = -(parseInt($('.jackgallery3 .tbcont .slider').css('width'))-(parseInt($('.jackgallery3 .tbcont').css('width'))));
	
	makeScrollable($('.tbcont'), $('.slider'));
	
	$('.normativebutton').click(function(){
		if ($('#regione').val()!=0){
			if ((location.href.indexOf('news')!=-1)&&(navigator.appName == 'Microsoft Internet Explorer')){
				location.href="../../regione.jsp?id="+$('#regione').val();
			}else{
				location.href="regione.jsp?id="+$('#regione').val();
			}
		}
		else {
			alert("selezionare prima una regione");
			return false;
		}
	});
	$('.informazionibutton').click(function(){
		if ($('#regione2').val()!=0){
			if ((location.href.indexOf('news')!=-1)&&(navigator.appName == 'Microsoft Internet Explorer')){
				location.href="../../regione.jsp?id="+$('#regione2').val();
			}else{
				location.href="regione.jsp?id="+$('#regione2').val();
			}
		}
		else {
			alert("selezionare prima una regione");
			return false;
		}
	});
	
	
	//ricerca
		
	$('.Search_btn').click(function(){
		if ($('.Search_campo').val()=='inserisci il testo per la ricerca'){
			alert("inserire un valore di ricerca nel campo");
			return false;
		}
		else{
			if ((location.href.indexOf('news')!=-1)&&(navigator.appName == 'Microsoft Internet Explorer')){
				location.href="../../ricerca.jsp?query="+$('.Search_campo').val();
			} else {
				location.href="ricerca.jsp?query="+$('.Search_campo').val();
			}
		}
	});
	$('.Search_campo').focus(function(){
			this.value='';
		}).blur(function(){
			if(this.value=='')
				this.value='inserisci il testo per la ricerca';
		}).keypress(function(event) {
		if (event.keyCode == '13') {
			if ((location.href.indexOf('news')!=-1)&&(navigator.appName == 'Microsoft Internet Explorer')){
				location.href="../../ricerca.jsp?query="+$('.Search_campo').val();
			}
			else{
				location.href="ricerca.jsp?query="+$('.Search_campo').val();
			}
		}
	});
	
	
	
	
});

function votas(sond,risp){
	$.ajax({
		type: 'get', 
		data: 'idsondaggio='+sond+'&idvoto='+risp,
		url: 'sondaggio.jsp',
		success: function(result){
			$('#cont_sondaggio').html(result)
		}
	});
}
