$(document).ready(function() {
	var currentSlide = 1;

	$('div.slide').hide();
	
	$('div.topSlide').html($('#slide1').html());
	$('div.bottomSlide').hide();
	
	$('a#sliderPrev').click(function() {
		try {
			currentSlide--;
			var previousHeight = $('#secondary-content').outerHeight();
			var newHeight = previousHeight;

			if (currentSlide == 1) $('a#sliderPrev').hide();
			$('a#sliderNext').show();
			
			$('.topSlide')
				.css({ zIndex: '400' })
				.fadeOut('slow');
			
			$('.bottomSlide')
				.html($('div#slide' + currentSlide).html())
				.css({
					position: 'absolute',
					top: '20px',
					zIndex: '500',
					left: '-1000px'
				})
				.show()
				.animate({
					left: '0'
				}, 'normal', 'swing', function() {
					$('.topSlide')
						.css({
							zIndex: '400',
							top: '0',
							position: 'relative'
						})
						.removeClass('topSlide')
						.addClass('bottomSlide')
						.hide();

					$(this)
						.css({
							zIndex: '500',
							top: '0px',
							position: 'relative'
						})
						.removeClass('bottomSlide')
						.addClass('topSlide');
						
						newHeight = $('#secondary-content').outerHeight();

						if (newHeight < previousHeight) {
							var bMargin = previousHeight - newHeight;
							$('#secondary-content').css({marginBottom: bMargin + 'px'})
						}
						else $('#secondary-content').css({marginBottom: '0'});

						$('html, body').animate({scrollTop: $('body').attr('scrollHeight') }, 'slow');
						// $('html, body').scrollTop($('body').attr('scrollHeight'));
				});
		}
		catch(err) {
			return false;
		}
		
		return false;
	});


	$('a#sliderNext').click(function() {
		try {
			currentSlide++;
			var previousHeight = $('#secondary-content').outerHeight();
			var newHeight = previousHeight;

			if (currentSlide == $('div.slide').length) $('a#sliderNext').hide();
			$('a#sliderPrev').show();
			
			$('div.bottomSlide')
				.html($('div#slide' + currentSlide).html())
				.fadeIn('normal');
			
			$('div.topSlide')
				.css({
					position: 'absolute',
					top: '20px'
				})
				.animate({
					left: '-1000px'
				}, 'normal', 'swing', function() {
					$('div.bottomSlide')
						.removeClass('bottomSlide')
						.addClass('topSlide');
					$(this)
						.css({
							zIndex: '400',
							position: 'relative',
							top: '0px',
							left: '0px'
						})
						.removeClass('topSlide')
						.addClass('bottomSlide')
						.html('')
						.hide();
						
					newHeight = $('#secondary-content').outerHeight();
					if (newHeight < previousHeight) {
						var bMargin = previousHeight - newHeight;
						$('#secondary-content').css({marginBottom: bMargin + 'px'})
					}
					else $('#secondary-content').css({marginBottom: '0'});

					$('html, body').animate({scrollTop: $('body').attr('scrollHeight') }, 'slow'); 
					// $('html, body').scrollTop($('body').attr('scrollHeight'));
				});
		}
		catch(err) {
			return false;
		}

		return false;
	});

	
});