$(document).ready(function()
{
    //pobierz dzielnice
    getDistricts();

    //autocomplete
    var emptyTag = [""];
    
    // Autocomplete dla ulic
    $("#search-street").autocomplete({
        source: function(request, response)
        {
			$.ajax({
            	url: "/include/ajax.php",
            	dataType: "json",              
            	data:
            	{
              		term : request.term,
              		action : 'search',
                	by : 'street',
                	city : 2,
                	district : $('#search-1-district').val(),
                	estate : $('#search-1-estate').val()
              	},
              	success: function(data)
              	{                
              		response(data);
              	}
			});
        },
        minLength: 3,
        delay: 400,
        select: function(event, data)
        {
            if (data.item) document.location = '/' + data.item.id; 
        }
    });

    $('#search-street').keypress(function(e)
    {
        if (e.keyCode == 13)
        {
            e.preventDefault();

            document.location = '/szukaj/ulica/Warszawa/' + $('#search-1-district').val() + '/' + $('#search-1-estate').val() + '/' + $('#search-street').val();
        }
    });

    $('#button-1').click(function(e)
    {
    	if ($('#search-street').val() == 'Wpisz nazwę ulicy...')
    	{
    		document.location = '/lista_ulic/Warszawa/' + $('#search-1-district').val() + '/' + $('#search-1-estate').val() + '/A';
    	}
    	else
    	{
    		document.location = '/szukaj/ulica/Warszawa/' + $('#search-1-district').val() + '/' + $('#search-1-estate').val() + '/' + $('#search-street').val();
    	}
    });

    // Autocomplete dla informacji
    $("#search-info").autocomplete({
        source: function(request, response)
        {
			$.ajax({
            	url: "/include/ajax.php",
            	dataType: "json",              
            	data:
            	{
              		term : request.term,
              		action : 'search',
                	by : 'info',
                	city : 2,
                	district : $('#search-1-district').val(),
                	estate : $('#search-1-estate').val()
              	},
              	success: function(data)
              	{                
              		response(data);
              	}
			});
        },
        minLength: 3,
        delay: 400,
        select: function(event, data)
        {
            if (data.item) document.location = '/' + data.item.id; 
        }
    });

    $('#search-info').keypress(function(e)
    {
        if (e.keyCode == 13)
        {
            e.preventDefault();
            
            document.location = '/szukaj/info/Warszawa/' + $('#search-1-district').val() + '/' + $('#search-1-estate').val() + '/' + $('#search-info').val();
        }
    });

    $('#button-2').click(function(e)
    {
        document.location = '/szukaj/info/Warszawa/' + $('#search-1-district').val() + '/' + $('#search-1-estate').val() + '/' + $('#search-info').val();
    });

    $("#search-street").focus(function()
    {
        var val = $(this).attr('value');
        
        if (val == 'Wpisz nazwę ulicy...')
        {
            $(this).attr('value', '');
        }
    });

    $("#search-street").blur(function()
    {
        var val = $(this).attr('value');
        
        if (val == '')
        {
            $(this).attr('value', 'Wpisz nazwę ulicy...');
        }
    });

    $("#search-info").focus(function()
    {
        var val = $(this).attr('value');
        
        if (val == 'Wpisz nazwę (miejsca, budynku, osoby, firmy), której szukasz...')
        {
            $(this).attr('value', '');
        }
    });

    $("#search-info").blur(function()
    {
        var val = $(this).attr('value');
        
        if (val == '')
        {
            $(this).attr('value', 'Wpisz nazwę (miejsca, budynku, osoby, firmy), której szukasz...');
        }
    });
});

// po załadowaniu strony załaduj wszystkie dzielnice i wszystkie osiedla
function getDistricts()
{
    $.post('/include/ajax.php',
    {
        'action' : 'getDistricts',
        'id'     : 2
    },
    function(data)
    {
        $("#search-1-district").html(data);
        $("#select-1").jqTransform();
        $("#select-1").fadeOut(250).fadeIn(250);
    });

    $.post('/include/ajax.php',
    {
        'action' : 'getFullEstates',
        'id'     : 2
    },
    function(data)
    {
        $("#search-1-estate").html(data);
        $("#select-2").jqTransform();
        $("#select-2").fadeOut(250).fadeIn(250);
    });
}

// po wybraniu dzielnicy załaduj jej osiedla i ulice
function getEstates()
{
	$.post('/include/ajax.php',
    {
        'action' : 'getEstates',
        'id'     : $("#search-1-district").val()
    },
    function(data)
    {
        $("#search-1-estate").html(data);
        $("#select-2").jqTransform();
        $("#select-2").fadeOut(250).fadeIn(250);
    });
}

