jQuery(function($){init()});

var backgroundSourceWidth, backgroundSourceHeight;
var backgroundImageCaption;
var slotW = 150;
var slotH = 150;
var distributionSlots;
var slotBufferX = 50;
var slotBufferY = 50;
var minWindowWidth = 950;
var minWindowHeight = 600;


function init(){
	if(swfobject.hasFlashPlayerVersion("9")){
		initFlash();
	} else {
		initHTML();
	}
}

function initFlash(){
	$("body").html("<div id='flash' />");
	swfobject.embedSWF("swf/Menu11.swf", "flash", "100%", "100%", "10");
}

function initHTML(){
	processBackground();
	showHeader();
	showUtils();
	setPlanets();
	setWindow();
}

function setWindow(){
	$(window).resize(windowResize);
	$(window).resize();
}

function windowResize(){
	if($("#backgroundImage").length > 0) resizeBackground();
	if($("#backgroundImageCaption").length > 0) positionBackgroundImageCaption();
	// if($("#utils").length > 0) positionUtils();
	if($("#planetLayer").length > 0) distributePlanets();
}

function processBackground(){
	var backgroundNode = $("figure")[Math.floor(Math.random()*$("figure").length)];
	backgroundImageCaption = $($(backgroundNode).children("figcaption")[0]).text();
	var imagePath = $(backgroundNode).children("img")[0].src;
	var imageObject = new Image();
	$(imageObject).load(backgroundLoadComplete);
	imageObject.src = imagePath;
	$("figure").remove();
}

function backgroundLoadComplete(){
	setBackground(this);
	setBackgroundImageCaption();
}

function setBackground(imageObject){
	$("body").append("<img src='" + imageObject.src + "' id='backgroundImage' />");
	backgroundSourceWidth = $("#backgroundImage").width();
	backgroundSourceHeight = $("#backgroundImage").height();
	resizeBackground();
	$("#backgroundImage").hide().fadeIn(1000);
	$("body").append("<div id='backgroundOverlay' />");
}

function resizeBackground(){
	var scale = Math.max($(window).width()/backgroundSourceWidth, $(window).height()/backgroundSourceHeight);
	$("#backgroundImage").attr({width:backgroundSourceWidth*scale, height:backgroundSourceHeight*scale});
	$("#backgroundImage").css({left:($(window).width() - $("#backgroundImage").width())/2 + "px"});
	$("#backgroundImage").css({top:($(window).height() - $("#backgroundImage").height())/2 + "px"});
}

function setBackgroundImageCaption(){
	$("body").append("<div id='backgroundImageCaption'><p>" + backgroundImageCaption + "</p></div>");
	positionBackgroundImageCaption();
	$("#backgroundImageCaption").hide().show(1000);
}

function positionBackgroundImageCaption(){
	$("#backgroundImageCaption").css({top:$(window).height() - $("#backgroundImageCaption").height() + "px"});
}

function showHeader(){
	$("header").hide().show(1000);
}

function showUtils(){
	// positionUtils();
	$("#utils").hide().fadeIn(1000);
	$(".utilButton").hover(utilButtonRollover, utilButtonRollout).click(utilButtonClick);
}

function positionUtils(){
	$("#utils").css({left:$(window).width() - $("#utils").width() + "px", top:$(window).height() - $("#utils").height() + "px"});
}

function utilButtonRollover(){
	$(this).css({"background-image":"url(img/green.png)"});
}

function utilButtonRollout(){
	$(this).css({"background-image":"url(img/red.png)"});
}

function utilButtonClick(){
	$(this).css({"background-image":"url(img/red.png)"});
}

function setPlanets(){
	if(ie()) $(".planetBase").remove();
	$(".planet").each(initPlanet);
	$(".planet").css({left:Math.round($(window).width()/2 - 15) + "px", top:Math.round($(window).height()/2 - 15) + "px"});
	$(".planet").live({mouseover:planetRollOver, mouseout:planetRollOut, click:planetClick});
	//distributePlanets();
}

function initPlanet(index, element){
	$(element).data("href", $(element).find("a").attr("href"));
	var planetLabel = $(element).find("a").text();
	$(element).find("p").text(planetLabel);
}

function planetRollOver(e){
	$($(this).children(".planetLabel")[0]).css({"background-image":"url('img/green.png')", cursor:"pointer", color:"#000"});
	if(!ie()) $($(this).children(".planetBase")[0]).css({"-webkit-box-shadow":"#00fdf4 0px 0px 30px", "-moz-box-shadow":"#00fdf4 0px 0px 30px", "box-shadow":"#00fdf4 0px 0px 30px"});
}

function planetRollOut(e){
	$($(this).children(".planetLabel")[0]).css({"background-image":"url('img/gray.png')", cursor:"auto", color:"#fff"});
	if(!ie()) $($(this).children(".planetBase")[0]).css({"-webkit-box-shadow":"#00fdf4 0px 0px 0px", "-moz-box-shadow":"#00fdf4 0px 0px 0px", "box-shadow":"#00fdf4 0px 0px 0px"});
}

function planetClick(e){
	$(this).mouseout();
	window.location = $(this).data("href");
}

function distributePlanets(){
	var windowWidth = Math.max($(window).width(), minWindowWidth);
	var windowHeight = Math.max($(window).height(), minWindowHeight);
	var hDistributionCount = Math.floor(windowWidth/slotW);
	var vDistributionCount = Math.floor(windowHeight/slotH) - 2;
	var distrubutionCount = hDistributionCount*vDistributionCount;
	distributionSlots = new Array();
	for(var i = 0; i < distrubutionCount; i++){
		distributionSlots.push({x:slotW*(i % hDistributionCount), y:slotH + slotH*Math.floor(i/hDistributionCount)});
	}
	distributionSlots = FisherYates(distributionSlots);
	$(".planet").each(distribute);
}

function distribute(index, element){
	var targetX = Math.floor(distributionSlots[index].x + slotBufferX + (slotW - slotBufferX*2)*Math.random());
	var targetY = Math.floor(distributionSlots[index].y + slotBufferY + (slotH - slotBufferY*2)*Math.random());
	$($(element).children(".planetLabel")[0]).css({left:Math.round(-($($(element).children(".planetLabel")[0]).width() - 30)*targetX/$(window).width()) + "px"});
	$(element).clearQueue().animate({left:targetX, top:targetY}, 1000);
}
