/* -------- Navigation Logic BEGIN ----------- */
// create global timeout variables
var timeoutID;
var timeoutSectionID;
	
var navLoaded = false;
var navIntervalID = 0;

var activeSectionItem;
var activeSubNavItem;

navIntervalID = setInterval("setNavigation()", 100);

// once the navigation element is loaded, set it up for the site that is currently being viewed
function setNavigation()
{
	var navElementsLoaded = false;
	if (!navLoaded)
	{
		// check to see if the body loaded yet
		//if(($("body").length > 0) && ($("ul#mainnav li.has-subnav").length > 0) && ($("ul#sectionnav li.has-subnav").length > 0))  {
		if($("body").length > 0) {
			// set the active state in the navigation and subnavigation
			if($("body").attr("class")) {
				// if the current page is in a main section, set the active nav and subnav items
				if ($("#header").length > 0) {
					// check to see if the element with "header" id has loaded yet, to get its class
					activeSectionItem = "ul#mainnav li#item-" + $("body").attr("class"); 
					activeSubNavItem = "ul#mainnav li#item-"+ $("#header").attr("class");
					// check if the nav elements have loaded and set the variable 
					if (($(activeSubNavItem).length > 0) && ($(activeSectionItem).length > 0) && ($(activeSectionItem).children("ul").length > 0)) {
						//$(activeSubNavItem).addClass("selected");
						// if the body is NOT SE, we need to set hover handler for section nav
						if (($("body").attr("class") != "SE") && ($("ul#sectionnav li.has-subnav").length > 0)) {
								$("ul#sectionnav li.has-subnav").hover(rollOverSectionNav,resetSectionNav);
								navElementsLoaded = true;
						} else {
							navElementsLoaded = true;
						}
					}
				} 
			}	else {
				// otherwise set the main navigation item without a subnav, and turn sites nav on
				activeSectionItem = "ul#mainnav li#item-sites"; 
				activeSubNavItem = "ul#mainnav li#item-SE";
				//if ($("ul#mainnav li#item-" + $("body").attr("class")).children("a").length > 0) {
				//$("ul#mainnav li#item-" + $("body").attr("class")).children("a").addClass("selected");
					// check if the nav elements have loaded and set the variable 
				if (($(activeSubNavItem).length > 0) && ($(activeSectionItem).length > 0) && ($(activeSectionItem).children("ul").length > 0)) {
						navElementsLoaded = true;
				}
				//}
			}
			
			
			// if all the elements have loaded, show the active nav and subnav items and set handlers for hover on navigation
			if (navElementsLoaded) {
				$(activeSubNavItem).addClass("selected"); // is this ok to call in both cases?
				$(activeSectionItem).addClass("selected");
				$(activeSectionItem).children("ul").addClass("selected");
				// set handlers for hover on navigation
				if ($("ul#mainnav li.has-subnav").length > 0) { 
					$("ul#mainnav li.has-subnav").hover(rollOverNav,resetNav);
					navLoaded = true;
				}
			}
		} 
	} else {
		// elements have loaded and navigation has been set so clear interval to stop running the function
		clearInterval(navIntervalID);
	} 
}
	
// when user hovers over a main navigation item, show the correct nav item and subnavigation
function rollOverNav(){
		// be sure to clear a previous timeout
		window.clearTimeout(timeoutID);
		$("ul#mainnav li.has-subnav").removeClass("selected");
		$("ul#mainnav li.has-subnav ul").removeClass("selected");
		hoverItem = "ul#mainnav li#"+$(this).attr("id");
		$(hoverItem).addClass("selected");
		$(hoverItem).children("ul").addClass("selected");
}
// when user leaves navigation, reset navigation to default active items	
function resetNav(){
// add a delay to execute the navigation reset
	timeoutID = window.setTimeout(function(){
		$("ul#mainnav li.has-subnav").removeClass("selected");
		$("ul#mainnav li.has-subnav ul").removeClass("selected");
		$(activeSectionItem).addClass("selected");
		$(activeSectionItem).children("ul").addClass("selected");
		$(activeSubNavItem).addClass("selected");
	}, 400);
}
	
// when user hovers over a section navigation item, show the dropdown menu
function rollOverSectionNav(){
	$(this).addClass("selected");
}
// when the user leaves a section navigation item, hide the dropdown menu
function resetSectionNav(){
	$(this).removeClass("selected");
}	
/* -------- Navigation Logic END ----------- */

$(document).ready(function(){
	equalHeight($(".footer-column"));
 	$("#home-carousel .carousel-full").hover(function(){
		var exHeight = $(this).children(".carousel-excerpt").height();
		var exHeight = exHeight + 10;
		var newBtm = 458 - exHeight;
		$(this).children(".carousel-share").animate({top:"-40px"},300);
		$(this).children(".carousel-title").animate({bottom:exHeight+"px"},300,"linear");
		$(this).children(".carousel-excerpt").animate({top:newBtm+"px"},300,"linear");
 	},function(){
		$(this).children(".carousel-share").animate({top:"-80px"},300);
		$(this).children(".carousel-title").animate({bottom:0},300,"linear");
		$(this).children(".carousel-excerpt").animate({top:"458px"},300,"linear");
		$(".carousel-share-link").css("background","#a7c04a url(images/bg-carousel-share.png) 0 0 repeat-x")
 	});
	$("#home-carousel .carousel-share-link").click(function(){
		$(this).parents(".carousel-share").animate({top:"0px"},300,"linear");
		$(this).css("background-image","none");
		return false;
	});
	$("#login-tab").hover(function(){
		$(this).addClass("hovered");
	},function(){
		$(this).removeClass("hovered");
	}
	);
	$("#navNew li").hover(function(){
		$(this).addClass("hovered");
	},function(){
		$(this).removeClass("hovered");
	}
	);

	/* ------- Utilities BEGIN --------- */
	// remove the default text on the global search input box
	$("#keyword").click(function() { 
			    if ($(this).val() == 'Search Serious Eats') {   
			        $(this).data('original', $(this).val()).val('');
					$(this).addClass("selected");
			    }
			});
	// put the default text back into the global search input box if the user hasn't typed anything
			$("#keyword").blur(function() { 
			    if ($(this).val() == '') {   
					$(this).removeClass("selected");
			        $(this).val($(this).data('original'));
			    }
			});
	// show and hide the newsletter form signup box
	$("#subscribe-button").click(function(){
		$("#newsletter-form").toggle("fast");
		$(this).text($(this).text() == 'Go' ? 'Close' : 'Go');
	});
		/* ------- Utilities END --------- */
});

function equalHeight(group) {
    tallest = 0;      
    group.each(function() {
        thisHeight = jQuery(this).height();
        if(thisHeight > tallest) {
            tallest = thisHeight;
        }
    });
    group.height(tallest);
}


// provide scrollable API for the action buttons
function init_main_carousel(carouselInterval){  
    var root = $(".scrollable").scrollable({circular: true}).navigator({ navi:'ul#carousel-thumbs'}).autoscroll({ interval: carouselInterval });
}
