(function($){
	$.fn.codaSlider = function(options){
		var options = $.extend({
								horizontal: false
							}, options);
		
		
		var horizontal = options.horizontal;
		

		
		// when the DOM is ready...
		var $panels = $('#slider .scrollContainer > div');
		var $container = $('#slider .scrollContainer');
		
		// if false, we'll float all the panels left and fix the width 
		// of the container
		if (horizontal) {
			// float the panels left if we're going horizontal
			$panels.css({
				'float' : 'left',
				'position' : 'relative' // IE fix to ensure overflow is hidden
			});
			
			// calculate a new width for the container (so it holds all panels)
			$container.css('width', $panels[0].offsetWidth * $panels.length);
		}
		
		// collect the scroll object, at the same time apply the hidden overflow
		// to remove the default scrollbars that will appear
		var $scroll = $('#slider .scroll').css('overflow', 'hidden');
		
		// apply our left + right buttons
		$scroll
			.before('<button class="scrollButtons left"></button>')
			.after('<button class="scrollButtons right"></button>');
		
		// handle nav selection
		function selectNav() {
		//move the selected class in the navigation menu
		$(this)
			.parents('ul:first')
			  .find('a')
				.removeClass('selected')
			  .end()
			.end()
			.addClass('selected');
			
		//hide nav buttons if this panel is the last or the first one
		var last, first;
		last = $(this).parents('ul:first').find('a:last').hasClass('selected');
		first = $(this).parents('ul:first').find('a:first').hasClass('selected')
		
		if(last){
			$('button.right').addClass('hidden');
		}else{
			$('button.right').removeClass('hidden');
		}
		
		if(first){
			$('button.left').addClass('hidden');
		}else{
			$('button.left').removeClass('hidden');
		}
		
		
		}
		
		//check if the page is being refreshed
		function isRefresh()
		{
		if( $('#slider input#visited').val() == "" )
		{
			// This is a fresh page load
			$('#slider input#visited').val("1");
			
			return 'fresh';
			// You may want to add code here special for
			// fresh page loads
		}
		else
		{
			// This is a page refresh
		
			return 'refresh';
			// Insert code here representing what to do on
			// a refresh
		}
		}
		
		$('#slider .navigation').find('a').click(selectNav);
		
		
		
		//capture the clicks on labels in the message box and scroll to the panel in which their inputs are located.
		$('#messageBox label').livequery('click',function(e){
		var targetA = $(this).attr('for');
		var targetPanel = $('#'+ targetA).parents('.panel').attr('id');
		
		//moves the controllers into position
		trigger({ id : targetPanel });	
		
		//moves the panels into position when you click on a label
		$('#slider .navigation a[href$="' + targetPanel + '"]').click();
		
		return false;
		});
		
		
		// go find the navigation link that has this target and select the nav
		function trigger(data) {
		//moves the controllers into position
		var el = $('#slider .navigation a[href$="' + data.id + '"]').get(0);
		selectNav.call(el);
		}
		
		if (window.location.hash) {
		trigger({ id : window.location.hash.substr(1) });
		} else {
		var hash = $('ul.navigation a:first').attr('href').substr(1);
		//moves the controllers into position
		trigger({ id : hash });
		
		}
		
		
		/*BEGIN MOVE to custom.slider.js*/
		//capture tabbing for the last visible input or select tag in the panel, then move the focus the right button
		$('.panel').find(':not(option):visible:last').bind('keydown',function(e){
		if(e.keyCode == 9){
			$('button.right').focus();
			return false;
		}
		});
		
		//capture tabbing for the the right button,  and move the focus to the left button
		$('button.right').bind('keydown',function(e){
		if(e.keyCode == 9){
			$('button.left').focus();
			return false;
		}
		});
		/*END MOVE to custom.slider.js*/
		
		// offset is used to move to *exactly* the right place, since I'm using
		// padding on my example, I need to subtract the amount of padding to
		// the offset.  Try removing this to get a good idea of the effect
		var offset = parseInt((horizontal ? 
		$container.css('paddingTop') : 
		$container.css('paddingLeft')) 
		|| 0) * -1;
		
		
		var scrollOptions = {
		target: $scroll, // the element that has the overflow
		
		// can be a selector which will be relative to the target
		items: $panels,
		
		navigation: '.navigation a',
		
		// selectors are NOT relative to document, i.e. make sure they're unique
		prev: '.left', 
		next: '.right',
		
		// allow the scroll effect to run both directions
		axis: 'xy',
		
		onAfter: trigger, // our final callback
		
		offset: offset,
		
		lazy:true,
		
		// duration of the sliding effect
		duration: 500,
		
		// easing - can be used with the easing plugin: 
		// http://gsgd.co.uk/sandbox/jquery/easing/
		easing: 'swing'
		};
		
		// apply serialScroll to the slider - we chose this plugin because it 
		// supports the indexed next and previous scroll along with hooking 
		// in to our navigation.
		$('#slider').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, 
		// setting the duration to 1 because I don't want it to scroll in the
		// very first page load.  We don't always need this, but it ensures
		// the positioning is absolutely spot on when the pages loads.
		scrollOptions.duration = 1;
		$.localScroll.hash(scrollOptions);
		
		//moves the panels into position
		//handles page refresh
		if(isRefresh() == 'refresh'){			
			$('.navigation a:first').click();
		}
		
		$('.loader').css('display', 'none');
		$('#slider,.sliderWrapper').css('visibility', 'visible');
		$('.navigation').show();
	};		  
		  
		  
		  
})(jQuery);