$(document).ready( function() {	
	/* Set up placeholder text in input and textarea fields */
	insertPlaceholder( 'input:text' );
	setSupportHeight();
	setSubnavHeight();
	equalHeights('.subs .support-box');
	$("#state-affiliates ul").splitList(2, {
		'splitInto': 'rows'
	}).children('div').addClass('clearfix row');
	
	$('.addthis_toolbox').show().hover(function(){
	    $(this).toggleClass('hover');
	});
	
	$('#publication_list > :first-child').addClass('childfirstchild');
	
});


/* See if the browser understands the placeholder attribute */
function testPlaceholder() {
	var i = document.createElement('input');
	return 'placeholder' in i;
}

/* Insert placeholder text into inputs */
function insertPlaceholder( el ) {
	var hasPlaceholder = testPlaceholder();
	if( hasPlaceholder ) {
		return;
	} else {
		$( el ).each( function() {
			var inputValue = $(this).attr('value');
			var placeholder = $(this).attr('placeholder');
			if(inputValue != placeholder) {
				$(this).val( placeholder );
				inputValue = $(this).val();
			}
			
			$(this).focus( function() {
				if( inputValue == placeholder ) {
					$(this).val( '' );
				}
				inputValue = $(this).val();
			});
			
			$(this).blur( function() {
				inputValue = $(this).val();
				if( inputValue == placeholder || inputValue == '' ) {
					$(this).val( placeholder );
				} else {
					$(this).val( inputValue );
				}
				inputValue = $(this).val();
			});
			
		});
	}
}



/* Equalize the height of the intro support boxes */
function setSupportHeight() {
	var supportHeight = $('#support-boxes').height();
	$('.support-box').height(supportHeight-38);
}

/* Calculate intro div height on landing page and set the nav to match that height */
function setSubnavHeight() {
		var introHeight = $('.landing #intro').height();
		$('.landing #sub-nav').height(introHeight-34);
}

/* Equalize the height of the support boxes in the article*/
function equalHeights(box) {
	var currentTallest = 0;
	$(box).each(function(){
		if ($(this).outerHeight() > currentTallest) { currentTallest = $(this).outerHeight(); }
	});

	$(box).each(function(){
		
		if ($(this).outerHeight() !== currentTallest) { 
			var newHeight = $(this).height() + (currentTallest - $(this).outerHeight());
			$(this).height(newHeight);
			
		}
	});  
};



/* jQuery split a list into multiple rows or columns
* version 3.0 (02/10/2010)
* developed by David Zhou, Matt Billings, David Durst, and Chuck Harmston
* take an unordered list and split it into multiple divs.
* you can float the divs so it looks like multiple columns/lists.
* 
* usage: 
*    $(".dropdown ul").splitList(3);
*    $(".dropdown ul").splitList(3, { wrapClass: "div_class_name" });
*    $(".dropdown ul").splitList(3, { splitInto: "div_class_name" });
* 
* If option splitInto is set to 'cols' (as it is by default), it will
* split into the specified number of columns. Otherwise, it will split
* into rows, with the specified number of <li>s per row.
* 
*/

$.fn.splitList = function(n, options){
	settings = $.extend({
		wrapClass: false,
		splitInto: 'cols'
	}, options);
	return this.each(function(){
		var intoCols = (settings['splitInto'] == 'cols');
		$lis = jQuery(this).find("> li");		
		$inc = intoCols ? parseInt(($lis.length/n) + ($lis.length % n > 0 )) : n;
		var w = '<div' + (settings['wrapClass'] ? ' class="' + settings['wrapClass'] + '"' : '' ) + '></div>';
		for(var i=0; i<(intoCols ? n : Math.ceil($lis.length/n)); i++)
			$lis.slice($inc*i, $inc*(i+1)).wrapAll(w);
	});
};
