$(document).ready(doStuff);

function doStuff() {
	searchKeyword();
//	searchSuggest();
	refreshVendor();
}


function searchKeyword() {
	$("#go_butt").click(function(event){
		var keyword = $("#keyword").val();
		if (keyword.length > 0) {
			var _URL_HTTP = "http://"+document.domain;
			self.location = _URL_HTTP+"/index.php?module=adv_search_results&keyword=" + keyword;
		}
	});
}



function searchSuggest() {
	var elemPosition = -1;
	var searchBox = "#keyword";
	var resultsDiv = "#search_suggest_results_div";

	$("a").click( function(event) { //this is supposed to bring the value in the drop-down into the search box
//		$(searchBox).val($(this).html());
//			event.preventDefault();
	});
	

	function hideBox() {
		elemPosition = -1;
		$(resultsDiv).hide(); //hide the results dropdown
	}
	function startTimeout() {
		timer = setTimeout(hideBox,5000)
	}
	function resetTimeout() {
		if (typeof timer != "undefined") {
			clearTimeout(timer);
			timer = false;
		}
	}

	$(searchBox).keyup( function(event) {

		/************************
		/* replaced "keyCode" with "which"
		************************/
		var searchTerm = $(searchBox).val();
		var key = event.which;

		//search box loses focus
/*			$("#search_box").blur( function(event) {
			$("#search_results_div").hide();
		});*/

		$("body").click( function(event) {
			hideBox(); //clicking outside the results dropdown hides it
		});
		
		$(resultsDiv).mouseleave(function(event){
			startTimeout(); //when mouse leaves the dropdown a 5 second timer starts before the dropdown is hidden
		});

		$(resultsDiv).mouseenter(function(event){
			resetTimeout(); // if the mouse enters or re-enters the dropdown area, the timer is reset
		});

		switch (true) {
			case (key >= 48 && key <= 90): // a-z
			case (key == 8): // backspace
			case (key == 46): // delete
				elemPosition = -1; //reset selection position
				origSearchTerm = searchTerm; //copy the original search term here so it doesn't get changed by arrow keys
				getSuggestResults(searchTerm,searchBox,resultsDiv);
				break;

			case (key == 38): // up arrow
			case (key == 40): // down arrow
				var resultLength = $(resultsDiv+" div").length; //get the number of elements in the list
				if (key == 38) { //arrow up
					if (elemPosition >= 0) elemPosition --; //don't decrement lower than 0
					if (elemPosition < 0) elemPosition = resultLength - 1; //if you reach the top, next up circles you to the bottom
				}
				if (key == 40) { //arrow down
					elemPosition++;
					if (elemPosition > resultLength - 1) elemPosition = 0; //if you reach the bottom, next down circles you to the top
				}
				$(resultsDiv).show(); //if up or down keys are pressed, show the result list
//				$(".line a").css("background-color","#FFF"); //reset all line backgrounds to #FFF (white)
//				$("#line_"+elemPosition+" a").css("background-color","#DDD"); //set the background for the current element

				$(".line a").removeClass();
				$("#line_"+elemPosition+" a").addClass("highlighted");

				var resultLeft = $("#line_"+elemPosition+" .result_left").html();
				var resultMiddle = $("#line_"+elemPosition+" .result_middle").html();
				var resultRight = $("#line_"+elemPosition+" .result_right").html();
				$(searchBox).val(resultLeft+resultMiddle+resultRight); //bring highlighted value up in search box
				break;

			case (key == 27): // escape
				hideBox(); //hide the results dropdown
				$(searchBox).val(origSearchTerm); //bring back original search term
				$(searchBox).focus();
				break;
		} // end switch


	}); //search_box keyup

} //end function searchSuggest



function getSuggestResults(searchTerm,searchBox,resultsDiv){
			$.get("/index.php?module=search_suggest", { search_string: searchTerm },
				function(data) {
					var resultArray = data;
					eval(resultArray);
					var resultLength = resultArray.length;

					$(resultsDiv).html("");
					if (resultLength > 0) {
						
						var position = $(searchBox).position();
						var height = $(searchBox).outerHeight();
						var positionTop = position.top + height - 1;
						var positionLeft = position.left; 
						$(resultsDiv).css({"top":positionTop,"left":positionLeft});
						$(resultsDiv).show();
//							$("#search_results_div").append("<div id='line_0' class='line'><a href='/index.php?module=adv_search_results&keyword="+searchTerm+"'><span class='search_term'>"+searchTerm+"</span></a></div>");
						for (var i = 0; i < resultLength; i++) {
							var resultStr = resultArray[i][0].toLowerCase();
						
							var lenLeft = resultStr.search(searchTerm.toLowerCase());
							var posMiddle = lenLeft;
							var lenMiddle = searchTerm.length;
							var posRight = lenLeft + lenMiddle;
							var lenRight = resultStr.length - posRight;
							var resultLeft = resultArray[i][0].substr(0,lenLeft);
							var resultMiddle = resultArray[i][0].substr(lenLeft,lenMiddle);
							var resultRight = resultArray[i][0].substr(posRight,lenRight);
							resultLeft = "<span class='result_left search_term'>"+resultLeft+"</span>";
							resultMiddle = "<span class='result_middle search_term_enh'>"+resultMiddle+"</span>";
							resultRight = "<span class='result_right search_term'>"+resultRight+"</span>";
							
							$(resultsDiv).append("<div id='line_"+(i)+"' class='line'><a href='/index.php?module=adv_search_results&keyword="+resultArray[i][0]+"'>"+resultLeft+resultMiddle+resultRight+"</a></div>");

							$(resultsDiv+" div#line_"+(i)+" a").hover(
								function() { //mouse over
									$(".line a").removeClass();
									$(this).addClass("highlighted");
								},
								function() { //mouse out
								}
							); //end hover
						} //end for
						

					} else {
						$(resultsDiv).html("").hide(); //if there are no matching results hide the div
					}
				} //end function(data)
			); //end get
} // end getSuggestResults


function redirect(jumplocation) {
	self.location=jumplocation;
}

function refreshVendor() {
	$("#vend_id").change(function(event){
		var _URL_HTTP = "http://"+document.domain;
		var vendorId = $("#vend_id").val();
		redirect(_URL_HTTP + "/index.php?module=vendor&vend_id=" + vendorId);
	});
}



