// Helper functions
function prepPlaceholder(el) {
	if(el.attr('value') == '' || el.attr('value') == el.attr('placeholdertext')) {
		el.addClass('placeholder');
		if(el.attr('value') == '') {
			el.attr('value', el.attr('placeholdertext'));
		}
	} else {
		el.removeClass('placeholder');
	}
}
function togglePlaceholder(el) {
	// Check if the input already has a value...
	if((el.attr('value') != '') && (el.attr('value') != el.attr('placeholdertext'))) { return false; }
	if(el.attr('value') == el.attr('placeholdertext')) {
		el.attr('value', '');
	} else if(el.attr('value' == '')) {
		el.attr('value', el.attr('placeholdertext'));
	}
	el.toggleClass('placeholder');
	return true;
}
function clearPlaceholdersOnSubmit() {
	jQuery('form').submit(function(){
		jQuery('input[placeholdertext]').each(function(){
			var _this = jQuery(this);
			if(_this.attr('value') == _this.attr('placeholdertext')) {
				_this.attr('value','');
			}
		});
	});
}
function labelPlaceholders() {
	var els = jQuery('label[for].placeholder');
	els.hide();
	els.each(function(){
		var _this = jQuery(this);
		var _thisfor = '#' + _this.attr('for');
		_thisfor = jQuery(_thisfor);
		_thisfor.attr('placeholdertext',_this.text());
		prepPlaceholder(_thisfor);
		_thisfor.focus(function(){
			togglePlaceholder(_thisfor);
		});
		_thisfor.blur(function(){
			togglePlaceholder(_thisfor);
		});
	});
	clearPlaceholdersOnSubmit();
}

jQuery(document).ready(function($) {
	// Run these functions on DOMready
	labelPlaceholders();
	
	// :last-child fix for IE
	$('.pipe li:last-child, .tabs li:last-child, ul#navigation-primary li:last-child').addClass('last-child');
	// :first-child fix for IE
	$('.tabs li:first-child').addClass('first-child');
	
	// Special button classname
	$('input[type=submit], input[type=button]').addClass('button');
});