/*Funzioni Per Jquery*/
$(document).ready(
	function () {
		slida();
		apriDettagli('');
		navColonna('');
		paginazioneAjax('');
		svuotaCampo('');
		scrollaTo('');
		menuDiscesa('');
		goTopHs('');
	}
);

function goTopHs(idElemento){
	$("a.back2Top").hide();
	$(window).scroll(function(){
		if($(window).scrollTop()!='0')	$("a.back2Top").fadeIn("slow");
		else							$("a.back2Top").fadeOut("slow");
	});
}

function menuDiscesa(idElemento){
	$('.dropdown').each(function () {
		$(this).parent().eq(0).hoverIntent({
			timeout: 200,
			over: function () {
				var current = $('.dropdown:eq(0)', this);
				current.slideDown(100);
			},
			out: function () {
				var current = $('.dropdown:eq(0)', this);
				current.fadeOut(200);
			}
		});
	});
}

function svuotaCampo(idElemento){
	$("input.svuotabile").focus(function(){
		if($(this).val()==$(this).attr('title'))	$(this).val('');
	});
	$("input.svuotabile").blur(function(){
		if($(this).val()=='')	$(this).val($(this).attr('title'));
	});
}

function paginazioneAjax(idElemento){
	$("#divNavigaHome a").click(function(){
		qs=$(this).attr("href");
		qs=qs.substring(qs.indexOf("?"));
		if(!$(this).hasClass("prevDisattivo")&&!$(this).hasClass("nextDisattivo")){
			if(qs!=''){
				$.ajax({
					url: "../ajax/paginaNotizieHome.asp"+ qs,
					data: ({}),
					dataType: "html",
					cache: false,
					beforeSend: function(msg){
					},
					success: function(msg){
						$("#divNotiziaHome").html(msg);
						$.ajax({
							url: "../ajax/navigazioneNotizieHome.asp"+ qs,
							data: ({}),
							dataType: "html",
							cache: false,
							beforeSend: function(msg){
							},
							success: function(msg){
								$("#divNavigaHome").html(msg);
								paginazioneAjax("");
							},
							error: function(XMLHttpRequest, textStatus, errorThrown){}
						});	
					},
					error: function(XMLHttpRequest, textStatus, errorThrown){}
				});		
			}
		}
		return false;
	});
}

function navColonna(idElemento){
	$(idElemento +'#pulsNP a').click(function(){
		xanno=$(this).attr("title");
		qs=$(this).attr("href");
		cliccato=$(this);
		tr=$(cliccato).closest("tr");
		
		if(qs.indexOf("/eventi")!=-1)
			pag="eventi"
		else if(qs.indexOf("/news")!=-1)
			pag="news"
		else
			pag="archivio"

		$.ajax({
			url: assoluto +"ajax/colonnaNotizie.asp?anno="+ xanno +"&pag="+ pag,
			data: ({}),
			dataType: "html",
			cache: false,
			beforeSend: function(msg){
			},
			success: function(msg){
				colonna=msg;
				$.ajax({
					url: assoluto +"ajax/pulsantiNP.asp?anno="+ xanno +"&pag="+ pag,
					data: ({}),
					dataType: "html",
					cache: false,
					beforeSend: function(msg){
					},
					success: function(msg){
						$("#contieniElenco").html(colonna);
						$("#pulsNP").html(msg);
						
						scrollWin ("#box","slow");
						
						apriDettagli('');
						navColonna('');
					},
					error: function(XMLHttpRequest, textStatus, errorThrown){
						
					}
				});
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				
			}
		});
		
		return false;
	});
}

function apriDettagli(idElemento){
	$(idElemento +"#leftCol .opener").click(function(){
		divVicino=$(this).next("div");
		if($(divVicino).is(":visible")){
			$(divVicino).slideUp();
		}
		else{
			$("div.raggruppaGiorni").not($(divVicino)).slideUp("slow")
			$(divVicino).slideDown();
			$("#leftCol .opener").removeClass("opened");
			$(this).addClass("opened");
		}

	});
}

function slida(){
    $(".slides").slides({
		generateNextPrev: true,
		paginationClass: 'dotsPagination',
		//play: 6000,
		pause: 500,
		hoverPause: true,
		slideSpeed: 600
	});
	$(".slidesAjax").slides({
		generateNextPrev: true,
		generatePagination: false,
		play: 5000,
		pause: 500,
		slideSpeed: 600,
		hoverPause: true,
		animationComplete: function(current){
			div=$(".prodImage:visible").closest("div.containerSliderAjax");
			codice=$(".prodImage:visible").find("img").attr("id").substring(4,900);
			$.ajax({
				url: assoluto +"ajax/prodottiHome.asp?codice="+ codice,
				data: ({}),
				dataType: "html",
				cache: false,
				beforeSend: function(msg){
				},
				success: function(msg){
					$(".prodDetails").html(msg);
				},
				error: function(XMLHttpRequest, textStatus, errorThrown){
					
				}
			});
		}
	});
}

//scroll to top
function scrollaTo(idElemento){
	$("a[href^='#']").click(function(e) {
		if(isNaN($(this).attr("href").replace("#",""))){
			scrollWin ($(this).attr("href"),"slow");
			return false;
		}
	});
	
	//Reindirizzo la pagina al punto corretto
	if(window.location.hash!=""){
		doveHash=window.location.hash.replace("#","") +"pag";
		scrollWin($("a[name='"+ doveHash +"']"),"slow");
	}
}
