(function(){

	var iterations  = 64;
	var pixelHeight = 12;
	var pixelWidth  = 12;
	var width       = Math.floor($(window).width() / pixelWidth);
	var height      = Math.floor($(window).height() / pixelHeight);
	var minReal     = -2;
	var maxReal     = 1;
	var minImag     = -1.2;
	var maxImag     = minImag + (maxReal - minReal) * (height / width);
	var realFactor  = (maxReal - minReal) / (width - 1);
	var imagFactor  = (maxImag - minImag) / (height - 1);
	var colors      = [];
	var startColor  = [30, 34, 38];
	var endColor    = [36, 40, 44];
	
	for (var 
		i = 0, 
		step1 = (endColor[0] - startColor[0]) / iterations, 
		step2 = (endColor[1] - startColor[1]) / iterations,
		step3 = (endColor[2] - startColor[2]) / iterations;
		i <= iterations; 
		i++
	) {
		colors.push([startColor[0] + Math.floor(i * step1), startColor[1] + Math.floor(i * step2), startColor[2] + Math.floor(i * step3)]);
	}

	$(document).ready(function() {
		var mandelbrotContainer = $("#mandelbrot");
		function setPixel(x, y, color) {
			mandelbrotContainer.append($(document.createElement("span")).css({ left: (x * pixelWidth) + "px", top: (y * pixelHeight) + "px", "background-color": "rgb(" + color.join(",") + ")" }));
		}
		
		var pixelMap = [];
		for (var i = 0; i < width; i++) {
			for (var j = 0; j < height; j++) {
				pixelMap.push([i, j]);
			}
		}
		
		var mandelbrotInterval = null;
		function setNextPixel() {
			var pixel = getNextPixel();
			if (pixel === null) {
				window.clearInterval(mandelbrotInterval);
				return;
			}
			
			var x = pixel[0], y = pixel[1];
			var cReal = zReal = minReal + (x * realFactor);
			var cImag = zImag = maxImag - (y * imagFactor);
			for (var i = 0, zImag2, zReal2; i < iterations; i++) {
				zImag2 = zImag * zImag;
				zReal2 = zReal * zReal;
				if (zReal2 + zImag2 > 4) {
					break;
				}
				
				zImag = 2 * zReal * zImag + cImag;
				zReal = zReal2 - zImag2 + cReal;
			}
			
			//if (i !== iterations) {
				setPixel(x, y, colors[i]);
			//}
		}
		
		function getNextPixel() {
			if (pixelMap.length === 0) {
				return null;
			}
			
			return pixelMap.splice(Math.floor(Math.random() * pixelMap.length), 1)[0];
		}
		
		//mandelbrotInterval = window.setInterval(setNextPixel, 100);
	});

}());

(function(){
	
	var cloudXDir = [];
	var cloudYDir = [];
	var clouds    = [];
	
	var moveClouds = function() {
		for (var i = 0, len = clouds.length, bg, x, y, width, height; i < len; i++) {
			bg = clouds[i].style.backgroundPosition.split(" ");
			if (bg.length !== 2) {
				bg = [100, 100];
			}
			
			width  = clouds[i].offsetWidth;
			height = clouds[i].offsetHeight;
			
			x = -parseInt(bg[0]) + cloudXDir[i];
			y = -parseInt(bg[1]) + cloudYDir[i];
			
			if (x < 0 || x + width > 600) {
				cloudXDir[i] = -cloudXDir[i];
				x = Math.max(0, Math.min(x, 600 - width));
			}
			if (y < 0 || y + height > 600) {
				cloudYDir[i] = -cloudYDir[i];
				y = Math.max(0, Math.min(y, 600 - height));
			}
			
			clouds[i].style.backgroundPosition = "-" + x + "px -" + y + "px";
		}
	}
	
	$(document).ready(function() {
		clouds = document.getElementById("menu-main").getElementsByTagName("a");
		for (var i = 0, len = clouds.length, rise, run; i < len; i++) {
			rise         = Math.floor(Math.random() * 2) + 1;
			run          = Math.floor(Math.random() * 2) + 1;
			
			cloudXDir[i] = Math.random() < .5 ? -run  : run;
			cloudYDir[i] = Math.random() < .5 ? -rise : rise;
		}
		
		window.setInterval(moveClouds, 100);
	});

}());