(function(){
	var lastXHR = null;
	var lastValue = "";
	$('.js-word-input').live('keydown',function(){
		var self = this;
		setTimeout(function(){
			var tval = $(self).val();
			//window.console&&console.log(tval);
			if(tval == '') {
				$(".js-word-options").css("display", "none");
				//lastXHR && lastXHR.abort && lastXHR.abort();
			} else {
				if(tval.length > 1) {
					if (tval == lastValue) {
					
						return;
					} else {
						lastValue = tval;
						var dataToSend = {"tval":tval};
						lastXHR && lastXHR.abort && lastXHR.abort();
						lastXHR = $.ajax({
							url:'/ajax/word-test',
							type: 'GET',
							data: dataToSend,
							dataType: 'html',
							success: function(out){
								if(out == '') {
									return;
								} else {
									$("#content").fadeOut(100);
									$(".cnt-product-listing-wrapper-home").fadeOut(100);
									$("#content-dvk").fadeOut(100);
									$(".cnt-product-heading-block").fadeOut(100);
									$("#cnt-catalog-block").fadeOut(100);
									$("#cnt-filter-group").fadeOut(100);
									$(".cnt-product-listing-wrapper").fadeOut(100);									
									$("#searchs").html("<a href=\"javascript://\" class=\"js-closest\">← <span>Вернуться обратно</span></a><br/><br/><a href=\"javascript://\" class=\"js-search-one inactive\">Товары</a><a href=\"javascript://\" class=\"js-search-two\">Текстовые страницы</a><br/><br/><div style=\"width:70%;height:80%;\" class=\"cnt-search-one\"><table id=\"cnt-product-listing\"><tr>" + out + "</div>");
									$("#searchs").fadeIn(1000);
								}
							}					
						});			
					}
				} else {
					$("#searchs").fadeOut("show");
					$("#content").fadeIn(100);
					$(".cnt-product-listing-wrapper-home").fadeIn(100);
					$("#content-dvk").fadeIn(100);
					$(".cnt-product-heading-block").fadeIn(100);
					$("#cnt-catalog-block").fadeIn(100);
					$("#cnt-filter-group").fadeIn(100);
					$(".cnt-product-listing-wrapper").fadeIn(100);
					return;
				}
			}
		}, 0);
	})
	return;
	
})()

$(document).ready(function(){
	$('.js-closest').live("click",function(){
		var els = $(".js-prompt");
		var eles = els.attr("title");
		$("#searchs").fadeOut("show");
		$("#content").fadeIn(100);
		$(".cnt-product-listing-wrapper-home").fadeIn(100);
		$("#content-dvk").fadeIn(100);
		$(".cnt-product-heading-block").fadeIn(100);
		$("#cnt-catalog-block").fadeIn(100);
		$("#cnt-filter-group").fadeIn(100);
		$(".cnt-product-listing-wrapper").fadeIn(100);
		$(els).removeClass("js-changed");
		$(els).val(eles);
	});

	$('.js-search-two').live("click",function(){
		if($('.js-search-two').hasClass() == 'inactive') {
			return;
		} else {
		$('.js-search-one').removeClass('inactive');
		$('.js-search-two').addClass('inactive');
		$('.cnt-search-one').css("display","none");
		$('.cnt-search-two').css("display","block");
		
	}
	});
	$('.js-search-one').live("click",function(){
		if($('.js-search-one').hasClass() == 'inactive') {
			return;
		} else {
		$('.js-search-two').removeClass('inactive');
		$('.js-search-one').addClass('inactive');
		$('.cnt-search-two').css("display","none");
		$('.cnt-search-one').css("display","block");
	}
	});
});

