$(function() {
    $(".streekkeuze li").each(function() {
        $(this).children("a[@href='#']").click(function() {
            $(this).parent().addClass("mutating");
            var isOpen = $(this).parent().attr("class") == "open mutating";
            $(".streekkeuze li.open").each(function() {
                 if($(this).find(".mutating").length == 0) {
                    slideUp($(this));
                }
         });
         if(!isOpen) {
           slideDown($(this).parent());
		   if($(this).hasClass("setRegion")){
			  setRegion($(this).html()); 
		   }
		   if($(this).hasClass("setDistrict")){
			  setDistrict($(this).html()); 
		   }		   
         }
         $(this).parent().removeClass("mutating");
     });
     if($(this).find(".active").length == 0) {
        $(this).children("ul").hide();
     } else {
        $(this).addClass("open");
     }
});

     $("#artikel").autocomplete("/ajaxsearch.php", {
     		width: 206,
     		selectFirst: false
     });
     $("#artikel").result(function(event, data, formatted) {
     		if (data)
     			$(this).parent().next().find("input").val(data[1]);
	});


});

function slideUp(element) {
    element.removeClass("open");
    element.children("ul").slideUp("slow");
}

function slideDown(element) {
     element.addClass("open");
     element.children("ul").slideDown("slow");
}

function url(input){
    return input;
}

function setRegion(str){
	var obj = document.getElementById("winemap");
	if (obj && typeof obj.setRegio != "undefined") {
		obj.setRegio(str);
	}
}

function setDistrict(str){
	var obj = document.getElementById("winemap");
	if (obj && typeof obj.setDistrict != "undefined") {
		obj.setDistrict(str);
	}
}