$(document).ready(function(){
	if ($(".accordion").length) {
		$(".accordion").accordion({ 
			clearStyle: true,
			collapsible: true,
			active: 1
		});
	} // if
	
	if ($(".sub_accordion").length) {
		$(".sub_accordion").accordion({ 
			clearStyle: true,
			collapsible: true,
			active: 1
		});
	} // if
});

var timer = null;
var current = 0;
var maxResults = 10;
var searchFor = "";

function KeyCheck(myfield, e) {
	
	var keycode;
	if (window.event) {
		keycode = window.event.keyCode;
	} else if (e) {
		keycode = e.which;
	} else {
		return true;
	} // if-else if-else
	if (keycode == 38) {
		if (current == 1) {
			current = maxResults;
		} else {
			current = (current - 1);
		} // if-else
		highLightElement(current, "up");
	} else if (keycode == 40) {	//	down
		if (current == maxResults) {
			current = 1;
		} else {
			current = (current + 1);
		} // if-else
		highLightElement(current, "down");
	} else if (keycode == 13) {
		$("#title").val($("#suggestionItem"+current+" a").text());
		$("#suggestions").fadeOut("slow");
	} else {
		if ($('#title').val().length > 0) {
			getSuggestions($('#title').val());
		} else {
			$("#suggestions").fadeOut("slow");
		} // if-else
	} // if-else
	
}//function

function KeyCheckMvAlm(myfield, e) {
	
	var keycode;
	if (window.event) {
		keycode = window.event.keyCode;
	} else if (e) {
		keycode = e.which;
	} else {
		return true;
	} // if-else if-else
	if (keycode == 38) {
		if (current == 1) {
			current = maxResults;
		} else {
			current = (current - 1);
		} // if-else
		highLightElement(current, "up");
	} else if (keycode == 40) {	//	down
		if (current == maxResults) {
			current = 1;
		} else {
			current = (current + 1);
		} // if-else
		highLightElement(current, "down");
	} else if (keycode == 13) {
		$("#title").val($("#suggestionItem"+current+" a").text());
		$("#suggestions").fadeOut("slow");
	} else {
		if ($('#title').val().length > 0) {
			getSuggestionsMvAlm($('#title').val());
		} else {
			$("#suggestions").fadeOut("slow");
		} // if-else
	} // if-else
	
}//function

function pickValue(value) {
	$("#title").val(value);
	$("#suggestions").fadeOut("slow");
} // function

function highLightElement(current, action) {
	$("#suggestions").find(".highLight").removeClass("highLight");
	$("#suggestionItem"+current).addClass("highLight");	
} // function

function getSuggestions(value) {		
		// Stop previous ajax-request
	if (timer) {
		clearTimeout(timer);
	} // if
	
	timer = setTimeout(function () {
		$.ajax({
			type: "POST",
			url: "http://www.pferdeurlaub.at/de/get_suggestions",
			data: "suche="+value,
			beforeSend: function(e) {
				$("#ajaxLoader").show();
			},
			success: function(e) {
				var responseText = jQuery.trim(e);
				$("#suggestions").fadeIn("slow").html(responseText);
				$("#ajaxLoader").hide();
				current = 0;
			},
			error: function(e) {
				$("#ajaxLoader").hide();
			}
		}); // ajax
		return false;
	}, 200);
		
} // function

function getSuggestionsMvAlm(value) {		
		// Stop previous ajax-request
	if (timer) {
		clearTimeout(timer);
	} // if
	
	timer = setTimeout(function () {
		$.ajax({
			type: "POST",
			url: "http://www.pferdeurlaub.at/de/get_suggestions_mv_alm",
			data: "suche="+value,
			beforeSend: function(e) {
				$("#ajaxLoader").show();
			},
			success: function(e) {
				var responseText = jQuery.trim(e);
				$("#suggestions").fadeIn("slow").html(responseText);
				$("#ajaxLoader").hide();
				current = 0;
			},
			error: function(e) {
				$("#ajaxLoader").hide();
			}
		}); // ajax
		return false;
	}, 200);
		
} // function

function clearInput(id) {
	
	if ($("#"+id+"_prefilledValue").length) {
		if ($("#"+id).val() == $("#"+id+"_prefilledValue").val()) {
			$("#"+id).val('');
		} // if
	} // if
	
	if ($("#"+id+"_errorMessage").length) {
		if ($("#"+id).val() == $("#"+id+"_errorMessage").val()) {
			$("#"+id).val('');
		} // if
	} // if
	
	if ($("#"+id+"_prefilledNotRequiredValue").length) {
		if ($("#"+id).val() == $("#"+id+"_prefilledNotRequiredValue").val()) {
			$("#"+id).val('');
		} // if
	} // if
	
} // function

function reloadSearchResult(id) {
	var formData = $('#reiterhofsuche').serialize();
	$.ajax({
		type: "POST",
		url: "/ajax-reiterhof-suchergebnis.html",
		data: formData,
		beforeSend: function(e) {
			$("#ajaxLoader").show();
		},
		success: function(e) {
			var responseText = jQuery.trim(e);
			$('#ajaxContent').html(responseText);
			$("#ajaxLoader").hide();
			return false;
			//$("#suggestions").fadeIn("slow").html(responseText);
			//current = 0;
		},
		error: function(e) {
			$("#ajaxLoader").hide();
			return false;
		}
	}); // ajax
	
} // function
