// when the DOM is ready...
$(document).ready(function () {

	//	1. scroll
	//	2. selection of navigation
	//	3. image buttons
	//	4. support horizontal nav
	
	var horizontal = false;
	
	var $panels = $('#featured .panel');
	var $container = $('#featured .scrollContainer');
	var $scroll = $('#featured .scroll').css('overflow', 'hidden');
	
	$scroll
		.before('<span class="scrollButtons left"></span>')
		.after('<span class="scrollButtons right"></span>');
	
	if (horizontal) {
		$panels.css({
			'float' : 'left',
			'position' : 'relative'
		});
		
		$container.css('width', $panels[0].offsetWidth * $panels.length);
	}
	
	$('#featured .navigation a').click(selectNav);
	
	function selectNav() {
		$(this)
			.parents('ul:first')
				.find('a')
					.removeClass('selected')
				.end()
			.end()
			.addClass('selected');
	}
	
	var scrollOptions = {
		target: $scroll,
		items: $panels,
		navigation: '.navigation a',
		prev: 'span.left',
		next: 'span.right',
		axis: 'xy',
		duration: 300,
		easing: 'swing', 
		onAfter: trigger
	};
	
	function trigger(data) {
		var el = $('#featured .navigation').find('a[href$="' + data.id + '"]').get(0);
		selectNav.call(el);
	}
	
	if (window.location.hash) {
		trigger({ id: window.location.hash.substr(1) });
	} else {
		$('#featured .navigation a:first').click();
	}
	
	
	$('#featured').serialScroll(scrollOptions);
	$.localScroll(scrollOptions);

	// first hide the navigation buttons    
var $buttons = $('img.right').add('img.left').hide();

// start to automatically cycle the tabs
var cycleTimer = setInterval(function () {
   $scroll.trigger('next');
}, 3300);

// select some trigger elements to stop the auto-cycle
var $stopTriggers = $('#slider .navigation').find('a') // tab headers
   .add('.scroll')                                     // panel itself
   .add("a[href^='#']")                               // links to a tab
   .add('span.left')
   .add('span.right');

// this is the function that will stop the auto-cycle
function stopCycle() {
   $stopTriggers.unbind('click.cycle');   // remove the no longer needed stop triggers
   clearInterval(cycleTimer);             // stop the auto-cycle itself
   $buttons.show();                       // show the navigation buttons
}

// bind stop cycle function to the click event using namespaces
$stopTriggers.bind('click.cycle', stopCycle);
	
});
