$(function () { x.init(); });

var x = {
	COLORS: ['#C3DA2E', '#E40560', '#54534B'],
	IMAGES: [1, 2, 3, 4, 5, 6],
	PAUSE: 1000,
	SPEED_IN: 1500,
	SPEED_OUT: 800,
	activeColor: 0,
	imagesLoaded: 0,
	TRANS_EXT: 'png',
	
	init: function()
	{
		x.shuffle(x.COLORS);
		x.shuffle(x.IMAGES);
		
		// preload the three dot images
		$(x.IMAGES).each(function() {
			var imagePath = x.makeImagePath(this);
			$(new Image()).load(function() {
				if (++x.imagesLoaded == x.IMAGES.length)
				{
					x.setColor();
				}
			}).attr('src', imagePath);
		});
	},
	
	fadeOut: function(onComplete)
	{
		$('#body-landing').animate({'opacity': 0}, x.SPEED_OUT, function(){
			onComplete();
		});
	},
	
	newColor: function()
	{
		x.fadeOut(function() { x.setColor(); });
	},
	
	setColor: function()
	{
		$('#body-landing').css('opacity', 0).css('background-color', x.COLORS[x.activeColor]).animate({'opacity': 1}, x.SPEED_IN, function(){
			if (x.activeColor < x.COLORS.length)
			{
				setTimeout(x.newColor, x.PAUSE);
			} else
			{
				setTimeout(function() { x.fadeOut(function() { location.href = '_/#home'; }); }, x.PAUSE);
			}
		});
		$('#body-landing .landing').css('background-image', 'url('+x.makeImagePath(x.IMAGES[x.activeColor])+')');
		x.activeColor++;
	},
	
	makeImagePath: function(i)
	{
		return 'img/intro/' + i + '.' + x.TRANS_EXT;
	},
	
	shuffle: function(arr)
	{
		for (var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x);
		return arr;
	}
};