// Tyler Mulligan - www.detrition.net

$(document).ready(function() {
				
	$("#pageNum a:first").addClass("pageCur");
		
	$("div.scrollable").scrollable({
		size: 3,
		easing: "swing"
	})
	
	// setup # links
	var api = $("div.scrollable").scrollable(); 
	var curPage = parseInt(window.location.hash.replace("#page","")); // get the # from the #page
	api.setPage(curPage-1);
	// If page navigated to is not page 1, load the first picture from the page
	if (api.getPageIndex()!=0) {
		api.click(api.getIndex());
		api.next(); // stupid 'fix'
		var picID = $(".active img").attr("id");
		imageLoad('big/'+picID+'.jpg');
	}
	
	if($("#curPic").height() < $("#curPic").width()) {
			  $('#picView img').height(480);
			  $('#picView img').width(728);
			  $('#picView').addClass("wide");
	 } else {
		 	  $('#picView img').height(720);
			  $('#picView img').width(480);
			  $('#picView').removeClass("wide");
	}
	
	$("#pageNum a").removeClass("pageCur");
	$("#pageNum a:eq("+api.getPageIndex()+")").addClass("pageCur");
	//var picIndex = api.getIndex();
	//alert(picIndex);
	//api.click(picIndex);
	
	
	// on Page number click
	$("#pageNum a").click(function() {
	
		// get handle to the API 
		var api = $("div.scrollable").scrollable(); 
			 
		// perform a simple API call 
		api.setPage($(this).html()-1);
		$("#pageNum a").removeClass("pageCur");
		$(this).addClass("pageCur");
		setTimeout("window.location.hash = 'page"+$(this).html()+"'",250);
		
	});

	$(".clickable").click(function(){ 
		//$("#curPic").fadeTo("slow", 1);
		var picID = $(this).attr("id");
		imageLoad('big/'+picID+'.jpg');
		  if(this.height > this.width) {
			  $('#picView img').height(720);
			  $('#picView img').width(480);
			  //$('#picView img').height(1092);
			  //$('#picView img').width(728);
			  $('#picView').removeClass("wide");
		  } else {
			  $('#picView img').height(480);
			  $('#picView img').width(728);
			  $('#picView').addClass("wide");
		  }
		$(".thumb").removeClass("active");
		$(this).parent().addClass("active");
		$('html,body').animate({scrollTop: 600}, 1000);
		return false;
	});
	
	$("div.scrollable div").click(function() {
		var api = $("div.scrollable").scrollable(); 
		$("#pageNum a").removeClass("pageCur");
		$("#pageNum a:eq("+api.getPageIndex()+")").addClass("pageCur");
		window.location.hash = "page"+(api.getPageIndex()+1);
	});

	$("#prevPic").click(function(){ 
		
					// Fix picture width -- not working

			if($("#curPic").height() < $("#curPic").width()) {
					  $('#picView img').height(480);
					  $('#picView img').width(728);
					  $('#picView').addClass("wide");
			 } else {
					  $('#picView img').height(720);
					  $('#picView img').width(480);
					  $('#picView').removeClass("wide");			
			}
		
		var picID = $("#picView img").attr("src");
		// Yeah, not ideal but okay
		picID = picID.replace("big/","");
		picID = picID.replace(".jpg","");
		
		// Well that's lol
		newID = $(".items #" + picID).parent().prev().children("img").attr("id");
		
		if (newID!=null) {
			// Load new image
			imageLoad('big/'+newID+'.jpg');
			
			var api = $("div.scrollable").scrollable(); 
						
			var picIndex = api.getIndex();
			api.seekTo(picIndex-1);
			
			// 
			if((picIndex-1)%api.getConf().size==0) {
				$("#pageNum a").removeClass("pageCur");
				$("#pageNum a:eq("+api.getPageIndex()+")").addClass("pageCur");
				window.location.hash = "page"+(api.getPageIndex()+1);				
			}
			
		}
	});

	$("#nextPic").click(function(){ 

			// Fix picture width -- not working

			if($("#curPic").height() < $("#curPic").width()) {
					  $('#picView img').height(480);
					  $('#picView img').width(728);
					  $('#picView').addClass("wide");
			 } else {
					  $('#picView img').height(720);
					  $('#picView img').width(480);
					  $('#picView').removeClass("wide");			
					  //$('#picView img').height(1092);
					  //$('#picView img').width(728);
			}

		var picID = $("#picView img").attr("src");
		// Yeah, not ideal but okay
		picID = picID.replace("big/","");
		picID = picID.replace(".jpg","");
		
		// Well that's lol
		newID = $(".items #" + picID).parent().next().children("img").attr("id");
		
		if (newID!=null) {
			// Load new image
			imageLoad('big/'+newID+'.jpg');
			
			var api = $("div.scrollable").scrollable(); 
						
			var picIndex = api.getIndex();
			api.seekTo(picIndex+1);
			
			if((picIndex+1)%api.getConf().size==0) {
				$("#pageNum a").removeClass("pageCur");
				$("#pageNum a:eq("+api.getPageIndex()+")").addClass("pageCur");
				window.location.hash = "page"+(api.getPageIndex()+1);				
			}
			
			//$("#pageNum a").removeClass("pageCur");
			//$("#pageNum a:eq("+api.getPageIndex()+")").addClass("pageCur");
			//window.location.hash = "page"+(api.getPageIndex()+1);
		}
	});

	$(".prevPage").click(function() {
		// get handle to the API 
		var api = $("div.scrollable").scrollable(); 
		$("#pageNum a").removeClass("pageCur");
		$("#pageNum a:eq("+api.getPageIndex()+")").addClass("pageCur");
		window.location.hash = "page"+(api.getPageIndex()+1);
		//alert();
		return false;
	});

	$(".nextPage").click(function() {
		// get handle to the API 
		var api = $("div.scrollable").scrollable(); 
		$("#pageNum a").removeClass("pageCur");
		$("#pageNum a:eq("+api.getPageIndex()+")").addClass("pageCur");
		window.location.hash = "page"+(api.getPageIndex()+1);
		//alert();
		return false;
	});
	
});
