function auto_slide(total_slides,slide_width)
{
	var slideWidth = slide_width;
	var current = $('#slideshow-navigation a.active').attr('rel');
	var next = current++;
	if(next >= total_slides)
	{
		next = 0;
	}
	
	set_active_nav_square(next);
	next++;

	if($('#slide-inner .contents').is(':visible'))
	{
		$('#slide-inner .contents:visible').stop().animate({'bottom' : -500}, 800, 'swing',function(){
			$('#slide-inner').stop().animate({
			'marginLeft' : slideWidth*(-next)+slideWidth
			},function(){
				$('#slide-inner .contents').stop().delay(100).show().animate({'bottom' : 3}, 800, 'swing');
			});
		});
	} else {
		$('#slide-inner').stop().animate({
		'marginLeft' : slideWidth*(-next)+slideWidth
		},function(){
			$('#slide-inner .contents').stop().delay(100).show().animate({'bottom' : 3}, 800, 'swing');
		});
	}
}

function set_active_nav_square(position)
{
	$('#slideshow-navigation a').removeClass('active');
	$('#slideshow-navigation a:eq('+position+')').addClass('active');
}

$(document).ready(function(){
// move this chunk out to separate js file!
	var current_position = 0;
	var autospeed = 7000;
	var slideWidth = 670;
	var slides = $('#slideshow .slide');
	var numberOfSlides = slides.length;
	var squares_nav = '';

	// Remove scrollbar in JS
	$('#slides-container').css('overflow', 'hidden');

	// wrap all .slides with #slide-inner div
	slides.wrapAll('<div id="slide-inner"></div>')
	// float left to display horizontally, readjust .slides width
	.css({
	  'float' : 'left',
	  'width' : slideWidth
	});

	// hide all the slides' contents div
	$('#slide-inner .contents').hide();

	// insert the slide navigation squares
	/*if(numberOfSlides > 1)
		{
			for (i=1;i<=numberOfSlides;i=i+1)
			{
				squares_nav += '<a href="#" class="" rel="' + i + '">' + i + '</a>';
			}
			$('#slideshow-navigation').html(squares_nav);
		}
	*/	

	// Remove the class 'active' from all the nav squares and add it to the appropriate one
	set_active_nav_square(current_position);

	// Set #slide-inner width equal to total width of all slides
	$('#slide-inner').css('width', slideWidth * numberOfSlides);

	// if it's the first slide on page load, show the contents
	if(current_position < 1)
	{
		$('#slide-inner .contents').delay(100).show().animate({'bottom' : 3}, 800, 'swing');
	}

	// now ready to auto slide
	auto_slide_show = setInterval(function(){ auto_slide(numberOfSlides,slideWidth) }, autospeed );


	// skip to a specific slide using the squares nav
	$('#slideshow-navigation a').live('click',function(){

		clearInterval(auto_slide_show); // stop the auto slide show

		var slide_to = $(this).attr('rel');

		// slide down the content box if it is visible
		if($('#slide-inner .contents').is(':visible'))
		{
			$('#slide-inner .contents:visible').stop().animate({'bottom' : -500}, 800, 'swing',function(){
				$('#slide-inner').stop().animate({
				'marginLeft' : slideWidth*(-slide_to)+slideWidth
				},function(){
					$('#slide-inner .contents').stop().delay(100).show().animate({'bottom' : 3}, 800, 'swing');
				});
			});
		} else {
			$('#slide-inner').stop().animate({
			'marginLeft' : slideWidth*(-slide_to)+slideWidth
			},function(){
				$('#slide-inner .contents').stop().delay(100).show().animate({'bottom' : 3}, 800, 'swing');
			});
		}
		
		move_to_pos = parseInt($(this).attr('rel'));
		current_position = move_to_pos-1;

		set_active_nav_square(current_position);

		// restart the auto slide show
		auto_slide_show = setInterval(function(){ auto_slide(numberOfSlides,slideWidth) }, autospeed );
		
		return false;
	});

});
