// JavaScript Document
(function($) {
	$.extend($.fx.step,{
	    backgroundPosition: function(fx) {
			var height=32;
            if (fx.state === 0 && typeof fx.end == 'string') {
                var start = $.curCSS(fx.elem,'backgroundPosition');
                start = toArray(start);
                fx.start = [start[0],start[2]];
                var end = toArray(fx.end);
                fx.end = [end[0],end[2]];
                fx.unit = [end[1],end[3]];
				fx.status=(fx.end[1]==0);
			}
			if(fx.status){
				var i=0;
				var f=true;
				while(i<5 && f){
					if(fx.start[1]<(-((height*(5-i))-(height/2)))){
						fx.start[1]=-(height*(5-i));
						f=false;
					}
					i++;
				}
			}else{
				var i=0;
				var f=true;
				while(i<5 && f){
					if(fx.start[1]<(-((height*i)+(height/2)))){
						fx.start[1]=-(height*i);
						f=false;
					}
					i++;
				}
			}
			var step=-(Math.round(fx.end[1]/height))-(Math.round(fx.start[1]/height));
			var pos;
			if(step<1){
				pos=0;
			}else{
				pos=1/step;
			}
				
			if(fx.pos<0.2){
				pos*=1;
			}else if(fx.pos<0.4){
				pos*=2;
			}else if(fx.pos<0.6){
				pos*=3;
			}else if(fx.pos<0.8){
				pos*=4;
			}else{
				pos*=5;
			}
			if(pos>1){
				pos=1;
			}
			pos=Math.round(pos*100)/100;
			var nowPosX=[];
			nowPosX[0] = ((fx.end[0] - fx.start[0]) * pos) + fx.start[0] + fx.unit[0];
            nowPosX[1] = Math.round(((fx.end[1] - fx.start[1] ) * pos) + fx.start[1]) + fx.unit[1];

			var x1=-fx.start[1];
			var x2=-fx.end[1];
			
			fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];

           function toArray(strg){
               strg = strg.replace(/left|top/g,'0px');
               strg = strg.replace(/right|bottom/g,'100%');
               strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
               var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
               return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
           }
        }
	});
})(jQuery);

$(function(){
	$('#main .link a')
		.css( {backgroundPosition: "0 0"} )
		.mouseover(function(){
			$(this).stop().animate({backgroundPosition:"(0 -160px)"}, {duration:500})
		})
		.mouseout(function(){
			$(this).stop().animate({backgroundPosition:"(0 0)"}, {duration:500})
		})
});

function effect(id,speed,opacity,delay){
	var s='';
	if(opacity==1){
		s='$("#'+id+'").show();';
	}
	setTimeout(s+'$("#'+id+'").fadeTo('+speed+','+opacity+',function(){if(!'+opacity+')$("#'+id+'").hide()})',delay);
}
function prep(){
	$("#frase1").hide();
	$("#frase1").css("opacity","0");
	$("#frase2").hide();
	$("#frase2").css("opacity","0");
	for(i=1;i<9;i++){
		setTimeout('$("#img'+i+'").css("opacity","0");$("#img'+i+'").hide();',0);
	}
}
var immagini=[];
var time=0;
var delayFrase=800;
var delayLoghi=500;
var delayFade=2000;
var fadeIn=700;
var fadeOut=500;

function animazione1(){
	var time=0;
	effect('frase1',500,1,time);
	time+=delayFrase;
	for(i=0;i<immagini.length;i++){
	    effect('img'+immagini[i],fadeIn,1,time);
		time+=delayLoghi;
	}
	time+=delayFade;
	for(i=0;i<immagini.length;i++){
	    effect('img'+immagini[i],fadeOut,0,time);
	}
	effect('frase1',500,0,time);
	
	return time;
}
function animazione2(){
	var time=0;
	effect('frase2',500,1,time);
	time+=delayFrase;
	for(i=0;i<immagini.length;i++){
	    effect('img'+immagini[i],fadeIn,1,time);
		time+=delayLoghi;
	}
	time+=delayFade;
	for(i=0;i<immagini.length;i++){
	    effect('img'+immagini[i],fadeOut,0,time);
	}
	effect('frase2',500,0,time);
	
	return time;
}
function play(){
	var delay1=delayFrase+6*delayLoghi+delayFade+1000;
	var delay2=delay1+delayFrase+4*delayLoghi+delayFade+1000;
	
	setTimeout('caricaLoghi(0)',0);
	setTimeout('animazione1()',500);	
	setTimeout('caricaLoghi(1)',delay1);
	setTimeout('animazione2()',delay1+500);	
	setTimeout('play()',delay2+500);
}
$(document).ready(function(){
	$("#frase1").hide();
	$("#frase1").css("opacity","0");
	$("#frase2").hide();
	$("#frase2").css("opacity","0");
	for(i=1;i<38;i++){
		setTimeout('$("#img'+i+'").css("opacity","0");$("#img'+i+'").hide();',0);
	}
	play();
});

function istanzaXHR() {
	var obj;
	if (window.XMLHttpRequest) {
		obj = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		obj = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return obj;
}

loghi=istanzaXHR();

function onStateChange() {
	if (loghi.readyState == 4) {
		if (loghi.status == 200) {
			immagini=loghi.responseText.split(";");
		} else {
		}
	}	
}

function caricaLoghi(i) {
	try {	
		loghi.open("POST", "pages/loghi.php", true);
		loghi.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		loghi.onreadystatechange = onStateChange;
		loghi.send("loghi="+i);
	} catch (e) {
	}
}

