/**
 * @copyright	FAIRRANK deutschland GmbH, www.fairrank.de
 * @author		Armin Ruediger Vieweg [av]
 */

var maxSuggestions = 10;
var timerTmp = 0;

/* end of global vars */


$(document).ready(function(){
	// Construct JS-Elements
	$("#sucheform label").remove();
	$("#sucheform br").remove();
	$("#sucheform").append("<input type='hidden' id='radioC' name='art' value='Vorwahl'>");
	$("form div").css("display", "block");

	$("#eingabe1").attr("autocomplete", "off");
	$("<ul/>").attr("id", "resultwindow").insertAfter("#sucheform");


	for (var i = 1; i <= maxSuggestions; i++)
		li = $("<li/>").addClass("empty").appendTo("#resultwindow");


$("#tabVorwahl").click(function(){
	$(this).addClass("aktiv");
	$("#tabOrt").removeClass("aktiv");
	$("#radioC").val("Vorwahl");
//	$("#formexample").text("z.B. 069");
	$("#eingabe1").css("border-color", "#7F9DB9");
	$("#eingabe1").val("");
	$("#eingabe1").focus();
});

$("#tabOrt").click(function(){
	$(this).addClass("aktiv");
	$("#tabVorwahl").removeClass("aktiv");
	$("#radioC").val("Ort");
//	$("#formexample").text("z.B. Frankfurt am Main");
	$("#eingabe1").css("border-color", "#7F9DB9");
	$("#eingabe1").val("");
	$("#eingabe1").focus();
});



$("form#sucheform").submit(function(){
	return false;
});


$("#eingabe1").keyup(function(event){

    checkSuchfeldInit();

		if ($(this).val().charAt(0) == "0" && $("#radioC").val() == "Ort") {
			$("#tabVorwahl").addClass("aktiv");
			$("#tabOrt").removeClass("aktiv");
			$("#radioC").val("Vorwahl");
//			$("#formexample").text("z.B. 069");
			$("#eingabe1").css("border-color", "#7F9DB9");
		}
		else if ($(this).val().charAt(0) != "0" && $(this).val().charAt(0) != "" && $("#radioC").val() == "Vorwahl") {
			$("#tabOrt").addClass("aktiv");
			$("#tabVorwahl").removeClass("aktiv");
			$("#radioC").val("Ort");
//			$("#formexample").text("z.B. Frankfurt am Main");
			$("#eingabe1").css("border-color", "#7F9DB9");
		}

	switch(event.keyCode) {
		case 40: 	selectResultwindow("down");
					return false;

		case 38:	selectResultwindow("up");
					return false;

		case 13:	enterSuggest();
					return false;

		case 37||39:return false;
	}

	$("#resultwindow li").removeClass("selected");

	//Starte Timer für Vorschläge
	if ($(this).val().length > 1) {
		window.setTimeout("getSuggestions()", 600);
		timerTmp = timerTmp + 1;
	}
	else {
		$("ul#resultwindow").fadeOut(250);
		$("#resultwindow").removeClass("newData");
		//alert($("#minheight").height());
	}
});

$("#eingabe1").blur(function(){
	$("#resultwindow").fadeOut();
});


$("#resultwindow li").click(function(){enterSuggest();});
$("#resultwindow li").mouseover(function(){$("#resultwindow li").removeClass("selected"); $(this).addClass("selected");});
$("#resultwindow li").mouseout(function(){$(this).removeClass("selected");});


$("#senden1").click(function(){
	$("#eingabe1").val($.trim($("#eingabe1").val()));
	if ($("#eingabe1").val().length < 1) {
		$("#eingabe1").css("border-color", "#F00");
		$("#eingabe1").focus();
		return false;
	}

	if ($("#resultwindow").css("display") == "none") {
		window.location.href = "/suche/" + encodeURIComponent($("#eingabe1").val());
		//alert("Go");
	}
	return false;
});


});


function getSuggestions() {
	var b = 0;
	searchfield = $("#eingabe1");

	if($("#radioC").val() == "Vorwahl") {
		$("#resultwindow").addClass("priVorwahl");
	}
	else {
		$("#resultwindow").removeClass("priVorwahl");
	}



	if (timerTmp > 0) {
		timerTmp = timerTmp - 1;
		if (timerTmp > 0) { return false; }
	}

	if (searchfield.val().length > 1) {
a = $.ajax({
url:"/inc/suggest.php?q=" + encodeURIComponent(searchfield.val()),
dataType:"json",
async:false,
success: function(data){
				b = 1;
				$.each(data.items, function rofl(i, item){
					li = $("#resultwindow li:eq(" + i + ")").text(item.ort);
					span = $("<span/>").prependTo(li);
					span.text(item.vorwahl);
					li.removeClass("empty");
					li.removeClass("selected");

				});

				for (var i = data.items.length; i <= maxSuggestions - 1; i++) {
					// Remove rest
					li = $("#resultwindow li:eq(" + i + ")").text("");
					li.addClass("empty");
					li.removeClass("selected");
				}
		}
});
	}


	if(	b == 1) {
		$("ul#resultwindow").fadeIn(250);
	}
	else {
		$("ul#resultwindow").fadeOut(250);
	}

	$("#resultwindow").removeClass("newData");
}

function selectResultwindow(direction) {
	var a = 0;
	$("#resultwindow li").each(function(){
		if( $(this).hasClass("selected") ) {
			$(this).removeClass("selected");
			if(direction == "down") {
				$(this).next().addClass("selected");
			}
			else if (direction == "up") {
				$(this).prev().addClass("selected");
			}
			a = 1;
			return false;
		}
	});

	if (a == 0) {
		if (direction == "down") {
			$("#resultwindow li:first").addClass("selected");
		}
		else if (direction == "up") {
			var lastVisible = maxSuggestions - $("#resultwindow li.empty").length - 1;
			$("#resultwindow li:eq(" + lastVisible + ")").addClass("selected");
		}
	}
}

function enterSuggest(){

	if ($("#resultwindow").css("display") == "none") {
		$("#senden1").trigger("click");
	}
	else {
		var type = $("#radioC").val();

		if (type == "Vorwahl" && $("#resultwindow li.selected span").text() != "") {
			$("#eingabe1").val($("#resultwindow li.selected span").text());
		}
		else
			if (type == "Ort" && $("#resultwindow li.selected").text() != "") {
				ort = $("#resultwindow li.selected").text();
				ort = ort.replace(/[0-9]/gi, "");
				$("#eingabe1").val(ort);
			}

		$("#eingabe1").css("border-color", "#7F9DB9");
		$("#resultwindow").fadeOut();
		$("#eingabe1").focus();
	}


}
