$(function() {
	var self = this;
	self.input = $("#search").select().focus();
	
    //handles searching the document
	self.performSearch = function() {

    // remove the highlights from a previous search
    $("span.highlight").removeClass("highlight");
    
    // normally all ul are hidden, we show them for the search
    show_all_leistungen();
    
		//create a search string
		var phrase = self.input.val().replace(/^\s+|\s+$/g, "");					
		phrase = phrase.replace(/\s+/g, "|");
		
		//make sure there are a couple letters
		if (phrase.length < 3) { return; }			
		
		//append the rest of the expression
		phrase = ["\\b(", phrase, ")"].join("");
		
		//search for any matches
		var count = 0;
		$(".post h2, .post li").each(function(i, v) {
		
			//replace any matches
			var block = $(v);
			block.html(
				block.html().replace(
					new RegExp(phrase, "gi"), 
					function(match) {
						count++;
						return ["<span class='highlight'>", match, "</span>"].join("");
					}));
			
		});
		
		//update the count
		$(".result-count").text(count + " results on this page!");
		
		//clear this search attempt
		//should be gone anyways...
		self.search = null;
	
	};

	self.search;
	self.input.keyup(function(e) {
		if (self.search) { clearTimeout(self.search); }
		
		//start a timer to perform the search. On browsers like
		//Chrome, Javascript works fine -- other less performant
		//browsers like IE6 have a hard time doing this
		self.search = setTimeout(self.performSearch, 300);
		
	});
	
	// go over all ul and mark them with an id
	self.ul_counter = 1;
	$(".post ul").each(function(index, element){
	  element = $(element);
	  
	  var parentTag = element.parent().get(0).tagName;
    
    // only take top level ul
	  if (parentTag != 'LI') {
	    element.attr('id', 'ul_' + self.ul_counter);
	    self.ul_counter++;
	  };
	});
	
	// go over all h2 and attach the toggle event for that id
	self.h2_counter = 1;
	$(".post h2").each(function(index, element){
	  element = $(element);
	  
	  var local_id = "#ul_" + self.h2_counter;
	  
	  element.click(function(){
	    $(local_id + ' ul').toggle();
	    $(local_id).toggle();
	  });
	  
	  self.h2_counter++;
	});
	
	if ($("#header").text() == 'Leistungen') {
	  // initially hide all leistungen
	  hide_all_leistungen();
	  
	  // make all h2 clickable
	  $('.post h2').addClass('clickable');
	};

	$('#wir_koennen').click(function(){
			url = "http://www.wir-können-steuern.de";
			$(location).attr('href',url);
		}
	);
  
});

function hide_all_leistungen(){
  $(".post ul").hide();
}

function show_all_leistungen(){
  $(".post ul").show();
}

function toggle_kanzlei(div_to_show){
	// hide all
	$(".kanzlei").hide();
	$("#"+div_to_show).show();
}

function toggle_anfahrt(div_to_show){
	// hide all
	$(".anfahrt").hide();
	$("#"+div_to_show).show();

	if (div_to_show == 'routenplaner') {
	  $('google_iframe').src = $('google_iframe').src;
	};
}

/*
  TODO modify script to only fire when search bar is present
  TODO get jquery from google cdn
  INFO http://hugoware.net/blog/more-jquery-magic-search-highlighting/highlight.html
  INFO http://hugoware.net/blog/more-jquery-magic-search-highlighting
  INFO http://lomalogue.com/jquery/quicksearch/
*/
