jQuery(document).ready(function($)
{
	/* Home page double rollover */
	$(".homeRolloverLink").mouseover(function(){
		iImageName = $(this).children().attr('src');
		aImageName = iImageName.replace('_i.gif', '_a.gif');
		if(iImageName.search('_icon_') == -1){
			iIconImageName = iImageName.replace('text', 'icon');	
			aIconImageName = aImageName.replace('text', 'icon');
			$("img[src="+iIconImageName+"]").attr('src', aIconImageName);
		}
		else{
			iTextImageName = iImageName.replace('icon', 'text');	
			aTextImageName = aImageName.replace('icon', 'text');
			$("img[src="+iTextImageName+"]").attr('src', aTextImageName);
		}
		
		$(this).children().attr('src', aImageName);
	});		
	
	$(".homeRolloverLink").mouseout(function(){
		aImageName = $(this).children().attr('src');
		iImageName = iImageName.replace('_a.gif', '_i.gif');
		if(iImageName.search('_icon_') == -1){
			iIconImageName = iImageName.replace('text', 'icon');	
			aIconImageName = aImageName.replace('text', 'icon');
			$("img[src="+aIconImageName+"]").attr('src', iIconImageName);
		}
		else{
			iTextImageName = iImageName.replace('icon', 'text');	
			aTextImageName = aImageName.replace('icon', 'text');
			$("img[src="+aTextImageName+"]").attr('src', iTextImageName);
		}
		
		$(this).children().attr('src', iImageName);
	});			
		
	/* Featured home page items */
	var imageNum = 1;
	var clicked = 0;
	$("#homeFeaturedPager > ul").children().click(function(){
		clicked = 1;
		// if a number is clicked, we show that image
		if(isFinite($(this).html())){
			$(this).siblings().removeClass("highlight");
			$(this).addClass("highlight");
			displayImage($(this).html());
			imageNum = $(this).html();
		}
		// if the forward button is clicked, we jump to the next image
		else{	
			imageNum = $(".highlight").html();
			// if we're at the end of the list, we jump to the start of the list
			if(imageNum == $(".featuredImage").length){
				imageNum = 1;	
				hightlightFirstButton();
			}
			// otherwise, we just move to the next image
			else{
				imageNum++;
				highlightNextButton();
			}			
			displayImage(imageNum);	
		}				
	});
	
	/* Timer for the home page */
	if($(".featuredImage").length > 1){
		$.timer(4000, function (timer) {
	
			if(clicked == 1){
				timer.reset(4000);
				clicked = 0;
			}
			if(imageNum >= $(".featuredImage").length){
				imageNum = 1;
				hightlightFirstButton();
			}
			else{
				imageNum++;
				highlightNextButton();
			}								
			displayImage(imageNum);		
		});
	}
	
});

function displayImage(num){
	$(".featuredImage").css("display", "none");
	$("#featuredImg"+num).fadeIn("normal");	
	$(".featuredText").css("display", "none");
	$("#featuredText"+num).fadeIn("normal");	
}

function hightlightFirstButton(){
	$(".highlight").removeClass("highlight");
	$("#homeFeaturedPager > ul > li:first-child").addClass('highlight');
}

function highlightNextButton(){
	$(".highlight").removeClass("highlight").next().addClass("highlight");
}
