﻿$(function () {

	var numberOfCharForDetailedSearch = 3;
	var showCouponSuggestion = false;
	var showDealSuggestion = false;

	var $scrollPane;

	$(this).click(function (event) {

		$('.list_name').html("");
		$('.list_name').hide();
		$('#tblAutoSuggest').hide();

	});

	$("input[name$='txtSearch']").bind('keyup', function (e) {

		if (e.keyCode == 27) {

			$('.list_name').html("");
			$('.list_name').hide();
			$('#tblAutoSuggest').hide();
			return;

		}

		if (e.keyCode == 40) {

			if ($('.highlight').length < 1) {

				$('.list_name div li:first').addClass('highlight');

			} else {

				if ($('.highlight').next().length < 1) {

					$('.highlight').removeClass('highlight');
					$('.list_name div li:first').addClass('highlight');

				} else {

					var itsNext = $('.highlight').removeClass('highlight').next().addClass('highlight');

					if (itsNext.is(':hidden')) {

						$(this).trigger({ type: 'keyup', keyCode: 40 });

					}

				}

			}

			$(this).val($('.highlight').text());
			$scrollPane.data('jsp').scrollTo(parseInt($('.highlight').position().left), parseInt($('.highlight').position().top));

			return;
		}


		if (e.keyCode == 38) {

			if ($('.highlight').length < 1) {

				$('.list_name div li:last').addClass('highlight');

			} else {

				if ($('.highlight').prev().length < 1) {

					$('.highlight').removeClass('highlight');
					$('.list_name div li:last').addClass('highlight');

				} else {

					var itsPrev = $('.highlight').removeClass('highlight').prev().addClass('highlight');

					if (itsPrev.is(':hidden')) {

						$(this).trigger({ type: 'keyup', keyCode: 38 });

					}

				}

			}

			$(this).val($('.highlight').text());
			$scrollPane.data('jsp').scrollTo(parseInt($('.highlight').position().left), parseInt($('.highlight').position().top));

			return;

		}


		if (e.keyCode == 13) {

			$(this).val($('.highlight').text());
			$('.list_name').html("");
			$('.list_name').hide();
			$('#tblAutoSuggest').hide();

			return;

		}


		if (e.keyCode == 8) {

			if ($.trim(this.value) == '') {

				$('.list_name').html("");
				$('.list_name').hide();
				$('#tblAutoSuggest').hide();
				return;

			}

		}


		if ($.trim(this.value) == '') {

			$('.list_name').html("");
			$('.list_name').hide();
			$('#tblAutoSuggest').hide();
			return;

		}

		fetchAutoSuggestData();

	});

	function fetchAutoSuggestData() {

		var sHTML = "";

		$.ajax({
			type: "POST",
			url: "/autocomplete.asmx/getsearchautosuggestlistjson",
			data: "{'prefixText':'" + $("input[name$='txtSearch']").val() + "'}",
			contentType: "application/json; charset=utf-8",
			dataType: "json",
			timeout: 10000,
			success: function (data) {

				sHTML += "<div class='search-scroll-pane'>";
				sHTML += data.d;
				sHTML += "</div>";

				sHTML = sHTML.toLowerCase();

				if ($("input[name$='txtSearch']").val().length >= numberOfCharForDetailedSearch) {

					if (showCouponSuggestion == true) {

						sHTML += "<div id='divAutoSuggestCouponsTitle' name='divAutoSuggestCouponsTitle' style='font-size: 12px; width: 550px;'>";

						sHTML += "<div style='height: 10px;'/>";
						sHTML += "<table width='550' border='0' cellpadding='0' cellspacing='0'>";
						sHTML += "<tr>";
						sHTML += "<td width='20'>";
						sHTML += "<hr color='#BFBFBF' size='1' />";
						sHTML += "</td>";
						sHTML += "<td width='120'>";
						sHTML += "<b class='couponContents' style='font-size: 12px;'>&nbsp;&nbsp;Matching Coupons&nbsp;&nbsp;</b>";
						sHTML += "</td>";
						sHTML += "<td width='322'>";
						sHTML += "<hr color='#BFBFBF' size='1' />";
						sHTML += "</td>";
						sHTML += "<td width='68'>";
						sHTML += "&nbsp;&nbsp;<a href='/search.aspx?q=" + $("input[name$='txtSearch']").val() + "' class='couponLinks' style='font-size: 11px;'>View All</a>&nbsp;&nbsp;";
						sHTML += "</td>";
						sHTML += "<td width='20'>";
						sHTML += "<hr color='#BFBFBF' size='1' />";
						sHTML += "</td>";
						sHTML += "</tr>";
						sHTML += "<tr>";
						sHTML += "<td colspan='5' style='padding: 5px;'>";
						sHTML += "<div id='divAutoSuggestCoupons' name='divAutoSuggestCoupons' style='font-size: 12px; text-align: left;'>Loading Coupons....</div>";
						sHTML += "</td>";
						sHTML += "</tr>";
						sHTML += "</table>";

						sHTML += "</div>";

					}
					
				}

				$('.list_name').html(sHTML);

				$scrollPane = $('.search-scroll-pane');

				/***************** String Matching, Scrolled List of Match, Ajaxed Load of Coupons and Deals ***********/

				var strToMatch = $("input[name$='txtSearch']").val();
				var bFound = false;

				$('.list_name').show();

				$('.list_name div li').each(function () {

					$(this, 'a').mouseover(function (event) {

						$(this).addClass('highlight');

					});

					$(this, 'a').mouseout(function (event) {

						$(this).removeClass('highlight');

					});

					$(this, 'a').click(function (event) {

						$("input[name$='txtSearch']").val($(this).text());

						$(location).attr("href", "/search.aspx?q=" + $.URLEncode($(this).text()));
						//$("input[name$='btnSearch']").trigger('click');

						event.preventDefault();

					});

					if ($(this).text().toLowerCase().indexOf(strToMatch.toLowerCase()) > -1) {

						var entryText = $(this).text();

						entryText = entryText.replace(new RegExp(strToMatch.toLowerCase(), "gi"), "<b style='font-size: 12px; color: #2781e6;'>" + strToMatch.toLowerCase() + "</b>");

						$(this).html("<a href=''>" + entryText + "</a>");

						$(this).show();
						bFound = true;

					} else {

						$(this).hide();

					}

				});

				if (bFound == true) {

					$('.list_name').show();
					$('#tblAutoSuggest').show();

					var scrollPaneSettings = {
						showArrows: true,
						horizontalGutter: 0,
						verticalGutter: 0,
						animateScroll: true,
						arrowScrollOnHover: false
					};

					$scrollPane.jScrollPane(scrollPaneSettings);

					$('.highlight').removeClass('highlight');
					$('.list_name div li:first').addClass('highlight');

					$('#divAutoSuggestCoupons').html("<div class='ajaxLoader2' style='width='550px;'></div>");
					$('#divAutoSuggestDeals').html("<div class='ajaxLoader2' style='width='550px;'></div>");

					if ($("input[name$='txtSearch']").val().length >= numberOfCharForDetailedSearch) {

						if (showCouponSuggestion == true) {

							$.ajax({
								type: "POST",
								url: "~/AutoComplete.asmx/GetSearchAutoSuggestCoupons",
								data: "{'prefixText':'" + $("input[name$='txtSearch']").val() + "'}",
								contentType: "application/json; charset=utf-8",
								dataType: "json",
								timeout: 10000,
								success: function (data) {

									$('#divAutoSuggestCoupons').html(data.d);

								},
								error: function (e) {

									$('#divAutoSuggestCoupons').html(e.Message);

								}

							});

						}

						if (showDealSuggestion == true) {

							$.ajax({
								type: "POST",
								url: "~/AutoComplete.asmx/GetSearchAutoSuggestDeals",
								data: "{'prefixText':'" + $("input[name$='txtSearch']").val() + "'}",
								contentType: "application/json; charset=utf-8",
								dataType: "json",
								timeout: 10000,
								success: function (data) {

									$('#divAutoSuggestDeals').html(data.d);

								},
								error: function (e) {

									$('#divAutoSuggestDeals').html("");

								}

							});

						}

					}

					return;

				} else {

					$('.list_name').html("");
					$('.list_name').hide();
					$('#tblAutoSuggest').hide();

					return;

				}

				/***************** String Matching, Scrolled List of Match, Ajaxed Load of Coupons and Deals ***********/

			},
			error: function (e) {

				sHTML += "";

			}

		});

	}

});

