$(function()
{
	$('#tnmt-menu').hover(
		function()
		{
			$(this).find('dd').slideDown(150);
		},
		function(){
			$(this).find('dd').slideUp(150);
		}
	);
	
	// Dealer Locator
	$("#dealer-btn input").val("Enter a zip code...");
	var targetOffset = $("#dealer-locator").offset().top;
	$("#dealer-btn span").hide();
	$("#dealer-btn a").click(function()
	{
		$(this).hide();
		$("#dealer-btn form").fadeIn("fast");
		$("#dealer-btn input").focus();
		return false;
	});
	// Zip input focus
	$("#dealer-btn input").keydown(function()
	{
		if ($(this).val() == "Enter a zip code...")
		{
			$(this).val("");
		}
	});
	$("#dealer-btn input").blur(function()
	{
		if ($("#dealer-locator:hidden").length != 0)
		{
			$("#dealer-locator").slideUp(400);
			$("#dealer-btn form").fadeOut("fast", function()
			{
				$("#dealer-btn input").val("Enter a zip code...");
				$("#dealer-btn a").show();
			});
		}
	});
	// Zip form submit
	$("form#dealers").submit(function(e)
	{
		var zip = $(this).find('input').val();
		
		e.preventDefault();
		
		$("#dl-container").fadeOut("fast").empty();
		
		$('#dl-container').load('/ajax/locator.cfm?zip='+zip, function(){
			initialize();
			$('#dealer-locator #loader').fadeOut('fast');
		});
		
		$("#dl-container").fadeIn("fast");
		$("#dealer-btn span").show();
		
		$("#dealer-locator").slideDown(400);
		$("html,body").animate({
			scrollBottom: targetOffset
		}, 400);
	});
	// Clear form
	$("#dealer-btn span").click(function()
	{
		$(this).hide();
		$("#dealer-locator").slideUp(400);
		$("#dealer-btn form").fadeOut("fast", function()
		{
			$("#dealer-btn input").val("Enter a zip code...");
			$("#dealer-btn a").show();
			
			$('#dealer-locator #loader').show();
		});
	});
	
	// Product details tabs
	var index = 0;
	var detail_height = 0;
	$("#details > li:not(.active)").hide().css("opacity", 0);
	$("#details > li").each(function()
	{
		if ($(this).height() > detail_height)
		{
			detail_height = $(this).height();
		}
	});
	$("#details").height(detail_height);
	
	$("#details-nav li").click(function(e)
	{
		$("#details-nav li").removeClass("active");
		$(this).addClass("active");
		
		index = $(this).attr("id").match("[0-9]");
		index = parseInt(index);
		
		$("#details > li.active").animate({	opacity: 0 }, 500, function() {
			$(this).hide();
			$("#details > li").each(function()
			{
				if ($(this).attr("id").indexOf(index) != -1)
				{
					$(this).addClass("active").show().animate({ opacity: 1 }, 500);
				} else
				{
					$(this).removeClass("active");
				}
			});
		});
		
		e.preventDefault();
		e.stopPropagation();
	});
	
	// Send page to a friend form
	$("#send-page form").submit(function(e)
	{
		e.preventDefault();
		var email = $("#send-page #femail").val();
		var link = $("#send-page #link").val();
		console.log(email);
		$.get("/components/staf.cfc?method=send&email="+email+"&link="+link+"&returnformat=plain", function(result)
		{
			$("#send-page form").replaceWith("<p style='display:none;'>Your link has been successfully sent to <strong>" + result + "</strong></p>");
			$("#send-page p").fadeIn("fast");
		}, "text");
	});
});
