$(function(){
	
	var naviClass = "clearthinking";
	$(".navi").addClass(naviClass);
	
	// Hero slides
	$("#slides").scrollable({
		circular: true,
		mousewheel: true,
		easing: 'swing',
		onBeforeSeek: function(event, tabIndex){
			var a = $(".navi a").eq(tabIndex);
			$(".navi").removeClass(naviClass);
			naviClass = a.attr('rel');
			$(".navi").addClass(naviClass);
		}
	}).navigator();
	
	// Gallery horizontal slider
	$(".gallery_slides").scrollable({
		mousewheel: true,
		easing: 'swing',
		next: ".gallery_next",
		prev: ".gallery_prev",
		items: ".gallery_items",
		circular: true,
		speed:1000
	}).autoscroll({
			interval: 5000
	});
	
	$(".featuredthumb").scrollable({
		mousewheel: true,
		easing: 'swing',
		items: ".items",
		circular: true,
		speed:1000
	}).autoscroll({
			interval: 5000
	});
	
	// Pause autoscrolling to begin with
	$("#slides .scrollable:gt(1)").each(function(){
		api = $(this).data("scrollable");
		api.pause();
	});
	
	// When slide selected, start gallery autoscroll, pause others
	$(".navi a").click(function(){
		api_current = $($(this).attr('href')+" .scrollable").data("scrollable");
		api_current.play();
		$("#slides .scrollable").not($(this).attr('href')+" .scrollable").each(function(){
			api = $(this).data("scrollable");
			api.pause();
		});
	});
	
	$("div.show_more").each(function(){
		var container = "div.show_hide";
		var item = "div";
		var items = $(this).prev(container).children(item);
		var limit_str = $(this).attr('data-limit') || "6";
		var limit = parseInt(limit_str);
		var show_link = $(this).children("a.more");
		
		if(items.length > limit){
			items.hide();
			items.slice(0, limit).show();
			show_link.click(function(){
				var n = items.filter(':visible').length;
				items.slice(n, n+limit).show(0, function(){
					lastdiv = $("div.thumb:last");
					position = lastdiv.offset();
					// $.scrollTo(position.top+lastdiv.height()+130, 200);
				});
				if(items.filter(':hidden').length == 0){
					$(this).remove();
				}
				return false;
			});
		} else {
			show_link.remove();
		}
	});
	
	
	
});
