(function($) {
	
	$.fn.buttonup = function(args) {

		/* Merge the arguments and given example text into one options object. */
		var options = $.extend({}, $.fn.buttonup.defaults, args);		
		
		return this.each(function() {

			/* Merge the plugin defaults with the given options and, if present, any metadata. */
			if ($.metadata) {
			  var o = $.extend({}, $.fn.buttonup.defaults, $(this).metadata(), options);
			} 
			else {
			  var o = options;
			}
			
			/* Save current jQuery object */
			var $this = $(this);
						
			/* Move button away, but don't hide() it because it needs to be 
			present to make some browsers happy */
			$this.css({
									'position' 	: 'absolute',
									'left'			: '-9999px',
									'width'			: '1px',
									'height'		: '1px'
								});								
			
			/* Build a link element for the basis of our button */					
			var link = $("<a href='#' id='" + $this.attr('id') + "'>" + $this.html() + "</a>")
										.insertBefore($this)
										.addClass(o.className)
										.wrapInner("<span></span>")
										.css({
											'cursor'						: 'pointer',
											'padding'						: '0px',
											'border'						: 'none',
											'text-align'				: 'center',
											'display'						: 'block',
											'background-color'	: 'transparent',
											'background-repeat'	: 'no-repeat', 
											'text-decoration'		: 'none',
											'overflow'					: 'hidden',
											'display'						: '-moz-inline-box',
											'display'						: 'inline-block',
											'outline-style'			: 'none',
											'outline-width'			: 'medium'												
										});
										
      /* Store original text color */										
      link.data('original_color', link.css('color'));										

			/* Add the inner span using the same background image */
      link.find('span').css({
															'display'								: 'block',
															'white-space'						: 'nowrap',
															'line-height'						: o.height + 'px',	
															'margin'								: '0px 0px 0px ' + Math.floor(o.horizontalPadding / 2) + 'px',
															'padding'								: '0px ' + o.horizontalPadding + 'px 0px ' + Math.ceil(o.horizontalPadding / 2) + 'px',
															'background-image'			: link.css("background-image"),
															'background-repeat'			: 'no-repeat', 
															'background-position'		: 'right top',
															'background-attachment'	: 'scroll'
														});

      /* Event handlers */
			if (o.showHoverState) {				
				link.hover(function() {
					if ($(this).parents('li').hasClass('active') == false) {																
						showHoverState($(this), o);
					}
				}, function() {
					if ($(this).parents('li').hasClass('active') == false) {											
						showNormalState($(this), o);
					}
				});				
				
				link.focus(function() {
					showHoverState($(this), o);
				});	

			};

			if (o.showClickState) {
				link.mousedown(function() {
					showClickState($(this), o);
				});
			};			
						
			link.click(function() {
				// hide all news tables
				$('#front_news table').hide();
				
				// and show the one we need
				var section = $(this).attr('id').split("news_nav_")[1];
				$('#news_'+section).show();
				
				link.parents('li').siblings('li').each(function(index) {
					if ($(this).hasClass('active')) {
						$(this).removeClass('active');
						showNormalState($(this).find('a'), o);
					};
				});
				showActiveState($(this), o)
				$(this).parents('li').addClass('active');
				return false;
			});	
											
		});				
		
		
		/* 
		 * Private functions 
		 */
		
		function showHoverState(link, o) {
			link.css({
				'background-position' : 'left -' + o.height + 'px',
				'text-decoration'			: 'none',
				'color'								: o.hoverTextColor ? o.hoverTextColor : link.data('original_color')
			});
			link.find('span').css({
				'background-position' : 'right -' + o.height + 'px'
			});	
		}

		function showActiveState(link, o) {
			link.css({
				'background-position' : 'left -' + (o.height * 2) + 'px',
				'text-decoration'			: 'none',
				'color'								: o.clickTextColor ? o.clickTextColor : link.data('original_color')
			});
			link.find('span').css({
				'background-position' : 'right -' + (o.height * 2) + 'px'
			});	
		}

		function showNormalState(link, o) {
			link.css({						
				'background-position' : 'left top',
				'color'								: link.data('original_color')							
			});
			link.find('span').css({
				'background-position' : 'right top'
			});							
		}
			
	}
				
	$.fn.buttonup.defaults = {
		className					: "button",
		height						: 40,
		horizontalPadding	: 20,
		showHoverState		: false,
		submitTextColor		: "#666"
	};
		
})(jQuery);
