/*
#####################################################
# 
# Phenotype.net v2009
# Content Slider v1.0
#
# Hand-crafted by Phenotype (phenotype.net)
# Uses jQuery.scrollTo, jQuery.localScroll and jQuery.serialScroll (http://flesler.blogspot.com/)
# Derived from http://jqueryfordesigners.com/coda-slider-effect/
#
#####################################################
*/

	/**
	* @param elements:Object 		(slides, container, scroller, parent, navigation)
	* @param options:Object			(orientation, type, buttons)
	* @param scrollOptions:Object	(scrollTo options)
	*/
	
	function contentSlider(elements, options, scrollOptions) {
	
		////////////////////////////////////////////////////////////////////
		//	Initialise slider variables
		////////////////////////////////////////////////////////////////////
		
		var $slides = elements.parent +' '+ elements.slides;
		var $container = elements.container;
		var $scroller = elements.scroller;
		var $parent = elements.parent;
		var $navigation = elements.navigation;
		
		////////////////////////////////////////////////////////////////////
		//	Setup slider based on provided options
		////////////////////////////////////////////////////////////////////
		
		// Set orientation
		if(options.orientation == 'horizontal') {
			
			$($slides).css({
				'float' : 'left',
				'position' : 'relative' // IE fix to ensure overflow is hidden
			});
		  
			// Calculate a new width for the container (so it holds all slides)
			$($container).css('width', $($slides)[0].offsetWidth * $($slides).length);
		}

		// Setup scroller
		$($scroller).css({'overflow-x' : 'hidden', 'overflow-y' : 'hidden'});

		// Apply navigation buttons if selected
		if(options.buttons == true) {
			$($scroller)
				.before('<a id="previous">Previous</a>')
				.after('<a id="next">Next</a>');
		}
		
		////////////////////////////////////////////////////////////////////
		//	Navigation logic
		////////////////////////////////////////////////////////////////////
		  
		// Handle navigation selection
		function selectNavigation() {
			$(this)
				.parents($navigation +':first')
					.find('li')
					.removeClass('selected')
					.end()
				.end()
			.parents('li')
			.addClass('selected');
		}
		
		// Bind navigation functions to navigation links
		$($navigation).find('a').click(selectNavigation);

		// go find the navigation link that has this target and select the nav
		function trigger(data) {
			// Set heights if using fluid height content
			if(options.type == 'fluid') {
				// Get current slide
				var currentSlide = '#'+data.id;
				// Animate height of scroller to equal current slide height
				$($scroller).animate({ height: $(currentSlide).height() }, { duration: 1000, easing: 'swing' });
			}
			
			// Get navigation item to select
			var element = $($navigation).find('a[href$="' + data.id + '"]').get(0);
			
			// Select navigation
			selectNavigation.call(element);
		}

		if(window.location.hash) {
			trigger({ id : window.location.hash.substr(1) });
		} else {
			$($navigation + ' a:first').click();
		}
		
		////////////////////////////////////////////////////////////////////
		//	Slider engine
		////////////////////////////////////////////////////////////////////

		// offset is used to move to *exactly* the right place
		var offset = parseInt((options.orientation == 'horizontal' ? 
			$($container).css('paddingTop') : 
			$($container).css('paddingLeft')) 
		|| 0) * -1;

		// Set default scrollTo options
		var axis = (options.orientation == 'horizontal' ? 'x' : 'y');
		
		var defaultScrollOptions = {
			target: $scroller,
			items: $($slides),
			navigation: $navigation + ' a',
			prev: '#previous', 
			next: '#next',
			axis: axis,
			onAfter: trigger,
			offset: offset
		};
		
		// Merge default options with user-provided options
		$.extend(scrollOptions, defaultScrollOptions);

		// apply serialScroll to the slider
		$($parent).serialScroll(scrollOptions);

		// now apply localScroll to hook any other arbitrary links to trigger the effect
		$.localScroll(scrollOptions);

		// finally, if the URL has a hash, move the slider in to position 
		scrollOptions.duration = 1;
		$.localScroll.hash(scrollOptions);
	}
	