/// <reference path="jquery-1.3.2.min-vsdoc.js" />

// Time (in seconds) per slide to be shown before
// automatically going to the next slide
timePerSlide = 10;
slideCounter = 0;
nextSlide = 0;

// Setup the rotating features on the home page
$(document).ready(function() {
	// Add the progress bar
	$("#feature").after('<div id="feature-progress"><div style="height: 9px; background: #999;"></div></div>');

	// preload all images so there will be no delay
	$.each(homeFeatures, function(i, val) {
		jQuery("<img>").attr("src", homeFeatures[i].background);
		if (homeFeatures[i].banner) {
			jQuery("<img>").attr("src", homeFeatures[i].banner);
		}
	});
	// intercept all links in list, allowing the user to
	// skip to that slide
	$("#featurelist li a").click(function() {
		if ($(this).parent().hasClass("selected")) return false;
		
		// reset the counter
		slideCounter = $("#featurelist li a").index(this) - 1;
		if (slideCounter < 0) {
			slideCounter = homeFeatures.length - 1;
		}

		// what's the next slide?
		nextSlide = slideCounter + 1;
		if (nextSlide > homeFeatures.length - 1) {
			nextSlide = 0;
		}

		// stop all animations
		$("#feature-progress div").stop().css("width", "0");
		
		// transition to this new slide
		transitionToSlide(function() {

			slideCounter++;
			if (slideCounter < 0) {
				slideCounter = homeFeatures.length - 1;
			}

			showSlide();
		});

		return false;
	});

	// Begin the rotating animation of slides
	fillSlide();
	showSlide();

});
// function to animate the progress bar and switch to
// the next item in the slideshow
function showSlide() {

	// what's the next slide?
	nextSlide = slideCounter + 1;
	if (nextSlide > homeFeatures.length - 1) {
		nextSlide = 0;
	}

	// bring up the new feature callout
	$("#feature-progress div").css("width", "100%").animate({
		width: "0"
	}, timePerSlide * 1000, "linear", function() {


		transitionToSlide(function() {
			// up the slide counter
			slideCounter++;
			if (slideCounter > homeFeatures.length - 1) {
				slideCounter = 0;
			}
			showSlide();
		});
	});
}
function fillSlide(){
	// replace the content in the callout with the next slide's content
	$("#feature-callout h2").remove();
	$("#feature-callout p:not(.lnk)").html(homeFeatures[nextSlide].description);
	$("#feature-callout p.lnk a").html(homeFeatures[nextSlide].buttonLabel);
	$("#feature-callout p.lnk a").attr("href", homeFeatures[nextSlide].buttonLink);

	// if this is a product, add the headline into the callout
	if (homeFeatures[nextSlide].type == "product") {
		$("#feature-callout").prepend("<h2>" + homeFeatures[nextSlide].headline + "</h2>");
	}

	// remove any headline, info not in the callout
	$("#feature > h2, #feature > h3, #feature > img").remove();

	// switch to the appropriate class
	$("#feature").removeClass().addClass(homeFeatures[nextSlide].type);

	// swap out the background
	$("#feature").css("background-image", "url(\"" + homeFeatures[nextSlide].background + "\")");
	$("#featurelist li.selected").removeClass("selected");	
	$("#featurelist li").eq(nextSlide).addClass("selected");

	// add in the remaining items, depending on the slide type
	if (homeFeatures[nextSlide].type == "event" || homeFeatures[nextSlide].type == "prism" ||
       homeFeatures[nextSlide].type == "award") {
		$("#featurelist").after("<h2>" + homeFeatures[nextSlide].headline + "</h2>");
	}

	if (homeFeatures[nextSlide].type == "event") {
		$("#feature > h2").after("<h3>" + homeFeatures[nextSlide].info + "</h3>");
	}

	if (homeFeatures[nextSlide].type == "award") {
		$("#feature > h2").after('<img src="' + homeFeatures[nextSlide].banner + '" alt="" />');
	}
}
function transitionToSlide(func) {
	$("#feature-callout").slideUp("slow", function() {
		fillSlide();
		// slide down the callout feature
		$("#feature-callout").slideDown("slow", function() {
			func();
		});
	});
}