// ActionScript file
var numeroElementi=0;	// Numero totale dei lavori presenti nel sito
var tempX = 0;
var tempY = 0;
var muovi=true;
var posY;				// Posizione y di tutto il contenuto del sito
var altezzaContenitore=0;
var myWidth = 0;
var myHeight = 0;
var maxSpeed=30;
var minSpeed=1;
var timer=0;
var timerTasto=0;
var idSchedaScelta=0;

var marginLeftCentratura=0;


var timerMouse=0;
var muoviConMouse=true;




$(function() {
		   $(this).mousewheel(function(event, delta) {
		   		clearInterval(timerTasto);
				clearInterval(timer);
						if(!IE)
								{				
								var elemento=document.getElementById('centratura').offsetHeight;
								altezzaContenitore = document.getElementById("centratura").offsetHeight;
								}
							else
								{
								altezzaContenitore= $("#centratura").innerHeight();
								}
								
					if(delta>0&&posY<0)
						{
						posY=parseInt($("#centratura").css("top"))+delta*30;
						$("#centratura").css("top",posY+"px");
						if(parseInt($("#centratura").css("top"))>=0)
							{
							$("#centratura").css("top","0px");
							}
						}
					if(delta<0&&posY>-(altezzaContenitore-altezza()))
						{

						posY=parseInt($("#centratura").css("top"))+delta*30;
						$("#centratura").css("top",posY+"px");
						}
						
					event.stopPropagation();
					event.preventDefault();
					
					});
			});





// Variabile che tiene conto se il browser  internet exploer.

var IE = document.all?true:false;

// Se  internet explorer cattura il moviemento del mouse

if (!IE) document.captureEvents(Event.MOUSEMOVE)




// Quando il documento  pronto vengono inizializzate le prime azioni

$(document).ready(function(){	
	numeroElementi=getElementsByClassName("singolaSchedaEsterna");
	posY=parseInt($("#centratura").css("top"));	
	
	Shadowbox.init();
	
	$(".chiudiLogIn").click(chiudiLogIn);
	$(".chiudiSingolaScheda").click(chiudiSingola);
	$(".sez").click(apriPerScelta);
	$(".clip").click(appariIngrandito);
	
	inizializzaElementi();
	inizializzaSingolaImmagine();
	
	
	$(window).resize(function(){				// Ridimensiona l'altezza del contenitore principale ogni volta che espando la grandezza della pagina
	
	//marginLeftCentratura=($(this).width()-600)/2;
	//	$("#centratura").css("left",marginLeftCentratura+"px");
	 $(".esternoPrincipale").css("height",altezza()+"px");
	});
	
	$(".esternoPrincipale").css("height",altezza()+"px"); // Setta l'altezza del contenitore principale come l'altezza di tutta la pagina html
	
	$("").mousemove(getMouseXY);		// Prendo la x e la y del mouse ogni volta che lo muovo
	
	
	
	$(window).keydown(noNumbers);
	$(window).keyup(tastoSu);
	
	$("#pass").val("");
	$("#logIn").click(openPanelLogIn);
	$("#send").click(sendReq);
	
	
	});


function sendReq()	{
	$.post("login.php", { user : $("#username").val(), pass:$("#pass").val()  },
			  function(data){
			  $("#req").html(data);
			  $("#req").slideDown("slow"); 
			  });
	}

function openPanelLogIn()	{
		$("#contClientLogin").slideDown("slow");
	}

function tastoSu()	{
		idDown=false;
		clearInterval(timerTasto);
	}
	
	


// P R E N D E  I L   T A S T O 

function noNumbers(e){
var keynum;
var keychar;
var numcheck;

isDown=true;

if(window.event) // IE
  {
  keynum = e.keyCode;
  }
else if(e.which) // Netscape/Firefox/Opera
  {
  keynum = e.which;
  }
 if(parseInt(keynum)==40)
 	{
		posY+=10;
	}
if(parseInt(keynum)==38)
	{
		posY-=10;
	}
	
	iniziaAMuovere(parseInt(keynum));
	
}


function iniziaAMuovere(num)	{
		clearInterval(timerTasto);
		clearInterval(timer);
		clearInterval(timerMouse);
		timerTasto=window.setTimeout("muoviConFrecce('1',"+num+")",0);
		}
	
function muoviConFrecce(valore,dir)	{
	posY=parseInt($("#centratura").css("top"));
	if(!IE)
			{				
			var elemento=document.getElementById('centratura').offsetHeight;
			altezzaContenitore = document.getElementById("centratura").offsetHeight;
			}
		else
			{
			altezzaContenitore= $("#centratura").innerHeight();
			}
			
	if(valore=="1")
		{
		timerTasto=window.setInterval("muoviConFrecce('0',"+dir+")",1);
		}
		
	if(parseInt(dir)==38)
		{
			if(posY<0)
				{
					posY+=10;
				}
				else
				{
					posY=0;
				}
		}
	
	if(parseInt(dir)==40)
		{
				
				if(posY>-(altezzaContenitore-altezza()))
					{
						posY-=10;
					}
		}
	$("#centratura").css("top",posY+"px");
	
	}

// Calcola l'altezza della pagina

function altezza()	{
   	 return $(this).height(); 
	}
	
// Restituisce il numero di nodi che hanno una stessa classe

function getElementsByClassName(className, tag, elm) {
	var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
	var tag = tag || "*";
	var elm = elm || document;
	var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
	var returnElements = [];
	var current;
	var length = elements.length;
	for (var i=0; i<length; i++) {
		current = elements[i];
		if (testClass.test(current.className)) {
			returnElements.push(current);
		}
	}
	return returnElements.length;
}


// Calcola la x e la y del mouse

function getMouseXY(e) {

	//var offset = $("#centratura").offset();
	//alert("OFFSET JQUERT = "+ $("#centratura").innerHeight());
	
//	alert("OFFSET NORMALE = "+document.getElementById('centratura').offsetHeight);

  if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft
    tempY = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX
    tempY = e.pageY
  }  
 
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}  
  if(tempY<30&&tempY>0)
  	{
		muovi=true;
		if(muoviConMouse)
			{
			muoviConMouse=false;
			muoviInAlto();
			}
	}
  else if(tempY>altezza()-30)
  	{
		altezzaContenitore = document.getElementById("centratura").offsetHeight;
		if(!IE)
			{				
			//alert("ALTEZZA CONTEINTORE"+altezzaContenitore);
			var elemento=document.getElementById('centratura').offsetHeight;
			
			
			}
		else
			{
			//	alert("ALTEZZA CONTEINTORE EXPLORER CACCA"+altezzaContenitore);
			altezzaContenitore= $("#centratura").innerHeight();
			
			}
		muovi=true;
		if(muoviConMouse)
			{
			muoviConMouse=false;
			muoviInBasso();
			}
	}
	else
		{
		//alert("FUORI");
			muoviConMouse=true;
			clearInterval(timerMouse);
			muovi=false;
		}
  return true
}

// Muove il sito in basso 

function muoviInBasso()	{
	//alert(altezza());
	//alert(altezzaContenitore);
		clearInterval(timer);		// Cancella il timer che riposta la pagina alla posizione iniziale
		clearInterval(timerTasto);
		if(muovi)
			{
			
			//	alert("ALTEZZA CONTENITORE = "+altezzaContenitore);
				
				if(posY>-(altezzaContenitore-altezza()))
				{
						//posY-=(10-(myHeight-tempY))*maxSpeed/30;
						//posY-=30;
						//alert("MUOVI IN BASSO");
						//scrollaMouse(1,posY)	
						timerMouse=window.setInterval("mouseAlLimite(-1)",1);
						
						//mouseAlLimite(posY)
						
					//	$("#centratura").css("top",posY+"px");
					//	$('body').stop();
					//	$("body").animate({margin:"0px"},10,"linear",muoviInBasso);
						
				}
				else
					{
					clearInterval(timerMouse);
					}
			}
	}


function mouseAlLimite(dir)
	{
		
		posY+=dir;
		
		
		
		if(dir<0)
			{
			posY-=maxSpeed-(altezza()-tempY);
			if(posY<-(altezzaContenitore-altezza()))
				{
				clearInterval(timerMouse);
				}
			}
		if(dir>0)
			{
			posY+=maxSpeed-(tempY);
			if(posY>0)
				{
				clearInterval(timerMouse);
				$("#centratura").css("top","0px");
				}
			}	
			
		$("#centratura").css("top",posY+"px");
		
		if(posY>0)
				{
				//clearInterval(timerMouse);
				$("#centratura").css("top","0px");
				}
	}

// Muove il sito in alto


function muoviInAlto()	{
		clearInterval(timer);	  // Cancella il timer che riposta la pagina alla posizione iniziale
		clearInterval(timerTasto);
		if(muovi)	
			{
				if(posY<0)
					{
					timerMouse=window.setInterval("mouseAlLimite(1)",1);
					}
				else
					{
						clearInterval(timerMouse);
						$("#centratura").css("top","0px");
					}
			}

	}

function inizializzaElementi() {
	$(".titoloSingolaScheda").css(opacity=0.5);
	$(".titoloSingolaScheda").mouseover(sopra);
	$(".titoloSingolaScheda").mouseout(fuori);
	$(".titoloSingolaScheda").click(apri);
	$(".titoloSingolaScheda").attr("aperto","false");
	
	//$(".singolaSchedaEsterna").mouseover(posizione)
	}

function muoviSchede()	{
	
		if($(this).attr("attivo")=="true")
			{
		clearInterval(timer);
		var numero=parseInt(String($(this).attr("id")).slice(1));
		var spostaA=-743;
	//	spostaA-=calcolaSpazio(numero);
	//	spostaA=calcolaSpazio(numero);
		posY=parseInt($("#centratura").css("top"));
		spostaA=posY-calcolaSpazio(numero)+8;
		vaiA(spostaA);
			}
		//$("#centratura").stop();
		
		//$("#centratura").animate({"marginTop":spostaA+"px"},500,"linear",function(){posY=spostaA;$("#centratura").css("marginTop",posY+"px");});
	}
	
	
function calcolaSpazio(numero)	{
		var posizione=10;
		var p = $("#singolaScheda"+numero);
		var offset = p.offset();	
		posizione=offset.top;
		return posizione;
	}


function sopra() {	
	if ($(this).attr("aperto")=="false") {
		//	playSound();
			$(this).css({
			cursor:"pointer",
			backgroundColor:"rgb(0,0,0)", 
			fontWeight:"bolder",
			color: "rgb(255,255,255)"
			});
			//playSoundOver();
		}
	}
	
function fuori() {
	if ($(this).attr("aperto")=="false") {
			$(this).css({
			backgroundColor:"rgb(255,255,255)", 
			fontWeight:"normal",
			color: "rgb(0,0,0)"
			});
		}
	}

function apri() {
	
	if ($(this).attr("aperto")=="false") {
			$(this).attr("aperto","true");
			$(this).css({
			backgroundColor:"rgb(255,255,255)", 
			fontWeight:"normal",
			color: "rgb(0,0,0)",
			cursor: "default"
			});
			
			idSchedaScelta=this.id.slice(19);
		//	if( $("#titoloSingolaScheda"+idSchedaScelta).attr("vista")=="true")
		//		{
					$("#contenutoSingolaScheda" + idSchedaScelta).slideDown("slow",function(){$(this).parent().parent().attr("height",this.offsetHeight);});
		/*			}
			else
				{
		$.post("singolaScheda.php", { id : idSchedaScelta },
			  function(data){
				  $("#titoloSingolaScheda"+idSchedaScelta).attr("vista","true");
						$("#contenutoSingolaScheda" + idSchedaScelta).html(data);
						$(".chiudiSingolaScheda").click(chiudiSingola);
						Shadowbox.init();
						$(".sez").click(apriPerScelta);
						$(".clip").click(appariIngrandito);
						$("#contenutoSingolaScheda" + idSchedaScelta).slideDown("slow",function(){$(this).parent().parent().attr("height",this.offsetHeight);});
			  });
				}*/

			

			//$("#contenutoSingolaScheda"+(this.id.slice(19))).setAttribute("aperto", "true");
		}
	}

function chiudiSingola() {
	$(this).parent().slideUp("slow",function(){$(this).parent().parent().attr("height","23");$(".particolareSingolaScheda[id='_" + $(this).attr("id").slice(22) + "']").slideUp(10);});
	$(this).parent().prev().attr("aperto", "false");
}

function chiudiLogIn() {
	$(this).parent().slideUp("slow",function(){$("#req").html("");$("#req").slideUp("slow");});
}
	

function apriPerScelta() {
	var attuale=-1;
	var numero=parseInt($(this).attr("id").slice(3))-2;
	if (numero!="-1") {
		
		$(".singoloNumeroLavori[genere!='" + numero + "']").css("text-decoration","line-through");
		$(".singoloNumeroLavori[genere!='" + numero + "']").attr("attivo","false");
		$(".singoloNumeroLavori[genere!='" + numero + "']").hover(function(){$(this).css("text-decoration","line-through");},function(){$(this).css("text-decoration","line-through");});

		$(".singoloNumeroLavori[genere='" + numero + "']").css("text-decoration","none");
		$(".singoloNumeroLavori[genere='" + numero + "']").attr("attivo","true");
		
		$(".singolaSchedaEsterna[genere!='" + numero + "']").slideUp("slow");
				
		$(".singolaSchedaEsterna[genere='" + numero + "']").slideDown("slow");
		
		$(".singoloNumeroLavori[genere='" + numero + "']").css("text-decoration","none");
		$(".singoloNumeroLavori[genere='" + numero + "']").hover(function(){$(this).css("text-decoration","line-through");},function(){$(this).css("text-decoration","none");});
	} else {
		$(".singoloNumeroLavori").attr("attivo","true");
		$(".singoloNumeroLavori").css("text-decoration","none");
		$(".singoloNumeroLavori").hover(function(){$(this).css("text-decoration","line-through");},function(){$(this).css("text-decoration","none");});
		$(".singolaSchedaEsterna").slideDown("slow");
	}
}


function playSoundOver()	{
	$(".suono").append("<embed src='./suono.mp3' hidden='true' autostart='true' loop='false' />");
	}
	
	
function inizializzaSingolaImmagine()	{
	$("#logo").click(tornaSu);
	$(".singoloNumeroLavori").click(muoviSchede);
//	$(".singoloNumeroLavori").mouseover(appariNome);
//	$(".singoloNumeroLavori").mouseout(scompariNome);
	$(".singoloNumeroLavori").attr("opacity",0);
	}
	
function appariIngrandito()	{

	$(".particolareSingolaScheda[id='_" + $(this).attr("idLavoro") + "']").slideDown("slow");
	var dove=-240*$(this).attr("idClip");
	$(".contenitoreScrollabile[id='_" + $(this).attr("idLavoro") + "']").animate({top: dove+"px"}, 500,"swing" );
	}	





////////////////////////////////////////////////////////////////////////
//////// C O D I C E   P E R   T O R N A R E   I N   C I M A  //////////
////////////////////////////////////////////////////////////////////////


function tornaSu()	{
		clearInterval(timer);
		if(posY<0)
			{
		timer=window.setTimeout("scrolla('1')",0);
			}
		}
	
function scrolla(valore)	{
	if(valore=="1")
		{
		timer=window.setInterval("scrolla('0')",1);
		}
		
	if(posY<=30)
		{
		posY+=(posY)*(-1/15);
		$("#centratura").css("top",posY+"px");
		}
	else
		{
		clearInterval(timer);
		}
	}
	
function vaiA(posFinale)	{
		if(posY>posFinale)
			{
		timer=window.setTimeout("scrollaUp('1',"+posFinale+")",0);
			}
		}
		
function scrollaUp(valore,posFinale)	{
	if(valore=="1")
		{
		timer=window.setInterval("scrollaUp('0',"+posFinale+")",1);
		}
		
	if(posY>=posFinale-30)
		{
		posY+=(posY-posFinale)*(-1/15);
		$("#centratura").css("top",posY+"px");
		}
	else
		{
		clearInterval(timer);
		}
	}
	
	
function scrollaMouse(valore,posFinale)	{
		$("#centratura").css("top",posFinale+"px");
		timerMouse=window.setInterval("scrollaMouse('0',"+posFinale+")",1);
	}


