var a_data_plz = new Array();

$(document).ready(function() {
	
	if($('#ort').val() === $('#ort').attr('title')) {
		$('#ort').css('color', '#aaaaaa');
	}
	
	if( $('#ort').val() === $('#ort').attr('title') || $('#ort').val() === '' || $('#ort').val().indexOf('Bundesland') !== -1) {
		$('#ps_proximity').attr('disabled', 'disabled');
		$('#qs_proximity').attr('disabled', 'disabled');
	}
	
	$('input[title]').each(function() {
	//$('#ort').each(function() {
		
		if($(this).val() === '') {
			$(this).val($(this).attr('title'));
			$(this).css('color', '#777777');
			
			$('#ps_proximity').attr('disabled', 'disabled');
			$('#qs_proximity').attr('disabled', 'disabled');
		}

		$(this).focus(function() {
			if($(this).val() === $(this).attr('title')) {
				$(this).val('');
				$(this).css('color', '#000000');
			}
		});

		$(this).blur(function() {
			if($(this).val() === '') {
				$(this).val($(this).attr('title'));
				$(this).css('color', '#777777');
				
				$('#ps_proximity').attr('disabled', 'disabled');
				$('#qs_proximity').attr('disabled', 'disabled');
			}
		});
	});
	
	$('#sort').change(function() {

        xajax_changeSort($('#sort').val(), request_array, site);

    });

	initAutocomplete();
	
	$('#ort').result(function(event, data){
		
		if(data[0].length > 3 && data[0].indexOf('Bundesland') === -1) {
			$('#ps_proximity').removeAttr('disabled');
			$('#qs_proximity').removeAttr('disabled');
		}
		else if(data[0].indexOf('Bundesland') !== -1) {
			$('#ps_proximity').attr('disabled', 'disabled');
			$('#qs_proximity').attr('disabled', 'disabled');
		}
	});
});

function initAutocomplete()
{
	$.ajax({ type: "POST",url: http_path+"Jquery/autocompletestart", 
			 dataType: "xml", async: false, error: function(){}, success: function(xml)
			 {
				i=0;
				$("item", xml).each(function(i) {
					val = $(this).text();
					a_data_plz[i] = val;
					i++;
				});
			}
	});
	
	fwidth=$("#ort").width();
	if(fwidth==0){
		fwidth=200;
	}
	
	if(checkBrowserName('firefox'))
	{
		factor = 5;
	}
	else 
		factor = 6;
	
	$("#ort").autocomplete(a_data_plz, {
		minChars: 1,
		autoFill: false,
		max: 10,
		delay: 10,
		mustMatch: false,
		multiple: false,
		matchContains: true,
		scrollHeight: 220,
		formatItem: formatItemForSearch,
		formatMatch: formatMatch,
		selectFirst: true,
		width: fwidth+factor,
		formatResult: formatResult,
		jobSearch: true
	});

}

