	var viewportX;
	var viewportY;
	var thumbnailWidth = 239;
	var scrollSpeed = 800;
	$(document).ready(function() {
		$(".scroller .items ul").each(function() {
			width = $(this).find('li').length * thumbnailWidth;
			$(this).css('width', width+'px');
		});
		viewportX = $('div.items').width();
		viewportY = $('div.items').height();
		
		// Tabs
		$(".gallery .tabs li a").click(function() {
			tabIndex = $(this).parent('li').prevAll('li').length;
			$(".gallery .tabs li.current").removeClass('current');
			$(this).parent('li').addClass('current');
			scrollToRow(tabIndex);
			
			newContent = $('.slider_content .tab_content:eq('+tabIndex+')');
			changeContent(newContent);
			
			return false;
		});
		
		// Button configuration
		$("div.next a").click(function() {
			if ($(this).hasClass('disabled')) {return false; };
			scrollRight();
			return false;
		});
		$("div.prev a").click(function() {
			if ($(this).hasClass('disabled')) {return false; };
			scrollLeft();
			return false;
		});
		
		// Item click handler
		$(".scroller .items").click(function(e) {
			if (e.target.nodeName != 'LI') {
				li = $(e.target).parents('li');
			} else {
				li = $(e.target);
			}
			ul = li.parents('ul');
			ulIndex = ul.prevAll('ul').length;
			liIndex = li.prevAll('li').length;
			
			newContent = $('.slider_content .row_content:eq('+ulIndex+') .item_content:eq('+liIndex+')');
			changeContent(newContent);
			
			return false;
		});
		checkScrollerButtons();
		
	});
	
	function changeContent(newContent) {
		oldHeight = $('.slider_content').height();
		$('.slider_content').height(oldHeight);
		$('.slider_content .item_content,.initial_content,.tab_content').hide();
		newContent.fadeIn(function () {
			$('.slider_content').animate(
				{
					height: newContent.height()+parseInt($('.slider_content').css('padding-bottom').replace('px', ''))
				}
			);
		});
	}
	
	function scrollToRow(rowIndex) {
		$('.scroller .items').scrollTo({top: (rowIndex * viewportY)+'px', left:'0px'}, scrollSpeed, {onAfter: checkScrollerButtons});
		
	}
	
	function checkScrollerButtons() {
		rowIndex = $(".gallery .tabs li.current").prevAll().length;
		list = $('.scroller .items ul:eq('+rowIndex+')');
		viewport = $('.scroller .items');
		
		// Is there more content to the right?
		if (list.width() <= (viewport.attr('scrollLeft') + viewport.width())) {
			$('.next a').addClass('disabled');
		} else {
			$('.next a').removeClass('disabled');
		}
		// Is there more content to the left?
		if (viewport.attr('scrollLeft') == 0) {
			$('.prev a').addClass('disabled');
		} else {
			$('.prev a').removeClass('disabled');
		}
	}
	
	function scrollRight() {
		$('.scroller .items').scrollTo('+='+viewportX+'px', {axis: 'x', duration: scrollSpeed, onAfter: checkScrollerButtons});
	}
	
	function scrollLeft() {
		$('.scroller .items').scrollTo('-='+viewportX+'px', {axis: 'x', duration: scrollSpeed, onAfter: checkScrollerButtons});
	}