// DOM Ready
$(document).ready(function(){
	
	// Project accordion

	
	// Show/hide function based on div id
	$('.toggle').click(function() { 
		var id = $(this).attr('id'); // gets e.g. post-10
		$('#' + id + '-more').slideToggle('fast').removeClass('hidden');
		//$('.toggle').toggle();
		return false; 
	});
	
	//scrollpane parts
		var scrollPane = $( ".scroll-pane" ),
			scrollContent = $( ".scroll-content" );
		
		//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" );
		
		//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

	
	$('div.sliderGallery').each(function () {
		var ul = $('ul', this);
		var productWidth = ul.innerWidth() - $(this).outerWidth();

		var slider = $('.slider', this).slider({ 
			handle: '.handle',
			minValue: 0, 
			maxValue: productWidth, 
			slide: function (ev, ui) {
				ul.css('left', '-' + ui.value + 'px');
			}, 
			stop: function (ev, ui) {
				ul.animate({ 'left' : '-' + ui.value + 'px' }, 500, 'linear');
			}
		});
	});

	
	
	// All the slider functions.

	$('#slider2').anythingSlider({
			
		height : 240,
		expand : false,
		buildNavigation : false,
		changeBy : 2,
		startStopped : true,
		infiniteSlides : false
	});
	
	$('.tlslide').click(function() { 
		var id = $(this).attr('id').substring(0,1);
		
		$('#slider2').anythingSlider(id);
		return false; 
	});
	
	
	$("#slide-jump").click(function(){
		$('#slider2').anythingSlider(4);
	});

	
	$('#slider')
		.anythingSlider({
			//width        : 500,
			height       : 118,
			autoPlay: true,
			startText: '',
			stopText: '',
			pauseOnHover: false
		})
		.anythingSliderFx({
			inFx: {
				'.caption-top'    : { top: 0, opacity: 0.8, duration: 400 },
				'.caption-right'  : { right: 0, opacity: 0.8, duration: 400 },
				'.caption-bottom' : { bottom: 0, opacity: 0.8, duration: 400 },
				'.caption-left'   : { left: 0, opacity: 0.8, duration: 400 }
			},
			outFx: {
				'.caption-top'    : { top: -50, opacity: 0, duration: 350 },
				'.caption-right'  : { right: -150, opacity: 0, duration: 350 },
				'.caption-bottom' : { bottom: -50, opacity: 0, duration: 350 },
				'.caption-left'   : { left: -150, opacity: 0, duration: 350 }
			}
		})
		/* add a close button (x) to the caption */
		.find('div[class*=caption]')
		.css({ position: 'absolute' })
		.prepend('')
		.find('.close').click(function(){
		var cap = $(this).parent(),
       ani = { bottom : -100 }; // bottom
      if (cap.is('.caption-top')) { ani = { top: -50 }; }
      if (cap.is('.caption-left')) { ani = { left: -150 }; }
      if (cap.is('.caption-right')) { ani = { right: -150 }; }
      cap.animate(ani, 400, function(){ cap.hide(); } );
    });
});
	


	
	


	
	//$(function(){
	//	$('#slider').anythingSlider();
	//});
