$(document).ready(function() {
	// Clear out navigation text and use the nice background image with anti-aliased fonts
	$('#hnav LI A').text('').addClass('imaged');
	// Clear out header text use nice background image instead
	$('#header2 H1 A').text('').addClass('imaged');
	// Animate home page callouts
	if ($('#home_col_1').length > 0) {
		(function() {
			function reset_titles(node) {
				if (typeof node == 'undefined') {
					var ntitle = $('.callout-title')
					var nsub = $('.callout-subtitle');
				} else {
					var ntitle = $('.callout-title', node);
					var nsub = $('.callout-subtitle', node);
				}
				ntitle.css('opacity', 0).css('left', '-300px');
				nsub.css('opacity', 0).css('left', '600px');
			}
			reset_titles(); // reset all titles at the get-go
			// Discover callouts
			var callouts = [];
			$('#home_col_1 .callout-layer').each(function() {
				$(this).hide();
				$(this).css('top', 0);
				reset_titles($(this));
				callouts.push($(this));
			});
			// Animate 
			var callout_i = 0;
			function next_callout() {
				var prev_i = callout_i;
				callout_i++;
				if (callout_i >= callouts.length) {
					callout_i = 0;	
				}
				if (prev_i > -1) {
					reset_titles(callouts[prev_i]);
					callouts[prev_i].fadeOut(1000);
				}
				callouts[callout_i].fadeIn(1000, function() {
					next_text();
				});
			}
			// Animate in text
			function next_text() {
				var title = $('.callout-title', callouts[callout_i]);
				var subtitle = $('.callout-subtitle', callouts[callout_i]);
				// Slide in the title and subtitle
				title.animate({ opacity: 1, left: '100px' }, { duration: 1000, easing: 'easeInCubic', complete: function() {
					title.animate({ left: '30px' }, { duration: 6000, easing: 'easeOutCubic', complete: function() {
						next_callout();
					}});
				}});
				subtitle.animate({opacity: 1, left: '30px' }, { duration: 1000, easing: 'easeInCubic', complete: function() {
					subtitle.animate({opacity: 1, left: '100px' }, { duration: 4000, easing: 'easeOutQuint' });
				}});
			}
			// Fade first one in and keep it going
			callouts[0].fadeIn(1000, function() {
				next_text();
				//setInterval(next_callout, 6000);
			});
		})();
	}
	// About page callout control
	$('#about_callout_control LI A').click(function() {
		var i = $(this).attr('rel');
		var show = $('#about_callout_' + i);
		$('#about_callout LI').hide();
		show.fadeIn(1000);
		return false;
	});
	
	
	

	
	// Projects list navigation?
	if ($('.projects_list').length > 0) {
		(function() {
			var fore = 'project_img1';
			function goto_project(slug) {
				var prev = fore;
				fore = fore == 'project_img1' ? 'project_img2' : 'project_img1';
				// Update image of fore
				$('#' + fore).html('<img src="/design/images/projects/' + slug + '.jpg?2">');
				// Fadeout previous/background layer, fade in fore layer
				$('#' + fore).fadeIn(1000);
				$('#' + prev).fadeOut(1000);
				// Hide other text, show selected text
				$('#project_text DIV').hide();
				$('#text_' + slug).slideDown(1000);
				// Update active links
				$('.projects_list LI A').removeClass('active');
				$('.projects_list LI A[rel='+slug+']').addClass('active');
			}
			$('.projects_list A').click(function() {
				goto_project($(this).attr('rel'));
				return false;
			});
			$('#project_text DIV').hide();
			$('#project_text').css('display', 'block');
			goto_project('hospital');
		})();
	}
});
$(window).load(function() {
	// Match column heights
	if ($('#col1').length > 0 && $('#col2').length > 0) {
		var max_height = Math.max($('#col1').height(), $('#col2').height());
		$('#col1').height( max_height );
		$('#col2').height( max_height );
	}
});
