$(document).ready(function()
	{

		// Auto-populate form fields with their titles
		$('.populate').each(function(){
			var e = $(this);
			if (!e.val()) { e.val(e.attr('title')); }
			e.focus(function(){
				var e = $(this);
				if (e.attr('title') == e.val()) { e.val(''); }
			});
					
			e.blur(function(){
				var e = $(this);
				if (!e.val()) { e.val(e.attr('title')); }
			});
		});
		
		// Facebox
		if($('ul.portfolio-listing li a').size() > 0) {
			$('ul.portfolio-listing li a').facebox(
				{loadingImage: '/assets/img/facebox/loading.gif'}
			).click(function(){
				pageTracker._trackPageview($(this).attr('href'));
			});
		}
		
		// External links in new windows
		function externalLinks(scope)
		{
			$('a[href^=http]:not([href*=veekee.ca])').attr('target', '_blank');
		}
		externalLinks();	
		
		// News pagination
		$('#news li.pagination a').live('click', function()
		{
			var target = $(this).attr('href');
			$('div#news').fadeTo('fast', 0.3).load(target + ' div#news ul', 
				function(){$('div#news').fadeTo('fast', 1); externalLinks($('div#news')); });
			return false;
		});
	}
);

$(document).bind('reveal.facebox', function()
	{
		
		// Gallery functionality
		$('#images div').hide();
		$('#images div:first').show();
		$('#thumbs li a:first').addClass('current');
		
		$('#thumbs li a').click(function()
		{	
			// IE7 makes this needlessly complicated
			var target = $(this).attr('href').substring($(this).attr('href').indexOf('#'));
			$('#images div').hide();
			$('#images div' + target).fadeIn('fast');
			$('#thumbs li a').removeClass('current');
			$(this).addClass('current');
			return false;
		});

	}
);