$(document).ready(function() {

	function addCommas(str) {
		str += '';
		x = str.split('.');
		x1 = x[0];	
		rgx = /(\d+)(\d{3})/;
		while (rgx.test(x1)) {
			x1 = x1.replace(rgx, '$1' + ',' + '$2');
		}
		return x1;
	}
	
	function howOld(squares,days,seconds,bbRatio) {
		today = new Date();
		birthDay = new Date(1983,10,25,23,0,0);
		secondsSince1970 = today.getTime()/1000;
		secondsSinceBirthday = birthDay.getTime()/1000;
		secondsOld = Math.round(secondsSince1970 - secondsSinceBirthday);
		daysOld = Math.round(secondsOld/60/60/24);
		secondsSinceBB = 433620000000000000;
		bbAliveRatio = (secondsOld/secondsSinceBB)*100;
		if ( squares ) {
			squares = daysOld/100;
			html = '<div class="square" title="100 days"></div>';
			remainderDays = daysOld%100;
			widthHeightOfSquare = Math.round(Math.sqrt(remainderDays));
			return $('#age').repeat(squares, html).find('.square:last').css({'width' : widthHeightOfSquare, 'height' : widthHeightOfSquare}).attr('title', remainderDays + ' days');
		} else if ( days ) {
			return $('#age').append('<p>' + addCommas(daysOld) + ' days <span title="which is equivelant to">&#8660;</span> <span class="seconds"></span> seconds old</p>');
		} else if ( seconds ) {
			return $('#age span.seconds').html(addCommas(secondsOld));
		} else if ( bbRatio ) {
			return $('#age').append('<p>' + bbAliveRatio + '%</p>');
		}
	}
	
	function gravity() {
		massMe = 57;
		massMars = 6.4185 * Math.pow(10,23);
		radiusMeMars = 78341212000;				
		result1 = 0.0000000000667 * ((massMe * massMars) / (Math.pow(radiusMeMars,2)));
		
		squareMeMars = 40000 * Math.sqrt(result1);		
		
		massHouse = 453592;
		radiusMeHouse = 10;				
		result2 = 0.0000000000667 * ((massMe * massHouse) / (Math.pow(radiusMeHouse,2)));
		
		squareMeHouse = 40000 * Math.sqrt(result2);
		
		percentDiff = Math.round(result2 / result1)*100;
		
		return $('#gravity').find('.meMarsForce')
			.css({'width' : squareMeMars, 'height' : squareMeMars, '-moz-border-radius' : squareMeMars, '-webkit-border-radius' : squareMeMars, 'border-radius' : squareMeMars})
			.parent().find('.meHouseForce')
			.css({'width' : squareMeHouse, 'height' : squareMeHouse, '-moz-border-radius' : squareMeHouse, '-webkit-border-radius' : squareMeHouse, 'border-radius' : squareMeHouse}).append('<p class="percentDiff"><span>' + addCommas(percentDiff) + '%</span> larger</p>');
		
		
		
				
	}
	
	function weather() {
		paper = Raphael('weather',400 ,300);
		paper.circle(400,0,180).attr({'fill': 'r#FFEA02-#FFF', 'stroke-width': 0});
		sun = paper.circle(400,0,150).attr({'fill': '#FFEA02', 'stroke-width': 0});
		function run() {
			sun.animate({r: 155, fill:'#FFBA02'}, 2000, "<", function() {
				sun.animate({r: 150, fill:'#FFEA02'}, 2000, ">");
			});	
		}
		run();
		setInterval(function(){ run() }, 4000);
	}
	
	function thing() {
		paper = Raphael('thing',650 ,500);
		colours = ['#00414E','#CCF600','#FF5A00','#E9003A','#00A580','#E3D003'];
		width = 1000;
		for (i = 0; i < 2000; i++) {
			multiplier = i*6;
			colour = colours[Math.floor(Math.random()*colours.length)];	
			x = 2.5*multiplier % width;
			y = 6*i / 200;
			paper.circle(2+x,2+y,2).attr({'fill': colour, 'stroke-width': 0});
		}

	}
	
	howOld(true);
	howOld('',true,'');
	setInterval(function(){howOld('','',true)},1000);
	//howOld('','','',true);
	gravity();
	//thing();
	//weather();
	
	$('a[rel^=overlay]').click(function() {
		$('body').append('<div id="overlay" title="Click to close (or ESC key)"><div class="container"></div></div>');
		$('body').append('<div id="loading"</div>');
		$('#loading').css({'top' : $(window).height()/2 + $(window).scrollTop()-20, 'left' : $(window).width()/2-20});
		$('#overlay').css({'height' : $('body').height()});
		$('#overlay .container').css({'margin-top': $(window).scrollTop()+60});
		var imgPath = $(this).attr('href');
		var img = new Image();
		$(img).load(function() {
			$(this).hide();
			$('#overlay .container').append(this);
			maxImageHeight = $(window).height()-120;
			maxImageWidth = $(window).width()-120;
			imgWidth = $(this).width();
			imgHeight = $(this).height();
			ratio = imgWidth / imgHeight;
			if(imgHeight > maxImageHeight) {
				imgHeight = maxImageHeight;
				imgWidth = imgHeight * ratio;
			}
			if(imgWidth > maxImageWidth) {
				imgWidth = maxImageWidth;
				imgHeight = imgWidth / ratio;
			}
			$('#overlay .container').css({'width' : imgWidth}).css({'height' : imgHeight});
			$(this).attr('width', imgWidth);
			$(this).attr('height', imgHeight);
			$('#loading').remove();
			$(this).fadeIn('slow', function() { 
				$('#overlay .container').append('<p class="close"><a href="javascript:;">Close</a></p>');						
			});
		}).attr('src', imgPath);
		return false
	});
	$('body').click(function() {
		$('#overlay, #loading').remove();
	});
	$(document).keydown(function(e) {
		code = (e.keyCode ? e.keyCode : e.which);
		if(code == 27) { // Esc key
			$('#overlay, #loading').remove();
		}
	});
	$('a[rel^=overlay]').each(function() {
		$(this).attr('title', 'View larger version');
	});
	
});

$.fn.repeat = function(times,string) {
	this.each(function(){
		buff = string;
		for (i=1; i < times; i++) {
			buff += string;
		}
		$(this).append(buff);
	});
	return this;
}
