// R E Q U I R E S
// jquery

// Disable auto widont
$.jqwidont.auto(false);

$(document).ready(function() {

	// add classes for IE8 or lower not supporting child selectors
	
	// S T R U C T U R A L
	// Clear div following last div
	$('div.last + div:not(.feature)').addClass('clear');
	
	// T Y P O G R A P H I C A L
	
	// Global
	// Clear top margins from first text blocks in div
	$('div > h4:first-child').addClass('first');
	// Awards page tweaks
	$('.column > p:first-child').addClass('first');
	$('.column p:empty').remove();
	// $('div > p:last-child').addClass('last');
	$('body:not(.perspectives, .perspectives-wam) .image + h4, .image + h2, .image + p, .thumb + h4, .slideshow + h4').addClass('afterimg');
	$('.practice .row + p').addClass('clear');
	
	// Widont, applies to anything with a widont class
	$('.content p, .widont').widont();
	
	// Portfolio / People related footer
	// Apply summary height to its 'a' tag 
	$('.springboard .summary a.overlaid').height($('.springboard .summary a.overlaid').parent().height());
	
	// Practice
	// Add spacing above first h1
	$('.content .span-12:first-child > h1:first-child').addClass('firsth1');
	// Add line break
	$('.content > div > h1').parent().addClass('line');
	$('.content .span-12:first-child > h1').parent().removeClass('line');
	// Make inline p align to top
	$('.content div:not(.promo) p.inline:first-child').parent().addClass('hasinline');
	
	// Process
	// Add lines after H3
	$('.process .span-3 > h3').addClass('line');
	$('.process .content > .span-12:not(:first)').addClass('line');
	$('.process .span-6 > h3').addClass('line');
	
	// Careers
	// Add class to ul tags appearing in the wysiwyg content
	$('.careers .content ul').addClass('lined');
	
	// B E H A V I O U R A L
	
  // This will run each time the portfolio index is reloaded
  // via the filters
  $('.portfolio#results').bind('portfolio_load', function(event) {
    slideshow_summary();
  });
  
  $('#results').bind('results_load', function(event) {
    $('.image + h4, .image + h2, .image + p, .thumb + h4, .slideshow + h4').addClass('afterimg');
    Hyphenator.addExceptions('en', 'Miles, Warren, Maurice, Mahoney');
    Hyphenator.run();
    hyphenator_strays();
    align_summaries();
    if ($(this).hasClass('portfolio')) {
      $('.portfolio#results').trigger('portfolio_load');
    }
  });
  
  $('#results').trigger('results_load');
  $('#results.portfolio').trigger('init_lazyload');
  
  $('#results.chronology').trigger('init_lazyload');

	// Home
	$('.idle .grid h2, .idle .grid h1').each(function() {
		offset = ($(this).parent().height() / 2) - ($(this).height() / 2);
		$(this).css({paddingTop: offset + 'px'});
	});
	$('.over .grid h2 a, .over .grid h1 a').each(function() {
		offset = ($(this).parent().parent().height() / 2) - ($(this).height() / 2);
		$(this).css({paddingTop: offset + 'px', paddingBottom: offset + 'px'});
	});

	// Search
	$('#searchbox').val(label);
	$('#searchbox').focus(function() {
		if($(this).val() == label) {
			$(this).val('');
			// $(this).css('background-color', '#ffffff');
		}
	}).blur(function() {
		if($(this).val() == '') {
			$(this).val(label);
			// $(this).css('background-color', '');
		}
	});
	$('#searchbox').submit(function() {
		_gaq.push(['_trackEvent', 'Search', 'Submit', $(this).val()]);
	});
	
	// Float message
	$(window).scroll(function() {
		$('#message').css('top', $(window).scrollTop());
	});
	
	// People linked panels
	$('.people #results .box a.overlaid').each(function () {
		$(this).height($(this).parent().outerHeight());
	});
	
});

// Words not detected by hyphenator
function hyphenator_strays () {
  $('.summary .hyphenate').each(function() {
    $(this).text($(this).text().replace('Christchurch', 'Christ-church'));
  });
}
