var thumbnailsVisible = false;
var scrollPane = false;
var scrollToOnLoad = false;

$(window).load(function(){
	if ($("body").attr("id") == "home") {
		resizePhotoOnHomepage();
	}
	if ($("body").attr("id") == "portfolio") {

		resizePortfolio();

		$("div.scrollpane").each(function(){
			$(this).jScrollHorizontalPane({
				scrollbarHeight: 9,
				scrollbarMargin: 0,
				showArrows: true,
				arrowSize: 11,
				minimumWidth: 160,
				resize: false,
				reset: true
			});
		});
		
	}
	if ($("body").attr("id") == "photo") {
		thumbnailsVisible = readCookie("thumbnailsVisible");
		if (thumbnailsVisible == "false") thumbnailsVisible = false;

		resizePhoto();
		
		$("div.scrollpane").each(function(){
			scrollPane = $(this).jScrollHorizontalPane({
				scrollbarHeight: 9,
				scrollbarMargin: 0,
				showArrows: true,
				arrowSize: 11,
				minimumWidth: 160,
				resize: false,
				reset: true
			});
		});

		if (scrollToOnLoad) {
			var w = scrollPane.parent().width();
			var pos = scrollToOnLoad - Math.floor(w / 2);
			// console.log(scrollPane, scrollToOnLoad, w, pos);
			// if (pos > (w / 2)) {
				scrollPane[0].scrollTo(pos, true);
			// }
		}

	}
	if ($("body").attr("id") == "news") {

		resizeNews();

		$("div.scrollpane-2").each(function(){
			$(this).jScrollHorizontalPane({
				scrollbarHeight: 9,
				scrollbarMargin: 0,
				showArrows: true,
				arrowSize: 11,
				minimumWidth: 160,
				resize: false,
				reset: true
			});
		});
		
	}
});

$(window).resize(function(){
	if ($("body").attr("id") == "home") {
		resizePhotoOnHomepage();
	}
	if ($("body").attr("id") == "photo") {
		// resizePhoto();
		if (resizePhoto()) {
			$("div.scrollpane").each(function(){
				
				$(this).parent().find(".jScrollPaneTrack, .jScrollArrowLeft, .jScrollArrowRight").each(function(){
					$(this).remove();
				});
				
				$(this).unwrap().css("height", "80px").jScrollHorizontalPane({
					scrollbarHeight: 9,
					scrollbarMargin: 0,
					showArrows: true,
					arrowSize: 11,
					minimumWidth: 160
				});
				
			});
		}
	}
	if ($("body").attr("id") == "portfolio") {

		if (resizePortfolio()) {
			$("div.scrollpane").each(function(){
				$(this).parent().find(".jScrollPaneTrack, .jScrollArrowLeft, .jScrollArrowRight").each(function(){
					$(this).remove();
				});
				$(this).unwrap().css("height", "135px").jScrollHorizontalPane({
					scrollbarHeight: 9,
					scrollbarMargin: 0,
					showArrows: true,
					arrowSize: 11,
					minimumWidth: 160
				});
			});
		}

	}
	if ($("body").attr("id") == "news") {
		if (resizeNews()) {
			$("div.scrollpane-2").each(function(){
				$(this).parent().find(".jScrollPaneTrack, .jScrollArrowLeft, .jScrollArrowRight").each(function(){
					$(this).remove();
				});
				$(this).unwrap().css("height", "258px").jScrollHorizontalPane({
					scrollbarHeight: 9,
					scrollbarMargin: 0,
					showArrows: true,
					arrowSize: 11,
					minimumWidth: 160
				});
			});
		}
	}
	
});

function resizeNews() {

	var currentWidth = parseInt($("div.scrollpane-2").first().css("width"));
	var changed = false;
	var contentWidth = $(window).width() - 44;
	var numberOfItems = Math.floor(contentWidth / 165);
	var width = numberOfItems * 165;
	if (width != currentWidth) {
		$("div.scrollpane-2").each(function(){
			$(this).css("width", width+"px");
			changed = true;
		});
	}
	return changed;
	
}

function resizePortfolio() {
	var currentWidth = parseInt($("div.category").first().css("width"));
	var changed = false;
	var contentWidth = $(window).width() - 72;
	var numberOfItems = Math.floor(contentWidth / 88 / 2);
	var width = (numberOfItems * 88) - 8;
	if (width != currentWidth) {
		$("div.category, div.scrollpane").each(function(){
			$(this).css("width", width+"px");
			changed = true;
		});
	}
	return changed;
}

function resizePhotoOnHomepage() {
	var width = $(window).width();
	var height = $(window).height();
	var maxWidth = width - 223;
	var maxHeight = height - 34;
	var max = maxWidth > maxHeight ? maxHeight : maxWidth;
	$("#homepage-top img, #homepage-bottom img").each(function(){
		$(this).css("width", max+"px").css("height", max+"px");
	});
	$("#loading").fadeOut();
}

function resizePhoto() {

	var image = $("#content img");
	var contentWidth = $(window).width() - 255;
	var contentHeight = $(window).height() - 34;
	if (thumbnailsVisible) contentHeight -= 80 + 12;
	// if (thumbnailsVisible) contentHeight -= 75 + 12;
	var imageWidth = image.attr("w");
	var imageHeight = image.attr("h");
	var contentRatio = contentWidth / contentHeight;
	var imageRatio = imageWidth / imageHeight;
	if (contentRatio > imageRatio) {
		var height = contentHeight;
		var width = height * imageRatio;
	} else {
		var width = contentWidth;
		var height = width / imageRatio;
	}
	image.css("width", width+"px").css("height", height+"px");
	
	// position thumbnail opener/closer (attach to bottom of photo)
	$("#thumbnail-toggler").css("bottom", $(window).height() - height - 12);
	$("#thumbnail-toggler a").attr("class", (thumbnailsVisible ? "arrow down" : "arrow up"));
	$("#thumbnail-toggler label").html((thumbnailsVisible ? "Hide" : "Show") + "&nbsp;thumbnails");

	$("#loading").fadeOut();
	$("#thumbnail-toggler").fadeIn();
	$("#thumbnail-label").fadeIn();

	if (thumbnailsVisible) {

		// $("div.scrollpane").each(function(){
		// 	$(this).parent().find(".jScrollPaneTrack, .jScrollArrowLeft, .jScrollArrowRight").each(function(){
		// 		$(this).remove();
		// 	});
		// 	$(this).unwrap().css("height", "80px").jScrollHorizontalPane({
		// 		scrollbarHeight: 9,
		// 		scrollbarMargin: 0,
		// 		showArrows: true,
		// 		arrowSize: 11,
		// 		minimumWidth: 160
		// 	});
		// });

		// update width of #thumbnails, to match width of photo
		$("#thumbnails, .scrollpane").css("width", width).show();
		return true;
	} else {
		$("#thumbnails").hide();
		return false;
	}
	
	// return true;
	
}

function toggleThumbnails() {
	thumbnailsVisible = !thumbnailsVisible;
	eraseCookie("thumbnailsVisible");
	createCookie("thumbnailsVisible", thumbnailsVisible, 7);
	// resizePhoto();
	if (resizePhoto()) {
		$("div.scrollpane").each(function(){
			
			$(this).parent().find(".jScrollPaneTrack, .jScrollArrowLeft, .jScrollArrowRight").each(function(){
				$(this).remove();
			});
			
			$(this).unwrap().css("height", "80px").jScrollHorizontalPane({
				scrollbarHeight: 9,
				scrollbarMargin: 0,
				showArrows: true,
				arrowSize: 11,
				minimumWidth: 160
			});
			
		});
	}
	
}

function createCookie(name, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
		var expires = "; expires="+date.toGMTString();
	} else {
		var expires = "";
	}
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(";");
	for (var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == " ") c = c.substring(1, c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name, "", -1);
}
