
var currentcategory = 0;

var slider_vals = new Array(1, 5, 10, 20, 50, 1000);



function showFormErrors(errorAr) {
	if(errorAr) {
		var html = '<ul>';
    if (errorAr.length > 0) {
      html = html + '<li>Bitte geben Sie ein, wonach Sie suchen.</li>';
    }
//		for (var i in errorAr) {
//      html = html + '<li>' + errorAr[i] + '</li>';
//		}
		html += '</ul>';
		
		$loginFail = $("<div title=\'Suche fehlgeschlagen\'></div>");
		$loginFail.html(html);
		$loginFail.appendTo('body');
		$loginFail.dialog({
			bgiframe: true,
			modal: true,
			width: 500,
			height: 'auto',
			resizable: false,
			buttons: {
				Ok: function() {
					$(this).remove();
				}
			}
		});
	}
}

 
function openCategory(show) { 
	if(!jQuery.dynatree) {
		return;
	}
	
	var curnode = $('#categoryitems').dynatree('getActiveNode');
	if(curnode && curnode.data.id == currentcategory) { 
		toggleCategories();
	} else {
		$('#categoryitems').dynatree('getTree').activateKey('key_'+currentcategory);
		
		if(show && !$("#searchtoggle").hasClass('opened')) {
			toggleCategories();
		}
	}
}

function toggleCategories() {
	if(!$("#searchtoggle").hasClass('opened')) {
		$("#searchtoggle").addClass('opened');
		$("#categorieslist").show('blind');
	} else {
		$("#searchtoggle").removeClass('opened');
		$("#categorieslist").hide('blind');
	}
}

function selectCategory(node) { 
	// TODO fix selection on index page
	if(!node) alert('please fix');
	if(node.data.selectable == 0) {
		node.expand(true);
		return false;
	}
	
	if(node.data.id > 0) {
		$("#what").val(node.data.title);
		$("#what").effect('highlight', {color: '#b42c21'}, 700);
		currentcategory = node.data.id;
	} else {
		$("#what").val('');
		currentcategory = 0;
		$("#what").blur();
	}
	$("#categoryid").val(node.data.id);
	$("#currentcategory").html(node.data.title);
	
	if(!node.hasChildren()) {
		toggleCategories();
	} else {
		node.expand(true);
	}
	
}


$(document).ready(function() {
	$("#what").change(function() {
		$("#categoryid").val('0');
		$("#currentcategory").html('Alle Fachbereiche');
		currentcategory = 0;
		
		if($("#searchtoggle").hasClass('opened')) {
			$("#searchtoggle").removeClass('opened');
			$("#categorieslist").hide('blind');
		}
	});

	
	
	
	
	$("#searchform").bind('submit', function(e) {
		var what = $("#what").val();
		var where = $("#where").val();
		
		errors = new Array();
		
		if(what == 'Was?' || what.length < 3) {
			errors[0] = "Bitte geben Sie ein, wonach Sie suchen.";
		}
		
		$("#searchform > input").each(function() {
			$(this).triggerHandler('focus');
		});
		
		if(errors.length > 0) {
			showFormErrors(errors);
			e.preventDefault();
		} else {
			$(".searchloader")
				.css("visibility", "visible")
				.animate({opacity: 1}, 300, "linear")
			;
		}
	});
});

