// JavaScript Document

$(document).ready(function() {	
	home_articles_controller.items = $(".homepodarticles2 .homepodarticles_item");
	home_articles_controller.showNextItem();	   	
});



var home_articles_controller = {
	items : [], 						// this will hold the items (set on doc.ready)
	showDelay: 5000,					// number of milliseconds each slide is shown for.
	currentItem: -1, 
	
	
	showNextItem : function () {
		
		home_articles_controller.currentItem += 1;
		if (home_articles_controller.currentItem >= home_articles_controller.items.length) {
			home_articles_controller.currentItem = 0;
		}
		
	
		// reset each item
		$(home_articles_controller.items).each(function() {
			$(this).removeClass("homepodarticles_item_selected");	
			home_articles_controller.setItemTitle(false, $(this).find(".homepodarticles_item_title IMG"));
		});

	
		// change the background and title images
		var selectedItem = $(home_articles_controller.items[home_articles_controller.currentItem]);
		selectedItem.addClass("homepodarticles_item_selected");
		home_articles_controller.setItemTitle(true, selectedItem.find(".homepodarticles_item_title IMG"));
		
		// load the new hero image	
		var thumbImg = selectedItem.find(".homepodarticles_item_thumb");
		var mainImageSrc = thumbImg.attr("src").replace("_sml","");
		
		$(".homepodarticles_image").html('<img src="' + mainImageSrc + '" alt="" />');
		$(".homepodarticles_image IMG").hide();
		$(".homepodarticles_image IMG").load(function() {
			$(this).fadeIn();
			home_articles_controller.startTimer();
		});
		
		// set the caption text and fade in
		$(".homepodarticles_caption_txt").hide();		
		$(".homepodarticles_caption_txt").html(selectedItem.find(".homepodarticles_image_caption").html());
		$(".homepodarticles_caption_txt").fadeIn();	
	},
	
	setItemTitle : function(isSelected, titleImg) {
		var titleSrc = titleImg.attr("src");
		titleSrc = titleSrc.replace("_selected","");
		if (isSelected) {
			titleSrc = titleSrc.replace(".gif","_selected.gif");
		}
		titleImg.attr("src", titleSrc);		
	},
	
	startTimer : function() {
		showTimer = setTimeout(home_articles_controller.showNextItem,home_articles_controller.showDelay);
	}
}


