$(function (){ 
// check if Hash in address bar
	checkHash();
// add onclick to in-project Navigation, image Enlarge links, lightbox closers
	loadNavClicks();
	$("#projectNav a").click( function(){
		newHash = $(this).attr("href");
		loadPreview(newHash);
		$(this).addClass("current");
	});

	$("#enlarge, #preview img").click( function(){enlargeImage();return false;});
	$("#popupBg, #closePopup").click(function(){hidePopup();return false;});
	$("#popupBg").css("cursor","pointer");	
});
var currentImg;
var navPage = 1;
function loadNavClicks()
{
// add hover states to navigation images
	$("#sidebar li a img").hover(function(){$(this).addClass("hover");},function(){$(this).removeClass("hover");});
	$("#navToggle").click( function(){ navToggle();});
}
function navToggle()
{
 if(navPage==1)
 { $("#sidebar").load("nav2.php", function(){loadNavClicks()});
  navPage = 2;
 }
 else
 {
  $("#sidebar").load("nav1.php", function (){loadNavClicks()});
    navPage = 1;
 }
}
function loadPreview(newHash)
{
//clear onstate on link list
	$("#projectNav a").removeClass("current");
//format Hash	
	newIndex = newHash.replace("#","");
//get current image
	currentImg = $("#preview img").attr("src");
//find digit to replace
	indexPlace = currentImg.indexOf(".") - 1;
	oldIndex = currentImg.charAt(indexPlace);
	newImg = currentImg.replace( oldIndex, newIndex);
//replace image
	$("#preview img").attr("src", newImg);
	
}
function enlargeImage()
{
//get current image
	currentImg = $("#preview img").attr("src");
	currentLarge = $("#popup img").attr("src");
	largeImg = currentImg.replace("pv","lg");
	if(currentLarge == largeImg)
	{ loadPopup();}
	else
	{	$("#popup img").remove();
			$("#loading").toggle();
		$("#popup").image(largeImg, function(){	$("#loading").toggle(); loadPopup();});	
	}
}
function checkHash()
{ var currentAnchor = document.location.hash; 
  if(currentAnchor && currentAnchor!= "#" && currentAnchor!="#1")
	{loadPreview(currentAnchor);
	linkIndex = currentAnchor.replace("#","") - 1;
	$("#projectNav a:eq(" + linkIndex + ")").addClass("current");		
	}	
  else{$("#projectNav a:first").addClass("current");}
}
var popupStatus = 0;

function loadPopup(){
//loads popup only if it is disabled
if(popupStatus==0){
 $("#popupBg").css({ "opacity": "0.7"}); 
	$("#popupBg").fadeIn("slow");
	$("#popup").fadeIn("slow");
	centerPopup();
	popupStatus = 1;
	}
}

function hidePopup(){
//disables popup only if it is enabled
if(popupStatus==1){
	$("#popupBg").fadeOut("slow");
	$("#popup").fadeOut("slow");
	popupStatus = 0;
	}
}

$.fn.image = function(src, f){ 
   return this.each(function(){ 
     var i = new Image();
     i.onload = f; 
     i.src = src; 
     this.appendChild(i);
   }); 
 } 

//centering popup
function centerPopup(){
//request data for centering
var windowWidth = document.documentElement.clientWidth;
var windowHeight = document.documentElement.clientHeight;
var windowHeight = screen.availHeight;
var popupHeight = $("#popup").height();
var popupWidth = $("#popup").width();
var newTop = windowHeight/2-popupHeight/2;
var newLeft = windowWidth/2-popupWidth/2;
if(newTop<0) newTop = 0;
if(newLeft<0) newLeft = 0;
//centering
	$("#popup").css({
	"top": newTop,
	"left": newLeft
});

//required for ie6
	$("#popupBg").css({"height": windowHeight});

}






