// =========================== GLOBAL JAVASCRIPT ===========================
// 	We maken gebruik van jQuery 1.3.2 via de Google AJAX Api.
// 	Onderstaande scripts worden op elke pagina gebruikt
// =========================================================================


// =========================== LOAD FUNCTIONS

// General extension to get URL variables
$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});


// Function to open specific searchpanel by URL variable
(function($){
  openSearchPanelByUrl = function() {
    
	var allVars = $.getUrlVars();
	var urlSearchPanel = $.getUrlVar('sp');

	// check if there is an anchor in the url
	var myFile = document.location.toString();
	var myAnchor = "empty";
	if (myFile.match('#')) { // the URL contains an anchor
		
		var anchorFromUrl = myFile.split('#')[1];
		if((anchorFromUrl == 'jq_searchcontent1') || (anchorFromUrl == 'jq_searchcontent2'))
		{
			myAnchor = "notempty";
		}
	}

	// only use this automatic function when nog anchor (#jq_searchcontent1 or #jq_searchcontent2) is added to the url
	if(myAnchor == 'empty')
	{
		if((urlSearchPanel == '1') || (urlSearchPanel == '2'))
			addActiveSearchElement(urlSearchPanel);
	}
	
	function addActiveSearchElement(urlSearchPanel){
		$("body").find('a#search_tab' + urlSearchPanel).removeClass("search_tab").addClass("search_tab_on");	
		$("body").find('div#jq_searchcontent' + urlSearchPanel).animate({height: 'toggle', opacity: 'toggle'}, "slow");
	}
	
	//	function removeActiveSearchElement(){
	//		$("body").find('a.search_tab_on').removeClass("search_tab_on").addClass("search_tab");
	//		$("body").find('div.search_tab_content:visible').animate({height: 'toggle', opacity: 'toggle'}, "slow");
	//	}
	
  };
})(jQuery);



$(window).load(function(){
						
	// function to check if you need to open a searchpanel
	openSearchPanelByUrl();
	
	// Open/close searchpanel and de/activate searchbutton onclick event
	$('a.search_tab,a.search_tab_on,a.close_search_tab').searchtabslide();
	
	// IE6 fix for hover state of searchbutton
	$('input#searchsubmit').buttonHover({
		class_normal: 'searchsubmit',
		class_hover: 'searchsubmit_ie6_on'
	});	
	
});






// SCRIPT FOR THE SEARCHFILTER DROPDOWNS ======================================================================== */
$(document).ready(function() {

	// Click functionality on the link in the top of dropdown - Toggles dropdown open/close
	$(".dropdown dt a").click(function() {
		var dropdown_id = $(this).parents(".dropdown").attr("id");
		
		$("dl.dropdown dd div").not($("#" + dropdown_id)).hide();				
		$("#" + dropdown_id + " dd div").toggle();
		
	});
					
	$(".dropdown dd ul li a").click(function() {
		
		// default dropdown title (needed in case you delete selected filter
		var default_title = $(this).parents(".dropdown").attr("alt");
		
		// Selected element				
		var a_text = $(this).html();
		var url_var_value = $(this).attr("href").replace("#", "");
		
		// Get variable name to use for this dropdown, to add to the submit url
		var url_var_name = $(this).parents(".dropdown").attr("title");
		//alert(url_var_name);
	
		// First we check if the visitor wanted to delete a filter
		var delete_filter = false;
		if(url_var_value == "delete")
			delete_filter = true;

		// Add selected value to dropdown title element
		if(delete_filter == true)
			$(this).parents(".dropdown").find("dt a").html(default_title);
		else
			$(this).parents(".dropdown").find("dt a").html(a_text);
			
			
		// Submit id
		var submit_element_id = $(this).parents(".dropdown").find("dt").attr("id"); 
		//alert(submit_element_id);
	
		// Get all existing variables from submit link
		var current_result = $("a#" + submit_element_id).attr("href");
		if (current_result == szDefaultSearchUrl)
			current_result = '';
		current_result = current_result.replace(szAdvertisementsUrl + '&', '');
		current_result = current_result.replace(szAdvertisementsUrl, '');
		var all_vars = current_result.replace("?", "").split('&');
		//alert(all_vars.length);
		//alert(all_vars);
	
	
		// Values to build link
		var buildlink = szAdvertisementsUrl + '&';
		var new_variable = true;
		
		// No variable have been added yet to the link
		if(all_vars == "")
		{
			// Only (re)add variable if the visitor did not select delete
			if(delete_filter == false)
				buildlink = buildlink + url_var_name + "=" +  url_var_value ;
		}
		else // Rebuild
		{
			for(var i = 0; i < all_vars.length; i++)
			{
				var var_total = all_vars[i].split('=');
				var var_name = var_total[0];
				var var_value = var_total[1];
				//alert(all_vars[i]);
				//alert(var_name);
				//alert(var_value);
				
				// If the url_var_name already exist then overricde existing value
				// alert(url_var_name + "=" + var_name);
				if(url_var_name == var_name)
				{
					new_variable = false;
					if(delete_filter == false)
						buildlink = buildlink + "&" + var_total[0] + "=" + url_var_value;
					
				}
				else // add the existing variable + value again to the link!!!!!
				{
					buildlink = buildlink + "&" + var_total[0] + "=" +  var_total[1];
				}
			}
			
			// If the variable does not already exist then add a new variable to the link
			if(new_variable == true)
			{
				//alert(url_var_name + "=" + var_name);
				//alert("new_variable = " + new_variable);

				if(delete_filter == false)
					buildlink = buildlink + "&" + url_var_name + "=" +  url_var_value;
			}
				
		}
		
		buildlink = buildlink.replace("?&", "?")
		buildlink = buildlink.replace("&&", "&")
		//alert(buildlink);

		// Add builded link to submit element
		var submit_element = $("a#" + submit_element_id);
		submit_element.attr("href", buildlink);
		
		// Add value as text to submit element for testing
		//submit_element.html(buildlink);
		
		// if submit url is empty then also remove the ? and replace it with #
		if(submit_element.html() == "?")
		{
			submit_element.attr("href","");
			submit_element.html("(LEEG)"); // testvalue
		}	
						 
		 // if you selected a dropdown item then show the delete item.
		 // if you selected the delete item then set the delete item on hidden
		if(url_var_value == "delete")
			$(this).parents(".dropdown").find("li.dropdown_delete").css("display","none");
		else
			$(this).parents(".dropdown").find("li.dropdown_delete").css("display","block");

		// Hide dropdown
		$(".dropdown dd div").hide();
		
		// End
		return false;
		
	});
				
	$(document).bind('click', function(e) {
		var $clicked = $(e.target);
		if (! $clicked.parents().hasClass("dropdown"))
			$(".dropdown dd div").hide();
	});
	

});
	
// END: SCRIPT FOR THE SEARCHFILTER DROPDOWNS ======================================================================== */
