$(document).ready(function () {

	$.ajax({
		type: "GET",
		url: "/weather-proxy.asp",
		dataType: "xml",
		success: parseWeatherXml,
		error: function () {
			$("#lblTime").parent().replaceWith("<p>Weather data temporarily unavailable</p>");
		}
	});

	var whiteHeight = $("#whiteness").height();
	$("#whiteness").height(40);
	setTimeout('$("#whiteness").animate({ height: ' + whiteHeight + ' }, 500)', 1500);
	setTimeout('$("#whiteness").animate({ height: 40 }, 750)', 7500);
	$("#whiteness").hoverIntent({
		over: function () { $(this).animate({ height: whiteHeight }, 250); },
		out: function () { $(this).animate({ height: 40 }, 250); },
		timeout: 750
	});
	$("#whiteness div:first").fadeTo(1, .75);
	$("#subContainer").animate(
		{ top: 0 }, 1000,
		function () {
			$("ul").parent().animate({ width: 'toggle' }, 1000);
		}
	);

	var esbWidth = $("#111").width();
	$("#111").width(esbWidth * 1.5)
	$("#111").parent().animate({ top: 0 }, 500, function () {
		$("#111").animate({ width: esbWidth }, 500);
		$(this).animate({ left: 344 }, 500, function () {
			$("#btn_tower_lights").fadeIn();
			$("#btn_buy_tickets").fadeIn();
			$("#btn_sustainability").fadeIn();
			$("#home_tower_lights").fadeIn();
		});
	});


	$(".clouds").fadeTo(1, 0);

	animateCloud($("#cloud1"), -300, -600, 30000);
	setTimeout("animateCloud($(\"#cloud2\"), -600, -600, 30000)", 7000);
	setTimeout("animateCloud($(\"#cloud3\"), -300, -600, 30000)", 14000);
	setTimeout("animateCloud($(\"#cloud4\"), -600, -600, 30000)", 22000);
	setTimeout("animateCloud($(\"#cloud5\"), -400, 1200, 30000)", 2000);
	setTimeout("animateCloud($(\"#cloud8\"), -600, 1200, 30000)", 9000);
	setTimeout("animateCloud($(\"#cloud7\"), -400, 1200, 30000)", 16000);
	setTimeout("animateCloud($(\"#cloud6\"), -600, 1200, 30000)", 24000);


});

function animateCloud(cloud, goToTop, goToLeft, animDuration) {
	if (!cloud.length) {
		return false;
	}
	var cloudWidth = cloud.width();
	var cloudDiv = cloud.parent();
	var cloudPosition = cloudDiv.position();
	var cloudTop = cloudPosition.top;
	var cloudLeft = cloudPosition.left;

	cloud.width("100%");
	cloudDiv.width(cloudWidth * .35);

	cloudDiv.animate({ top: goToTop, left: goToLeft, width: cloudWidth * 2, opacity: 1 }, animDuration, function () {
		cloud.fadeTo(1, 0, function () {
			cloudDiv.css({ top: cloudTop, left: cloudLeft }).width(cloudWidth);
			animateCloud(cloud, goToTop, goToLeft, animDuration);
		});
	})
	cloud.fadeTo(10000, 1);
}


function parseWeatherXml(xml) {
	$(xml).find("loc").each(function () {
		$("#lblTime").after($(this).children("tm").text());
		$("#lblSunset").after($(this).children("suns").text());
	});
	$(xml).find("cc").each(function () {
		$("#lblWeather").after($(this).children("t").text() + ", " + $(this).children("tmp").text() + "&deg;");
	});
	$(xml).find("day[d='0']").each(function () {
		$("#lblDate").after($(this).attr("t") + ", " + $(this).attr("dt"));
	});
}
