$(function()
{
	Tour.init();
	
});

var Tour =
{
	section_index:0,
	
	init:function()
	{
		if ( ! $('#tour').length)
		{
			return false;
		}
		
		var $nav = $('ul.nav');
		
		var $prev_link = $nav.find('li.prev a');
		var $next_link = $nav.find('li.next a');
		
		var $step_links = $('ul.steps a');
		
		$prev_link.click(function()
		{
			Tour.section_index--;
			
			Tour.toggle_nav_btns($nav);
			
			Tour.move_pane();
			return false;
		});
		
		$next_link.click(function()
		{
			Tour.section_index++;
			
			Tour.toggle_nav_btns($nav);
			
			Tour.move_pane();
			return false;
		});
		
		$step_links.click(function()
		{
			var $this = $(this);
			
			var this_href = $this.attr('href');
			
			var target_div_id = this_href.split('/').pop();
			
			var section_index = $("div.content div").index($('div' + target_div_id));
			
			Tour.section_index = section_index;
			
			Tour.toggle_nav_btns($nav);
			
			Tour.move_pane(section_index);
			
			return false;
		});
	},
	
	toggle_nav_btns:function($nav)
	{
		var $prev = $nav.find('li.prev');
		var $next = $nav.find('li.next');
		
		var num_steps = $('#tour div.step').size();
		
		$('#tour').removeAttr('class').addClass('step-' + (Tour.section_index +1));
		
		if (Tour.section_index > 0)
		{
			$prev.fadeIn();
		}
		else
		{
			$prev.fadeOut();
		}
		
		if (Tour.section_index < (num_steps -1))
		{
			$next.fadeIn();
		}
		else
		{
			$next.fadeOut();
		}
	},
	
	move_pane:function(section_index)
	{	
		if ( ! section_index)
		{
			section_index = Tour.section_index;	
		}
		
		var $content = $("#tour div.content");
		
		var new_left_pos = section_index * 700;
		new_left_pos = '-' + new_left_pos + 'px';
		
		$content.animate(
		{ 
			left: new_left_pos
		},
		500);
	}
};