var slideshowSpeed = 4000;


$(document).ready(function(){
			


	var interval;
	var activeContainer = 1;	
	var currentImg = 0;
	var animating = false;
	var navigate = function(direction) {
		if(animating) {
			return;
		}
		
		// Check which current image we need to show
		if(direction == "next") {
			currentImg++;
			if(currentImg == photos.length + 1) {
				currentImg = 1;
			}
		}
		
		// Check which container we need to use
		var currentContainer = activeContainer;
		if(activeContainer == 1) {
			activeContainer = 2;
		} else {
			activeContainer = 1;
		}
		
		showImage(photos[currentImg - 1], currentContainer, activeContainer);
	};
	
function fisherYates ( myArray ) {
  var i = myArray.length;
  if ( i == 0 ) return false;
  while ( --i ) {
     var j = Math.floor( Math.random() * ( i + 1 ) );
     var tempi = myArray[i];
     var tempj = myArray[j];
     myArray[i] = tempj;
     myArray[j] = tempi;
   }
}
	
	var currentZindex = -1;
	var showImage = function(photoObject, currentContainer, activeContainer) {
		animating = true;
		
		// Make sure the new container is always on the background
		currentZindex--;
		
		// Set the background image of the new active container
		$("#headerimg" + activeContainer).css({
			"background-image" : "url(" + photoObject.image + ")",
			"display" : "block",
			"z-index" : currentZindex
		});
		
		
		$("#headerimg" + currentContainer).fadeOut(2000,function() {
			setTimeout(function() {
				animating = false;
			}, 500);
		});
	};
	
if ($("#bgroll").length != 0){
	
var photos = [ 
{"image" : "/includes/img/iniziativa/01.jpg"},
{ "image": "/includes/img/iniziativa/02.jpg" },
{ "image": "/includes/img/iniziativa/03.jpg" },
{ "image": "/includes/img/iniziativa/04.jpg" },
{ "image": "/includes/img/iniziativa/05.jpg" },
{ "image": "/includes/img/iniziativa/06.jpg" },
{ "image": "/includes/img/iniziativa/07.jpg" },
{ "image": "/includes/img/iniziativa/08.jpg" },
{ "image": "/includes/img/iniziativa/09.jpg" },
{ "image": "/includes/img/iniziativa/10.jpg" },
{ "image": "/includes/img/iniziativa/11.jpg" },
{ "image": "/includes/img/iniziativa/12.jpg" },
{ "image": "/includes/img/iniziativa/13.jpg" }



];	
	
	fisherYates(photos);
	
	
	
	navigate("next");
		interval = setInterval(function() {
		navigate("next");
	}, slideshowSpeed);
	}		
			
			
if ($("#donaroll").length != 0){

    var photos = [{ "image": "/includes/img/dona/01.jpg" }, { "image": "/includes/img/dona/02.jpg" }, { "image": "/includes/img/dona/03.jpg"}];	
	navigate("next");
		interval = setInterval(function() {
		navigate("next");
	}, slideshowSpeed);
	}				
			
			
$(".archivio").fancybox({'titleShow': false});		
$("#mondo").fancybox({'titleShow': false});

$('#scrollable').jScrollPane({showArrows:false});


var myelements = $('b.hover');
myelements.each(function() { 
	
	
	//$(this).css("border","9px solid red"); 
	
				var el = $(this);
        var pos = el.position();
				$(this).parent().next().css({left: pos.left });	
	
	});



closetimer = 0;
	if($("#nav")) {
		$("#nav b").click(function() {
		clearTimeout(closetimer);
			
			if(this.className.indexOf("hover") != -1) {
				$("#nav em").removeClass("hover");
				$("#nav ul ul:visible").slideUp(200);
				$("#nav i").removeClass("hover");
				$(this).parent().next().fadeOut("slow");
				$(this).removeClass("hover");
			} else {
				$("#nav b").removeClass();
				$(this).addClass("hover");
				$("#nav em").removeClass("hover");
				$("#nav ul ul:visible").slideUp(200);
				$("#nav i").removeClass("hover");
				$("#nav ul:visible").fadeOut("slow");
				$(this).parent().next().fadeIn("slow");
				var el = $(this);
        var pos = el.position();
				$(this).parent().next().css({left: pos.left });
			}
			



//if($(this).parent().attr('class')=="single")
return true;
//else
//	return false;

		
		
		});

		$("#nav i").mouseover(function() {
		clearTimeout(closetimer);
			if(this.className.indexOf("hover") != -1) {
				$(this).removeClass("hover");
			}
			else {
				$("#nav i").removeClass();
				$(this).addClass("hover");
			}
			return false;
		});



		$("#nav").mouseover(function() {
		clearTimeout(closetimer);
		});

		$("#nav").mouseout(function() {
			closetimer = window.setTimeout(function(){
			$("#nav ul ul ul:visible").fadeOut("slow");
			$("#nav em").removeClass("hover");
			$("#nav ul ul:visible").slideUp(200);
			$("#nav i").removeClass("hover");
			}, 500);
		}); 
	}









	$('.contentWrap').hover(function(){
		$(this).find('.hover_link').stop().animate({top:'320px'},{queue:false,duration:500});
	}, function(){
		$(this).find('.hover_link').stop().animate({top:'0px'},{queue:false,duration:500});
	});




$("#register_error").hide('blind');


jQuery.validator.setDefaults({
	debug: false,
	success: "valid"
});


var optionsSubmit = {
	success:showResponse,
	url:'submit.php',
	//,data: { partecipa: '0'},
	cache	: false,
	
beforeSubmit: function () {
$('#results_trans').html('Attendere per favore...')
},	
	
	clearForm: true,
	resetForm: true
	}; 

 
function showResponse(responseText, statusText, xhr, $form)  {

$("#results_trans").html(responseText);

} 

$("#register_form").validate({
submitHandler: function(form) {
$(form).ajaxSubmit(optionsSubmit); 
	return false;   	
},

errorPlacement: function(error, element) {
	$("#register_errors").html(error);
		},

invalidHandler: function(form, validator) {},
 	
  rules: {
  	cifra: {  required: true,
      number: true}
  },
  messages:{
  	cifra:  {required: "Dato richiesto",
    number: 'Solo numeri!' }
  }
});




$('#ris_dati').fancybox();




	}); 
