(function($) {
	
	$.splash = function() {
		
		var splashDiv = $("#splash"),
		splashImage = splashDiv.children("img"),
		splashThumbs = $("<div />").addClass("splash-thumbs").appendTo(splashDiv),
		oHeight = parseInt(splashImage.attr("height"), 10),
		oWidth = parseInt(splashImage.attr("width"), 10),
		win = $(window).resize(sizeIt),
		footer = $("footer"),
		infos = $("#info"),
		activated = false,
		noClick = false;
		
		init();
		
		function init() {
		
			sizeIt();
			
			var a, img, st;
			
			$("#bg-images").children("ul").each(function() {
				
				a = $("<a />").attr("href", "#/" + $(this).attr("title").toString().toLowerCase().split(" ").join("_")).appendTo(splashThumbs).click(clicked);
				img = $("<img />").attr("src", $(this).attr("data-rel")).appendTo(a);
			
			});	
			
			if(document.URL.search("#/") === -1) {
			
				splashDiv.css({display: "none", visibility: "visible"}).fadeIn(300);
				
			}
			else {
			
				clicked();
				
			}
			
		}
		
		$.splash.activate = function() {
		
			footer.css("display", "none");	
			if(infos.length) infos.css("display", "none");
			
			splashDiv.stop(true, true).fadeIn(300);
			noClick = false;
			
		}
		
		function startIt() {
		
			$.address.crawlable(seoCrawlable);
			$.htmlMusic(musicSettings, html5);
			$.bgSlideshow(bgSettings);
			$.infoToggle();
				
			musicSettings = null;
			bgSettings = null;
			seoCrawlable = null;
			html5 = null;
			
		}
		
		function clicked() {
			
			if(noClick) return;
			
			footer.css("display", "block");
			
			if(infos.length) infos.css("display", "block");
			
			if(!activated) {
				
				splashDiv.stop(true, true).css({display: "none", visibility: "visible"});	
				activated = true;
				startIt();
				
			}
			else {
				
				noClick = true;
				splashDiv.stop(true, true).fadeOut(300, reactivate);
				
			}
			
		}
		
		function reactivate() {
		
			$.bgSlideshow.reActivate();
			
		}
		
		function sizeIt() {
			
			var winTall = win.height(), winWide = win.width(), perc, w, h, x, y, 
			tall = winTall / oHeight,
			wide = winWide / oWidth;
			
			perc = (wide > tall) ? wide : tall;
			w = Math.ceil(oWidth * perc);
			h = Math.ceil(oHeight * perc);
				
			(w > winWide) ? x = (w - winWide) >> 1 : x = 0;
			(h > winTall) ? y = (h - winTall) >> 1 : y = 0;
			
			splashImage.width(w).height(h).css({top: -y, left: -x});
			
		}
		
	}
	
})(jQuery);

