// Hide/show text: 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');
}
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($) {
	$('.tabset').tabs(); // jQuery UI tabs
	labelPlaceholders();
	
	$('rule:last-child').addClass('last-child');
	
	$('input').each(function(){
		_this = $(this);
		_this.addClass(_this.attr('type'));
	});
	// Pipe deliniated lists
	$('.pipe li:not(:last-child)').append(' | ');
});