// JavaScript Document

function searchDates(p_city, p_date)
{
	$(".action-box").hide();
	var treffer = 0;
	
	for (i=0; i<p_city.length; i++)
	{
		var city = p_city[i];										
		var date = p_date;
	
		if (city=="sonstige")
		{			
			$(".action-box").show();
		
			$(".action-box").each(function(){
				var cities = new Array("Ahaus", "Gescher", "Heek", "Legden", "Stadtlohn", "Südlohn", "Vreden");
				var curr_city = $(this).find(".city").html();
				curr_city = curr_city.replace(" ", "");
				
				for (t=0; t<cities.length; t++)
				{
					//alert(curr_city+" : "+cities[t]);
					if (curr_city == cities[t])
					{
						$(this).hide();	
					}
				}
			});
		} else {
			city = city.replace(" ", "").replace("ue", "ü");
			treffer = 0;
			message = 0;
			$(".treffer").remove();
			//alert(city);
			//$(".action-box:hidden").each(function(){
			//	$(this).show();					   
			//});	
		
			if (city!="" && date=="") {
				$(".action-box").each(function(){
					var curr_city = $(this).find(".city").html();
					curr_city = curr_city.replace(" ", "");
					
					if (curr_city!=city)
					{
						//$(this).fadeOut("def");
					} else {
						$(this).show();
						treffer = treffer + 1;
					}
				});
			} else if (date!="" && city=="") {
				date_arr = date.split(".");		
				date = new Date();
				date.setFullYear(date_arr[2], date_arr[1], date_arr[0]);
				
				$(".action-box").each(function(){
					var curr_date = $(this).find(".starttime").html();
					curr_date_arr = curr_date.split(".");
					curr_date = new Date();
					curr_date.setFullYear(curr_date_arr[2], curr_date_arr[1], curr_date_arr[0]);
					
					if (curr_date>=date)
					{
						$(this).show();
						treffer = treffer + 1;
					} else {
						//$(this).fadeOut("def");
					}
					
					if ($(this).find(".endtime").html().length>0)
					{
						var endtime_date = $(this).find(".endtime").html();
						endtime_date_arr = endtime_date.split(".");
						endtime_date = new Date();
						endtime_date.setFullYear(endtime_date_arr[2], endtime_date_arr[1], endtime_date_arr[0]);
						
						if (date<=endtime_date)
						{
							$(this).show();
							treffer = treffer + 1;
						}
					}
				});
			} else if (date!="" && city!="") {						
				date_arr = date.split(".");		
				date = new Date();
				date.setFullYear(date_arr[2], date_arr[1], date_arr[0]);
				
				$(".action-box").each(function(){
					var curr_date = $(this).find(".starttime").html();
					curr_date_arr = curr_date.split(".");
					curr_date = new Date();
					curr_date.setFullYear(curr_date_arr[2], curr_date_arr[1], curr_date_arr[0]);
					
					var curr_city = $(this).find(".city").html();
					curr_city = curr_city.replace(" ", "");
					
					if (curr_date>=date && curr_city==city)
					{
						$(this).show();
						treffer = treffer + 1;
					} else {
						//$(this).fadeOut("def");
					}
					
					if ($(this).find(".endtime").length>0)
					{
						var endtime_date = $(this).find(".endtime").html();
						endtime_date_arr = endtime_date.split(".");
						endtime_date = new Date();
						endtime_date.setFullYear(endtime_date_arr[2], endtime_date_arr[1], endtime_date_arr[0]);
						
						if (date<=endtime_date && curr_city==city)
						{
							$(this).show();
							treffer = treffer + 1;
						}
					}
				});
			} else {
				message=1;
			}
		
		}
	
	}
	
	if (treffer>0)
	{
		$(".action-box:first").before('<div class="treffer">Treffer: '+treffer+'</div>');
	} /*else if (message==0){
		$(".action-box:first").before('<div class="treffer">Ihre Suche ergab keinen Treffer.</div>');
	}*/
}

$(document).ready(function(){
	var old_input_text = "";
	$("#header .input-text").focus(function(){
		old_input_text = $(this).val();
		$(this).val("");
	});				  
/*	
	$("#header .input-text").blur(function(){
		$(this).val(old_input_text);									   
	});
*/	
	$("#dropdown > ul > li").hover(function(){
		$(this).find(".level2").show();										
	},function(){
		$(this).find(".level2").hide();	
	});
		
	/*$("#dropdown > ul > li").hover(function(){	
		$(this).find("ul").not('.opened').slideDown(500, function(){
			$("#dropdown > ul > li").find(".opened").stop(true).hide();
			$("#dropdown > ul > li").find(".opened").removeClass('opened');
			$(this).addClass('opened');										   
		});										
	}, function(){
		$("#dropdown > ul > li").find(".opened").stop(true).hide();
		$("#dropdown > ul > li").find(".opened").removeClass('opened');
	});
	
	
	$("#header").bind("mouseleave", function(){
		$("#dropdown > ul > li").find(".opened").stop(true).hide();
		$("#dropdown > ul > li").find(".opened").removeClass('opened');							 
	});*/
	
	var get_vars = document.location.search;
	get_vars = get_vars.split("&");
	get_date = get_vars.pop();
	get_city = get_vars.pop();
	
	if (get_date!=undefined && get_city!=undefined)
	{
		get_date = get_date.split("=");
		get_city = get_city.split("=");
		var city = new Array();
		
		switch(get_city[1])
		{
			default: city = new Array();
			case "48683": city[0] = "Ahaus"; break;
			case "48712": city[0] = "Gescher"; break;
			case "48619": city[0] = "Heek"; break;
			case "48739": city[0] = "Legden"; break;
			case "48703": city[0] = "Stadtlohn"; break;
			case "46354": city[0] = "Suedlohn"; break;
			case "48691": city[0] = "Vreden"; break;
			case "Borken": city = new Array("Ahaus", "Gescher", "Heek", "Legden", "Stadtlohn", "Suedlohn", "Vreden"); break;
			case "sonstige": city[0] = "sonstige"; break;
		}
		searchDates(city, get_date[1]);
	}
	
	$(".action-box").each(function(){
		var counter = 0;
		$(this).find(".bodytext").not(":first").hide();
		$(this).find(".bodytext").each(function(i){
			counter = counter + i;
		});
		
		if (counter>0)
		{
			$(this).find(".title").addClass("link").click(function(){
				if (!$(this).hasClass("opened"))
				{
					$(this).addClass("opened");
					$(this).closest(".action-box").find(".bodytext").not(":visible").slideDown(500);														  				} else {
					$(this).removeClass("opened");
					$(this).closest(".action-box").find(".bodytext").not(":first").slideUp(500);	
				}
			});	
		}
	});
	
	/*$("#action-search").submit(function(){
		var city_select = $(this).find(".select-city").val();										
		var date = $(this).find(".text-date").val();
		
		var city = "";
		
		switch(city_select)
		{
			default: city = "";
			case "48683": city = Ahaus; break;
			case "48712": city = Gescher; break;
			case "48619": city = Heek; break;
			case "48739": city = Legden; break;
			case "48703": city = Stadtlohn; break;
			case "46354": city = Südlohn; break;
			case "48691": city = Vreden; break;
			case "Borken": city = Kreis Borken; break;
			case "Überregional": city = Überregional; break;
		}
		alert(city);
		
		searchDates(city, date);
									
		return false;									
	});*/
	
	$("#action-search .reset").click(function(){
		treffer = 0;
		$(".treffer").remove();
		$(".action-box:hidden").each(function(){
			$(this).fadeIn("def");					   
		});							   
	});
	
});