jQuery(function($){
	var viewPortId = 		'#sidebar-accordion-list-wrapper';
	var contentId = 		'#sidebar-accordion-list';
	var scrollButton = 	'#sidebar-content .scroll-button';
	var viewportHeight = $(viewPortId).height();
	$(scrollButton).hover(function(){
		$(this).addClass('on-hover');
	},function(){
		$(this).removeClass('on-hover');
	});
	$(scrollButton).click(function(){
		var contentHeight = $(contentId).height();
		if ( contentHeight > viewportHeight) {
			var scrollIncrement = Math.round(viewportHeight);
			var top = getContentTop();
			switch (buttonDirection(this)) {
				case "up": 		if (canScroll(this,"up")) 	top = String(top+scrollIncrement);
					break;
				case "down":  if (canScroll(this,"down")) top = String(top-scrollIncrement);
					break;
			}
			setContentTop(top+"px");
		}
	});
	$("#sidebar-accordion-nav div.album-header").click(function(){
		var all_headers = $("#sidebar-accordion-nav div.album-header");
		all_headers.css("cursor","wait");
		var all_items = $("#sidebar-accordion-nav .album-photosets");
		all_items.removeClass('selected');

		var groupID = this.id.replace("album-header-","")
		var these_items = $("#sidebar-accordion-nav #photosets-of-album-" + groupID)
		these_items.show();
		if (!these_items.hasClass("loaded")) {
			var url = "http://www.racing.ups.com/wp-content/themes/upsracing/upsracing-gallery-items.php?group_name=album&group_id=" + groupID;
			var html = jQuery.ajax({url:url,async:false}).responseText;
			these_items.html(html);
			these_items.addClass("loaded");
		}
		setScrollable();
		all_items.css("display","none")
		these_items.show("fast").addClass("selected");
		all_headers.css("cursor","pointer");
		this.style.cursor= "default";
	});	function buttonDirection(button) {
		return button.id.replace("sidebar-accordion-nav-","");
	}
	function getContentTop() {
		var top = Number($(contentId).css("top").replace(/[a-z]+/i,""));
		return top;
	}
	function setContentTop(top) {
		$(contentId).animate({top:top},375,function(){
			setScrollable();
		});
	}
	function setScrollable() {
		$(scrollButton).each(function(){
			switch (buttonDirection(this)) {
				case "up":
					if (canScroll(this,"up"))
						$(this).addClass('scrollable');
					else
						$(this).removeClass('scrollable');
					break;
				case "down":
					if (canScroll(this,"down"))
						$(this).addClass('scrollable');
					else
						$(this).removeClass('scrollable');
					break;
			}
		});
	}
	function canScroll(button,direction) {
		var contentHeight = $(contentId).height();
		var scrollable;
		switch (direction) {
			case "up":
				scrollable = (getContentTop()<0);
				break;
			case "down":
				scrollable = (Math.abs(getContentTop())<(contentHeight-viewportHeight));
				break;
			default:
				scrollable = false;
		}
		return scrollable;
	}
});



