﻿

$(document).ready(function () {


	var dz;
	var dz2;

	//Setup Auto-Complete in header
	var txtBMK = $("#bmk").val();
	var options = { serviceUrl: '/product_keyword_ac.ashx', params: {'bmk':txtBMK}, maxHeight: 150, width: 160, minChars: 1, deferRequestBy: 0, noCache: true, onSelect: function (value, data) { $('#txtNews').val(value); window.location.href = data; } };
	dz = $('#txtNews').autocomplete(options);
	/*
	var dz2;
	var options2 = {  serviceUrl: '/product_keyword_ac.ashx', maxHeight: 150, width: 160,  minChars: 1, deferRequestBy: 0, noCache: true,		onSelect: function(value, data){$('#searchbox').val(value); window.location.href=data; } };
	dz2 = $('#searchbox').autocomplete(options2);
	
	var options2 = {  serviceUrl: '/product_keyword_ac.ashx', maxHeight: 150, width: 160,  minChars: 1, deferRequestBy: 0, noCache: true,		onSelect: function(value, data){$('#searchbox').val(value); window.location.href=data; } };
	dz2 = $('#searchbox').autocomplete(options2);
	*/

	//Make sure the search goes to the right page. //Obsolete
	$('#frmSearch').attr('action', '/shop/');

	/* Sets up newsletter widget to work */
	/* If the DOM elements for the newsletter widget are changed, then THIS WILL BREAK! */
	$('#submitNews_home, #submitNews_foot').click(function (e) {
		var $this = $(this);
		var $wait = $this.parent().prev('div.news_wait');
		var $news = $wait.parent();
		var $sub = $news.prev();

		e.preventDefault();
		var se = $this.prev('input').attr('value');
		if (se == null || se.length < 4) { alert("Please provide your complete email address."); }
		else {
			$wait.show();
			$this.hide();
			$.get("newsletter.ashx",
					{ emc2: se },
					function (data) { $news.hide(); $sub.show(200); }
				);
		}
	});

	//showDelay is shorter because we want the menu to still feel responsive without being instant.
	var showDelay = 200;
	var hideDelay = 400;
	$('#store_menu, #community_menu, #da_menu').each(
		function () {
			var inTimer = null,
				outTimer = null;
			$(this).hover(
				function () {
					var delayShowMenu = showMenu(this);
					//clears timer for current action so that we don't have multiple event handlers queuing up.
					if (inTimer) {
						clearTimeout(inTimer);
						inTimer = null;
					};
					//clears hide timer before setting show timer
					clearTimeout(outTimer);
					inTimer = setTimeout(delayShowMenu, showDelay);
				},
				function () {
					//functions exactly the same as above function, but hides menu instead
					var delayHideMenu = hideMenu(this);
					if (outTimer) {
						clearTimeout(outTimer);
						outTimer = null;
					};
					clearTimeout(inTimer);
					outTimer = setTimeout(delayHideMenu, hideDelay);
				}
			);
		}
	);
	//These functions return functions that are used in the setTimeout for the nav menu dropdown
	function showMenu(obj) {
		return (function () {
			var $link = $(obj).children('.main_link');
			var $dropdown = $link.next('div');
			$link.addClass("switchnavcolor");
			$dropdown.fadeIn('fast');
		});
	};
	function hideMenu(obj) {
		return (function () {
			var $link = $(obj).children('.main_link');
			var $dropdown = $link.next('div');
			$link.removeClass("switchnavcolor");
			$dropdown.hide();
		});
	};

	$("ul.subnav").parent().append("<span></span>"); //Only shows drop down trigger when js is enabled - Adds empty span tag after ul.subnav

	$("#ddCulture").click(function () {
		var $this = $(this);
		var $subnav = $('ul.subnav', $this);
		var $parent = $this.parent();
		$subnav.slideDown('fast');

		$parent.mouseleave(function () {
			$subnav.slideUp('normal');
		});
	});
});
