if (typeof($p) == 'undefined') $p = {};

$p.slideShow = function(){
	//Variables
	var container = document.getElementById('slideShow');
	var slideCont = document.getElementById('ssContainer');
	var empower = $('#empowerYou p')[0];
	
	// Navigate to where the user wants to
	function scrollTo(link){
		var target = $(link).attr('href');
		$(container).find('a.active').removeClass('active');
		
		$(slideCont).scrollTo(target, 500, {
			offset: {top:0, left:-245},
			onAfter:function(){
				$(link).addClass('active');
				// Make sure the nav is in the right state of active/inactive
				checkLocation();
			}
		});
	}
	// Update the nav icons
	function checkLocation(){
		// If we're first
		if($(container).find('.select a:first').hasClass('active')){
			$(container).find('span.left').addClass('inactive');
			$(container).find('span.right').removeClass('inactive');
		// If we're last
		} else if($(container).find('.select a:last').hasClass('active')) {
			$(container).find('span.right').addClass('inactive');
			$(container).find('span.left').removeClass('inactive');
		// otherwise..
		} else {
			$('span.inactive').removeClass('inactive');
		}
	}
	// Ajax call to geth the accompanying text
	function getEmpower(target){
		$(empower).fadeOut(200, function(){
			$.post(
				'/dna-reports/empower-copy',
				'page='+ $('body:first').attr('id') +'&condition=' + target,
				function(data){
					empower.innerHTML = data;
					$(empower).fadeIn(200);
				},
				'html'
			);
		});
	}
	// Set up
	function init(){
		
		// Hook up the navigation links
		$(container).find('a').click(function(event){
			event.preventDefault();
			if(this.className != 'active'){
				scrollTo(this);
				getEmpower(this.getAttribute('href').replace('#', ''))
			}
		});
		
		// The next/previous links
		$(container).find('span.nav').click(function(){
			var next;
			if($(this).hasClass('left')){
				next = $(container).find('.select a.active').prev('a');
			} else {
				next = $(container).find('.select a.active').next('a');
			}
			if(next.length > 0){
				scrollTo(next);
				getEmpower(next.attr('href').replace('#', ''))
			}		
		});		
	}
	return function(){
		init();
	};
}();

$(document).ready(function(){
	if(document.getElementById('slideShow')){
		$p.slideShow();
	}
});