
var menutimer = null;
var menuact = null;

function setHomeLocation()
{
	if(google.loader.ClientLocation.latitude!="" && google.loader.ClientLocation.longitude!="") {
		$.getJSON(URL + "searchzip_ct.php?lat=" + google.loader.ClientLocation.latitude + "&lon=" + google.loader.ClientLocation.longitude, function(fData, status) {
			location.href=URL + 'plz.php?setloc=' + fData.geo_id;
		});
	}
	else
	{
		alert("Leider kann Ihre Location nicht geortet werden.\nBitte w&auml;hlen Sie Ihre Stadt manuell aus");
	}
}

$(document).ready(function(){
	$("#head_navi > li > a").each(function(){

		$(this).mouseenter(function(){
			menu_highlight($(this).attr("id").split("_")[1]);
		});

		$(this).next().mouseenter(function(){
			menu_highlight($(this).attr("id").split("_")[1]);
		});

		$(this).mouseleave(function(){
			menu_timer($(this).attr("id").split("_")[1]);
		});

		$(this).next().mouseleave(function(){
			menu_timer($(this).attr("id").split("_")[1]);
		});
		
	});

	$("a").each(function(){$(this).click(function(){$(this).blur();})});
	
	$("#ac_city").autocomplete(URL + "searchzip_ct.php", {
		width: 690,
		delay: 100,
		max: 100,
		selectFirst: true,
		scrollHeight: 350,
		minChars: 0
	});

	$("#ac_city").result(function(event, data, formatted) {
		//var select = $(this);
		//window.setTimeout(function() {
		//	var data = select.selected();
			if (data) {
				var value = data[1];
				$("#ac_data").val(value);
				location.href=URL + 'plz.php?setloc=' + value;		
			}
		//}, 1000); 
	});
	
	
	$("#ac_button").click(function(){
		if($("#ac_data").val()!="") location.href= URL + 'plz.php?setloc=' + $("#ac_data").val();
		else
		{
			$("#ac_city").val("");
			$("#ac_city").focus();
			//$("#ac_city").click();
		}
	}).css("cursor", "pointer");
	
	
	
	$("#plzchange").click(function(){
		if($("#ac_data").val()!="") location.href= URL + 'plz.php?setloc=' + $("#ac_data").val();
		else
		{
			$("#ac_city").focus();
			$("#ac_city").click();
		}
	});
	
	
	$("#sign").click(function(){
		if($("#ac_data").val()!="") location.href= URL + 'plz.php?setloc=' + $("#ac_data").val();
		else
		{
			$("#ac_city").focus();
			$("#ac_city").click();
		}
	});
	
	$("#sign1").click(function(){
		if($("#ac_data").val()!="") location.href= URL + 'plz.php?setloc=' + $("#ac_data").val();
		else
		{
			$("#ac_city").focus();
			$("#ac_city").click();
		}
	});
	
/*	$("#ac_city").click(function(){
		if($("#ac_data").val()!="") {
			location.href= URL + 'plz.php?setloc=' + $("#ac_data").val();
		}
		else {
			var input = $(this);
			input.data('value', input.val());
			input.val('');
			window.setTimeout(function() {
				$("#ac_city").select();
			}, 200);
			//$("#ac_city").click();
		}
	});

*/
	$("#ac_city").focus(function() {
		if($("#ac_data").val()!="") {
			location.href= URL + 'plz.php?setloc=' + $("#ac_data").val();
		}
		else {
			var input = $(this);
			input.data('value', input.val());
			input.val('');
		}
	});

	$("#ac_city").blur(function() {
		var input = $(this);
		window.setTimeout(function() {
			if (!input.val()) {
				input.val(input.data('value'));
			}		
		}, 200);
	});

	var outtimer = null;
	
	$(".ac_results").live("mouseout", function(){outtimer = window.setTimeout("$('.ac_results').hide();$('#ac_city').blur();", 500)});
	$(".ac_results").live("mouseover", function(){window.clearTimeout(outtimer)});
	
	
	
	/*
	$("#ac_city").focus(function(){
		$("#ac_city").val("");
	});
	
	$("#ac_city").blur(function(){
		if($("#ac_city").val()=="") $("#ac_city").val(sWACityLong);
	});
	*/
	
});

function menu_highlight(mid)
{
	if(mid!=menuact && menuact!=null)
	{
		menu_downlight(menuact);
	}
	if(menutimer) clearTimeout(menutimer);
		
	if(document.getElementById("subnav_" + mid)) 
	{
		$("#nav_" + mid).attr("class", "a1hover");
		$("#subnav_" + mid).show();
	}
	else
	{
		$("#nav_" + mid).attr("class", "a1hoversingle");
	}
	menuact = mid;
}



function menu_downlight(mid)
{
	if(document.getElementById("subnav_" + mid)) 
	{
		$("#nav_" + mid).attr("class", "a1");
		$("#subnav_" + mid).hide();
	}
	else
	{
		$("#nav_" + mid).attr("class", "a1");
	}
	menuact = null;
}

function menu_timer(mid)
{
	menutimer = setTimeout(function(){
			menu_downlight(mid);
		},500);
}


function loadFilteredMarkers(oFilter)
{
	if(strpos(window.location, 'map.php'))
	{
		markerfilter = oFilter;
		clearMarkers();
		loadMarkers(map.getCenter(), map.getZoom(), map.getBounds());
	}
	else
	{
		location.href= URL + 'map.php?k=' + oFilter.key + '&v=' + oFilter.value;
	}
}


var menuHL = null;


function strpos (haystack, needle, offset) {
    var i = (haystack+'').indexOf(needle, (offset || 0));
    return i === -1 ? false : i;
}
