$(document).ready(function(){
	setFormFieldsStates();
	$("#complexSearcher").bind("submit", function(){
		goSearch();
		return false;
	});
	
});

function goSearch(){
	var action = $("#action").children("option:selected").attr("value")
	var type = $("#action_"+action).children("option:selected").attr("value")
	var city = $("#city").children("option:selected").attr("value")
	var district = $("#district").children("option:selected").attr("value")
	var priceVal = $('#price_'+type).val();
	var room = $('#room').val();
	var area = $('#area').val();
	var actions = {"sell":"na-sprzedaz","rent":"do-wynajecia"}
	var types = {"flats":"mieszkania-","houses":"domy-","plots":"dzialki-","offices":"lokale-uzytkowe-"}
	
	var url = types[type]+actions[action]+'/'+urlRewriteEscape(city)
	if (city.toLowerCase() == 'warszawa' && district != 'wszystkie') url += ':'+urlRewriteEscape(district)
	url += '/'+priceVal+'/'
	if (type == 'plots') url+= area + '/'
	else url += room + '/'

	document.location.href = 'http://' + location.host + '/' + url
}
function urlRewriteEscape(txt){
	txt = txt.replace( /ą|Ą/g, 'a')
	txt = txt.replace( /ę|Ę/g, 'e')
	txt = txt.replace( /ó|Ó/g, 'o')
	txt = txt.replace( /ś|Ś/g, 's')
	txt = txt.replace( /ł|Ł/g, 'l')
	txt = txt.replace( /ż|Ż|ź|Ź/g, 'z')
	txt = txt.replace( /ć|Ć/g, 'c')
	txt = txt.replace( /ń|Ń/g, 'n')
	txt = txt.toLowerCase()	
	txt = txt.replace( /[^a-z]/g, '-')
	return txt
}

function setFormFieldsStates(){
	var action = $("#action").children("option:selected").attr("value")
	var type = $("#action_"+action).children("option:selected").attr("value")
	var district = $("#district").children("option:selected").attr("value")
	var priceVal = $('#price_'+type).val();
	var room = $('#room').val();
	var area = $('#area').val();
	complexSearcher('action');
	$('#action_'+action).val(type);	
	complexSearcher('action_'+action);
	complexSearcher('city')
	$('#price_'+type).val(priceVal);	
	$('#district').val(district);
	$('#room').val(room);
	$('#area').val(area);
}

function complexSearcher( act ){

    var selected_item;

    switch (act){
        case 'action':
            selected_item = $("#action").children("option:selected").attr("value");
            if (selected_item=='sell')
            {
                $('#action_sell').show();
                $('#action_sell').val('houses');
                $('#action_rent').hide();
                
            }
            else if (selected_item=='rent')
            {
                $('#action_sell').hide();
                $('#action_rent').show();
                $('#action_rent').val('houses');

            }
            $('#price_houses').show();
            $('#price_houses').val('0-500');
            $('#price_flats').hide();
            $('#price_plots').hide();
            $('#price_offices').hide();
            roomAreaSwitcher('room')                
            
        break
        
        case 'action_sell':
            selected_item = $("#action_sell").children("option:selected").attr("value");
            if (selected_item=='houses')
            {
                $('#price_houses').show();
                $('#price_houses').val('0-500');
                $('#price_flats').hide();
                $('#price_plots').hide();
                $('#price_offices').hide();
                roomAreaSwitcher('room')                
            }        
            else if (selected_item=='flats')
            {
                $('#price_houses').hide();
                $('#price_flats').show();
                $('#price_flats').val('0-300');
                $('#price_plots').hide();
                $('#price_offices').hide();
                roomAreaSwitcher('room')                
            }        
            else if (selected_item=='plots')
            {
                $('#price_houses').hide();
                $('#price_flats').hide();
                $('#price_plots').show();
                $('#price_plots').val('50-300');
                $('#price_offices').hide();
                roomAreaSwitcher('area')
            }        
            else if (selected_item=='offices')
            {
                $('#price_houses').hide();
                $('#price_flats').hide();
                $('#price_plots').hide();
                $('#price_offices').show();
                $('#price_offices').val('0-400');
                roomAreaSwitcher('room')                
            }
        break
        
        case 'action_rent':
            selected_item = $("#action_rent").children("option:selected").attr("value");
            if (selected_item=='houses')
            {
                $('#price_houses').show();
                $('#price_houses').val('0-500');
                $('#price_flats').hide();
                $('#price_offices').hide();
                roomAreaSwitcher('room')                
            }        
            else if (selected_item=='flats')
            {
                $('#price_houses').hide();
                $('#price_flats').show();
                $('#price_flats').val('0-300');
                $('#price_offices').hide();
                roomAreaSwitcher('room')                
            }        
            else if (selected_item=='offices')
            {
                $('#price_houses').hide();
                $('#price_flats').hide();
                $('#price_offices').show();
                $('#price_offices').val('0-400');
                roomAreaSwitcher('room')                
            }
        break
        
        case 'city':
            selected_item = $("#city").children("option:selected").attr('label')
            if (selected_item=='Warszawa' || selected_item=='warszawa' || selected_item=='WARSZAWA')
            {
                $('#district').show();
                $('#district').val(0);
            }
            else
            {
                $('#district').hide();
            }        
        
        break
        
        
    }
}


function roomAreaSwitcher(field){
    
    if (field == 'room')
    {
        $('#room_c').show();
        $('#room').val(0);
        $('#area_c').hide();
    }
    else
    {
        $('#area_c').show();
        $('#area').val(0);
        $('#room_c').hide();
    }
    
}


