
strDefaultQueryValue = 'typ hier uw zoekterm';


$(document).ready(function() {
		    
			function initAgendaForm()
			{
			/* show for js enabled */
				$("#js-agenda-form").show();

				//change label in select-value is value is set
				function setLabel(title,rel) {
					var value = $('a[rel='+rel+']').attr("title");
					$('.select-value[title='+title+']').children("span").text(value);
				}
			
				$('input[type=hidden]').each(
					function()
					{
						if(($(this).attr("value")) != "") 
						{
							var rel = $(this).attr("value");

							title = $('a[rel='+rel+']').attr("title");
							var selector = $(this).attr("name");

							try{
								if (title.length > 18) {
									title = title.substring(0, 15) + "...";
								}
								else {
									title = title;
								}
							}
							catch(o){}
							
							//bind 2 valuepair to the same control
							if(selector == "Regio")
								selector = "Locatie";
							
							$('.select-value[id='+selector+']').children("span").text(title);
						}
					}	
				);
										
			//set values onclick
			$("#js-agenda-form ul li a").click(function(event) {
				
				event.preventDefault();
			
				event.stopPropagation();
				var listItemWidth   = 160;
				var listItemPadding = 24;
				
				//show children lists
				if($(this).parent("li").hasClass("list-parent")) { 
					$(this).parents($(".dropdown-list")).children("li").children("ul").hide();
					$(this).parent("li").children("ul").show();
					
					//set style for collapsed
					$("#js-agenda-form ul li.active").removeClass("active");
					$(this).parent("li").addClass("active");
					
					if($(this).parent("li").children("ul").hasClass("double")) {
						$(this).parents($(".dropdown-list")).children("ul").css({"width": +(listItemWidth*2+listItemPadding*2)+"px"});
						
						
						$(this).parents($(".dropdown-list")).children("ul").children("li").children("a").css({"width": +(listItemWidth*2+listItemPadding)+"px"});
						$(this).parents($(".dropdown-list")).children("ul.double").children("li").children("a").css({"width": +(listItemWidth)+"px"});
						
						$(this).parents($(".dropdown-list")).children("ul").children("li.list-parent").children("a").css({"background-position": "0px -73px"});
						$(this).parents($(".dropdown-list")).children("ul").children("li.list-parent.active").children("a").css({"background-position": "0px -106px"});
					}
					else {
						$(this).parents($(".dropdown-list")).children("ul").children("li").children("a").css({"width": +listItemWidth+"px"});
						$(this).parents($(".dropdown-list")).children("ul").css({"width": +(listItemWidth+listItemPadding)+"px"});
						
						$(this).parents($(".dropdown-list")).children("ul").children("li.list-parent").children("a").css({"background-position": "0px -6px"});
						$(this).parents($(".dropdown-list")).children("ul").children("li.list-parent.active").children("a").css({"background-position": "0px -39px"});
					}
				}
				
				//set title of input as value and close
				else if($(this).parent("li").hasClass("clear-value")) {
					$(this).parents(".dropdown-list").children(".select-value").children("span").css({"font-weight": "bold"});
					$(this).parents(".dropdown-list").children(".select-value").children("span").text($(this).parents(".dropdown-list").children(".select-value").attr("title"));
					$(this).parents(".dropdown-list").children("ul").toggle();
					
					//fill hidden form field
					var newValue = "";
					
					var selectorReference = $(this).parents(".dropdown-list").children(".select-value").attr("id");
					$("input[@name='"+selectorReference+"']").val(newValue);
					// just make sute the location/region is reset. This is a 'spacial' control
					$("input[@name='Locatie']").val('');
					$("input[@name='Regio']").val('');
					
				}
				//set value input and close
				else { 
					$(this).parents(".dropdown-list").children(".select-value").children("span").css({"font-weight": "normal"});
					
					//shorten string if too long
					if (($(this).attr("title")).length > 18) {
						string = $(this).attr("title").substring(0, 18) + "...";
					}
					else {
						string = $(this).attr("title");
					}
					
					$(this).parents(".dropdown-list").children(".select-value").children("span").text(string); 
					$(this).parents(".dropdown-list").children("ul").toggle();
					
					//fill hidden form field
					var newValue = $(this).attr("rel");
					
					// check what for an id to use for form binding 
					if($(this).parent().parent().attr("id") == 'Regio' || $(this).parent().parent().attr("id") == 'Locatie'){
					
						var selectorReference = $(this).parent().parent().attr("id");
						
						if(selectorReference == 'Regio'){
							$("input[@name='Locatie']").val('');
						}
						else
							$("input[@name='Regio']").val('');						
					}
					else
						var selectorReference = $(this).parents(".dropdown-list").children(".select-value").attr("id");
					
					if (newValue != selectorReference) {     
					      $("input[@name='"+selectorReference+"']").val(newValue);
					}
				}

			});
			
			//toggle the list
			$("#js-agenda-form .select-value").click(function(event) {
				$(".dropdown-list").children("ul").hide();
				$(this).parent($(".dropdown-list")).children("ul").toggle();
				event.stopPropagation();
			});

			//hide dropdowns outside click
			$('html').click(function() {
				$(".dropdown-list").children("ul").hide();
			 });
			 
			 
			$(function() {
			/*
				Task	: Swap formfield
				Usage	: applies to all formfield with specific classname.
			*/
		
			swapValues = [];
			
			$(".search-query").each(function(i){
				swapValues[i] = $(this).val();
				$(this).focus(function(){
					$(this).css("color", "#3d3d3d");
					if ($(this).val() == swapValues[i]) {
						$(this).val("");
					}
					}).blur(function(){
						if ($.trim($(this).val()) == "") {
							
							if ($.trim($(this).val()) == strDefaultQueryValue ) 
								$(this).val(swapValues[i]);	
							else
								$(this).val(strDefaultQueryValue);	
							$(this).css("color", "#BBBBBB");
						}
					});
				});
			});
			
		
			//event teaser hover
			 $("div.event-teaser").hover(
				  function () {
					$(this).css("background-color", "#F8F8F8");
				  }, 
				  function () {
					$(this).css("background-color", "#FFFFFF");
				  }
				);
			
			}
						
			initAgendaForm();
			
			function matchDouble() {
			
				var listItemWidth   = 160;
				var listItemPadding = 24;
			
				$("ul.double.active").each(
					function() {
						$(this).parents($(".dropdown-list")).children("ul").css({"width": +(listItemWidth*2+listItemPadding*2)+"px"});
						
						$(this).parents($(".dropdown-list")).children("ul").children("li").children("a").css({"width": +(listItemWidth*2+listItemPadding)+"px"});
						$(this).parents($(".dropdown-list")).children("ul.double").children("li").children("a").css({"width": +(listItemWidth)+"px"});
						
						$(this).parents($(".dropdown-list")).children("ul").children("li.list-parent").children("a").css({"background-position": "0px -73px"});
						$(this).parents($(".dropdown-list")).children("ul").children("li.list-parent.active").children("a").css({"background-position": "0px -106px"});
						}
					);
				
			}
			
			matchDouble();
});

function doForm(){
	try{
		if($(".search-query").val() == strDefaultQueryValue)
			$(".search-query").val("");
	
		document.atsearch.submit()
	}
	catch(o){alert(o)}
}
