$(document).ready(function(){
	if(window.location.hash){
		$("#images ul li.active").hide();
		translateHash();
	}
});

function showImg(imgIndex,setHash){
	var imgLen = $("#images ul li").length;
	if(imgIndex < 0){
		imgIndex = imgLen -1;
	}else if(imgIndex == imgLen){
		imgIndex = 0;
	}
	$("#images ul li.active").fadeOut("fast",function(){
		$(this).removeClass("active");
		$("#slider_btns ul li.active").removeClass("active");
		var el = $("#images ul li")[imgIndex];
		$(el).fadeIn("fast",function(){
			$(this).addClass("active");
			$("#slider_btns ul li:nth-child(" + (imgIndex + 1) +")").addClass("active");
			if(setHash == 1){
				var newHash = $("li.active img").attr("title").replace(" ","-").toLowerCase();
				window.location.hash = newHash;
			}
		var title = $("li.active img").attr("title");
            	$("#fp_title").text(title);
		var desc = $("li.active img").attr("alt");
            	$("#fp_desc").text(desc);
		});
	});	
}

function prevNext(direction){
	var curIndex = $("#images ul li.active").index();
	var nextIndex = curIndex + direction;
	showImg(nextIndex,1);
}

function translateHash(){
	var hashTranslated = window.location.hash.replace("-"," ").replace("#","");
	$("#images ul li").each(function(){
		if($("img",this).attr("title").toLowerCase() == hashTranslated){
			showImg($(this).index());
		}
	});
}
