/*funzione di scorrimento della gallery di news*/
var gallerydidawidth=0;

function GalleryTopJack_avanti(){
	if ($('.GalleryTopJackNav ul li a').index($('.GalleryTopJackNav ul li a.HoverNavTop'))<$('.GalleryTopJackNav ul li a').length-1){
		$('.GalleryTopJackNav ul li a.HoverNavTop').removeClass('HoverNavTop').parent().next().children().eq(0).addClass('HoverNavTop');
		
		$('.GalleryTopJackInnerContainer').stop(false, false).animate({"left": "-=654px"}, 600);
		$('.GalleryTopJackTitleInnerContainer').stop(false, false).animate({"left": "+=305px"}, 600);
		
	}else{
		$('.GalleryTopJackNav ul li a.HoverNavTop').removeClass('HoverNavTop');
		$('.GalleryTopJackNav ul li a').eq(0).addClass('HoverNavTop');
		
		$('.GalleryTopJackInnerContainer').stop(false, false).animate({"left": "0px"}, 600);
		$('.GalleryTopJackTitleInnerContainer').stop(false, false).animate({"left": -gallerydidawidth+305}, 600);
		
	}
}

function scorrigallery (num){
	fotonascoste=$('.GalleryContainer .altrefoto .gall'+num+' img').length-1;
	if (fotonascoste>=0){
		nuovafoto=Math.round(Math.random()*(fotonascoste));
		tempfotosrc=$('.GalleryContainer .altrefoto .gall'+num+' img').eq(nuovafoto).attr('src');
		tempfotoalt=$('.GalleryContainer .altrefoto .gall'+num+' img').eq(nuovafoto).attr('alt');
		tempfoto2src=$('.GalleryContainer .foto'+num+' .main').attr('src')
		tempfoto2alt=$('.GalleryContainer .foto'+num+' .main').attr('alt')
		$('.GalleryContainer .foto'+num+' .fade').fadeIn(500).attr('src', tempfotosrc).attr('alt', tempfotoalt);
		$('.GalleryContainer .foto'+num+' .main').fadeOut(500, function(){
				$('.GalleryContainer .foto'+num+' .main').attr('src', tempfotosrc).attr('alt', tempfotoalt);
					$('.GalleryContainer .foto'+num+' .main').show();
					$('.GalleryContainer .foto'+num+' .fade').hide();
					$('.GalleryContainer .altrefoto .gall'+num+' img').eq(nuovafoto).attr('src', tempfoto2src).attr('alt', tempfoto2alt);
			});
	}
	prossimo =Math.round(Math.random()*3+1);
	tempoprossimo=Math.round(Math.random()*3000+2000)
	setTimeout("scorrigallery("+prossimo+")", tempoprossimo);
}

//funzione per il cambio del tipo di input text => password
function password_change(){
html = '<input class="myKidsinput" type="password" name="password0" id="password" value="" />';
	$('#password').after(html).remove();
	$('#password').focus();
	$('#password').blur(function(){
		if(this.value==''){
			html = '<input class="myKidsinput" type="text" name="password0" id="password" value="Password" />';
			$(this).after(html).remove();
			$('#password').focus(function(){ 
				password_change();
			});
		}
	});
}

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

}
var myimages=new Array()

//sondaggio asincrono
function votas(sond,risp){
	$.ajax({
		type: 'get', 
		data: 'idsondaggio='+sond+'&idvoto='+risp,
		url: 'sondaggi.jsp',
		success: function(result){
			$('#cont_sondaggio').html(result);
			Cufon.replace('.text', { fontFamily: 'Century Gothic' });
			Cufon.now();
		}
	});
	
	
}

//preload
(function($) {
  var cache = [];
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)

//gallery3 scorrimento automatico
function gallery3scorri(){

	if ($('.jackgallery3 .tbcont .actual').next().length==0){
		$questo=$('.jackgallery3 .tbcont img').eq(0);
	} else {
		$questo=$('.jackgallery3 .tbcont .actual').next();
	}

	$('.jackgallery3 .main').fadeOut(500, function(){
		$('.jackgallery3 .main').attr('src', '').attr('alt', '');
		$('.jackgallery3 .main').attr('src', $questo.attr('title')).attr('alt', $questo.attr('alt')).load(function(){
			$('.jackgallery3 .main').show();
			$('.jackgallery3 .fade').hide();
		});
	});
	$('.jackgallery3 .fade').attr('src', $questo.attr('title')).attr('alt', $questo.attr('alt')).fadeIn(300);
	$('.jackgallery3 .tbcont img').removeClass('actual');
	$questo.addClass('actual');
	$('.jackgallery3 .didascalia').html($questo.attr('alt'));
}

function gallery3scorriindietro(){

	if ($('.jackgallery3 .tbcont .actual').prev().length==0){
		$questo=$('.jackgallery3 .tbcont img').eq($('.jackgallery3 .tbcont img').length-1);
	} else {
		$questo=$('.jackgallery3 .tbcont .actual').prev();
	}

	$('.jackgallery3 .main').fadeOut(500, function(){
		$('.jackgallery3 .main').attr('src', '').attr('alt', '');
		$('.jackgallery3 .main').attr('src', $questo.attr('title')).attr('alt', $questo.attr('alt')).load(function(){
			$('.jackgallery3 .main').show();
			$('.jackgallery3 .fade').hide();
		});
	});
	$('.jackgallery3 .fade').attr('src', $questo.attr('title')).attr('alt', $questo.attr('alt')).fadeIn(300);
	$('.jackgallery3 .tbcont img').removeClass('actual');
	$questo.addClass('actual');
	$('.jackgallery3 .didascalia').html($questo.attr('alt'));
}

$(document).ready(function(){

	/*sezioneattutale*/
	$('.currentSect').css("background-color", $('.currentSect').css("border-left-color"));

	//preload immagini
	$.preLoadImages($('.jackgallery3 .tbcont .slider img').map(function(){
		return $(this).attr("title");
	}).get().join(", "));
	
	//intervallo di scorrimento
	if ($('.jackgallery3').length>0){
		gallery3interval=setInterval('gallery3scorri()', 6000);
	}
	
	/*gallery di news scorrimento manuale*/
	gallerydidawidth=$('.GalleryTopJackNav ul li a').length*305;
	$('.GalleryTopJackTitleInnerContainer').css({"left": -gallerydidawidth+305});
	
	$('.GalleryTopJackNav ul li a').click(function(){
		if (!$(this).hasClass('HoverNavTop')){
			clearInterval(intervalloTopJack);
			$('.GalleryTopJackNav ul li a.HoverNavTop').removeClass('HoverNavTop');
			$(this).addClass('HoverNavTop');
			numero=$('.GalleryTopJackNav ul li a').index($(this));
			cont1offset=(-(numero*654));
			cont2offset=(-gallerydidawidth+((numero+1)*305));
			
			$('.GalleryTopJackInnerContainer').stop(false, false).animate({"left": cont1offset+"px"}, 600);
			$('.GalleryTopJackTitleInnerContainer').stop(false, false).animate({"left": cont2offset+"px"}, 600);
		}
		$(this).blur();
		intervalloTopJack = setInterval('GalleryTopJack_avanti()', 10000);
		return false;
	});
	
	/*fotogallery setup iniziale*/
	if (navigator.appName!='Microsoft Internet Explorer'){
		$('.GalleryContainer .foto1').transform({'rotate': '5deg'}).css({'left': '20px', 'top': '52px'});
		$('.ContainerBoxGallery .foto1').transform({'rotate': '-3deg'}).css({'left': '20px', 'top':'51px'});
		$('.GalleryContainer .foto2').transform({'rotate': '-7deg'}).css({'left': '150px', 'top': '9px'});
		
		$('.ImagesOverlay').transform({'rotate': '-2deg'});
	}
	
	
	/*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();
	});
	
	/*fotogallery tip*/
	
	// $('.GalleryContainer').bind('mousemove', function(e){
		// margine=($(window).width()-$('.Template').width())/2;
		// if (margine<0) {margine=0;}
		
		// ipsilon= e.pageY -this.offsetTop -20;
		// ics= e.pageX - this.offsetLeft - margine;
		// $('.GalleryContainer .GalleryDidascalie').css({'top': ipsilon , 'left': ics});
	// }); 
	sopratooltip=false;
	$('.GalleryContainer .elementoGallery').bind('mouseenter', function(){
		testo=$(this).children().eq(0).children().eq(1).attr('alt');
		if ($(this).hasClass('foto1')){$('.GalleryContainer .GalleryDidascalie').css({'top': '140px', 'left': '85px'});}
		if ($(this).hasClass('foto2')){$('.GalleryContainer .GalleryDidascalie').css({'top': '90px', 'left': '220px'});}
		if ($(this).hasClass('foto3')){$('.GalleryContainer .GalleryDidascalie').css({'top': '120px', 'left': '350px'});}
		$('.GalleryContainer .GalleryDidascalie').children().eq(0).attr('href', $(this).children().eq(0).attr('href')).children().eq(0).html(testo);
		$('.GalleryContainer .GalleryDidascalie').stop(true, true).fadeIn();
		//.stop(true, true)
	}).bind('mouseleave', function(){
		if (!sopratooltip){
			$('.GalleryContainer .GalleryDidascalie').stop(true, true).fadeOut();
		}
	});
	$('.GalleryContainer .GalleryDidascalie').bind('mouseover', function(){
		sopratooltip=true;
	}).bind('mouseleave', function(){
		$(this).stop(true, true).fadeOut();
	});
	
	$('.BoxGallery .elementoGallery').bind('mouseenter', function(){
		testo=$(this).children().eq(1).attr('alt');
		if ($(this).hasClass('foto1')){$('.BoxGallery .GalleryDidascalie').css({'top': '135px', 'left': '20px'});}
		$('.GalleryDidascalie').stop(true, true).fadeOut();
		$(this).next('.GalleryDidascalie').stop(true, true).fadeIn().children().eq(0).html(testo);
		//.stop(true, true)
	}).bind('mouseleave', function(){
		if (!sopratooltip){
			$(this).next('.GalleryDidascalie').stop(true, true).fadeOut();
		}
	});
	$('.BoxGallery .GalleryDidascalie').bind('mouseover', function(){
		sopratooltip=true;
	}).bind('mouseleave', function(){
		$(this).stop(true, true).fadeOut();
	});

	/* accessoutenti top*/
	$('#user').focus(function(){
			if ($(this).val()=='Nome Utente')
				this.value='';
		}).blur(function(){
			if(this.value=='')
				this.value='Nome Utente';
	});

	$('#password').focus(function(){ 
		password_change();
	});
	$('.myKids .myKidsSend').click(function(){
		if ($(this).val()=="entra"){
		
			var loc = $('base').attr("href");
			location.href=(loc+'mykids/0/mykids.html?act=0&username0='+$('#user').val()+'&password0='+$('#password').val()+'&article='+$('#article').val());
			return false;
		}
	});
	
	
	//ricerca
	$('.CampoSubmit').click(function(){
		if (($('.CampoSearch').val()=='chiave di ricerca')||($('.CampoSearch').val()=='')){
			alert("inserire un valore di ricerca nel campo");
			return false;
		} else {
			var loc = $('base').attr("href");
			location.href=(loc+"ricerca.jsp?query="+$('.CampoSearch').val());
		}
	});
	$('.CampoSearch').focus(function(){
			this.value='';
		}).blur(function(){
			if(this.value=='')
				this.value='chiave di ricerca';
		}).keypress(function(event) {
		if (event.keyCode == '13') {
			if (($('.CampoSearch').val()=='chiave di ricerca')||($('.CampoSearch').val()=='')){
			alert("inserire un valore di ricerca nel campo");
			return false;
			} else {
				var loc = $('base').attr("href");
				location.href=(loc+"ricerca.jsp?query="+$('.CampoSearch').val());
			}
		}
	});
	
	//gallery semplice gallery 3
	$('.jackgallery3').mouseenter(function (){
		if ($('.jackgallery3 .tbcont .slider img').length>1){
			$('.jackgallery3 .tbcont').stop(true, true).slideDown();
		}
	}).mouseleave(function (){
		$('.jackgallery3 .tbcont').stop(true, true).slideUp();
	});
	$('.jackgallery3 .tbcont img').mouseenter(function(){
		$(this).stop().animate({'opacity':'1'});
	}).mouseleave(function(){
		$(this).stop().animate({'opacity':'0.7'});
	}).click(function(){
		clearInterval(gallery3interval);
		$questo=$(this);
		$('.jackgallery3 .main').fadeOut(500, function(){
			$('.jackgallery3 .main').attr('src', '').attr('alt', '');
			$('.jackgallery3 .main').attr('src', $questo.attr('title')).attr('alt', $questo.attr('alt')).load(function(){
				$('.jackgallery3 .main').show();
				$('.jackgallery3 .fade').hide();
			});
		});
		$('.jackgallery3 .fade').fadeIn(300).attr('src', $(this).attr('title')).attr('alt', $(this).attr('alt'));
		$('.jackgallery3 .tbcont img').removeClass('actual');
		$(this).addClass('actual');
		$('.jackgallery3 .didascalia').html($questo.attr('alt'));
		gallery3interval=setInterval('gallery3scorri()', 6000);
		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'));
	
	$('.jackgallery3 .scorrisinistra').click(function(){
		clearInterval(gallery3interval);
		gallery3scorriindietro();
		$('.jackgallery3 .tbcont').stop(true, true).slideUp();
		gallery3interval=setInterval('gallery3scorri()', 6000);
	});
	$('.jackgallery3 .scorridestra').click(function(){
		clearInterval(gallery3interval);
		gallery3scorri();
		$('.jackgallery3 .tbcont').stop(true, true).slideUp();
		gallery3interval=setInterval('gallery3scorri()', 6000);
	});
	
	
	//pulsnte aggiungi ai preferiti asincrono
	$('.preferiti').click(function(){
		if ($('.preferiti').attr("articolo")!=null){
		$.ajax({
			type: 'get', 
			data: 'idelemento='+$('.preferiti').attr("articolo")+'&elemento=articolo',
			url: 'preferiti.jsp',
			success: function(result){
				if (result!='error'){
					$('.preferiti').removeAttr('href').removeAttr('articolo').removeClass('preferiti').addClass('text').html('Articolo aggiunto ai tuoi Preferiti').css({'float':'none','color': '#3F3F3F' });
					Cufon.replace('.text', { fontFamily: 'Century Gothic' });
					cufon.now();
				} else {
					alert('si č verificato un errore. riprovare pių tardi.');
				}
			}
			});
		}
		if ($('.preferiti').attr("gallery")!=null){
		$.ajax({
			type: 'get', 
			data: 'idelemento='+$('.preferiti').attr("gallery")+'&elemento=gallery',
			url: 'preferiti.jsp',
			success: function(result){
				if (result!='error'){
					$('.preferiti').removeAttr('href').removeAttr('gallery').removeClass('preferiti').addClass('text').html('Fotogallery aggiunta ai tuoi Preferiti').css({'float':'none','color': '#3F3F3F' });
					Cufon.replace('.text', { fontFamily: 'Century Gothic' });
					cufon.now();
				} else {
					alert('si č verificato un errore. riprovare pių tardi.');
				}
			}
			});
		}
		return false;
	});
	
	
	//external link
	$('.ExternalLink').click(function(){
		window.open($(this).attr('href'));
		return false;
	});
	
	//bottone cambio password
	$('.PaginaIntera .Bottone').mouseover(function(){
		$(this).css('background-image', 'url(images/Btn300x25hover.png)');
	}).mouseout(function(){
		$(this).css('background-image', 'url(images/Btn300x25.png)');
	});
	
	//caratteri massimi testo commento
	if ($('#add_comments_text').length>0){
		var caratteri_massimi=1000;
		if ($('#add_comments_text').val().length>caratteri_massimi){
			$('#add_comments_text').val($('#add_comments_text').val().substring(0, caratteri_massimi));
		}
		$('.caratteridisp b').html(caratteri_massimi-($('#add_comments_text').val().length))
		
		$('#add_comments_text').keyup(function(){
			if ($('#add_comments_text').val().length>caratteri_massimi){
				$('#add_comments_text').val($('#add_comments_text').val().substring(0, caratteri_massimi));
			}
			$('.caratteridisp b').html(caratteri_massimi-($('#add_comments_text').val().length))
		}).keydown(function(){
			if ($('#add_comments_text').val().length>caratteri_massimi){
				$('#add_comments_text').val($('#add_comments_text').val().substring(0, caratteri_massimi));
			}
			$('.caratteridisp b').html(caratteri_massimi-($('#add_comments_text').val().length))
		});	
	}
	
	$('.commento').click(function(){
		$('.add_comment').slideDown(500);
		var targetOffset = $('.add_comment').offset().top;
		$('html,body').animate({scrollTop: targetOffset}, 1000);

		return false;
	});
	
	
});


