var lastNumber = 0;
var boxColors = {};
		
boxColors[1] = "#e09ddc";
boxColors[2] = "#e09ddc";
boxColors[3] = "#e09ddc";
boxColors[4] = "#e09ddc";
boxColors[5] = "#e09ddc";
boxColors[6] = "#e09ddc";
boxColors[7] = "#e09ddc";
boxColors[8] = "#e09ddc";
boxColors[9] = "#e09ddc";
boxColors[10] = "#e09ddc";
boxColors[11] = "#e09ddc";
boxColors[12] = "#e09ddc"; 


function getRandomNum(start,end,noNum) {
	var randomnumber=Math.floor(Math.random()*parseFloat(end))+parseFloat(start);
	while(randomnumber == noNum) {
		randomnumber=Math.floor(Math.random()*parseFloat(end))+parseFloat(start);
	}
	return randomnumber;
}

// Funzione per abilitare l'apertura random dei box
var clientTimedSlideBox = function() {
	$(document).everyTime(5000,"clientbox", function() {
		var randomnumber = getRandomNum(1,8,lastNumber);
		$(".cover", "#client-box-"+randomnumber).animate({ backgroundColor: boxColors[randomnumber]  }, 350);
		$(".cover","#client-box-"+randomnumber).animate({top:'0px'},{queue:false,duration:160});
		$(".cover","#client-box-"+randomnumber).oneTime(3500, function(){
			$(".cover", "#client-box-"+randomnumber).animate({ backgroundColor: "white" }, 350);
			$(this).animate({top:'141px'},{queue:false,duration:160});
		});
		lastNumber = randomnumber;
	});
}




$(document).ready(function(){
	
	$("#clientsGrid a[rel]").overlay({effect: 'apple_aqua', expose: '#000'});
	$("#allClientsGrid a[rel]").overlay({effect: 'apple_aqua', expose: '#000'});
	
	//Full Caption Sliding (Hidden to Visible)
	$('.boxgrid').hover(function(){
		
		if(!isScrolling) {
		$(".cover","#client-box-"+lastNumber).animate({top:'141px'},{queue:false,duration:160});
		$(".cover", this).animate({top:'0px'},{queue:false,duration:160});
		
		var id = $(this).attr("id");
		id = id.substring(id.lastIndexOf("-")+1);
		
		// Aggiungi colore allo sfondo del box
		$(".cover", this).animate({ backgroundColor: boxColors[id] }, 350);
		
		$(document).stopTime("clientbox");
		}
	}, function() {
		if(!isScrolling) {
		$(".cover", this).animate({ backgroundColor: "white" }, 350);
		$(".cover", this).animate({top:'141px'},{queue:false,duration:160});
		clientTimedSlideBox();
		}
	});
	
	clientTimedSlideBox();


	/*** Carica pagina con tutti i clienti ***/
/*	
	var hash = window.location.hash.substr(1);
	var href = $('#portfolio-nav li a').each(function(){
		var href = $(this).attr('href');
		if(hash==href.substr(0,href.length-5)){
			var toLoad = hash+'.php';
			$('#portfolio-wrapper').load(toLoad)
		}											
	});
*/
	//$('#allClientsGrid').hide();

	$('#portfolio-nav li a[class="allClientsGrid"]').click(function( event ){
		// Prevent the default event.
		event.preventDefault();

		$('#clientsGrid').slideUp('slow', function() {
		    $('#allClientsGrid').slideDown('slow');	
				
				$('#allClientsGrid .col1').delay(500).each(function(i) {
					$(this).delay(i*100).animate({ backgroundColor: '#DDDDDD' }, 75).delay(100).animate({ backgroundColor: '#333333' }, 75);
				});
				$('#allClientsGrid .col2').delay(750).each(function(i) {
					$(this).delay(i*100).animate({ backgroundColor: '#DDDDDD' }, 75).delay(100).animate({ backgroundColor: '#333333' }, 75);
				});
				$('#allClientsGrid .col3').delay(1000).each(function(i) {
					$(this).delay(i*100).animate({ backgroundColor: '#DDDDDD' }, 75).delay(100).animate({ backgroundColor: '#333333' }, 75);
				});
				$('#allClientsGrid .col4').delay(1250).each(function(i) {
					$(this).delay(i*100).animate({ backgroundColor: '#DDDDDD' }, 75).delay(100).animate({ backgroundColor: '#333333' }, 75);
				});
			
		});
		
		// Attiva la selezione del link cliccato
		$('#portfolio-nav li a[class="clientsGrid"]')
			.show();
		$(this).hide();
		
	});

	$('#portfolio-nav li a[class="clientsGrid"]').click(function( event ){
		// Prevent the default event.
		event.preventDefault();

		$('#allClientsGrid').slideUp('slow', function() {
		    $('#clientsGrid').slideDown('slow');	
		});
		
		// Attiva la selezione del link cliccato
		$('#portfolio-nav li a[class="allClientsGrid"]').show();
		$(this).hide();
		
	});
	
});
