var UTF8 = {
 
	// public method for url encoding
	Encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// public method for url decoding
	Decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
}

function ClearLastSearches()
{
    try 
    {
        if (typeof jQuery !== 'function') 
        {
            setTimeout(ClearLastSearches, 100);
            return;
        }
        document.cookie = "MyLocations=; path=/";
        $("#DropDownLastAddresses ul li").remove();
        $("#DropDownLastAddresses ul").append("<li> - </li>");
    } 
    catch (e) 
    {
    }
}

function AttachLastSearches()
{
    if (typeof jQuery !== 'function') { setTimeout(AttachLastSearches, 100); return; }
    try 
    {
        var Where = jQuery("#SearchBox_WhereInput");
        var Addresses = [];
        
        var MyLocation = document.cookie.split(';');
        MyLocationStr = "";
        for (var i = 0; i < MyLocation.length; i++)
        {
            if (MyLocation[i].toLowerCase().indexOf('mylocations') > -1)
            {
                MyLocationStr = MyLocation[i];
                break;
            }
        }

        var LastAddresses = unescape(MyLocationStr).replace(/^.*MyLocations=/g, '').split('&');

	    for (var i = 0; i < LastAddresses.length; i++)
	    {
	        var Parts = LastAddresses[i].split(';');
	        var Country = "";
	        var Zip = "";
	        var Town = "";
	        var Street = "";
	        for (var j = 0; j < Parts.length; j++)
	        {
	            try
	            {
	            var PartsParts = Parts[j].split('=');
	            for (var k = 0; k < PartsParts.length-1; k++)
	            {
	                switch(PartsParts[k])
	                {
	                    case "Country": Country = UTF8.Decode(PartsParts[k+1]); break;
	                    case "Zip":     Zip = UTF8.Decode(PartsParts[k+1]); break;
	                    case "Town":    Town = UTF8.Decode(PartsParts[k+1]); break;
	                    case "Street":  Street = UTF8.Decode(PartsParts[k+1].replace(/\+/ig, ' ')); break;
	                }
	            }
	            }
	            catch (e)
	            {
	                alert('Error: ' + e);
                }
	        }
	        Town = Town.replace(/\+/g, " ");
	        Street = Street.replace(/\+/g, " ");
		    Addresses.push(Country + '-' + Zip + ' ' + Town + (Street != "" ? ', ' + Street : ''));
	    }
    	
	    var Div = document.createElement("div");
	    Div.id = "DropDownLastAddresses";
	    Div.innerHTML = "<span>Bisherige Suchorte:</span>";
	    var Ul = document.createElement("ul");
	    for (var i = 0; i < Addresses.length; i++)
	    {
	        Ul.innerHTML += "<li>" + Addresses[i] + "</li>";
	    }

	    Div.appendChild(Ul);
        if (Addresses.length > 0) 
        {
            var ClearSearches = document.createElement("p");
            $(ClearSearches).html("Liste der bisherigen Suchen l&ouml;schen").attr("id", "ClearLastSearches").click(ClearLastSearches);
            Div.appendChild(ClearSearches);
        } else
	    {
		    $(Ul).append("<li> - </li>");
	    }
    	
	    Div.className = "LastAddresses";
	    Div.style.display = "none";
	    Div.style.position = "absolute";
	    Div.style.left = Where.offset().left + "px";
	    Div.style.top = Where.offset().top + Where.outerHeight() + "px";
	    Div.style.width = Where.outerWidth() + "px";
	    Div.style.zIndex = 99999;
	    document.body.appendChild(Div);
    	    	
	    var AddressDiv = $(".LastAddresses");
	    var AddressUl = $(".LastAddresses ul");
    	
	    var Closed = true;
	    var Focused = false;
    	
	    function OpenClose()
	    {
	        if (Closed)
	        {
	            AddressDiv.slideDown(100, function()
	            {
	                Closed = false;
	                AddressUl.focus();
	                Focused = true;
	            });
	        }
	        else
	        {
			    AddressDiv.slideUp(100, function()
	            {
	                Closed = true;
	                Focused = false;
	            });
	        }
	    }
    	
    	
	    $("body").click(function(){ if (!Closed){ OpenClose(); }});
    	
	    var SelectedIndex = -1;
    	
	    function OnKeyPress(e)
	    {
	        if (!Focused) return;
	        var UP = 38;
	        var DOWN = 40;
	        var RETURN = 13;
    	    
	        e = e || window.event;
    	    
	        var Length = jQuery(".LastAddresses ul li").length;
    	    
	        if (e.keyCode == UP)
	        {
	            SelectedIndex = Math.max(SelectedIndex-1, 0);
	        }
	        else if (e.keyCode == DOWN)
	        {
	            SelectedIndex = Math.min(SelectedIndex+1, Length-1);
	        }
	        else if (e.keyCode == RETURN)
	        {
	            var SelectedValue = jQuery(jQuery(".LastAddresses ul li")[SelectedIndex]).html();
	            jQuery("#SearchBox_WhereInput").focus().val(SelectedValue);
	            OpenClose();
	        }
	        else
	        {
	            OpenClose();
	        }
    	    
	        for (var i = 0; i < Length; i++)
	        {
	            if (i == SelectedIndex) jQuery(jQuery(".LastAddresses ul li")[i]).addClass("selected");
	            else jQuery(jQuery(".LastAddresses ul li")[i]).removeClass("selected");
	        }
    	    
	        e.cancelBubble = true;
	        e.returnValue = false;
	        if (e.stopPropagation) e.stopPropagation();
	        if (e.preventDefault) e.preventDefault();
	        return false;
	    }
    	
	    if (document.body.attachEvent)
	    {
	        document.body.attachEvent("onkeyup", OnKeyPress);
	    }
	    else if (document.body.addEventListener)
	    {
	        $("html").keypress(OnKeyPress);
	    }
    	
	    $(".LastAddresses ul li").click(function(e)
	    {
	        e = e || window.event;
	        var Target = e.target || e.srcElement;
	        var SelectedValue = jQuery(Target).html();
	        jQuery("#SearchBox_WhereInput").focus().val(SelectedValue);
	        OpenClose();
	    });
    	
    	
	    $(".LastAddresses ul li").mouseover(function(e)
	    {
	        e = e || window.event;
	        var Target = e.target || e.srcElement;
	        jQuery(".LastAddresses ul li").removeClass("selected");
	        jQuery(Target).addClass("selected");
	    });
    	
	    $(".LastAddresses ul li").mouseout(function(e)
	    {
	        jQuery(".LastAddresses ul li").removeClass("selected");
	    });
	} 
	catch(Exc)
	{
	    var D = document.getElementById("DropDownLastAddresses");
	    if (D != null) D.parentNode.removeChild(D);
	    setTimeout(AttachLastSearches, 100); 
	}
}

$(document).ready(function(){
AttachLastSearches();
});

(function(){
    if (typeof Map !== 'undefined' && Map != null && typeof Map.GetMap == 'function' && Map.GetMap() != null)
    {
        Map.GetMap().AttachEvent("onchangemapstyle", function(e)
        {
            if (e.mapStyle === 'b')
            {
                Map.GetMap().ShowMiniMap(Map.GetMap().GetWidth()-195, -1, VEMiniMapSize.Large);
            }
            else
            {
                Map.GetMap().HideMiniMap();
            }
        });
    }
    else
    {
        setTimeout(arguments.callee, 100);
    }

})();