// 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','');
			}
		});
	});
}
// Style 1: inputs with titles
function inputPlaceholders() {
	var els = jQuery('input[type=text][title].placeholder');
	els.each(function(){
		var _this = jQuery(this);
		// Set the placeholdertext attribute to the title
		_this.attr('placeholdertext',_this.attr('title'));
		prepPlaceholder(_this);
		_this.focus(function(){
			togglePlaceholder(_this);
		});
		_this.blur(function(){
			togglePlaceholder(_this);
		});
	});
	clearPlaceholdersOnSubmit();
}

function setStylesheet(stylesheet) {
	jQuery('link[rel~=stylesheet][title]').each(function(){
		this.disabled = true;
		if (this.title == stylesheet) {
			this.disabled = false;
			jQuery.cookie('stylesheet',this.title);
		}
	});
}

function buildStylesheetToggles(appendTo) {
	var toggleWrapper = jQuery('<li class="styleswitchers"><span id="styleswitcher-label">Font Size:</span></li>');
	var i = 0;
	jQuery('link[rel~=stylesheet][title]').each(function(){
		var title = jQuery(this).attr('title');
		jQuery('<a class="styleswitcher" href="#"></a>').text(title).attr('rel',title).attr('id', 'styleswitcher-' + i).appendTo(toggleWrapper);
		
		i++;
	});
	toggleWrapper.prependTo(appendTo);
}

/*
 * hoverFix 1.0
 * Crowd Favorite
 */
(function($) {
	$.fn.hoverFix = function(classname) {
		var c = classname || 'hover';
		this.hover(function(){
			$(this).addClass(c);
		},
		function(){
			$(this).removeClass(c);
		});
	};
})(jQuery);

// Put all your code in your document ready area
jQuery(function($){
	inputPlaceholders();
	
	$('.navigation li').hoverFix();
	
	$('#header .header-navigation > li').hover(
		function(){
			$(this).find('> a').addClass('hover-top-level');
		},
		function(){
			$(this).find('> a').removeClass('hover-top-level');
		}
	);
	
	$('#home-teaser-nav li:first-child').addClass('first-child');
	
	$('.pipe li:last-child').addClass('last-child'); // :last-child fix
	$('.columns .column:nth-child(even)').addClass('column-even');
	
	// Styleswitcher
	buildStylesheetToggles('#tophat ul.cflk-list');	
	$('.styleswitcher').click(function(){
		setStylesheet($(this).attr('rel'));
		return false;
	});
	var c = jQuery.cookie('stylesheet');	
	if(c) {
		setStylesheet(c);
	}
	
	// Setup the home page teaser section as tabs with a mouseover event
	$("#home-teasers").tabs({
		event: 'mouseover'
	});
});