/*
 * JavaScript that affects just the home page
 */
	var heroTimeout = null;    // maintains our timeout settings
	var numHeroes = null;	   // stores the number of hero slides
	var lastSlide = null;	   // references the lastSlide that was shown
	var hasFocus = true;
	
	// Focuses on the child-th "hero" slide
	function focus_hero(child) {
		// clear out any existing timeouts
		clearTimeout(heroTimeout);

		// loop through each slide to update classes accordingly
		$("#hero .left ul.images").children().each(function() {
			// indicates which slide we're dealing with
			var rel = $(this).attr("rel");
			
			//if (hasFocus) {  
				// if this is the slide we're supposed to be focusing on
				if (child == rel) { 
					// add the "active" class to the slide buttons
					$("#hero .left ul.buttons li[rel="+rel+"]").addClass("active");
					
					// get href value for the specific <a> we're dealing with
					var href = null;
					
					$(this).find("a").each(function() {
						href = $(this).attr("href");
						return;
					});
			
					// set overlay href
					$("#wrapper #hero > .left .link-overlay").html('<a href="'+href+'"></a>');
					// add the active class to the slide
					$(this).addClass("active");
					
					if (hasFocus) { 
						// fade in nicely
						$(this).fadeIn(500);
					} else {
						// just show instantly to avoid queuing
						$(this).show();
					}
				} else { 
					
					// if currently active we need to do some stuff
					if ($(this).hasClass("active")) { 
						// remove active class from the current button
						$("#hero .left ul.buttons li[rel="+rel+"]").removeClass("active");
						
						// remove active class from the current slide
						$(this).removeClass("active");
						
						if (hasFocus) { 
							// fade this slide out
							$(this).fadeOut(300);
						} else { 
							// just show instantly to avoid queuing
							$(this).hide();
						}
					}
				//}
			}
		});
		
		// determine next hero value
		var nextHero = child + 1;
		
		// if we exceed # of heroes start back at 1
		if (nextHero > numHeroes) {
			nextHero = 1;
		}
		
		// reset lastSlide
		lastSlide = child;
		
		// set timeout to run function again in 5 seconds
		heroTimeout = setTimeout(function() { focus_hero(nextHero); }, 5000);
	}

	// when the document/window blurs clear the time out to prevent queued transitions
	function onBlur() {
		/*
		clearTimeout(heroTimeout);
		heroTimeout = null;
		*/
		hasFocus = false;
	};
	
	// when the document/window regains focus, return focus to previous slide (or 1st slide if init)
	function onFocus(){
		/*
		if (lastSlide)
			focus_hero(lastSlide);
		else
			focus_hero(1);
		*/
		hasFocus = true; 
	};

	// Internet Explorer Settings
	if (/*@cc_on!@*/false) {
	    document.onfocusin = onFocus;
	    document.onfocusout = onBlur;
	// Other Browser Settings    
	} else {
	    window.onfocus = onFocus;
	    window.onblur = onBlur;
	}
	
	// Code to run on DOM load
	$(document).ready(function() {
		// determine # of slides
		numHeroes = $("#hero .left ul.images > li").size();
		
		// focus on primary slide
		focus_hero(1);
	
		// set click event for hero buttons
		$("#hero .left ul.buttons li").bind('click', function() {
			// determine which slide we want
			var rel = $(this).attr("rel");
			
			// focus on slide
			focus_hero(rel);
		});		
	});
