$(document).ready(function() {
	

	// *
	// *
	// * ONLOAD
	// *
	// *

	// BEIM LADEN DER SEITE FUER GECHECKTE HAUPTKATEGORIEN DEREN UNTERKATEGORIEN NACHLADEN UND DIE KLASSE AUF ACTIVE WECHSELN
	$('div#kategorien input:checkbox:checked').each(function(){
		katreload ($(this).attr('id').substr(4), true);
		$(this).siblings('a').attr('class', 'active');
	});
	$('div#gebiete input:checkbox:checked').each(function(){
		reload($(this).attr('id').substr(3), true);
		$(this).siblings('a').attr('class', 'active');
	});

	// umbruch für gebietsauswahl
	$('div#gebiete>ul>li:nth-child(4n)').css('clear', 'both');




	// *
	// *
	// * KLICK EVENTS
	// *
	// *

	// wenn eine checkbox für kategorie oder unterkategore geklickt wird, orte updaten und klasse wechseln
	$('div#kategorien input:checkbox').live( 'click', function() {
		updateOrtCount();
		getAllCount();
		if ($(this).is(':checked')) {
			$(this).siblings('a').attr('class','active');
		}
		else {
			$(this).siblings('a').removeAttr('class');
		}
	});

	// wenn eine checkbox für überkategorie geklickt wird, unterkategorien ein-oder ausklappen
	$('div#kategorien>ul>li>input:checkbox').click( function() {
		if ($(this).is(':not(:checked)')) {
			$(this).parent().children('div.katauswahl').html('');
		}
		else {
			katreload ($(this).attr('id').substr(4), false);
		}

	});

	// klick auf links n kategorien und unterkategorien ==> checkbox ändern, klasse ändern, orte und allcount updaten
	$('div#kategorien li a').live('click', function(){
		var myinput = $(this).siblings('input:checkbox:first');
		if (myinput.is(':checked')) {
			myinput.removeAttr('checked');
			$(this).siblings('div.katauswahl').html('');
			$(this).removeAttr('class');
		}
		else {
			myinput.attr('checked', 'checked');
			katreload(myinput.attr('id').substr(4), false);
			$(this).attr('class', 'active');
		}
		updateOrtCount();
		getAllCount();
		return false;
	});
	
	// wenn eine checkbox für gebiete oder orte geklickt wird, klasse ändern
	$('div#gebiete input:checkbox').live( 'click', function() {
		getAllCount();
		if ($(this).is(':checked')) {
			$(this).siblings('a').attr('class','active');
		}
		else {
			$(this).siblings('a').removeAttr('class');
		}
	});

	// wenn eine checkbox für ein gebiet geklickt wird, orte ein- oder ausklappen
	$('div#gebiete>ul>li>input:checkbox').click( function() {
		if ($(this).is(':not(:checked)')) {
			$(this).parent().children('div.ortsauswahl').html('');
		}
		else {
			reload($(this).attr('id').substr(3), false);
		}
		
	});

	// klick auf links n kategorien und unterkategorien ==> checkbox ändern, klasse ändern, orte und allcount updaten
	$('div#gebiete li a').live('click', function(){
		var myinput = $(this).siblings('input:checkbox:first');
		if (myinput.is(':checked')) {
			myinput.removeAttr('checked');
			$(this).siblings('div.ortsauswahl').html('');
			$(this).removeAttr('class');
		}
		else {
			myinput.attr('checked', 'checked');
			reload(myinput.attr('id').substr(3), false);
			$(this).attr('class', 'active');
		}
		getAllCount();
		return false;
	});

	// count ändern wenn sonstige suchoptionen geklickt wurden
	$('div#searchoptions input:checkbox').live('click',function(){
		getAllCount();

	});

	// count ändern und werte prüfen beim ändern von texteingabefeldern
	$('div#searchoptions input:text').change(function(){
		var value = $(this).attr('value');
		var test = value.match(/^(\d+|\s*)$/g);
		if(!test) {
			alert('Bitte nur Zahlen angeben! Wert wird zurückgesetzt.');
			$(this).attr('value','');
		}
		getAllCount();
	});


	// *
	// *
	// * FUNKTIONEN
	// *
	// *


// allcount aktualisieren
function getAllCount () {
	var poststring = '';
	$('div#kategorien>ul>li>input:checkbox:checked').each(function() {
		var temp = $(this).attr('id').substr(4);
		poststring += '&category['+temp+']='+temp;
		$(this).parent().find('div.katauswahl ul li input:checked').each(function(){
			var value = $(this).attr('value');
			value = escape(value);
			poststring += '&category['+temp+'][]='+value;
		});
	});

	//Alle angeklickten Gebiete und Orte zum Array machen
	$('div#gebiete>ul>li>input:checkbox:checked').each(function() {
		var temp = $(this).attr('id').substr(3);
		poststring += '&gebiete['+temp+']='+$(this).attr('value');
		$(this).parent().find('div.ortsauswahl ul li input:checked').each(function(){
			poststring += '&gebiete['+temp+'][]='+$(this).parent().attr('id').substr(3);
		});
	});

	$('div#searchoptions input:checkbox:checked').each(function() {
		var temp = $(this).attr('name');
		poststring += '&'+temp+'=checked';
	});
	$('div#searchoptions input:text').each(function() {
		var temp = $(this).attr('name');
		var text = $(this).attr('value');
		poststring += '&'+temp+'='+text;
	});


	$.post('ajaxfunctions.php', '&function=getAllCount'+poststring, function(data){
		$('span#allcount').html(data+" Ergebnisse gefunden");
	},'json');
}


// Unterkategorien für jeweiliges gebiet aktualisieren
function katreload(katstring, cookieload) {
	var mykats = '';
	$('div#kategorien input:checkbox:checked').each(function() {
		mykats += "&mykategorien[]=" + $(this).attr('id').substr(4);
	});
	$.post('ajaxfunctions.php', '&function=updateKategorien&checkedKategorie='+katstring+mykats+'', function(data){
		var kattext = '';
		kattext += '<ul class="katauswahl-listen" id="katauswahl_'+katstring+'">';
		//var_dump(data, 6);
		for (index in data['subchecked']) {
			kattext += '<li><input type="checkbox" name="category['+katstring+'][]" value="'+data['subchecked'][index][0]+'" /><a href="#">'+data['subchecked'][index][0]+' ['+data['subchecked'][index][1]+']'+'</a></li>';
		}
		kattext += '</ul>';
		$('div#kategorien ul li#cat'+katstring+' div.katauswahl').html(kattext);
		if (cookieload == true) {
			getCookieValues();
		}
	},'json');
}

// Orte für jeweiliges Gebiet ein- und ausklappen
function reload(gebiete, cookieload) {

	var gebietstring = '';
	gebietstring += "&mygebiete[]=" + gebiete;
	
	if (gebietstring != '') {
		var typestring = getTypeString();
		$.post('ajaxfunctions.php', '&function=updateOrte'+gebietstring+typestring+'', function(data){
			var orttext = '';
			$('div #ortsauswahl').html(orttext);
			for (index in data) {
				orttext += '<ul class="ortsauswahl-listen" id="ortswahl_gebiet'+data[index][0]+'">';
				for (ort in data[index][1]) {
					orttext += '<li id="ort'+data[index][1][ort].uid+'"><input type="checkbox" name="gebiete['+data[index][0]+'][]" value="'+data[index][1][ort].uid+'" /><a href="#">'+data[index][1][ort].Name+' [<span class="count">'+data[index][1][ort].ortcount+'</span>]</a></li>';
				}
				orttext += '</ul>';
				$('div#gebiete ul li#geb'+data[index][0]+' div.ortsauswahl').html(orttext);
				orttext = '';
			}
			if(cookieload==true) {
				getCookieValues();
			}
		},'json');
	}
	
}

// counts aller orte und gebiete aktualisieren
function updateOrtCount() {
	var typestring = getTypeString();
	$.post('ajaxfunctions.php', '&function=updateOrtCount'+typestring+'', function(data){
		$('span.count').each(function(){
			var id = $(this).parent().parent().attr('id');
			$(this).html(data[id]);
		});
	}, 'json');
}

// alle angeklickten Kategorien und Unterkategorien in einen für php verständlichen string zusammenfassen
function getTypeString() {
	var typestring = '';
	$('div#kategorien > ul > li > input:checked').each(function(){
		var test = $(this).attr('id').substr(4);
		typestring += '&categories['+test+']=1';
		$(this).parent().find('div.katauswahl ul li input:checked').each(function(){
			var value = $(this).attr('value');
			value = escape(value);
			typestring += '&categories['+test+']['+value+']=1';
		});
	});
	return typestring;
}

// Inhalt von cookies verarbeiten
function getCookieValues() {

	var cookie = $.cookie('subvalues');
	cookie = cookie.replace(/\+/g, ' ');
	var array = cookie.split('#');
	for (value in array) {

		$('input:checkbox[value="'+array[value]+'"]').each(function(){
			$(this).attr('checked', 'checked');
			$(this).siblings('a:first').attr('class','active');
		});

	}
	updateOrtCount();
	//$('input:checkbox[value="Sonstiges"]').siblings('a').css('background','red');
}

function var_dump(element, limit, depth)
{
	depth =	depth?depth:0;
	limit = limit?limit:1;

	returnString = '<ol>';

	for(property in element)
	{
		//Property domConfig isn't accessable
		if (property != 'domConfig')
		{
			returnString += '<li><strong>'+ property + '</strong> <small>(' + (typeof element[property]) +')</small>';

			if (typeof element[property] == 'number' || typeof element[property] == 'boolean')
				returnString += ' : <em>' + element[property] + '</em>';
			if (typeof element[property] == 'string' && element[property])
				returnString += ': <div style="background:#C9C9C9;border:1px solid black; overflow:auto;"><code>' +
									element[property].replace(/</g, '&amp;lt;').replace(/>/g, '&amp;gt;') + '</code></div>';

			if ((typeof element[property] == 'object') && (depth < limit))
				returnString += var_dump(element[property], limit, (depth + 1));

			returnString += '</li>';
		}
	}
	returnString += '</ol>';

	if(depth == 0)
	{
		winpop = window.open("", "","width=800,height=600,scrollbars,resizable");
		winpop.document.write('<pre>'+returnString+ '</pre>');
		winpop.document.close();
	}
	return returnString;
}


}); 

