(function($){
	//Scroll to the top (rather then jump)
	$('a[href=#container]').click(function(){
	    $('html, body').animate({scrollTop:0}, 'slow');
	    return false;
	});
	
	
	
	$('#newsletterbtn').click(function(e){
		$('#buttons li a').removeClass("selected");
		$(this).addClass("selected");
		$('#home-newsletter').css("display","block");
		$('#socialBox').css("display","none");
		e.preventDefault();
	});
	
	$('#socialbtn').click(function(e){
		$('#buttons li a').removeClass("selected");
		$(this).addClass("selected");
		$('#home-newsletter').css("display","none");
		$('#socialBox').css("display","block");
		e.preventDefault();
	});
	
	initSlideShowTop();
	
	
})(window.jQuery);

function initSlideShowTop() {
	//if element exist we're on the right page
	if (document.getElementById("slideshow")) {
		//Check how many items there are
		var num = 0;
		//Current Slide
		var current = 0;
		$('#slideshow ul li').each(function(i){
			/* Loop through all the slides and store their accumulative widths in totWidth */
			num ++;
		});
		//Set width
		var amount = num*960;
		$('#slideshow #mask ul').css('width', amount+'px');
		//If only one item in the slideshow then remove the button
		//Should be rare so there plugged into the HTML
		if (num == 1) {
			$('#btnRight').css('display','none');
			$('#btnLeft').css('display','none');
			return false;
		}
		
		var recursion = function(toggle) {
		setTimeout( function() {
			current ++;
		
			if(current == num) {
				var scrollAmount = 0;
				current = 0;
			} else {
				var scrollAmount = current * 960;
			}
			scroll(scrollAmount);
			recursion();
		}, 5000 );
		}
		recursion();
		
		$('#btnRight').click(function(e) {
			
			current ++;
		
			if(current == num) {
				var scrollAmount = 0;
				current = 0;
			} else {
				var scrollAmount = current * 960;
			}
			scroll(scrollAmount);
			e.preventDefault();
		});
		
		$('#btnLeft').click(function(e) {
			//current --;
			//How far it can go right
			if(current == 0) {
				var maxX = (num-1);
				current = maxX;
				var scrollAmount = maxX * 960;
			} else {
				current --;
				var scrollAmount = current * 960;
			}
			
			scroll(scrollAmount);
			e.preventDefault();
			});
		
	
	}
}


function scroll(x) {
	$('#slideshow #mask ul').stop().animate({marginLeft:-x+'px'},960);
}

