var q_static = "Wpisz czego szukasz, np. remont";


function setLoading()
{
    $("#tops_forms").html("<div style=\"text-align: center; font-size: 13px; padding-top: 30px; color: #5f5f5f\">Trwa wyszukiwanie - proszę czekać...</div>");
}

function showTopSForm(type)
{
    $("#ul_0").hide();
    $("#ul_1").hide();
    $("#ul_2").hide();
    $("#ul_3").hide();
    $("#ul_" + type).show();
    $("#tops_cur_pos").val(type);
}

function submitDispatcher()
{
    if($('#tops_q').val() == q_static)
    {
        $('#tops_q').val("");
    }
    
    var active_form = $("#tops_cur_pos").val();

    if(active_form == 0)
    {
        searchServs();
    }
    else if(active_form == 1)
    {
        searchTe();
    }
    else if(active_form == 2)
    {
        searchKB();
    }
    else if(active_form == 3)
    {
        searchForum();
    }
}

function searchServs()
{
    if($('#city_s').val() == "wpisz miasto")
    {
        $('#city_s').val("");
    }

    if($('#addr_s').val() == "ulica, numer")
    {
        $('#addr_s').val("");
    }
    
    var phrase = $('#tops_q').val();
    var city = $('#city_s').val();
    var pid = $('#pid_s').val();
    var lat = $('#lat_s').val();
    var lon = $('#lon_s').val();
    var addr = $('#addr_s').val();
    var dist = $('#dist_s').val();
    var sep = "?";
    var search_url = $("#serv_form_url").val();

    if(phrase != "")
    {
        search_url = search_url + sep + "q=" + encodeURI(phrase) + "&sdescr=1";
        sep = "&";
    }

    if(city != "")
    {
        search_url = search_url + sep + "city=" + encodeURI(city);
        sep = "&";
    }
    else if(pid > 0)
    {
        search_url = search_url + sep + "pid=" + pid;
        sep = "&";
    }

    if(!$('#scoord_s').is(':checked'))
    {
        setLoading();

        if(pid > 0)
        {
            window.location = search_url;
        }
        else if(phrase == "")
        {
            window.location = search_url;
        }
        else if(phrase != "" && city != "")
        {
            window.location = "/uslugi/" + encodeURI(phrase) + "," + encodeURI(city) + ".html";
        }
        else if(phrase != "" && city == "")
        {
            window.location = "/uslugi/" + encodeURI(phrase) + ".html";
        }
        else
        {
            window.location = search_url;
        }
    }
    else
    {
        geocoder = new google.maps.Geocoder();
        var address_to_find = "";

        if(city != "" && addr != "")
        {
            address_to_find = city + ", " + addr;
        }
        else if(city != "")
        {
            address_to_find = city;
        }

        if(address_to_find.length > 0)
        {
            geocoder.geocode( {
                'address': address_to_find
            }, function(results, status)

            {
                    if (status == google.maps.GeocoderStatus.OK)
                    {
                        lat = results[0].geometry.location.lat();
                        lon = results[0].geometry.location.lng();
                        search_url = search_url + "&lat=" + lat + "&lon=" + lon + "&dist=" + dist + "&addr=" + encodeURI(addr) + "&mp=1";
                        setLoading();
                        window.location = search_url;
                    }
                    else
                    {
                        alert("Nie można znaleźć podanej przez Ciebie lokalizacji: " + address_to_find);
                    }
                });
        }
        else
        {
            alert("Wpisz miasto oraz (opcjonalnie) ulicę i numer");
        }
    }
}

function searchTe()
{
    var phrase = $('#tops_q').val();
    var cat_id = $('#cid_z').val()
    var city = $('#city_z').val()
    var pid = $('#pid_z').val()
    var sep = "?";
    var search_url = "/zlecenia";

    if(phrase != "")
    {
        search_url = search_url + sep + "q=" + encodeURI(phrase);
        sep = "&";
    }

    if(cat_id != "")
    {
        search_url = search_url + sep + "cid=" + cat_id;
        sep = "&";
    }

    if(city != "")
    {
        search_url = search_url + sep + "city=" + encodeURI(city);
        sep = "&";
    }
    else if(pid > 0)
    {
        search_url = search_url + sep + "pid=" + pid;
        sep = "&";
    }

    setLoading();
    window.location = search_url;
}

function searchKB()
{
    var phrase = $('#tops_q').val();
    var section = "";
    var cat_name = $('#cid_b').find(":selected").attr("class");
    var cat_id = $('#cid_b').val()
    var search_url = '/baza-wiedzy/szukaj,' + cat_name + ',' + cat_id + ',' + encodeURI(phrase) + ',' + section + ',0.html';

    if(phrase == "" && section == "" && cat_name == "" && cat_id == "")
    {
        search_url = '/baza-wiedzy/';
    }

    setLoading();
    window.location = search_url;
}

function searchForum()
{
    var phrase = $('#tops_q').val();
    var fid = $('#fid_f').val();
    var f_name = $('#fid_f').find(":selected").attr("class");
    var sposts = 1;
    var search_url = "";

    if(phrase == "" && fid <= 0)
    {
        alert("Wprowadź szukane słowo lub wybierz forum");
    }
    else
    {
        if(phrase != "" && fid > 0)
        {
            search_url = '/forum/szukaj,' + encodeURI(phrase) + ',' + sposts + ',' + f_name + ',' + fid + ',0.html';
        }
        else if(phrase != "")
        {
            search_url = '/forum/szukaj,' + encodeURI(phrase) + ',' + sposts + ',wszystkie-fora,0,0.html';
        }
        else if(fid > 0)
        {
            search_url = '/forum/' + f_name + ',' + fid + ',0.html';
        }
        setLoading();
        window.location = search_url;
    }
}

(function($)
{
    var search = $("#tops_form");
    var forms = $("#tops_forms");
    var cur_pos = $("#tops_cur_pos");
    var q = $("#tops_q");
    var selects = search.find("select");
    var opts = search.find("option");
    var close_lock = false;

    selects.live('mouseenter', function()
    {
        close_lock = true;
    });
    selects.live('mouseout', function()
    {
        close_lock = false;
    });

    opts.live('click', function()
    {
        close_lock = true;
    });
    
    //search.mouseenter(function()
    search.click(function()
    {
        search.css("background", "transparent url('/gfx/icos.png') 0 -500px no-repeat");
        search.animate(
        {
            height: '160px'
        }, 200, function(){
            forms.show();
        });
    });

    $("#tops_q").focus(function()
    {
        search.css("background", "transparent url('/gfx/icos.png') 0 -500px no-repeat");
        search.animate(
        {
            height: '160px'
        }, 200, function(){
            forms.show();

        });
    });

    search.bind('mouseleave', function()
    {
        if(!close_lock)
        {
            search.stop();
            forms.hide();
            search.css({
                "height": "65px",
                "background": "none"
            });
            $("#tops_q").blur();
        }
    });

    $.ajax(
    {
        url: "/ax/getdata.jsp?op=8&pos=" + cur_pos.val(),
        success: function(msg)
        {
            forms.html(msg);
        },
        error: function(xhr, ajaxOptions, thrownError)
        {
        }
    });

    if(q.val() == q_static)
    {
        q.css("color", "#b0b0b0");
    }

    q.focus(function()
    {
        if(q.val() == q_static)
        {
            q.css("color", "#5f5f5f");
            q.val("");
        }
    });
    q.blur(function()
    {
        if(q.val() == "")
        {
            q.css("color", "#b0b0b0");
            q.val(q_static);
        }
    });
    q.keydown(function(event)
    {
        if(event.keyCode == '13')
        {
            submitDispatcher();
        }
    });

    $('#city_s').live('focus', function()
    {
        if($('#city_s').val() == "wpisz miasto")
        {
            $('#city_s').val("");
            $('#pid_s').val("0");
        }
    });
    
    $('#city_s').live('blur', function()
    {
        if($('#city_s').val() == "")
        {
            $('#city_s').val("wpisz miasto");
        }
    });

    $('#city_s').live('keydown', function(event)
    {
        if (event.keyCode == '13')
        {
            submitDispatcher();
        }
    });

    $('#pid_s').live('change', function()
    {
        if($('#pid_s').val() > 0)
        {
            $('#city_s').val("wpisz miasto");
        }
    });

    $('#scoord_s').live('change', function()
    {
        if($('#scoord_s').is(':checked'))
        {
            $('#addr_s').removeClass('dsbld');
            $('#dist_s').removeClass('dsbld');
            loadGMA();
        }
        else
        {
            $('#addr_s').addClass('dsbld');
            $('#dist_s').addClass('dsbld');
        }
    });

    $('#dist_s').live('click', function()
    {
        $('#scoord_s').attr('checked', true);
        $('#addr_s').removeClass('dsbld');
        $('#dist_s').removeClass('dsbld');
        loadGMA();
    });

    $('#addr_s').live('click', function()
    {
        $('#scoord_s').attr('checked', true);
        $('#addr_s').removeClass('dsbld');
        $('#dist_s').removeClass('dsbld');

        if($('#addr_s').val() == "ulica, numer")
        {
            $('#addr_s').val("");
        }
        $('#addr_s').focus();
        loadGMA();
    });

    $('#addr_s').live('blur', function()
    {
        if($('#addr_s').val() == "")
        {
            $('#addr_s').val("ulica, numer");
        }
    });

    $('#addr_s').live('keydown', function(event)
    {
        if (event.keyCode == '13')
        {
            submitDispatcher();
        }
    });

    $('#pid_z').live('change', function()
    {
        if($('#pid_z').val() > 0)
        {
            $('#city_z').val("");
        }
    });

    $('#city_z').live('change', function()
    {
        if($('#city_z').val() != "")
        {
            $('#pid_z').val("0");
        }
    });

    $("#choose0").click(function()
    {
        showTopSForm(0);
    });
    $("#choose1").click(function()
    {
        showTopSForm(1);
    });
    $("#choose2").click(function()
    {
        showTopSForm(2);
    });
    $("#choose3").click(function()
    {
        showTopSForm(3);
    });
        
    $("#tops_submit").click(function()
    {
        submitDispatcher();
    });

})(jQuery);
