jQuery('#nav li').each(function(a,b){
	
	var self = jQuery(this);
	
	//grab current background values
	var current_bg = jQuery('a', this).css('background-position');
	var current_bg = current_bg.split(" ");
	var x = current_bg[0];
	var y = current_bg[2];
	
	//set them again - Firefox bug apparently
	//jQuery(this).css({backgroundPosition: x + y});
	
	function animate_tab(element, action){
		
		//If the tab is already up, don't animate anything
		if(jQuery(element).hasClass('is_up')){ return false; }
		
		var end = action == 'show' ? '-53px' : '0';
	
		jQuery(element).stop(true, false).animate(
			{backgroundPosition: x + " " + end},
			{duration: 300}
		);
		
		jQuery('a', element).stop(true, false).animate(
			{backgroundPosition: x + " " + end},
			{duration: 0}		
		);

	}
	
	//Setup wordpress active states
	var active_states = ['current_page_item','current_page_item','current_page_parent','current_page_ancestor'];
	
	//Loop through each of the elements classes to see if it's active
	jQuery.each(active_states, function(a,b){
	
		if(jQuery(self).hasClass(b)){
				
			animate_tab(self, 'show');
			jQuery(self).addClass('is_up');
		
		}
		
	});
	
	//Control the mouse events	
	jQuery(this)
	.mouseover(function(){ animate_tab(this, 'show'); })
	.mouseout(function(){ animate_tab(this, 'hide'); });
	
});
