$(function(){

	// subnav width
	$('#nav ul ul').each(function(i){
		$(this).hide();
		mw = eval($(this).css('min-width').slice(0, -2));
		w = $(this).parent().width();
		if (w > mw) $(this).width(w);
	});
	
	$('#nav > ul > li').hover (
		function(){
			$(this).children('ul').fadeIn();
		},
		function(){
			$(this).children('ul').fadeOut();
		}
	);
	

	// float blocks in sections
	var m = 35;
	
	$('.section').each(function(i) {

		l = $(this).find('.block').length;
		pw = $(this).width();
		
		//console.log($(this));

		if (l > 1) {
			$(this).append('<div class="clear"></div>');
			$(this).find('.block').each(function(i2){
				mw = (l - 1) * m;
				w = Math.floor((pw - mw) / l);
				if (i2 > 0) $(this).css('margin-left',m + 'px');
				$(this).css({
					float: 'left',
					width: w + 'px'
				});
			});
		} 

	});

	// auto suggest
	var init_val = 'Name oder Branche';
	
	$('#finder #query').
	focus(function(){
		if ($(this).val() == init_val) $(this).val('');
	}).
	autocomplete('index.php', {
		width: 174,
		scrollHeight: 250,
		minChars:1,
		matchSubset:1,
		matchContains:1,
		cacheLength:10,
		formatItem:formatItem,
		selectOnly:1,
		extraParams: {
			id: 34,
			type: 98
		}
	});
	
	$('#finder #query').result(function(event, data, formatted) {
		$(this).parent().submit();
		return true;
	});
	
	$('#finder .send').click(function(){
		if ($('#finder #query').val() == init_val) {
			return false;
		} else {
			$(this).parent().submit();
		}
	})

	// finder hint
	$('#finder #hint').toggle(
		function(){
			$('#finder #desc').fadeIn();
		},
		function(){
			$('#finder #desc').fadeOut();
		}
	);

});

function formatItem(row) {
	//return row[0] + "<br><i>" + row[1] + "</i>";
	return row[0] + "<br /><i>" + row[1] + "<br />" + row[2] + "<br />" + row[3] + "</i>";
}
