function loadSplashImages() {
	for (var i = 2; i <= 7; i++) {
		var div = $(document.createElement('div')).appendTo($('#splash-container')).addClass('splash-item img'+i).css({
			'backgroundImage' : 'url(images/splash/j'+i+'.jpg)',
			'display' : 'none',
			'top' : 650
		});
	}
}

function animateTo(n) {
	var activeImage = $('#splash-container').children('.active:first');
	var targetImage = $('#splash-container').children('.img'+n+':first');
	
	if (activeImage.get(0) != targetImage.get(0)) {
		activeImage.removeClass('active').animate({
			'opacity' : 0
		}, 300);
		targetImage.addClass('active').css({
			'top' : 650,
			'opacity' : 0,
			'display' : 'block'
		}).animate({
			'top' : 0,
			'opacity' : 1.0
		}, 800)
		$('#splash-number').children().removeClass('active');
		$('#splash-number').children('li:eq('+(n-1)+')').addClass('active');
		
		var activeText = $('#splash-text-wrapper').children('.active:first');
		var targetText = $('#splash-text-wrapper').children('.splash-text-'+n);
		activeText.removeClass('active').addClass('on-animate').css({
			'opacity' : 1
		}).animate({
			'right' : 400,
			'opacity' : 0
		}, 800)
		targetText.addClass('active').css({
			'right' : -500,
			'opacity' : 0
		}).animate({
			'right' : 50,
			'opacity' : 1
		}, 500, function () {
			targetText.css({
				'opacity' : 'none'
			})
		})
	}
}

var activeNumber = 0;
var animateInterval = null;
var lastClick = new Date();

function autoAnimate() {
	animateInterval = window.setInterval(function () {
		if ((new Date()) - lastClick > 7888) {
			activeNumber += 1;
			activeNumber = activeNumber % 7;
			animateTo(activeNumber + 1);			
		}
	}, 8000);
}

function assignSplashNumberClick() {
	$("#splash-number").children().click(function () {
		var t = $(this);
		var n = parseInt(t.text().replace(/[^0-9]+/g, ''));
		activeNumber = n-1;
		//window.clearInterval(animateInterval);
		//animateInterval = null;
		lastClick = new Date();
		animateTo(n);
	})
}

$(document).ready(function () {
	$(document.body).height(Math.max($(window).height(), $(document.body).height()));
	$('#site-wrapper').height($(document.body).height());
	
	if ($("#splash-number").get(0)) {
		loadSplashImages();
		autoAnimate();
		assignSplashNumberClick();		
	}
});
