$(document).ready(function() {
	// Repopulate radios from uri
	$('#form_search input[name]:radio').each(function() {
		if ($(this).val() == site.parameters[$(this).attr('name')]) $(this).attr('checked', 'checked');
	});
	
	// Repopulate ref form
	$('#form_search_ref_riferimento').val(site.parameters['riferimento']);
	
	// Price limits activation
	var prices = $('#form_search_type_prezzo_min').add('#form_search_type_prezzo_max').attr('disabled', 'disabled');
	function contractChange(affitto) {
		$('option:not(:first-child)', prices).remove();
		$.each((affitto
			? [200, 400, 600, 800, 1000, 1500, 2000, 2500] 
			: [50000, 100000, 150000, 200000, 250000, 300000, 350000, 400000, 450000, 500000]),
		function(i, v) {
			prices.append('<option value="'+v+'">&euro;'+v+'</option>');
		});
		prices.removeAttr('disabled');
	}
	$('#form_search_type_affitto, #form_search_type_vendita').change(function() { contractChange($(this).val() == 'true') });
	$('#form_search_type_affitto:checked, #form_search_type_vendita:checked').each(function() { contractChange($(this).val() == 'true') } );
	
	// Repopulate selects from uri
	$('#form_search select[name]').each(function() {
		$('option[value=' + (site.parameters[$(this).attr('name')] || '') + ']:first', this).attr('selected', 'selected');
	});
	
	// Location activation
	if(places != undefined) {
		var r_select = $('#form_search_location_regione');
		var p_select = $('#form_search_location_provincia');
		var c_select = $('#form_search_location_citta');
		
		function p_filter(regione) {
			if (!regione) 
				return p_select.val('').attr('disabled', 'disabled') 
					&& c_select.val('').attr('disabled', 'disabled')
					|| true;
			$('option:not(:first-child)', p_select).remove();
			for(val in places[regione])
				$("<option></option>").attr('value', val).html(val).appendTo(p_select);
			p_select.removeAttr('disabled');
		}
		
		function c_filter(prov) {
			if (!prov) 
				return c_select.val('').attr('disabled', 'disabled')
					|| true;
			$('option:not(:first-child)', c_select).remove();
			citta = places[r_select.val()][prov];
			for(i = 0; i < citta.length; ++i)
				$("<option></option>").attr('value', citta[i]).html(citta[i]).appendTo(c_select);
			c_select.removeAttr('disabled');
		}
		
		var rv = r_select.val();
		var pv = p_select.val();
		var cv = c_select.val();
		
		if (!cv && !pv) c_select.attr('disabled', 'disabled');
		if (!pv && !rv) p_select.attr('disabled', 'disabled');
		
		if (rv) { r_select.val(rv); p_filter(rv); p_select.val(pv); }
		if (pv) { c_filter(pv); c_select.val(cv); }
		
		r_select.change(function() { p_filter($(this).val()) });
		p_select.change(function() { c_filter($(this).val()) });
	}
	
	// Category - tipology activation
	if(global_categories != undefined) {
		var cat_select = $('#form_search_type_categoria');
		var tip_select = $('#form_search_type_tipologia');
		
		function tip_filter(cat) {
			if (!cat)
				return tip_select.val('').attr('disabled', 'disabled')
					|| true;
			$('option:not(:first-child)', tip_select).remove();
			tip = global_categories[cat];
			for(i = 0; i < tip.length; ++i)
				$("<option></option>").attr('value', tip[i]).html(tip[i]).appendTo(tip_select);
			tip_select.removeAttr('disabled');
		}
		
		var catv = cat_select.val();
		var tipv = tip_select.val();
		
		if (cat_select.length) tip_filter(catv);
		if (tipv) tip_select.val(tipv);
		
		cat_select.change(function() { tip_filter($(this).val()) });
	}
	
	// Placeholders
	$('#form_search input.placeholder').placeholder();
	
	// Redirect instead of submit
	$('#form_search').submit(function(evt) {
		form = { 'page':'' };
		$('input[name]:radio:checked, select[name], .search_field[name], #form_search_ref_riferimento', this).each(function() { 
			$this = $(this);
			form[$this.attr('name')] = $this.val();
		});
		window.location = $(this).attr('action') + '/' + site.parameters.toURI(form);
		return false;
	});
	
	$('#form_search_ref:has(#form_search_ref_riferimento)').submit(function() {
		window.location = site.site_url('home/search/' + site.parameters.toURI({ 'page':'', 'riferimento':$('#form_search_ref_riferimento').val() }));
		return false;
	});
});
