function updateClock () {
	var t = new Date();
	var h = t.getHours(), m = t.getMinutes(), s = t.getSeconds();

	if (m < 10){
		m = "0" + m;
	}
	if (s < 10) {
		s = '0' + s;
	}
	$('#time').text(h+':'+m+':'+s);
	setTimeout("updateClock();",1000);
}
$(document).ready(function(){
	/**
	 * Add Scrollable Container to Home Page
	 */
	if($('.scrollable').length > 0) {
		$(".scrollable")
		.scrollable({circular: true})
		.autoscroll({ autoplay: true, interval: 4000 })
		.navigator()
		.each(
			function(){
				//
			}
		);
		$('.thumb').each(function(){
			$(this).find('a').bind('click',function(){
				return false;
			});
			$(this).bind('click',function(){
				var href = $(this).find('a').attr('href');
				window.open(href);
				return false;
			});
		});
	}
	var $menuLinks	= $("#menu a, a.inline-link");
	var $menuLists	= $('#menu li');
	var speedMenu		= 2000;

	$menuLinks.click(function(){
		var href = $(this).attr('href');
		switch(href){
			case '#home':	$menuLists.removeClass('selected'); $(this).parent('li').addClass('selected'); $('#body').animate({top:0},speedMenu);		break;
			case '#about':	$menuLists.removeClass('selected'); $(this).parent('li').addClass('selected'); $('#body').animate({top:-1000},speedMenu);	break;
			case '#work':	$menuLists.removeClass('selected'); $(this).parent('li').addClass('selected'); $('#body').animate({top:-2525},speedMenu);	break;
			case '#clients':$menuLists.removeClass('selected'); $(this).parent('li').addClass('selected'); $('#body').animate({top:-5025},speedMenu);	break;
			case '#blog':	$menuLists.removeClass('selected'); $(this).parent('li').addClass('selected'); $('#body').animate({top:-7600},speedMenu);	break;
			case '#lab':	$menuLists.removeClass('selected'); $(this).parent('li').addClass('selected'); $('#body').animate({top:-8630},speedMenu);	break;
			//case '#contact':$menuLists.removeClass('selected'); $(this).parent('li').addClass('selected'); $('#body').animate({top:-9610},speedMenu);	break;
			////case '#lab':	$menuLists.removeClass('selected'); $(this).parent('li').addClass('selected'); $('#body').animate({top:-9130},speedMenu);	break;
			//case '#contact':$menuLists.removeClass('selected'); $(this).parent('li').addClass('selected'); $('#body').animate({top:-10110},speedMenu);	break;
			case '#contact':$menuLists.removeClass('selected'); $(this).parent('li').addClass('selected'); $('#body').animate({top:-10610},speedMenu);	break;
		}
		return false;
	});

	$('#link-rss a').click(function(){
		$('#body').animate({top:-7600},speedMenu);
		return false;
	});

	$('#ronnie a').click(function(){
		$('#body').animate({top:-1000},speedMenu);
		return false;
	});

	$('#tech a').click(function(){
		//$('#body').animate({top:-9610},speedMenu);
		$('#body').animate({top:-10610},speedMenu);
		return false;
	});

	$('#wall-art a').click(function(){
		$('#body').animate({top:-8630},speedMenu);
		return false;
	});

	//$('#r2d2 a').click(function(){ // currently linking to ticatoca now :)
	//	$('#body').animate({top:-7600},speedMenu);
	//	return false;
	//});

	//$('.work-box p a[href^=#]').click(function(){
	//	if($(this).attr('href')=='#lab') {
	//		$('#body').animate({top:-8630},speedMenu);
	//	}
	//	return false;
	//});


	var speed = 1000;
	
	$('.column-container').masonry({

		//singleMode: false,
		// Disables measuring the width of each floated element.
		// Set to true if floated elements have the same width.
		// default: false

		columnWidth: 535,
		// Width in pixels of 1 column of your grid.
		// default: outer width of the first floated element.

		itemSelector: '.work-box:not(.invis)',
		// Additional selector to specify which elements inside
		// the wrapping element will be rearranged.
		// Required for Infinite Scroll with window resizing.

		//resizeable: true,
		// Binds a Masonry call to window resizes 
		// so layout appears fluid.
		// default: true

		animate: true,
		// Animates layout rearrangements.
		// default: false

		animationOptions: {
			duration: speed,
			queue: false
		}
		// A map of options for animation.

		//appendedContent: $('.new_content'),
		// Additional container element for appended content.
		// Useful for Infinite Scroll integration.

		//saveOptions: true
		// Masonry will use the options from previous Masonry
		// calls by default, so you only have to enter in options once
		// default: true

	}, function() {}
		// Optional callback.
		// 'this' will refer to the applicable elements Masonry just rearranged

	);
	$('#work-menu a').click(function(){

		$('#work-menu a').parent().removeClass('selected');
		$(this).parent().addClass('selected');

		var colorClass = '.' + $(this).attr('class');

		if(colorClass=='.all') {
			// show all hidden boxes
			$('.column-container').children('.invis').toggleClass('invis').animate({opacity: 1},{ duration: speed });
		} else {
			// hide visible boxes 
			$('.column-container').children().not(colorClass).not('.invis').toggleClass('invis').animate({opacity: 0},{ duration: speed });
			// show hidden boxes
			$('.column-container').children(colorClass+'.invis').toggleClass('invis').animate({opacity: 1},{ duration: speed });
		}
		$('.column-container').masonry();

		return false;
	});

	$('.hide-label span').hide();
	$('#name, #email, #message').bind({
		click: function () {
			var $i = $(this);
			if(
				$i.attr('value') == $i.parents('label').find('span').text()
				|| $i.attr('value')=='Please tell me your name'
				|| $i.attr('value')=='Please tell me your email'
				|| $i.attr('value')=='Please tell me your message'
			) {
				$i.attr('value','');
			}
			$i.parents('label').addClass('focussed').removeClass('error');
		},
		focus: function () {
			var $i = $(this);
			if(
				$i.attr('value') == $i.parents('label').find('span').text()
				|| $i.attr('value')=='Please tell me your name'
				|| $i.attr('value')=='Please tell me your email'
				|| $i.attr('value')=='Please tell me your message'
			) {
				$i.attr('value','');
			}
			$i.parents('label').addClass('focussed').removeClass('error');
		},
		blur: function () {
			var $i = $(this);
			if($i.attr('value') == '') {
				$i.attr('value',$i.parents('label').find('span').text());
			}
			$i.parents('label').removeClass('focussed');
		}
	});
	$('#contact form').bind('submit',function(){return false;});
	$('#contact-submit').bind({
		mouseover:function(){
			$(this).css({'background-position':'right top'});
		},
		mouseout:function(){
			$(this).css({'background-position':'left top'});
		},
		click:function(){
			var obj = {
				name: $('#name').val(),
				email: $('#email').val(),
				message: $('#message').val(),
				ajax: 'yes'
			};
			var url = '/contact/'
			$.post(url,obj,function(xml) {
				if($('response',xml).length < 1) {
					$('#contact-notice p').addClass('error').text('Oops! Could not send message');
				} else {
					var $status = $('status',xml).text();

					if($status=='ok') {
						$('<img src="/images/envelope-large.jpg" id="sent" style="position:absolute;width:277px;height:330px;z-index:100;left:128px;display:none;" />').insertAfter('#contact-notice');
						$('#sent').fadeIn(1000,function(){
							$('#name, #email, #message').val('').trigger('blur');
							$('label').removeClass('error');
							$('#contact-notice p:not(#time)').removeClass('error').addClass('success').text("Message Sent. I'll be in touch soon.");
							setTimeout("$('#contact img').fadeOut(3000,function(){$('#contact-notice p:not(#time)').removeClass('success').text('New Message');});",3000);
						});
					} else {
						$('#contact-notice p:not(#time)').addClass('error').text('Oops! I picked up some errors below.');
						if($('name',xml).length > 0) {
							$('#name').val('Please tell me your name');
							$('#name').parents('label').addClass('error');
						}
						if($('email',xml).length > 0) {
							$('#email').val('Please tell me your email');
							$('#email').parents('label').addClass('error');
						}
						if($('message',xml).length > 0) {
							$('#message').val('Please tell me your message');
							$('#message').parents('label').addClass('error');
						}
					}
				}
			});
		}
	});
	$('#hire-me-container form').submit(function(){
		return false;
	});
	$('#hire-submit').css({'opacity':0});
	$('#hire-submit').addClass('closed');
	$('#hire-me-submit').live('mouseover',function(){
		$(this).addClass('over');
	});
	$('#hire-me-submit').live('mouseout',function(){
		$(this).removeClass('over');
	});
	$('#hire-me-submit').live('click',function(){

		$('.error').removeClass('error');

		var name		= $('#hire-name').val();
		var description	= $('#hire-project').val();
		var urgency		= $('#hire-urgency').val();
		var budget		= $('#hire-budget').val();
		var email		= $('#hire-email').val();

		$.post('/hire/',{'ajax':true,'name':name,'description':description,'urgency':urgency,'budget':budget,'email':email}, function(xml) {
			if($('response',xml).length==1) {
				var $status = trim($('status',xml).text());
				//console.log($status);
				if($status=='true') {
					$('#hire-name, #hire-project, #hire-urgency, #hire-budget, #hire-email').val('').removeClass('error');
					$('#hire-me-failed').css({'background-position':'-354px top'});
					$('#hire-me-success').css({'background-position':'left top'});
					$('#hire-me-container').fadeOut('slow',function(){
						$('#hire-me').append('<div id="closeit"></div>');
						$('#closeit').unbind('click').bind('click',function(){
							$('#fancybox-close').trigger('click');
						});
						$('#closeit').unbind('mouseover').bind('mouseover',function(){
							$(this).addClass('over');
						});
						$('#closeit').unbind('mouseout').bind('mouseout',function(){
							$(this).removeClass('over');
						});
					});
				} else {
					$('#hire-me-failed').css({'background-position':'left top'});
					$('#hire-me-success').css({'background-position':'-354px top'});
					$('#hire-me-container').css({'background-position':'-354px top'});
					var node = $('status',xml);
					if(node.attr('name')=='true') {
						$('#hire-name').addClass('error');
					}
					if(node.attr('description')=='true') {
						$('#hire-project').addClass('error');
					}
					if(node.attr('urgency')=='true') {
						$('#hire-urgency').addClass('error');
					}
					if(node.attr('budget')=='true') {
						$('#hire-budget').addClass('error');
					}
					if(node.attr('email')=='true') {
						$('#hire-email').addClass('error');
					}
				}
			}
		});

		return false;
	});
	//$('#hire-email').mouseover(function(){
	//	$(this).val('');
	//});
	//$('#hire a').divbox({type: 'element',caption:false});
	$("#hire a").fancybox({
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'easingIn'			: 'easeOutBack',
		'easingOut'			: 'easeInBack',
		'onClosed'			: function () {
			$('#closeit').remove();
			$('#hire-me-failed').css({'background-position':'-354px top'});
			$('#hire-me-success').css({'background-position':'-354px top'});
			$('#hire-me-container').css({'display':'block','opacity':1,'background-position':'left top'});
			$('.error').removeClass('error');
		},
		'onComplete'		: function () {
			if($('#hire-submit').hasClass('closed')) {
				$('#hire-submit').animate({
					'opacity':1
				},1500,function(){
					$('#hire-submit').removeClass('closed');
				});
			}
		}
	});
	/*$.preload([
		"http://ronniewright.mystream.co.uk/images/hireme_note_1.png",
		"http://ronniewright.mystream.co.uk/images/hireme_note_2.png",
		"http://ronniewright.mystream.co.uk/images/hireme_note_blank.png",
		"http://ronniewright.mystream.co.uk/images/hireme_note_error.png"
	],{
		init: function(loaded, total) {
			if(console && console.log) {
				console.log("Loaded: "+loaded+"/"+total);
			}
		},
		loaded: function(img, loaded, total) {
			if(console && console.log) {
				console.log("Loaded: "+loaded+"/"+total);
			}
		},
		loaded_all: function(loaded, total) {
			if(console && console.log) {
				console.log("Loaded: "+loaded+"/"+total+". Done!");
			}
		}
	});*/

	if(ie6==true) {
		$('body').append('<div id="ie6bad" style="display:none;"><a href="#ie6badtext">IE6 BAD</a><div id="ie6badtext"><a href="#close" id="ie6badclose"></a></div></div>');
		$('#ie6bad a').fancybox({
			'transitionIn'		: 'elastic',
			'transitionOut'		: 'elastic',
			'easingIn'			: 'easeOutBack',
			'easingOut'			: 'easeInBack',
			'onComplete'		: function () {
				$('#ie6badclose').click(function(){
					$('#fancybox-close').trigger('click');
					return false;
				});
			}
		}).trigger('click');
	}

	updateClock();
});
function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
