$(function() {
	
	var myinterval;
	
	var slideit = function($dir) {
		
		$('#slideshow img:nth-child(' + $cur +')').css({ opacity: 0 });
		
		if($dir == 'left') {
			
			clearInterval(myinterval);

			if($cur > 1) {
				$cur--;
			} else {
				$cur = $tot;
			}
			
		} else if ($dir == 'right') {

			clearInterval(myinterval);

			if($cur < $tot) {
				$cur++;
			} else {
				$cur = 1;
				window.location = "http://shopbryna.com/store/fall-2011";
			}
			
		} else {
			if($cur < $tot) {
				$cur++;
			} else {
				$cur = 1;
			}
		}
		
		$('#slideshow img:nth-child(' + $cur +')').animate({ opacity: 1}, 500);
	};
	
	$tot = 0;
	$cur = 1;
	
	$('#slideshow img').each(function() {
		if($(this).attr('id') != 'first') {
			$(this).css({ opacity: 0, display: 'block' });
		}
		$tot++;
	});
	
	controlsHTML = '<a id="left"><</a><a id="right">></a><a id="link" href="/store/fall-2011"></a>';
	$('#slideshow').append(controlsHTML);
	
	$("#slideshow a").css({ opacity: .5 });
	
	$("#slideshow a").click(function() {
		//alert($(this).attr('id'));
		slideit($(this).attr('id'));
		
	});
	
	

	$(window).bind("load", function() {
		myinterval = setInterval(slideit, 5000);
	});
});
