/*
	This is the JavaScript file for the AJAX Suggest Tutorial

	You may use this code in your own projects as long as this 
	copyright is left	in place.  All code is provided AS-IS.
	This code is distributed in the hope that it will be useful,
 	but WITHOUT ANY WARRANTY; without even the implied warranty of
 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
	
	For the rest of the code visit http://www.DynamicAJAX.com
	
	Copyright 2006 Ryan Smith / 345 Technical / 345 Group.	

*/
//Gets the browser specific XmlHttpRequest Object

function stripTags (string)
{
	var matchTag = /<(?:.|\s)*?>/g;
	return string.replace (matchTag, "");
}

$(document).ready (function () {
	var mouseOverSuggestBox = false;
	
	$("#suggestBox").mouseenter (function () {
		mouseOverSuggestBox = true;
	}).mouseleave (function () {
		mouseOverSuggestBox = false;
	});

	$("#searchword").blur (function () {
		if (this.value=='')
			this.value='Suche...';
		
		if (!mouseOverSuggestBox)
			$('#suggestBox').fadeOut ('fast');
	});
	
	$("#searchword").focus (function () {
		if(this.value=='Suche...') {
			this.value='';
		} else {
			$('#suggestBox').fadeIn ('fast');
		}
	});

	var userSearchString;
	$("#searchword").keydown (function (e) {
		if (e.keyCode == 40 || e.keyCode == 38)
			e.preventDefault ();
	});
	
	$("#searchword").keyup (function (e) {
		e.preventDefault ();
		
		switch (e.keyCode) {
		case 40:
			// Pfeiltaste unten
			if (!$(".suggestlink").hasClass ("selected")) {
				$(".suggestlink:first").addClass ("selected");
			} else {
				$(".suggestlink.selected").removeClass ("selected").next ().addClass ("selected");
			}
			break;
		case 38:
			// Pfeiltaste oben
			if (!$(".suggestlink").hasClass ("selected")) {
				$(".suggestlink:last").addClass ("selected");
			} else {
				$(".suggestlink.selected").removeClass ("selected").prev ().addClass ("selected");
			}
			break;
		case 13:
			return;
		default:
			getSuggestion ();
			return;
		}
		
		if ($(".suggestlink").hasClass ("selected")) {
			if (!userSearchString)
				userSearchString = $("#searchword").val ();

			$("#searchword").val (stripTags ($(".suggestlink.selected").html ()));
		} else {
			$("#searchword").val (userSearchString);
			userSearchString = "";
		}
		
		return false;
	});
});


var ajaxCurrentSuggestionRequest;

function getSuggestion () {
	if ($("#searchword").val ().length < 2) {
       	$("#suggestBox").fadeOut ("fast");
        return;
	}

	if (ajaxCurrentSuggestionRequest) {
		ajaxCurrentSuggestionRequest.abort ();
	}

	ajaxCurrentSuggestionRequest = 
		$.ajax ({
			url: "/v2/ajaxSearch.php",
			type: "GET",
			dataType: "json",
			data: $("#searchForm").serialize (),
			success: function (json) {
				if (!json || json.count == 0) {
					$("#suggestBox").fadeOut ();
					return;
				}
				
				var suggestions = 0;					
				var suggest = "";
				var regExp = new RegExp ("(" + json.query + ")", "ig");
				for (i = 0; i < json.count; i++) {
					var result = json.results [i].name;
					
					if (suggestions >= 5)
						break;
					
					if (result) {
						suggest += "<a href=\"#\" class=\"suggestlink\" onclick=\"setSearch (this); return false;\">";
						suggest += result.replace (regExp, "<strong>$1</strong>") + "<br />";
						suggest += "</a>";
						suggestions++;
					}
				}
			
				$("#suggestions").html (suggest);
				$("#suggestBox").fadeIn ();
			}
		});
}

function searchSubmit () {
	var searchString = $("#searchword").val ();
		
	//$("#searchword").val ("");
	$("#suggestBox").fadeOut ('fast', function () {
		$("#suggestions").html ("");
			
		window.self.location = "/v2/index.php/list/#s:" + escape ((encodeURI (searchString))) + "/cat:" + escape ($("#searchCat").val ());
	});
		
	return false;
}

//Click function
function setSearch (element) {
	$("#searchword").val (stripTags ($(element).html ()));
	
	$("#suggestBox").fadeOut ('fast', function () {
		$("#suggestions").html ("");
	});
	
	$("#searchForm").submit ();
}
