var nofactbg;
var urls = new Array();
var imgs = new Array();
var colors = new Array("#eb51a9","#b315db","#00b9df","#ff6f00","#64cf6b","#ff002f","#494eeb","#8e96dd");

$(document).ready(function(){
	$(".navwhy").attr("id","menuselected");
	$(".navwhy img").attr("src","/_img/whydoremus_select.png");
	var thiscolor = colors[Math.floor(Math.random() * 8)];
	$("body").css("background-color",thiscolor);
	$("#contentOverlay").css("background-color",thiscolor);
	globalBinds();
	var hasBeenClicked = 0;

	$("#contentOverlay").delay(2500).fadeOut(1000);
	
	/* 2 following lines are for default message on home page. */
	//$("#tuckdesc").delay(3500).slideDown(250);
	//hasBeenClicked = 1;

	$("#contentWrapper").find(".statement").each(function() {
		urls.push($(this).attr("id").replace(/--/g,"/"));
		imgs.push(new Image());
	});
	for(var i = 0; i < $(".statement").length; i++) { imgs[i].src = urls[i].replace(/-/g, "."); }

	/* CUSTOM FACTS TRACKING BELOW */
	var thisFact = 1;
	var thisLabel ="";
	$(".statement").each(function() {
		$(this).click(function() {
			thisLabel = $(this).find(".headline").text();
			_gaq.push(['_trackEvent', 'Facts', thisLabel, 'Fact '+thisFact]);
		});
		thisFact++;
	});
	
	if ($.browser.msie && $.browser.version == '6.0') { nofactbg = "/_img/whydoremusbg2.gif"; }
	else { nofactbg = "/_img/whydoremusbg2.png"; }
	
	$(".toptable").click(function(event) {
		var thisstatement = $(this).parent().attr("id");
		var thisbackground = thisstatement.replace(/--/g,"/");

		if(!hasBeenClicked) {
			hasBeenClicked = 1;
			$("#tnavleft").fadeTo(200, 0.0, function() {$(this).slideUp(400, function(){$(this).remove();});});
			$("#factContent").find("#"+thisstatement+"desc").slideDown(250);
			$("body").css({
				"background-repeat" : "repeat",
				"background-image" : "url("+thisbackground.replace(/-/g,".")+")"
			});
			$(this).parent().attr("class","currentstatement");
		} else if(hasBeenClicked && $(".currentstatement").attr("id") != thisstatement) {
			$(".currentstatement").find(".headline").css("color","#999999");
			$(".currentstatement").attr("class","statement");
			$(".descHolder:visible").slideUp(150, function() {
				$("#contentWrapper").find("#"+thisstatement+"desc").slideDown(250);
			});
			$("body").css({
				"background-repeat" : "repeat",
				"background-image" : "url("+thisbackground.replace(/-/g,".")+")"
			});
			$(this).parent().attr("class","currentstatement");
		} else if(hasBeenClicked && $(".currentstatement").attr("id") == thisstatement) {
			$(".currentstatement").find(".headline").css("color","#999999");
			$(".descHolder:visible").slideUp(150);
			$(this).parent().attr("class","statement");
			thiscolor = colors[Math.floor(Math.random() * 8)];
			$("body").css({
				"background-color" : thiscolor,
				"background-repeat" : "no-repeat",
				"background-image" : "url("+nofactbg+")"
			});
			$("#contentOverlay").css("background-color",thiscolor);
			hasBeenClicked = 0;
		}

		$(this).find(".headline").animate({fontSize: "1.16em"}, 100);
		$(".currentstatement").find(".thumbnail").fadeOut(100);
	});

	$(".toptable").hover(
		function() {
			if($(this).parent().attr("class") == "currentstatement") { return false; }
			$(this).find(".headline").animate({fontSize: "1.5em"}, 100).css("color","#333");
			$(this).find(".thumbnail").fadeIn(200);
		}, 
		function() {
			if($(this).parent().attr("class") == "currentstatement") { return false; }
			$(this).parent().find(".thumbnail").fadeOut(100);
			$(this).find(".headline").animate({fontSize: "1.16em"}, 100).css("color","#999999");
		}
	);

});
