/*Funzioni Per Jquery*/
$(document).ready(
	function () {
		imageRotatore();
		shdDiv('');
		caricaElencoMesi('');
		caricaElencoMesiEventi('');
		caricaVideo('');
		paginazioneVideo('');
		submitForm('');
		vaiALogin('');
		switchProdottiHome('');
		switchVideoImg('');
		inviaSelectChange('');
		paginazioneProdotti('');
	}
);

function imageRotatore(){
	$(".paging").show();
	$(".paging a:first").addClass("active");
	
	//Get size of images, how many there are, then determin the size of the image reel.
	var imageWidth = $(".window").width();
	var imageSum = $(".image_reel img").size();
	var imageReelWidth = imageWidth * imageSum;
	
	//Adjust the image reel to its new size
	$(".image_reel").css({'width' : imageReelWidth});
	
	rotate= function(){
		triggerID=$(".paging a").index($(".paging a.active"))+1;
		if(triggerID==imageSum) triggerID=0;
		image_reelPosition = triggerID * imageWidth;
		
		$(".paging a").removeClass('active');
		$(".paging a").eq(triggerID).addClass('active');

		//Slider Animation
		indirizzoTesto=$(".image_reel img").eq(triggerID).attr("src").replace(".jpg",".htm");
		$.ajax({
			url: indirizzoTesto,
			data: ({}),
			dataType: "html",
			cache: false,
			beforeSend: function(msg){},
			success: function(msg){
				$("#testoRotator").html(msg);
				hovera();
				
				$(".image_reel").animate({ 
					left: -image_reelPosition
				}, 500 );
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){}
		});
		
		t=setTimeout(rotate,3000);
	}
	
	//On Hover
	hovera= function(){
		$(".image_reel a,#testoRotator a").hover(function() {
			clearTimeout(t); //Stop the rotation
		}, function() {
			t=setTimeout(rotate,3000); //Resume rotation
		});	
	}
	hovera();
	
	//On Click
	$(".paging a").click(function() {	
		clearTimeout(t);
		triggerID=$(".paging a").index($(this))-1;
		$(".paging a").removeClass('active');
		$(".paging a").eq(triggerID).addClass('active');

		rotate(); //Trigger rotation immediately

		return false; //Prevent browser jump to link anchor
	});
	
	var t=setTimeout(rotate,3000);
}

function caricaVideo(idElemento){
	$(idElemento +".selezionaVideo").click(function(){
		qs=$(this).attr("href");
		qs=qs.substring(qs.indexOf("?"));
		cliccato=$(this);
		$.ajax({
			url: "../ajax/video_youtube.asp"+ qs,
			data: ({}),
			dataType: "html",
			cache: false,
			beforeSend: function(msg){
				$("#divVideoTube").html("<img src=\"../images/struttura/loader.gif\">");
			},
			success: function(msg){
				$("#divVideoTube").html(msg);
				$(idElemento +".selezionaVideo").removeClass("selected");
				$(cliccato).addClass("selected");
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){}
		});		
		
		return false;
	});
}

function submitForm(idElemento){
	$(".inviaForm").click(function(){
		$(this).parents("form").submit();
	});
}

function paginazioneProdotti(idElemento){
	$("#paginazione a").click(function(){
		qs=$(this).attr("href");
		qs=qs.substring(qs.indexOf("?"));
		if(qs!=''){
			$.ajax({
				url: "../ajax/paginaProdotti.asp"+ qs,
				data: ({}),
				dataType: "html",
				cache: false,
				beforeSend: function(msg){
					$("#prodottiHome").css("text-align","center").html("<img src=\"../images/struttura/loader.gif\" class=\"loader\">");
				},
				success: function(msg){
					$("#prodottiHome").css("text-align","left").html(msg);
					switchProdottiHome("#prodottiHome ");
					$.ajax({
						url: "../ajax/navigazioneProdotti.asp"+ qs,
						data: ({}),
						dataType: "html",
						cache: false,
						beforeSend: function(msg){
							$("#paginazione").html("...");
						},
						success: function(msg){
							$("#paginazione").html(msg);
							paginazioneProdotti("");
						},
						error: function(XMLHttpRequest, textStatus, errorThrown){}
					});
				},
				error: function(XMLHttpRequest, textStatus, errorThrown){}
			});		
		}
		return false;
	});
}

function paginazioneVideo(idElemento){
	$("#videoNav a").click(function(){
		qs=$(this).attr("href");
		qs=qs.substring(qs.indexOf("?"));
		if(qs!=''){
			$.ajax({
				url: "../ajax/paginaVideoTube.asp"+ qs,
				data: ({}),
				dataType: "html",
				cache: false,
				beforeSend: function(msg){
					$("#videoElenco").css("text-align","center").html("<img src=\"../images/struttura/loader.gif\">");
				},
				success: function(msg){
					$("#videoElenco").css("text-align","left").html(msg);
					caricaVideo("#videoElenco ");
					$.ajax({
						url: "../ajax/navigazioneVideoTube.asp"+ qs,
						data: ({}),
						dataType: "html",
						cache: false,
						beforeSend: function(msg){
							$("#videoNav").html("...");
						},
						success: function(msg){
							$("#videoNav").html(msg);
							paginazioneVideo("");
						},
						error: function(XMLHttpRequest, textStatus, errorThrown){}
					});	
				},
				error: function(XMLHttpRequest, textStatus, errorThrown){}
			});		
		}
		return false;
	});
}

function vaiALogin(idElemento){
	$(".apriLoginRiservata").click(function(){
		scrollWin($("#colNews"),"slow");
		$("#DivFormLogin").hide().slideDown("slow",function(){
			mostraMessaggio(1,"Contenuti riservati","Per visualizzare i contenuti richiesti esegui l'accesso nel box login in alto a sinistra.","","");
		});
	});
}

function caricaElencoMesi(idElemento){
	$("#DivElencoAnni a").click(function(){
		qs=$(this).attr("href");
		qs=qs.substring(qs.indexOf("?"));
		title=$(this).attr("title");
		$.ajax({
			url: "../ajax/caricaMesi.asp"+ qs,
			data: ({}),
			dataType: "html",
			cache: false,
			beforeSend: function(msg){},
			success: function(msg){
				$(".selAnno span").html(title);
				$("#DivElencoAnni").slideToggle("slow",function(){
					$("#DivElencoMese").html(msg);
					$("#DivElencoMese").slideDown("slow");
				});
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){}
		});		
		
		return false;
	});
}

function caricaElencoMesiEventi(idElemento){
	$("#DivElencoAnniEventi a").click(function(){
		qs=$(this).attr("href");
		qs=qs.substring(qs.indexOf("?"));
		title=$(this).attr("title");
		$.ajax({
			url: "../ajax/caricaMesiEventi.asp"+ qs,
			data: ({}),
			dataType: "html",
			cache: false,
			beforeSend: function(msg){},
			success: function(msg){
				$(".selAnno span").html(title);
				$("#DivElencoAnniEventi").slideToggle("slow",function(){
					$("#DivElencoMese").html(msg);
					$("#DivElencoMese").slideDown("slow");
				});
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){}
		});		
		
		return false;
	});
}

function inviaSelectChange(idElemento){
	$(".changeSelect").change(function(){
		$(this).parents("form").submit();
	});
}

function switchProdottiHome(idElemento){
	$("#schedaProdotto li a").click(function(){
		qs=$(this).attr("href");
		qs=qs.substring(qs.indexOf("?"));
		cliccato=$(this);
		$.ajax({
			url: "../ajax/caricaProdotti.asp"+ qs,
			data: ({}),
			dataType: "html",
			cache: false,
			beforeSend: function(msg){
				$("#dettaglioProdotto").css("text-align","center").html("<img src=\"../images/struttura/loader.gif\" class=\"loader\">");
			},
			success: function(msg){
				$("#schedaProdotto a").removeClass("selected");
				$(cliccato).addClass("selected");
				$("#dettaglioProdotto").css("text-align","left").html(msg);
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){return true;}
		});		
		
		return false;
	});
}

function switchVideoImg(idElemento){
	$("#scambia a").click(function(){
		qs=$(this).attr("href");
		qs=qs.substring(qs.indexOf("?"));
		cliccato=$(this);
		$.ajax({
			url: "../ajax/caricaVideoProdotti.asp"+ qs,
			data: ({}),
			dataType: "html",
			cache: false,
			beforeSend: function(msg){
				$("#contenitoreDettProd").css("text-align","center").html("<img src=\"../images/struttura/loader.gif\" class=\"loader\">");
			},
			success: function(msg){
				$("#scambia a").removeClass("selected");
				$(cliccato).addClass("selected");
				$("#contenitoreDettProd").css("text-align","left").html(msg);
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){return true;}
		});		
		
		return false;
	});
}

function shdDiv(idElemento){
	//Mostro o nascondi i div al click su un link con class shd. Parametri: Tipo di link, Tipo di comparsa del div, Id del div
	//shd01biblio
	$(idElemento +"a[class*='shd']").click(function(){
		classe=$(this).attr("class");
		myregexp = new RegExp(/(shd[^ ]*)/);
		mymatch = myregexp.exec(classe);
		linkato=$(this);
		if(mymatch!=null){
			valore=mymatch[1].replace("shd","");
			tipologia=valore.substring(0,1);comparsa=valore.substring(1,2);idDiv=valore.substring(2);
			
			if($("#"+ idDiv).is(":hidden")){
				if(tipologia=='1') $(this).html("Chiudi scheda modifica");
				if(comparsa=='1')	$("#"+ idDiv).slideDown("slow",function(){$(linkato).addClass("chiuso")});
				else if(comparsa=='2') $("#"+ idDiv).show();
			}
			else{
				if(tipologia=='2' && comparsa=='1')
					$("#"+ idDiv).slideToggle("slow",function(){
						$("#"+ idDiv.replace("campoEsteso","menoEsteso")).show();
					});
				else{
					if(tipologia=='1') $(this).html("Apri scheda modifica");
					if(comparsa=='1') $("#"+ idDiv).slideToggle("slow",function(){$(linkato).removeClass("chiuso")});
					else if(comparsa=='2') $("#"+ idDiv).hide();
				}
				//if(tipologia=='2') $("#"+ idDiv.replace("campoEsteso","menoEsteso")).show();
			}
			return false;
		}	
	});
}
