$(function(){

	var div_width = $('.slide').css('width');
	div_width = parseFloat(div_width,10); // gets rid of units from CSS
	
	var divs = $('.slide').size(); // "size" = number of children
	
	var i = $('div#slides').attr('class'); // Grab slide number from class of the slides div
	i = parseFloat(i,10);
	var cur_pos = div_width*-i; // Move to current slide
	
	$('#slides').animate({left : cur_pos + "px"},0);

	$('#slides').css({width : divs*div_width + "px"});
	
	$('#next').click(function() {
		if(i + 1 < divs)
		{
			i++;
			cur_pos = div_width*-i;
			$('#slides').animate({left :  cur_pos + "px"},1000);
		}
		else
		{
			$('#slides').animate({left :  '0'},1500);
			i = 0;
		}
		return false;
	});

	$('#prev').click(function() {
		if(i - 1 >= 0)
		{
			i--;
			cur_pos = div_width*-i;
			$('#slides').animate({left :  cur_pos + "px"},1000);
		}
		else
		{
			$('#slides').animate({left :  -(divs-1)*div_width + "px"},1500);
			i = divs-1;
		}
		return false;
	});
	
	var index = 0;
	var k = 0;
	$('#sidebar_items img').each(function() { // Finds out which element we're on and puts value in the array in index
		img = $(this);
		if(img.hasClass("first"))
		{
			index = k;
		}
		k++;
	});
	
	var item_width = '79';
	var num_items = $('#sidebar_items img').size(); // "size" = number of children
	var items_width = num_items * item_width;
	
	var j = index;
	if(j > num_items-3)
	{
		j = num_items-3;
	}
	var item_pos = item_width*-j;

	$('#sidebar_items').css({width : items_width + "px"}); // Sets the size of the sidebar_items div
	
	$('#sidebar_items').animate({left : item_pos + "px"},0); // Move to the current element when the page loads
	
	$('#reveal_left').click(function() {
		if(j - 3 >= 0)
		{
			j -= 3;
		}
		else
		{
			j = 0;
		}
		item_pos = item_width*-j;
		$('#sidebar_items').animate({left :  item_pos + "px"},800);
		return false;
	});
	
	$('#reveal_right').click(function() {
		if(j + 5 < num_items)
		{
			j += 3;
		}
		else
		{
			j = num_items-3;
		}
		item_pos = item_width*-j;
		$('#sidebar_items').animate({left :  item_pos + "px"},800);
		return false;
	});
});