$(function() {
	//loadSlider();
	loadImageGallery();
	smallGallery();
});

function loadSlider() {
	//vars
	var conveyor = $(".content-conveyor", $("#sliderContent")),
	item = $(".item", $("#sliderContent"));
	
	//set length of conveyor
	conveyor.css("width", item.length * parseInt(item.css("width")));
			
	//config
	var sliderOpts = {
		max: (item.length * parseInt(item.css("width"))) - parseInt($(".viewer", $("#sliderContent")).css("width")),
		slide: function(e, ui) { 
			conveyor.css({"left": "-" + ui.value + "px"});
		}
	};
	
	//create slider
	$("#slider").slider(sliderOpts);
	$(".ui-slider-handle").prepend('<div class="forward">Forward</div>');
	
	$('.extendLeft').click(function() {
		conveyor.css({"left": "0px"});
		$(".ui-slider-handle").css({"left": "0px"});
	});
	$('.extendRight').click(function() {
		var max = (item.length * parseInt(item.css("width"))) - parseInt($(".viewer", $("#sliderContent")).css("width"));
		conveyor.css({"right": max});
		$(".ui-slider-handle").css({"left": "700px"});
	});
}

function loadImageGallery() {
	
	$("#galleryWrap").smoothDivScroll({scrollingSpeed: 10, mouseDownSpeedBooster: 3, autoScroll: "onstart", autoScrollDirection: "backandforth", autoScrollSpeed: 1, scrollingHotSpotLeft: "div.galleryLeft", scrollingHotSpotRight: "div.galleryRight", scrollWrapper: "div.galleryContainer", scrollableArea: "div.galleryContent", pauseAutoScroll: "mouseDown"});
	item = $(".item");
	$("#sliderContent").smoothDivScroll({scrollingSpeed: 3, mouseDownSpeedBooster: 3, autoScroll: "onstart", autoScrollSpeed: 1, scrollingHotSpotLeft: "div.galleryLeft", scrollingHotSpotRight: "div.galleryRight", scrollWrapper: ".viewer", scrollableArea: ".content-conveyor", pauseAutoScroll: "mouseDown"});
	//scrollpane parts
	var scrollPane = $( ".galleryContainer" ), scrollContent = $( ".galleryContent" );
	
	//build slider
	var scrollbar = $( ".scroll-bar" ).slider({
		slide: function( event, ui ) {
			if ( scrollContent.width() > scrollPane.width() ) {
				scrollContent.css( "margin-left", Math.round(
					ui.value / 100 * ( scrollPane.width() - scrollContent.width() )
				) + "px" );
			} else {
				scrollContent.css( "margin-left", 0 );
			}
		}
	});
	
	//append icon to handle
	var handleHelper = scrollbar.find( ".ui-slider-handle" ).mousedown(function() {
		scrollbar.width( handleHelper.width() );
	}).mouseup(function() {
		scrollbar.width( "100%" );
	}).append( "<span class='ui-icon ui-icon-grip-dotted-vertical'></span>" ).wrap( "<div class='ui-handle-helper-parent'></div>" ).parent();
	
	//change overflow to hidden now that slider handles the scrolling
	scrollPane.css( "overflow", "hidden" );
	var galleryWidth = 0;
	$('.galleryContent').children().each(function() {
		galleryWidth += $(this).outerWidth();	
	})
	$('.galleryContent').css('width',galleryWidth+'px');
	//size scrollbar and handle proportionally to scroll distance
	function sizeScrollbar() {
		var remainder = scrollContent.width() - scrollPane.width();
		var proportion = remainder / scrollContent.width();
		var handleSize = scrollPane.width() - ( proportion * scrollPane.width() );
		scrollbar.find( ".ui-slider-handle" ).css({
			width: handleSize,
			"margin-left": -handleSize / 2
		});
		handleHelper.width( "" ).width( scrollbar.width() - handleSize );
	}
	
	//reset slider value based on scroll content position
	function resetValue() {
		var remainder = scrollPane.width() - scrollContent.width();
		var leftVal = scrollContent.css( "margin-left" ) === "auto" ? 0 :
			parseInt( scrollContent.css( "margin-left" ) );
		var percentage = Math.round( leftVal / remainder * 100 );
		scrollbar.slider( "value", percentage );
	}
	
	//if the slider is 100% and window gets larger, reveal content
	function reflowContent() {
			var showing = scrollContent.width() + parseInt( scrollContent.css( "margin-left" ), 10 );
			var gap = scrollPane.width() - showing;
			if ( gap > 0 ) {
				scrollContent.css( "margin-left", parseInt( scrollContent.css( "margin-left" ), 10 ) + gap );
			}
	}
	
	//change handle position on window resize
	$( window ).resize(function() {
		resetValue();
		sizeScrollbar();
		reflowContent();
	});
	//init scrollbar size
	setTimeout( sizeScrollbar, 10 );//safari wants a timeout
}

function smallGallery() {
	$('ul.thumbs a').click(function() {
		var newSrc = $(this).attr('href');
		var newAlt = $(this).attr('title');
		$('img.featured_img').attr({src: newSrc, alt: newAlt});
		$('.highlight').removeClass('highlight');
		$(this).addClass('highlight');
		return false;
	});
}
