function hideHistory(inputId, act)
{
	$("#" + inputId + "_history")[act === "hide"? "hide": "show"]();
}
function getHtmlItem(item, cookieName)
{
	switch (cookieName)
	{
		case "cookie-js-heading_name":
			return "<span style='float:left'>" +
			       (item.searchType === "heading"?
			         "(תחום עיסוק)": "(שם בית עסק)") +
			       "</span>";
		case "cookie-js-where_name":
			return "";
		case "cookie-js-address":
			return "";
	}
}
function onKeyDown(ev, selector)
{
	
	var kc = ev.keyCode;
	if (kc === 38)
	{
		if($(selector + " .selectedItem").length > 0)
		{
			if ($(selector + " .selectedItem").prev().length > 0)
			{
				$(selector + " .selectedItem").removeClass("selectedItem")
				                              .prev().addClass("selectedItem");
			}
			else
			{
				$(selector + " div:last-child").addClass("selectedItem");
				$(selector + " div:first-child").removeClass("selectedItem");
			}
		}
		else
		{
			$(selector + " div:last-child").addClass("selectedItem");
		}
		return true;
	}
	if (kc === 40)
	{
		if ($(selector + " .selectedItem").length > 0)
		{
			if ($(selector + " .selectedItem").next().length > 0) 
			{
				$(selector + " .selectedItem").removeClass("selectedItem")
				                              .next().addClass("selectedItem");
			}
			else
			{
				$(selector + " div:first-child").addClass("selectedItem");
				$(selector + " div:last-child").removeClass("selectedItem");
			}
		}
		else
		{
			$(selector + " div:first-child").addClass("selectedItem");
		}
		return true;
	}
	if (kc === 13)
	{
		if ($(selector).parent().parent().css("display") !== "none")
		{
			$(selector + " .selectedItem").mousedown();
		}
		return false;								
	}
	return true;
}
function getHeadingNameJSON(cookieName, newVal, searchType)
{
	
	var newSearch_textList, search_text_cookie;
	search_text_cookie = getCookie(cookieName);
	if (search_text_cookie.indexOf(
	     '{"name":"' + newVal + '","searchType":"' + searchType + '"}') === -1)
	{
		search_text_cookie = eval(search_text_cookie);
		newSearch_textList = "{[";
		newSearch_textList += '{"name":"' + newVal + '","searchType":"' + searchType + '"}';
		if (search_text_cookie)
		{
		 $.each(search_text_cookie, function(i, item) {
			 if (i < 4)
			 {
				newSearch_textList +=
				 ',{"name":"' + item.name +
				 '","searchType":"' + item.searchType + '"}';
			 }
		 });
		}
		
		return (newSearch_textList += "]}");
	}
	else
	{
		return search_text_cookie;
	}
}
function getWhereNameJSON(cookieName, newVal)
{
	var newSearch_textList, search_text_cookie;
	search_text_cookie = getCookie(cookieName);
	if (search_text_cookie.indexOf(newVal) === -1)
	{
		search_text_cookie = eval(search_text_cookie);
		newSearch_textList = "{[";
		newSearch_textList += '{"name":"' + newVal + '"}';
		if (search_text_cookie)
		{
		 $.each(search_text_cookie, function(i, item) {
			 if (i < 4)
			 {
				newSearch_textList += ',{"name":"' + item.name + '"}';
			 }
		 });
		}
		
		return (newSearch_textList += "]}");
	}
	else
	{
		return search_text_cookie;
	}
}
function saveHistory(cookieName, newVal)
{
	var newSearch_textList, cookie;
	cookie = getCookie(cookieName);
	newVal = newVal.replace('"', "&quot;");
	if (newVal !== "" && (vIsShowMap == 1 ||
	    document.location.href.indexOf("/?") !== -1 ||
	    vPageName === "Integration-Maps"))
	{
		newSearch_textList = "";
		switch (cookieName)
		{
			case "cookie-js-heading_name":
				newSearch_textList =
				 getHeadingNameJSON(
				  cookieName, newVal,
                                  $("form input:radio[checked='true']").val());
				break;
			case "cookie-js-where_name":
				newSearch_textList = getWhereNameJSON(cookieName, newVal);
				break;
			case "cookie-js-address":
				newSearch_textList = getWhereNameJSON(cookieName, newVal);
				break;
			default:
				break;
		}
		
		setCookie(cookieName, newSearch_textList, 365);

	}
}
function deleteHistory(cookieName, inputId)
{
	delCookie(cookieName);
	$("#"+inputId+"_history .historyList").html("");
	$("#"+inputId+"_history .deleteMSG").show();
	$("#"+inputId+"_history .clearHistory").hide();
	setTimeout(
	 function ()
	 {
	  hideHistory(inputId, "show");
	 }, 100);
	setTimeout(
	 function ()
	 {
	  hideHistory(inputId, "hide");
	  $("#" + inputId + "_history .deleteMSG").hide();
	  $("#" + inputId + "_history .clearHistory").show();
	 },
	 1000);
	
}
function getHistory(e, cookieName, inputId, evt)
{
	var search_text_cookie;
	//$("#"+inputId)[0].attachEvent("onkeydown", enterClicked, false);
	$("#" + inputId).blur(function () {
		hideHistory(inputId, "hide");
		
	});
	$("#" + inputId).keydown(function (event) { return gpi.cookieHistory.enterPressed(event,inputId); });
	
	if ($("#" + inputId).val() === "" && $("#" + inputId + "_history").length > 0)
	{
		if ($("#" + inputId + "_history")[0].style.display === "none")
		{
			search_text_cookie = eval(getCookie(cookieName));
			if (search_text_cookie)
			{
				$("#" + inputId + "_history .historyList").html("");
				$(".history_head" + inputId).show();
				$.each(search_text_cookie, function(i, item){
						$("<div></div>")
						.html("<span style='float:right'>" +
						      item.name.substr(0, 64) + (item.name.length > 65? "...": "") +
						      "</span>" + getHtmlItem(item, cookieName))
						.css("width", "100%")
						.css("height", "15px")
						//.addClass(i==0?"selectedItem":"")
						.mouseover(function () { $(this).addClass("selectedItem"); })
						.mouseout(function () { $(this).removeClass("selectedItem"); })
						.mousedown(function () {
							if (evt)//XXX !== null && typeof evt !== "undefined")
							{
							 evt(item);
							}
							$("#" + inputId).val(item.name.replace(/&#39;/g, "'").replace(/&quot;/g, '"'));
							$("#" + inputId + "_history").hide();
							$("#" + inputId).focus();
							
						})
						.appendTo("#" + inputId + "_history .historyList"); 
				 });
				$("#" + inputId + "_history").show();
			}
		}
	}
	else
	{
		$("#" + inputId + "_history").hide(); 
	}
	return onKeyDown(e, ".history_head" + inputId + " .historyList");	
}
function headingNameHItemClick(clickedItem)
{
	$("form input:radio[value='" + clickedItem.searchType + "']")[0].checked = true;
}
function installHistory(inputId, width, marginRight, avoidMarginTopFix)
{
	var webkitLeftMarginFix = "auto",
	    nonIETopMarginFix = "auto";

	if (browser === "safari")
	{
		webkitLeftMarginFix = (inputId === "_search_text")? "41px": "22px";
	}
	if (browser !== "msie")
	{
		nonIETopMarginFix = (avoidMarginTopFix === 0)? "0": "2px";
	}
   
	$("<div></div>")
		.append(
			$("<div></div>")
				.attr("id", inputId + "_history")
				.addClass("autoComplete_history")
				.css("width", width)
				.css("margin-right", marginRight)
				.css("margin-left", webkitLeftMarginFix)
				.css("margin-top", nonIETopMarginFix)
				//.css("margin-top",(browser != 'msie')?0:0)
				.css("padding", 5)
				.hide())
		.appendTo($("#" + inputId).parent().parent());
	$(".history_head" + inputId).appendTo("#" + inputId + "_history");
}
function initHistory()
{
	installHistory("_search_text", 260, 10, 3);
	installHistory("_where_name", 200, 10, 3);
	setWhatHistoryText($("form input:radio[checked='true']").val());
	if (vPageName === "Business-Results" || vPageName === "Summaries")
        {
	    saveHistory("cookie-js-heading_name", vSearchText);
	    saveHistory("cookie-js-where_name", vWhereNameDefaultValue);
	}
}
function setWhatHistoryText(type)
{
	$("#search_text_title").html(type === "name"? "שם בית עסק": "תחום עיסוק - העזר/י בהשלמה האוטומטית");
}
if (!window.gpi)
{
 window.gpi = {};
}
if (!gpi.cookieHistory)
{
 gpi.cookieHistory = {};
}
gpi.cookieHistory.initMapHistory =
 function ()
 {
  installHistory("toAddress", 275, 10, 0);
  installHistory("fromAddress", 275, 10, 0);
  installHistory("fullAdress", 610, 10, 0);
 };
gpi.cookieHistory.enterPressed =
 function (event, inputId)
 {
  var kc = event.keyCode;
  if (kc === 13 &&
      $("#" + inputId + "_history").css("display") !== "none")
  {
   if (event.cancelable)
   {
    event.preventDefault();
   }
   return false;
  }
 };

