$(document).ready(function(){
// hover function for inputs and images
    $('.hover').hover(function(){
		$(this).attr('src', $(this).attr('src').replace(/\.(\w{3})/g, "Hover.$1"));
	},function() {
		$(this).attr('src', $(this).attr('src').replace(/Hover\.(\w{3})/g, ".$1"));
	});	

// homepage news fader
    if($('.nItem').length > 0) {
        $('.nItem').each(function(i) {
            $(this).attr('item', i);
            i++
            newsLength = i
        });
        
        $('.nItem').not(':first').hide();
        
		$('<div class="line clearfix"></div>')
		.append('<a href="#" id="nPrev"><< Newer News</a>')
		.append('<a href="#" id="nNext">Older News >></a>')
		.appendTo('#right');
		
		function updateButtons(){
			current = $('.nItem:visible').attr('item');
			enablePrev(current > 0);
			enableNext(current < newsLength - 1);
		}
		
		function enablePrev(value){
			enableButton($('#nPrev'), handlePrev, value);
		}		
		
		function enableNext(value){
			enableButton($('#nNext'), handleNext, value);
		}
			
		function enableButton(button, handler, value){
			cValue = button.css('opacity') > .5;
			if(cValue == value) return
			if(value)
				button.css('opacity', 1).click(handler);
			else
				button.css('opacity', 0).unbind('click', handler);
		}
		
		function handlePrev(){
			changeItem(parseInt($('.nItem:visible').attr('item')) - 1);
			return false;
		}
		
		function handleNext(){
			changeItem(parseInt($('.nItem:visible').attr('item')) + 1);
			return false;
		}
		
		function changeItem(itemNum){
			$('.nItem:visible').fadeOut(
				200, 
				function(){
					$('.nItem[item='+itemNum+']').fadeIn(200);
					updateButtons();
				}
			)
		}
		
		$('#nPrev').css('opacity', 0);
		$('#nNext').click(handleNext);
    }
    
	function animateUsWidth(w, dur){
		$('#us').animate({
				'width':w
			},
			{	
				'duration':dur,
				'queue':false
			}
		)
	}
	
	var usOutWidth
	usOutWidth = $('#us span').width() + 6;
	$('#us span').width(usOutWidth)
	$('#us').width(0)
	.hover(function(){animateUsWidth(usOutWidth, 200)}, function(){animateUsWidth(0, 300)});
	
	
    

});