jQuery.fn.clearDefault = function(options) {	   
	settings = jQuery.extend({
		highlightClass: "highlight"
	}, options);
	return this.each(function() {
		jQuery(this).focus(function() {
			if( this.value == this.defaultValue )
				jQuery(this).attr("value", "").addClass(settings['highlightClass']);
		}).blur(function() {
			if( !this.value.length )
				jQuery(this).attr("value", this.defaultValue).removeClass(settings['highlightClass']);
		});		
	});
};

$(document).ready(function() {

	$('.search-query').clearDefault();
	
	
	$('.items li.item:last-child,.more-events .item li:last-child,#footer li:last-child,.toolbar li:last-child,#filter-name li:last-child,#content #aside .section:last-child,.stories li:last-child').addClass('last');
	$('#topics-nav li:first-child,.toolbar li.pdf:first-child').addClass('first');
	
	$('.items p a.more,.pagination .paging .next,.pagination .paging .last,#content #aside .section .items .more a,#sidebar .section li.more a').addClass('symbol').wrapInner('<span></span>').append('&nbsp;&raquo;');
	$('.pagination .paging .previous,.pagination .paging .first').addClass('symbol').wrapInner('<span></span>').prepend('&laquo;&nbsp;');
	
	// Handles the generic tabset functionality
	$('.tabs').each( function(){
		var tabset = $(this);
		
		// Generate the tabs
		tabset.addClass('active').prepend('<ul class="tabnav"></ul>');
		var tabnav = tabset.children('.tabnav');
		tabset.find('li[id]').each(function(){
			tabnav.append('<li><a href="#' + $(this).attr('id') + '">' + $(this).children('h3').html() + '</a></li>');
		});
		
		// Give the first tab class="current", show the first tab's content
		tabnav.find('li:first-child').addClass('current');
		tabset.find('.tablist > li:not(:first-child)').hide();
		
		// On tab click, change the tab and content
		tabnav.find('a').click( function(event){
			event.preventDefault();
			tabset.find('.tablist > li').hide();
			// This regex is needed because IE7 automatically changes href="#anchor" to href="http://domain.tld/file#anchor" when rendering
			$( $(this).attr('href').match(/(#[^#]*)$/)[0] ).show();
			tabnav.children('li').removeClass('current');
			$(this).parent().addClass('current');
		});

	});
});
