var target;
var hpos = [48, 34, 34, 20, 76, 34, 6, 76, 62, 48, 20, 6, 62, 34, 6, 48, 34, 20];
var vpos = [76, 76, 62, 62, 48, 48, 48, 34, 34, 34, 34, 34, 20, 20, 20, 6, 6, 6];

var xpos = $.map(hpos, function(n, i){
	return (n + 16);
});
var ypos = $.map(vpos, function(n, i){
  return (n + 16);
});
$.fn.delay = function(time, func){
  return this.each(function(){
    setTimeout(func, time);
  });
}
function load(){
	$(".block #show").css({"background-color": "#AAAAAA", "border-width": "2px", "border-color": "#666666"}).animate({width: "12px", height: "12px", bottom: "-8px", right: "-8px"}, 200, function(){
		$("#layout").addClass("ready");
		show();
	});	
}
function show() {
	$("#tl .block div").each(function(){
		$(this).animate({bottom: vpos[$(this).index()], right: hpos[$(this).index()]}, {duration: 100, queue: "tl"});	
	});
	$("#tr .block div").each(function(){
		$(this).animate({bottom: vpos[$(this).index()], left: hpos[$(this).index()]}, {duration: 100, queue: "tr"});	
	});
	$("#br .block div").each(function(){
		$(this).animate({top: vpos[$(this).index()], left: hpos[$(this).index()]}, {duration: 100, queue: "br"});	
	});
	$("#bl .block div:not(#hide)").each(function(){
		$(this).animate({top: vpos[$(this).index()], right: hpos[$(this).index()]}, {duration: 100, queue: "bl"});	
	});
	$("#bl .block #hide").animate({top: vpos[17], right: hpos[17]}, {duration: 100, queue: "bl", complete: function(){
			wait();
		}
	});
}
function wait(){
	$(this).delay(100, function(){
		$("#layout").addClass("blink");
		$(this).delay(200, function(){
			$("#layout").removeClass("blink");
			$("#logo").show();
		});
	});	
}
function spin() {	
	$("#tl .block div").each(function(){
		$(this).animate({bottom: vpos[$(this).index()], right: "-" + xpos[$(this).index()]}, {duration: 200, queue: "tl"});	
	});
	$("#tr .block div").each(function(){
		$(this).animate({bottom: "-" + ypos[$(this).index()], left: hpos[$(this).index()]}, {duration: 200, queue: "tr"});
	});
	$("#br .block div").each(function(){
		$(this).animate({top: vpos[$(this).index()], left: "-" + xpos[$(this).index()]}, {duration: 200, queue: "br"});
	});
	$("#bl .block div:not(#hide)").each(function(){
		$(this).animate({top: "-" + ypos[$(this).index()], right: hpos[$(this).index()]}, {duration: 200, queue: "bl"});
	});
	$("#bl .block #hide").animate({top: "-" + ypos[17], right: hpos[17]}, {duration: 200, queue: "bl", complete: function(){
		loop();
		spin();
		}
	});
}
function loop(){
	$(".block div").removeAttr("style");
	$("#layout").addClass("reset");
}
function stop(){
	$.fxqueue("tl").stop();
	$.fxqueue("tr").stop();
	$.fxqueue("bl").stop();
	$.fxqueue("br").stop();
}
function hide() {
	$("#tl .block div").each(function(){
		$(this).animate({bottom: '-8px', right: '-8px'}, {duration: 100, queue: "tl"});
	});
	$("#tr .block div").each(function(){
		$(this).animate({bottom: '-8px', left: '-8px'}, {duration: 100, queue: "tr"});
	});
	$("#br .block div").each(function(){
		$(this).animate({top: '-8px', left: '-8px'}, {duration: 100, queue: "br"});
	});	
	$("#bl .block div:not(#hide)").each(function(){
		$(this).animate({top: '-8px', right: '-8px'}, {duration: 100, queue: "bl"});
	});
	$("#bl .block #hide").animate({top: '-8px', right: '-8px'}, {duration: 100, queue: "bl", complete: function(){
		skip();
		}
	});
}
function skip(){
	$(".block div:not(#hide)").hide();
	$(".block #hide").css({"background-color": "transparent", "border-width": "1px", "border-color": "#AAAAAA"}).animate({width: "4px", height: "4px", top: "-3px", right: "-3px"}, 200, function(){
		location.href = target;
	});	
}
$(document).ready(function(){
	$("a").click(function(){
		target = $(this).attr("href");
		stop();
		loop();
		$("#layout").addClass("blink");
		$(this).delay(200, function(){
			$("#layout").removeClass("blink");
			$(this).delay(100, function(){
				hide();
			});
		});
		$("#logo a").unbind();
		return false;
	});
});
$(window).load(function(){
	$(this).delay(1000, function(){
		load();
	});	
	$("#logo a").hover(function(){
		spin();	
	}, function(){
			stop();
			loop();
	});	
});
