//
// COPYRIGHT MASSIMILIANO FABRIZI
// www.maxfabrizi.com
//
   
   var galleryCount = 0;
   var showThumbs = false;
   var showInfo = false;
   var maxHeight = 0;	
   var currentPic = 0;
   var nextPic = 0;
   var prevPic = 0;	
   
   	 preloadImg = new Image();
	 preloadImg.src = "http://www.maxfabrizi.com/img/navE.gif"; 
	 preloadImg.src = "http://www.maxfabrizi.com/img/navG.gif"; 
	 preloadImg.src = "http://www.maxfabrizi.com/img/navL.gif"; 
	 preloadImg.src = "http://www.maxfabrizi.com/img/navP.gif"; 
	 preloadImg.src = "http://www.maxfabrizi.com/img/navR.gif"; 
	
$(document).ready(function(){
	if ($('#photoGallery').length > 0) {
	setGallery();
	}
 });

 
  function setGallery() {
	  

	  
	 
	galleryCount = 0;
	
	$('#photoGallery li').each(function() {
	
	galleryCount++;
	
	$(this).attr('id', galleryCount);
	$(this).click(function() {
	showNextPic();
	});

	$(this).hover(function() {
	$(this).css('cursor', 'pointer');
	});
	
	});
	
	galleryCount = 0;
	
	$('#photoThumbs div').each(function() {
		
	galleryCount++;	
	
	$(this).attr('id', 't' + galleryCount);
	$(this).click(function() {
	showPic(this.id);
	});
	
	$(this).hover(function() {
	$(this).css('cursor', 'pointer');
	});
	
	});
	

	
	
	





	var documentHash = location.hash;
	documentHash = documentHash.replace("#", "");
	documentHash = documentHash.replace("/", ""); 
	documentHash = eval(documentHash);
	if (documentHash>0 && documentHash<=galleryCount) {
	showPic(documentHash);	
	} else {
		
	if (showInfo!=null && showInfo) {

	$('#photoGallery').hide();
	$('#photoThumbs').hide();
	$('#navBlock').hide();
	$('#galleryText').fadeIn();
	
	} else {
	if (showThumbs!=null && showThumbs) {
	toggleView();	
	} else {
	showPic(1);
	}
	}
	
	
	
	
	};
	
	
	
	
	
 }
 
 
 function writeEmail() {
  emailE = ('maxfabrizi' + '.' + 'com')
  emailE = ('max' + '@' + emailE)	
  document.write(
    '<A href="mailto:' + emailE + '">' 
    + emailE + '</a>'
  )
 }


 function showNextPic()
{
	showPic(nextPic);
}

 function showPrevPic()
{
	if (prevPic > 0) {  
	showPic(prevPic);
	};
}

 function showPic(picId)
{
	
	currentPic = picId + "";
	currentPic = currentPic.replace("t", "");
	currentPic = eval(currentPic);
	
	if (currentPic > 0) {	
	
	$('#photoGallery').show();
	$('#photoThumbs').hide();	
	$('#photoGallery li').hide();
	$('#navBlock').fadeIn();	
	
	$('#photoGallery').width($('#'+currentPic).width());
	
	$('#'+currentPic).fadeTo(200, 1);
	currentPicTxt = $('#txt'+currentPic).html();
	if (currentPicTxt == null) {currentPicTxt = ""};
	$('#galleryCaption').html(currentPicTxt);
	
	nextPic = currentPic + 1;
	prevPic = currentPic - 1;
	if (nextPic > galleryCount) { nextPic = 0 };
	
	$('#navNText').html("");
	$('#navNText').append(currentPic + "/" + galleryCount);
	
	if (currentPic > 1) { 
	$('#navL').fadeTo(250, 1);
	} else {
	$('#navL').fadeTo(250, 0.2);
	}

	
	if (currentPic!=1) {
		location.hash = "/"+currentPic;
		} else {
		location.hash = "/"+currentPic;
		};
		
} else {
	
	if (currentPic == 0) {	
	toggleView();
	location.hash = "/";
	}
}
		
	return false;
} 




 function startGallery() {
	 
	$('#photoGallery').show();
	$('#photoThumbs').hide();
	$('#navBlock').fadeIn();
	$('#galleryText').hide();	
	showPic(1); 
 }



 function toggleView()
{
	if ($('#photoGallery').css('display')!='none') {
	$('#photoGallery').hide();
	$('#navBlock').fadeOut();
	$('#photoThumbs').show();
	$('#galleryText').hide();
	} else {
	$('#photoGallery').show();
	$('#photoThumbs').hide();
	$('#navBlock').fadeIn();
	$('#galleryText').hide();			
	}
	return false;
} 



