$(document).ready(function() {

	function resizeContainer() { // Sets container to width of browser window
		$('#site_container').width($(window).width());
	}
	
	resizeContainer();
	
	function addPadding() { // Centers slideshows to window size
		if ($(window).width() > 1025) {
			$('.slideshow, header h2, .footer_container').css({
				'padding-left' : (($(window).width() / 2) - 495) + 'px'
			});
			$('header h2, .footer_container').css({
				'padding-right' : (($(window).width() / 2) - 495) + 'px'
			})
		} else {
			$('.slideshow, header h2, .footer_container').css({
				'padding-left' : 20 + 'px'
			})
			$('header h2, .footer_container').css({
				'padding-right' : 20 + 'px'
			})
		}
	}
	
	addPadding();
	
	var currentActive = []; // Array that stores the currently active slide of each slideshow

	$('.slideshow').each(function() { 
		var slideshowIndex = $(this).index();
		currentActive[slideshowIndex] = 0; // Initializes array data
	});

	$('.slides').each(function() {
		$(this).width(($(this).children('.slide').length) * 990); // Sets slides width to number of children slides multiplied by 990
	});

	$('.control').click(function() {
	
		var slideshowIndex = $(this).parent('.slideshow').index(); // Which slideshow on the page has been clicked, beginning with 0
		
		$(this).siblings('.slides').stop(); // Stops current animation
				
		if (($(this).hasClass('next')) && currentActive[slideshowIndex] < ($(this).siblings('.slides').children('.slide').length - 1)) {
			currentActive[slideshowIndex] += 1;
			$(this).siblings('.slides').animate({
				marginLeft: '-' + currentActive[slideshowIndex] * 990
			});
		} else if (($(this).hasClass('prev')) && currentActive[slideshowIndex] > 0) {
			currentActive[slideshowIndex] -= 1;
			$(this).siblings('.slides').animate({
				marginLeft: '-' + currentActive[slideshowIndex] * 990
			});
		}
		
		$(this).parent().children().removeClass('inactive'); // Removes inactive class from all controls
		
		if (currentActive[slideshowIndex] == 0) {
			$(this).parent().children('.prev').addClass('inactive'); // Adds inactive class to prev button if on first slide
		} else if (currentActive[slideshowIndex] == ($(this).siblings('.slides').children('.slide').length - 1)) {
			$(this).parent().children('.next').addClass('inactive'); // Adds inactive class to next button if on final slide
		}		
		
		return false;
	
	})

	$('#learn_more').click(function() { // Scroll to bottom
		$.scrollTo( $('#about'), 600 );
		return false;
	});

	$('#back_to_top').click(function() { // Scroll to top
		$.scrollTo( $('header'), 600 );
		return false;
	})

    $(".tweet").tweet({ // Twitter
        username: "aaronshapiro",
        count: 1,
        retweets: false,
		template: "{text}",
        loading_text: "loading tweets..."
    });
		
	$(window).resize(function() {
		resizeContainer(); // Resizes site container on window resize
		addPadding(); // Adds padding to center slideshow on window resize
	});

});
