$(document).ready(function() {
	
	$("body").removeClass("nojs");
	
	/* Variables for scripts */
	var body = $("body");
	var windowWidth;
	var containerWidth = 990;
	
	/* Check for HTML5 form capability, see if we need the polyfill */
	function h5formSetup() {
		if ($(".h5form").length > 0) {
			H5F.setup($('.h5form'), {
			    validClass: "valid",
			    invalidClass: "invalid",
			    requiredClass: "required",
			    placeholderClass: "placeholder"
			});
		}
	}
	Modernizr.load([
	  {
	    test : Modernizr.inputtypes.email && Modernizr.input.required && Modernizr.input.placeholder,
	    nope : '/js/h5f.min.js',
	    callback : function () {
	      $("body").addClass("h5fneeded");
				h5formSetup();
	    }
	  }
	]);
	
	/* Main nav dropdowns */
	$("ul.sf-menu").superfish({
		animation: {height:'show'},
		speed:"fast",
		autoArrows:false,
		dropShadows:false
	}); 
	
	/* Homepage slideshow */
	var featureslideshow = $("div.slideshow").add("div.productslideshow");
	if ($(featureslideshow).length > 0) {
		$(featureslideshow).after("<div id=\"slideshownav\"><a href=\"#\" id=\"prevslide\">Previous</a><ul></ul><a href=\"#\" id=\"nextslide\">Next</a></div>").cycle({
			timeout: 7000,
			pause: true,
			prev: '#prevslide', 
			next: '#nextslide',
			pager: '#slideshownav ul',
			// callback fn that creates a thumbnail to use as pager anchor 
		pagerAnchorBuilder: function(idx, slide) { 
				return '<li><a href="#">' + (idx+1) + '</a></li>'; 
			},
			fx: 'fade',
			cleartype:0,
			containerResize:0
		});

		//If they click or focus on the slide nav, the slideshow stops auto-rotating
		$("div#slideshownav ul li a, a#prevslide, a#nextslide").click(function() {
			$(featureslideshow).cycle("pause");
		});
		$("div#slideshownav ul li a, a#prevslide, a#nextslide").focus(function() {
			$(featureslideshow).cycle("pause");
		})

	};
	
	var officeslideshow = $("div.marblegroup");
	$(officeslideshow).cycle({
		timeout: 7000,
		pause: false
	})
	
	/* Tagging the Marble list items by column */
	$("ul.marblefolks li:nth-child(5n)").addClass("col5");
	$("ul.marblefolks li:nth-child(5n-1)").addClass("col4");
	$("ul.marblefolks li:nth-child(5n-2)").addClass("col3");
	$("ul.marblefolks li:nth-child(5n-3)").addClass("col2");
	$("ul.marblefolks li:nth-child(5n-4)").addClass("col1");
	
	/* Tag the first row */
	$("ul.marblefolks li:nth-child(1)").addClass("row1");
	$("ul.marblefolks li:nth-child(2)").addClass("row1");
	$("ul.marblefolks li:nth-child(3)").addClass("row1");
	$("ul.marblefolks li:nth-child(4)").addClass("row1");
	$("ul.marblefolks li:nth-child(5)").addClass("row1");
	
	/* Tag the next-to-last row */
	$("ul.marblefolks li:nth-child(16)").addClass("row4");
	$("ul.marblefolks li:nth-child(17)").addClass("row4");
	$("ul.marblefolks li:nth-child(18)").addClass("row4");
	$("ul.marblefolks li:nth-child(19)").addClass("row4");
	
	/* Tag the last row */
	$("ul.marblefolks li:nth-child(20)").addClass("row5");
	$("ul.marblefolks li:nth-child(21)").addClass("row5");
	$("ul.marblefolks li:nth-child(22)").addClass("row5");
	$("ul.marblefolks li:nth-child(23)").addClass("row5");
	$("ul.marblefolks li:nth-child(24)").addClass("row5");
	
	/* Tag the last+ row */
	$("ul.marblefolks li:nth-child(25)").addClass("row6");
	$("ul.marblefolks li:nth-child(26)").addClass("row6");
	$("ul.marblefolks li:nth-child(27)").addClass("row6");
	$("ul.marblefolks li:nth-child(28)").addClass("row6");
	$("ul.marblefolks li:nth-child(29)").addClass("row6");
	
		
	/* Measuring window width for Meet the Marbles */
	function isWindowNarrow() {
		windowWidth = $(window).width();
		if (windowWidth - (containerWidth + 150) < 0) {
			body.addClass("narrowwindow");
		};
	};
	
	isWindowNarrow();
	
	/* Do on resize */
	$(window).smartresize(function(event) {
		body.removeClass("narrowwindow");
		isWindowNarrow();
	});
	
	/* Meet the Marbles show/hide */
	var showMarbleLink = $("a.showmarble");
	showMarbleLink.attr("role", "button");
	
	var hiddenMarbleContent = $(".marblehideme");
	hiddenMarbleContent.attr('aria-hidden',true);
	
	var allMarbleContent = $(".personinfo");
	
	var marbleIdToShow;
	
	/* Highlighting/dimming marbles on hover */
	showMarbleLink.hover(function() {
		$(this).removeClass("nothovered");
		$(this).parent("li").parent("ul").find("a.showmarble").not(this).addClass("nothovered");
	}, function() {
		if ($("li.expanded").length > 0) {
			$(this).addClass("nothovered");
		}
	});
	
	$("ul.marblefolks").hover(function() {

	}, function() {
		if (!$("li.expanded").length > 0) {
			showMarbleLink.removeClass("nothovered");
		}
	});
	
	function hideAllMarbles() {
		//alert("Hide all marbles!");
		hiddenMarbleContent.fadeOut(function(){
			$(this).addClass('marblehideme').removeAttr('style').attr('aria-hidden',true);
		});
		$(showMarbleLink).parent("li").removeClass("expanded");
		$(showMarbleLink).find("span").html("Show ");
	};
	
	function hideMarble(marblelink) {
		$(marblelink).find("span").html("Show ");
		$(marbleIdToShow).addClass('marblehideme').removeAttr('style').attr('aria-hidden',true);
		$(marblelink).removeClass("expanded");
	};
	
	function showMarble(marblelink) {
		$(marblelink).find("span").html("Hide ");
		$(marblelink).parent("li").addClass("expanded");
		$(marbleIdToShow).fadeIn(function(){
			$(this).removeClass('marblehideme').removeAttr('style').attr('aria-hidden',false);
		});
	};
	
	showMarbleLink.click(function() {
		
		// reset any other showing links/content
		$(".personinfo").filter(":visible").hide();
		$(".personinfo").filter(":visible").addClass('marblehideme').removeAttr('style').attr('aria-hidden',true);
		$("a.showmarble").not(this).parent("li").removeClass("expanded");
		$("a.showmarble").not(this).find("span").html("Show ");
		
		marbleIdToShow = $(this).attr("href");
		if ($(this).parent("li").hasClass("expanded")) {
			hideMarble(this);
		} else {
			showMarble(this);
		}
		return false;
	});

	$(document).click(function(e) { var target = e.target;
	    if (!$(target).is(showMarbleLink) && !$(target).parents().is(showMarbleLink) && !$(target).is($(".personinfo")) && !$(target).parents().is($(".personinfo"))) {
				hideAllMarbles();
				showMarbleLink.removeClass("nothovered");
	    }
	});
	
	
	/* Form submission/validation */
	$("form#contactform input.formbutton").click(function(event) {  
		event.preventDefault(); 
		
		$(this).parents("form").addClass("validated");
		
		var formIsValid = true; //default assumption that we'll prove false if needed
			
		/* Do some extra checking for Safari/Chrome just in case, */
		/* since they will still submit a form with invalid values (!!) */
		if (!$("body").hasClass("h5fneeded")) {
			if ($("form#contactform :invalid").length > 0) {
				formIsValid = false;
			};
		} else {
			formIsValid = document.getElementById("contactform").checkValidity();//$("form#contactform").checkValidity();
		};
		
		/* Passed all the tests, ready to go! */
		if (formIsValid == true) {
			//javascript:alert('success!');
			$("form#contactform").submit();
		} else {
			//alert("Not valid!");
		};

	});
	

});

/* Do on window load, instead of DOM ready */
$(window).load(function() {
	
	/* Equalize columns where needed */
	$("article.zebrasections div.column").equalHeights();
	
	/* Equalize heights on Contact page (special case) */
	if ($("body.contact").length > 0) {
		
		if ($("body.signup-thank-you").length > 0)
			var targetHeight = $("body.contact div.column:last-child").height();
		else
			var targetHeight = $("body.contact div.column:first-child").height();
		var topPadding = parseFloat($("body.contact div.column:nth-child(2) section").css("paddingTop"));
		var bottomPadding = parseFloat($("body.contact div.column:nth-child(2) section").css("paddingBottom"));

		targetHeight = targetHeight - topPadding - bottomPadding;
		
		if ($("body.signup-thank-you").length > 0)
			$("body.contact div.column:nth-child(1) section").css("height", targetHeight + "px");
		else
			$("body.contact div.column:nth-child(2) section").css("height", targetHeight + "px");

	}

	
});
