var zaehler = 0;

function buildButtons() {
	// help buttons
	$.each($('#anwalt-suche p.help'), function(key,obj) {
		$(obj).children('span.helptext').before('<span class="buttons"><a href="#" class="helpButton" tile="Hilfe">Hilfe</a></span>');
		$('a.helpButton').click(function() {
			$(this).parent().siblings('span.helptext').css('height','auto');
			$(this).parent().siblings('span.helptext').show(); return false;
		});
	});
	// help-close buttons
	$.each($('#anwalt-suche span.helptext'), function(key,obj) {
		$(obj).hide().append('<a href="#" class="close" title="Hilfe schließen">Hilfe schließen</a>');
		$('span.helptext a.close').click(function() {
			$(this).parent().siblings('span.helptext').css('height','0');
			$(this).parent().hide(); return false;
		});
	});
	// duplicate buttons
	$.each($('#anwalt-suche p.duplicatable'), function(key,obj) {
		if ($(obj).children('span.buttons').html()!=null)
			$(obj).children('span.buttons').append('<a href="#" class="duplicateButton" title="Feld duplizieren">Feld duplizieren</a>');
		else
			$(obj).append('<span class="buttons"><a href="#" class="duplicateButton" title="Feld duplizieren">Feld duplizieren</a></span>');
		$(obj).find('a.duplicateButton').click(function() {
			elem = $(this).parent().parent().clone(true);
			// count
			count = $('#anwalt-suche select.'+$(elem).find('select').attr('class')).length;
			if ($(elem).find('select').attr('class')=='fremdsprache' && count==3) return false;
			if ($(elem).find('select').attr('class')=='rechtsgebiet' && count==5) return false;
			if ($(elem).find('select').attr('class')=='fachanwaltschaft' && count==2) return false;
			if ($(elem).find('select').attr('class')=='weiterer-beruf' && count==6) return false;
			if ($(elem).find('select').attr('class')=='arge' && count==26) return false;
			if ($(elem).find('select').attr('class')=='recht-anderer-laender' && count==5) return false;
			// change ids, fors, ...
			zaehler = zaehler+1;
			$.each($(elem).find('select[id],label[for]'), function(key,obj) {
				if ($(obj).attr('id')) 	$(obj).attr('id',$(obj).attr('id')+zaehler);
				if ($(obj).attr('for')) {
					$(obj).attr('for',$(obj).attr('for')+zaehler);
					$(obj).html('und '+$(obj).html());
				}
			});
			$(this).parent().parent().after(elem);
			$(elem).find('span.buttons').html('<a href="#" class="deleteButton" title="Feld löschen">Feld löschen</a>');
			$(elem).find('a.deleteButton').click(function() {
				$(this).parent().parent().remove();
				return false;
			});
			return false;
		});
	});
}

function enableUmkreis() {
	if ($('#plz').val()!='')	$('#umkreis').attr('disabled',false);
	else						$('#umkreis').attr('disabled',true);
}

function disabledUmkreis() {
	if ($('#umkreis option:selected').val()=='')
		$('#umkreis').attr('disabled',true);
	$('#plz').keyup( function() { enableUmkreis(); });
	$('#plz').focus( function() { enableUmkreis(); });
	$('#plz').blur( function() { enableUmkreis(); });
	$('#asort').keyup( function() { enableUmkreis(); });
	$('#asort').focus( function() { enableUmkreis(); });
	$('#asort').blur( function() { enableUmkreis(); });
}

// call functions
$(document).ready(function() {
	buildButtons();
	disabledUmkreis();
	$('a.deleteButton').click(function() {
		$(this).parent().parent().remove();
		return false;
	});
	$("#asort").suggest("/anwalt-suche/ajax?land="+$('#land option:selected').val(), { });
});
