var site = (function($) {
	return {		
		"createOverlay" : function() {
			var append = "<div id=\"Overlay\">";
			
			for (i = 0; i < 16; i++) { 
				append += "<div></div>"; 
			}
			
			$("#InvestigatingBlock").after(append + "</div>");
			$("#Overlay div").css({"opacity" : 0.0});
		},
		
		"highlightOverlay" : function () {
			var maxVal = Math.round(Math.random() * 6);
			
			for (i = 0; i < maxVal; i++) {
				var index = Math.round(Math.random() * 16);
				var color = Math.round(Math.random() * 2) >= 1 ? "#FFFFFF" : "#B4CFDE";
				
				$("#Overlay div:eq(" + index + "):not(:animated)")
				.animate({opacity: 0.3}, {queue:true, duration: 1000})
				.animate({opacity:0.0}, 700)
				.css({"background-color" : color});
			}
		},
		
		"getQuote" : function() {
			var index = 0;
			
			//cache quotes
			if (typeof site.getQuote.quotesCache == "undefined") {
				jQuery.getJSON("data/quotes.json", function(json){
					site.getQuote.quotesCache = json;
					
					//Load random quote
					index = Math.round(Math.random() * (json.quotes.length - 1));
					site.loadQuote(json.quotes[index]);
				});
			} else {
				//Load random quote
				index = Math.round(Math.random() * (site.getQuote.quotesCache.quotes.length - 1));
				site.loadQuote(site.getQuote.quotesCache.quotes[index]);
			}
		},
		
		"loadQuote" : function(quoteObj) {
			var alt = $(document.createElement('textarea')).html((quoteObj.alt ? quoteObj.alt : quoteObj.author)).val();
		
			//Assign data to objects
			$("#QuoteBar blockquote p").fadeOut(1000, function(){ 
				$("#QuoteBar span.quoteAuthor").html("&mdash; " + quoteObj.author);
				$("#QuoteBar span.quoteSource").html(quoteObj.source);
				$("#QuoteBar blockquote p").html(quoteObj.quote).fadeIn();
				
				if (typeof quoteObj.image != "undefined") {
					$("#QuoteBar img.quoteImage")
						.attr('src', quoteObj.image)
						.attr('alt', alt)
						.attr('title', alt)
						.css({'display' : 'block'});
				} else {
					$("#QuoteBar img.quoteImage").css({'display' : 'none'});
				}
				
				if (quoteObj.cite) {
					$("#QuoteBar blockquote").attr('cite', quoteObj.cite);
					$("#QuoteBar span.quoteSource").html("<a href=\"" + quoteObj.cite + "\" target='_blank'>" + quoteObj.source + "</a>");
				}
			});	
		}
	}
})(jQuery);

(function($) {
	$(document).ready(function(){		
		// Load random quote
		site.quoteInterval = setInterval("site.getQuote();", 60000);
		
		// Link click on quote to change quote
		$("#QuoteBar p").click(function() {
			site.getQuote();
		});
		
		// Animate top menu
		site.createOverlay();
		site.overlayInterval = setInterval("site.highlightOverlay();", 5000);
		
		// Animate page background
		$(".movingBackground").css({backgroundPosition: "0px 0px"});
		$(".movingBackground").animate(
			{backgroundPosition: "4010px 0px"},
			{duration:4500, easing: "easeInOutQuint"}
		);

		// Create floating conduit
		$("#MenuBar").append('<div class="conduit"></div>');

		// Replace any evo info email links
		$('.evoinfo-conta-ct').each(function() {
			var text = $(this).html();
			text = text.replace(/ /g, '').replace(/\[at\]/, '@').replace(/\[dot\]/g, '.');
			var symbol = '@';
			email = '<a href="mai' + 'lto:evolutionary.informatics' + symbol + 'gm' + 'ail.com">' + text + '</a>'
			$(this).html(email);
		});

		// Replace any contact email links
		$('.conta-ctem-ail').each(function() {
			var text = $(this).html();
			text = text.replace(/ /g, '').replace(/\[at\]/, '@').replace(/\[dot\]/g, '.');
			var symbol = '@';
			email = '<a href="mai' + 'lto:' + text + '">' + text + '</a>'
			$(this).html(email);
		});
	});
})(jQuery);

