animation_on = false;
last_random = 0;
already_hidden = {};
hide_speed = 0;
hide_count = 0;
letter_speed = 0;
acceleration = 0;
acceleration_b = 0;
start_delay = 0;
d_speed = 0;
letter_breather = 0;

$(document).ready( function() {
	//$("#nut_container img").css('visibility', 'hidden');
	reset();
	startSpin();
});

$(window).load( function() {
	setTimeout("startHide();", start_delay);
});

function reset() {
	animation_on = true;
	hide_speed = 25; //650
	hide_count = 0;
	already_hidden = [];
	letter_speed = 400;
	acceleration = 0; //30
	acceleration_b = 25;
	start_delay = 2000;
	d_speed = 2500;
	letter_breather = 850;
}

function startSpin() {
	$("#nut_container img").css('visibility', 'visible');
	$("#nut_container img").css('opacity', '1');
	$("#nut_container img").each( function(index, value) {
		if ($(this).attr('id') != "d") {
			var random = Math.floor(Math.random()*360);
			var random_speed = Math.floor(Math.random()*1500);
			random_speed = random_speed + 500;
			spin(value, random, random_speed);
		} else {
			spin(value, 0, d_speed);
		}
	});
}

function startHide() {
	setTimeout("hide();", hide_speed);
}

function spin(nut, random, speed) {
	if (animation_on) {
		$(nut).css('rotate', random);
		var double_random = (random + 360);
		$(nut).animate({rotate: double_random}, speed, 'linear', function() {
			spin(nut, random, speed);
		});
	} else {
		if ($(nut).attr('id') == "d") {
			$(nut).animate({rotate: 0}, d_speed, 'linear');
			macadamiaLetters();
			setTimeout("showA4();", letter_breather);		
		}
	}
}

function hide() {
	if (hide_count == 62) {
		animation_on = false;
		return false;
	}
	
	hide_count++;
	hide_speed = hide_speed - acceleration;

	var random_nut = Math.floor(Math.random()*63);
	if (random_nut == 31) { random_nut++ }
	
	var random_speed = Math.floor(Math.random()*hide_speed);
	
	while (already_hidden[random_nut]) {
		random_nut = Math.floor(Math.random()*63);
		if (random_nut == 31) { random_nut++ }
	}
	
	already_hidden[random_nut] = true;
	var nut = $("#nut_container img").get(random_nut);
	$(nut).css({opacity: '0'});
	
	if (animation_on) {
		setTimeout("hide();", hide_speed);
	}
}

function macadamiaLetters() {
	var d = $("#d");
	$("#nut_container img").not(d).stop();
	$("#nut_container img").not(d).css('rotate', 0);
	
	$("#m1").css({paddingLeft: '0', paddingRight: '0', marginLeft: '284px'});
	$("#m1").attr('src', 'library/img/m.png');
	
	$("#a1").css({paddingLeft: '0', paddingRight: '0'});
	$("#a1").attr('src', 'library/img/a.png');
	
	$("#c").css({paddingLeft: '0', paddingRight: '0'});
	$("#c").attr('src', 'library/img/c.png');
		
	$("#a2").css({paddingLeft: '0', paddingRight: '0'});
	$("#a2").attr('src', 'library/img/a2.png');
	
	$("#d").css({opacity: '1', paddingLeft: '0', paddingRight: '0'});
	
	$("#a3").css({paddingLeft: '0', paddingRight: '0'});
	$("#a3").attr('src', 'library/img/a3.png');
	
	$("#m2").css({paddingLeft: '0', paddingRight: '0'});
	$("#m2").attr('src', 'library/img/m.png');
	
	$("#i").css({paddingLeft: '0', paddingRight: '0'});
	$("#i").attr('src', 'library/img/i.png');
	
	$("#a4").css({paddingLeft: '0', paddingRight: '0'});
	$("#a4").attr('src', 'library/img/a.png');
}

function showA4() {
	$("#a4").css({opacity: '1'});
	letter_speed = letter_speed - acceleration_b;
	setTimeout("showA2();", letter_speed);
}

function showA2() {
	$("#a2").css({opacity: '1'});
	letter_speed = letter_speed - acceleration_b;
	setTimeout("showA1();", letter_speed);
}

function showA1() {
	$("#a1").css({opacity: '1'});
	letter_speed = letter_speed - acceleration_b;
	setTimeout("showA3();", letter_speed);
}

function showA3() {
	$("#a3").css({opacity: '1'});
	letter_speed = letter_speed - acceleration_b;
	setTimeout("showM2();", letter_speed);
}

function showM2() {
	$("#m2").css({opacity: '1'});
	letter_speed = letter_speed - acceleration_b;
	setTimeout("showM1();", letter_speed);
}

function showM1() {
	$("#m1").css({opacity: '1'});
	letter_speed = letter_speed - acceleration_b;
	setTimeout("showC();", letter_speed);
}

function showC() {
	$("#c").css({opacity: '1'});
	letter_speed = letter_speed - acceleration_b;
	setTimeout("showI();", letter_speed);
}

function showI() {
	$("#i").css({opacity: '1'});
	letter_speed = letter_speed - acceleration_b;
	setTimeout("bomb();", letter_speed);
}

function bomb() {
	$("#coming_soon").show();
}

function stop() {
	animation_on = false;
}
