$(document).ready(function() {
	/* Modal popup */
	$(".fancybox").fancybox({
		type: "iframe",
		margin: 0,
		padding: 0,
		height: 630,
		width: 600
	});
	
	/* Scrolling */
	var scrollPane = $("#scrollPane");
	var scrollContent = $("#scrollContent");
	var scrollBarWrap = $("#scrollBarWrap");
	var scrollBar = $("#scrollBar").slider({
		orientation: "vertical",
		value: 100,
		slide: function(e, ui) {
			if (scrollable(scrollPane.height(), scrollContent.outerHeight())) {
				scrollContent.css("margin-top", scrollComputeMargin(scrollPane.height(), scrollContent.outerHeight(), ui.value) + "px");
			} else {
				scrollContent.css("margin-top", 0);
			}
		}
	});
	
	if (!scrollable(scrollPane.height(), scrollContent.outerHeight())) {
		scrollBarWrap.hide();
		scrollPane.removeClass("scrollable");
	}
	
	$("#scrollBarUp").mousehold(function() {
		if (scrollable(scrollPane.height(), scrollContent.outerHeight())) {
			scrollBar.slider("value", scrollBar.slider("value") + 1);
			scrollContent.css("margin-top", scrollComputeMargin(scrollPane.height(), scrollContent.outerHeight(), scrollBar.slider("value")) + "px");
		}
	});
	
	$("#scrollBarDown").mousehold(function() {
		if (scrollable(scrollPane.height(), scrollContent.outerHeight())) {
			scrollBar.slider("value", scrollBar.slider("value") - 1);
			scrollContent.css("margin-top", scrollComputeMargin(scrollPane.height(), scrollContent.outerHeight(), scrollBar.slider("value")) + "px");
		}
	});
});

/* Scrolling helper functions */
function scrollable(paneHeight, contentHeight) {
	if (contentHeight > paneHeight) return true;
	return false;
}

function scrollComputeMargin(paneHeight, contentHeight, sliderValue) {
	return Math.round((100 - sliderValue)/100 * (paneHeight - contentHeight));
}
