Date.prototype.relative_time = function()
{
	var relative_to = new Date();
	var delta = parseInt((relative_to.getTime() - this.getTime()) / 1000);
	
	delta = delta + (relative_to.getTimezoneOffset() * 60);
	
	switch (true)
	{
		case (delta < 60):
			return 'less than a minute ago';
		
		case (delta < 120):
			return 'about a minute ago';
		
		case (delta < 60 * 60):
			return (parseInt(delta / 60)).toString() + ' minutes ago';
		
		case (delta < 120 * 60):
			return 'about an hour ago';
		
		case (delta < 24 * 60 * 60):
			return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
		
		case (delta < 48 * 60 * 60):
			return '1 day ago';
		
		default:
			return (parseInt(delta / (24 * 60 * 60))).toString() + ' days ago';
	}
}

function build_slide_show()
{
	$("#slideshow").cycle({
		fx        : "shuffle",
		next      : "#slide_nav",
		timeout   : 0,
		speed     : 500,
		easing    : "",
		slideExpr : ".slide",
		cleartype: true, 
		cleartypeNoBg: true

	});
	
	$("#subscribe_submit").hover(
		function () { $(this).attr("src", "/images/footer/subscribe_submit_hover.gif") },
		function () { $(this).attr("src", "/images/footer/subscribe_submit.gif") }
	);
	 
}

$(function ()
{
	// prevent form being submitted if email address field is blank
	// could be extended so submit button image is a sprite and includes 'disabled' version
	// to indicate status
	$('#footer #subscribe').submit(function()
	{
		var email = $('#footer #subscribe #subscribe_email_address').val();
		if(email.length < 1)
		{
			return false;
		}
		else
		{
			return true;
		}
	});
	
	// Make sure any offsite links are set to open in new windows, and nofollow
	$('a').filter(function() {
		return this.hostname && this.hostname !== location.hostname;
	})
	.attr('target', '_blank')
	.attr('rel', 'nofollow')
	;
	
	//init facebox
	$('a[rel*=facebox]').facebox();
});

function show_lightbox() 
{
	jQuery.facebox({ ajax:'/lightbox.php'});
}

function load_google_map()
{
	
	if($('#map').length > 0)
	{
		if (!GBrowserIsCompatible())
		{
			return;
		}	
			
		var map = new GMap2(document.getElementById("map"));
		var office_loc = new GLatLng(50.832244, -0.139067);
		map.setCenter(office_loc, 15);
		var marker = new GMarker(office_loc);
		GEvent.addListener(
			marker,
			"click",
			function()
			{
				marker.openInfoWindowHtml(
					'<img src="/images/who_we_are/cwt_icon.gif" alt="CWT Digital" /><p style="margin-top: 1em;">CWT Digital</p>'
					);
				}
			);
		map.addOverlay(marker);
		map.setUIToDefault();
	}
}
