$(document).ready(function() {

	// Product info tabs
	$("#contact-nav, #map-nav").tabify();
	
	// Close popups on click outside
	$(document).click(function(e) {
		closePopups();
	});
	
	var $popups = $(".popup");
	function closePopups($leaveObj) {
		if(typeof $leaveObj == 'undefined') {
			$popups.hide();
		}
		else if($popups.size() > 0) {
			$popups.not($leaveObj).hide();
		}
	}
	
	// Don't close popup if we're clicking inside them
	$popups.click(function(e) {
		e.stopPropagation();
	});
	
	// Open Map Info
	$(".pin").click(function(e) {
		e.preventDefault();
		e.stopPropagation();
		$(".map-details").fadeIn(500);
		closePopups($(".map-details"));
	});
	
	// Close Map Info 
	$(".map-info-close").click(function(e) {
		e.preventDefault();
		$(".map-details").fadeOut(500);
	});
	
	// Open Info Request
	$(".info-request, .global-info-request").fancybox({
		type: 'iframe',
		width: 550,
		height: 620,
		centerOnScroll: true,
		hideOnContentClick: false,
		enableEscapeButton: false,
		hideOnOverlayClick: false,
		autoDimensions: false
	});
	
	// Close Info Request 
	$("#info-close").click(function(e) {
		e.preventDefault();
		$("#information-request").fadeOut(500);
	});
	
	
	// Show Terms Fancybox
	$("#show-terms").fancybox({
		width: 800,
		height: 500,
		autoDimensions: false,
		centerOnScroll: true
	});
	
	// Search submit
	$("#search .submit").click(function(e) {
		e.preventDefault();
		$(this).closest("form").submit();
	});
	
	
	
	// === Info Request form prevalue functionality ===
 
	var $login = $(".section");

	// Reset values on pageload
	var $loginField = $("input", $login);
	$loginField.val("");

	$loginField.blur(function() {

	// Store $(this)
	var zis = $(this);

	// store the label
	var $label = zis.siblings("span");

	// hide if there's text, show if blank
	if($.trim(zis.val()) == "") {
		$label.show();
		}
	else {
		$label.hide();
		}
	});
	$loginField.bind("focus click", function() {
		$(this).siblings("span").hide();
	});
	$("span", $login).click(function() {
		$(this).hide();
	});

	// Reset Form

	$(".clear").click(function() {
		$("span").hide();
	});
  
	// Clear textbox and return to default (focus/blur)
	$(".textbox-auto-clear").each(function(){
		var searchOrigVal = $(this).val(); // Store the original value
		$(this).focus(function(){
			if($(this).val() == searchOrigVal) {
				$(this).val('');
			}
		});

		$(this).blur(function(){
			if($(this).val() == '') {
				$(this).val(searchOrigVal);
			}
		});
	});
	
	// Category Slider 
	
	var $slider = $("#cat-slider > ul");
    var $items = $slider.children("li");
    var $next = $("#s-next");
    var $prev = $("#s-prev");
	var $catIntro = $("#category-intro");
    var scrollHeight = 21;
    var itemCount = $items.size();
	var activeIndex = 0;
    
	// Show selected category
	var $selectedItem = $items.filter(".active");
	if($selectedItem.size() == 1) {
		activeIndex = $items.index($selectedItem);
		$slider.css("top", function() {
			var currentOffset = parseInt($slider.css("top").replace("px",""));
			console.log(currentOffset);
			return (-(activeIndex * scrollHeight)) + currentOffset;
		});
		var activeText = $items.css("opacity", 0.5).eq(activeIndex).css("opacity", 1).children("p").text();
		$catIntro.text(activeText);
	}
	else {
		var firstText = $items.css("opacity", 0.5).eq(0).css("opacity", 1).children("p").text();
		$catIntro.text(firstText);
	}

	var canClick = true;
	$next.click(function(e) {
	    e.preventDefault();

	    if (canClick && activeIndex < (itemCount - 1)) {
	        canClick = false;

	        var text = $items.fadeTo(200, 0.5).eq(activeIndex + 1).fadeTo(200, 1).children("p").text();
	        $catIntro.text(text);
	        $slider.animate({
	            top: "-=" + scrollHeight + "px"
	        }, 500, function() {
	            canClick = true;
	            activeIndex++;
	        });
	    }
	});
	$prev.click(function(e) {
	    e.preventDefault();

	    if (canClick && activeIndex > 0) {
	        canClick = false;

	        var text = $items.fadeTo(200, 0.5).eq(activeIndex - 1).fadeTo(200, 1).children("p").text();
	        $catIntro.text(text);
	        $slider.animate({
	            top: "+=" + scrollHeight + "px"
	        }, 500, function() {
	            canClick = true;
	            activeIndex--;
	        });
	    }
	});
	
	// Show address panel if we're requesting product sample
	var $infoListItems = $(".info-list input");
	var $addressDetails = $("#address");
	$infoListItems.click(function() {
		var $this = $(this);
		var text = $this.siblings("label").text();
		if(text == "Product Sample") {
			$addressDetails.show();
		}
		else {
			$addressDetails.hide();
		}
	});
	
	// Clear info list on load
	$infoListItems.attr("checked", false);
	
	// Top navigation
	$(function() {
		$("#nav li").hover(function() {
			$(this).children("a").addClass("hover");
			$(this).parent().children("a").addClass("hover");
			$("ul:first", this).css('visibility', 'visible');
		}, function() {
			$(this).children("a").removeClass("hover");
			$(this).parent().children("a").removeClass("hover");
			$("ul:first", this).css('visibility', 'hidden');
		});
	});
	
	
});
