$(function() {
	// box balancing
	var maxHeight = 0;
	$('.landing .boxouts section ul, #homepage article nav section').each(function() {
		var h = $(this).height();
		maxHeight = (h > maxHeight) ? h : maxHeight;
	}).css({ minHeight: maxHeight+'px' });
	
	// accordion
	function expandAccordion(elem) {
		var elem = $(elem);
		
		var toOpen = $('.collapse', elem);
		toOpen.animate({ height: toOpen.data('originalHeight')+'px', paddingTop: '14px' }, 400, function() {
			$(this).parent().addClass('open').siblings().removeClass('open');
			$('h2 .arrow', $(this).parent()).fadeOut(200, function() { $(this).addClass('invert').fadeIn(200); })
		});
		
		var toClose = elem.siblings('.open');
		$('h2 .arrow', toClose).removeClass('invert');
		$('.collapse', toClose).animate({ height: 0, paddingTop: 0 }, 400)
	};
	
	$('.accordion .collapsible h2').click(function() {
		expandAccordion($(this).parent());
	}).prepend('<span class="arrow" />');
	
	$('.accordion .collapsible .collapse').each(function() {
		$this = $(this);
		$this.data('originalHeight', $this.height()).css({ height: 0, overflow: 'hidden' });
	});
	
	// open on page loads
	setTimeout(function() {
		expandAccordion($('.accordion .collapsible').first());
	}, 1000);
	
	
	// quick links 
	$('#quickLinks h3').click(function() {
		var divElem = $(this).next();
		var open = (divElem.height() == 0);
		
		divElem.animate({ height: (open) ? divElem.data('originalHeight')+'px' : 0 }, 400);
		$(this).toggleClass('open');
	});
	
	var ql = $('#quickLinks div').show();
	ql.data('originalHeight', ql.innerHeight()).height(0);
	
	// foot homepage
	// 762x321 -- original / target image width
	function positionDots() {
		var rel = $(this).attr('rel');
		if(rel.substr(0, 3) == 'pos')
		{
			var slide = $('.foot #homepage .slideshow .slides');
			var ratio = slide.innerWidth() / 762; // current / target
		
			var pos = rel.substr(3).split(/x/);
			$(this).parent().css({ top: (parseInt(pos[0]) * ratio)+'px', left: (parseInt(pos[1]) * ratio)+'px'});
			
			$(this).after('<span>'+$(this).text()+'</span>');
			var span = $(this).next('span');
			span.css({ left: (0 - (span.innerWidth(true) / 2)) + 'px', top: (0 - span.innerHeight()) + 'px' });
		}
	};
	
	$('#footRollover li a').each(positionDots);
	
	var resizeTimer = null; // prevent repeated calls
	$(window).resize(function() {
		clearTimeout(resizeTimer);
		resizeTimer = setTimeout(function() { $('#footRollover li a').each(positionDots); }, 150);
	});
	
	
	/* old browser fixes:
		- :before and :after's
	*/
	
	// placeholder
	if(!Modernizr.input.placeholder)
	{
		$('#menu .search input').focus(function() {
			var $this = $(this);
			$this.val(($this.val() == $this.attr('placeholder')) ? '' : $this.val());
		}).blur(function() {
			var $this = $(this);
			$this.val(($this.val() == '') ? $this.attr('placeholder') : $this.val());
		}).each(function() {
			var $this = $(this);
			$this.val(($this.val() == '') ? $this.attr('placeholder') : $this.val());
		});
	}
	
	// generated content
	if(!Modernizr.generatedcontent)
	{
		var after = [ 'header nav li.on a', '.sisea-results', '.accordion h2' ];
		$(after.join(',')).append('<span class="after"></span>');
		
		var before = [ '#homepage article', '#main', '#sidebarLeft > ul > li > a', '.sisea-results', '.sisea-result h3',
			'.landing .boxouts', '.accordion h2' ];
		$(before.join(',')).prepend('<span class="before"></span>');
	}
	
	// transitions
	if(!Modernizr.csstransitions)	
	{
		// portal page
		$('#portal #hurt li a').mouseenter(function() {
			$('div', this).animate({ bottom: 0 }, 200);		
		}).mouseleave(function() {
			$('div', this).animate({ bottom: $(this).data('originalBottom') }, 200);
		}).each(function() {
			$(this).data('originalBottom', $('div', this).css('bottom'));
		});
	}
	
	// teeny tiny screens
	function addBottomNav() {
		if(($('#bottomNav').length == 0) && ($(window).width() <= 480) && (window.location.pathname != '/'))
		{
			var toTop = '<a href="'+window.location.pathname+'#logo" class="jumpToTop">Top</a>';
			var toNav = '<a href="'+window.location.pathname+'#bottomNav" class="jumpToNav">Menu</a>';
			
			$('footer').before($('header nav ul').clone().attr('id', 'bottomNavInner'));
			$('#bottomNavInner')
				.wrap('<div id="bottomNav" />') // apparently you can't wrap until its in the DOM
				.before(toTop).after(toTop);
			$('#bottomNav li.on').append($('#sidebarLeft > ul').clone());
			
			// breadcrumbs
			var bc = '<p id="breadcrumb"><a href="/">Home</a>';
			$('header ul li.on a, #sidebarLeft li.on > a').each(function() {
				bc += ' &gt; <a href="'+$(this).attr('href')+'">'+$(this).text()+'</a>';
			});
			$('#main').before(bc).before(toNav);
			
			$('header nav, #sidebarLeft, #sidebarRight').hide(); // hide or remove?
		}
		/*else
		{
			$('#bottomNav').remove();
			$('header nav, #sidebarLeft').show();
		}*/
	};
	
	addBottomNav();
	/* hmmm, this introduces some odd use cases
	var resizeTimer = null;
	$(window).resize(function() {
		clearTimeout(resizeTimer);
		resizeTimer = setTimeout(addBottomNav, 300);
	});*/
});
