jQuery().ready(function(){
   
   jQuery('#accordionform').accordion({
      header: 'div.head',
      autoHeight: false,
      clearStyle: true,
      alwaysOpen: false,
	  active: ".starter"
   });
   
   $(".desc").qtip({
		style: {
			'color': '#666666',
			'font': '11px/13px Arial, Helvetiva, sans-serif'
		},
		position: {
			target: 'mouse'
		},
		adjust: {
			x: -15,
			y: -15,
			mouse: true
		}
	});
   
   
   function set_backgrounds(o) {
      $('.box').each(function() {
         var catblock = $(this).find('.catblock');
         var amount_checked = $(this).find('input:checked').length;
         if(amount_checked >= 1) {
            $(catblock).removeClass();
            $(catblock).addClass('catblock grau');
         }
         else {
            $(catblock).removeClass();
            $(catblock).addClass('catblock weiss');
         }
      });
      if(o != undefined) {
         if($(o).hasClass('bluelink')) o = $('#accordionform').find('.selected').find('.catblock');
         $(o).removeClass();
         $(o).addClass('catblock blau');
      }
   }
   
   function check_selected(o) {
      var li_box = $(this).parent().parent().parent().parent();
      if($(o).hasClass('box')) {li_box = o}
	  //if(!$(li_box).hasClass('laender')) alert("Nein");
      var punkte = $(li_box).find('.punkte');
      var parentdiv = $(li_box).find('.inputlist');
      var amount_checked = $(parentdiv).find('input:checked').length;
      if(amount_checked >= 1) {
	     if(!$(li_box).hasClass('laender')) {
			$(punkte).html(" <span style=\"color: #000000\">&ndash;</span> " + amount_checked + " Punkt(e) ausgewählt");
		 }
         $(punkte).css("display","inline");
      } else {
         $(punkte).css("display","none");
      }
   }
   
   function set_backgroundsRes(o) {
      $('.boxres').each(function() {
         var catblock = $(this).find('.catblockres');
         $(catblock).removeClass().addClass('catblockres weiss');
      });
      $(o).removeClass().addClass('catblockres blau');
   }
   
   var evt = $.browser.msie ? "click" : "change";
   $('.box input').unbind().bind(evt, check_selected);			
   $('.catblock').click(function() { 
      set_backgrounds(this);
   });
   $('.bluelink').click(function() { 
      set_backgrounds(this);
   });
   $('.box').each(function() {
      check_selected($(this));
   });
   set_backgrounds($('.catblock:first'));			
   $('.catblockres').click(function() { 
      set_backgroundsRes(this);
   });
   
   $('#searchlink').bind('click', function() {
		$('#searchlink').text('> Suche auswerten - lädt...');

		var pflicht = false;
		if($("li.box input[@type='checkbox'][@name='bundesweit']").attr('checked')) {
			pflicht = true;
		}
		else {
			$("li.laender input[@type='checkbox'][@name='laender[]']").each(function() {
				if($(this).attr('checked')) {
					pflicht = true;
				}
			});
		}
		
		if(!pflicht) {
			$('#searchlink').text('Suche auswerten');
			$('#fail').text('Bitte wählen Sie ein Bundesland oder "Bundesweit" aus.');

			set_backgrounds($('.starter .catblock'));
			if (!$('.starter').hasClass('selected')) {
				$("#accordionform").accordion("activate", 0);
			}
		}
		else {
			//Pflicht erfüllt (mindestens ein Land angeklickt)
			$("#accordionform").accordion("activate", false);
			$('#fail').text(' ');
			$("form[name='formular']").submit();
			return false;
	  }
	  
	  function preselectLand(land_id) {
		$("input[@type='checkbox'][@name='facts[]'][@value="+land_id+"]").attr("checked",true);
		check_selected(this);
	  }
	  
   });
   
});

function openAccContent(n) {
   $("#accordionform").accordion("activate", n)
}

function setLaender(all) {
   if(all.checked) {
      for(var i=1; i<=16; i++) {
		$("input[@type='checkbox'][@name='facts[]'][@value="+i+"]").attr("disabled", true);
		$("input[@type='checkbox'][@name='facts[]'][@value="+i+"]").attr("checked", false);
	  }
   }
   else {
      for(var i=1; i<=16; i++) {
		$("input[@type='checkbox'][@name='facts[]'][@value="+i+"]").attr("disabled", false);
	  }
   }
}

