var searchPoint;

$(document).ready(function(){

    $('.selector a').click(function(){
        $('.selector a').removeClass('selected');
        $(this).addClass('selected');
        $('.filter').hide();
        $('#' + $(this).attr('id') + '_container').show();
    });

    $('#SearchKeywords').bind('keyup', function (e) {
      if (e.keyCode == 13) {
        var q = $('#SearchKeywords').val();
        if (q)
        {
            $('#SearchMapResultsKW').html('<img src="/img/loading.gif" width="16" height="16" alt="" class="loading">').load('/map_search.php', { 'q': q }, function(){ initResultLinks(); });
        }
      }
    });

    $('#SearchKeywordsButton').click(function(){
        var q = $('#SearchKeywords').val();
        if (q)
        {
            $('#SearchMapResultsKW').html('<img src="/img/loading.gif" width="16" height="16" alt="" class="loading">').load('/map_search.php', { 'q': q }, function(){ initResultLinks(); });
        }
    });


    if ($('#SearchMap').val() && $('#SearchMap').val() != '-')
    {
        $('#SearchMapButton').click();
    }
    else if ($('#SearchKeywords').val())
    {
        $('#filter_keywords').click();
        $('#SearchKeywordsButton').click();
    }
});

function initResultLinks()
{
    $('#SearchMapResultsKW a.clear2').click(function(){
        $('#SearchKeywords').val('');
        $('#SearchMapResultsKW').html('');
        return false;
    });

    $('#SearchMapResultsKW a.search_point').click(function(){
        if (searchPoint)
        {
            map.removeOverlay(searchPoint);
            searchPoint.setCoordPoint(new YMaps.GeoPoint($(this).attr('pos_x'), $(this).attr('pos_y')));
        }
        else
        {
            searchPoint = new YMaps.Placemark(new YMaps.GeoPoint($(this).attr('pos_x'), $(this).attr('pos_y')), { style: 'default#lightbluePoint', hasBalloon: false });
        }
        map.addOverlay(searchPoint);
        map.setCenter(new YMaps.GeoPoint($(this).attr('pos_x'), $(this).attr('pos_y')), 16);

        $('#SearchMapResultsKW li').removeClass('current');
        $(this).parent().addClass('current');

        return false;
    });

    if ($('#SearchMapResultsKW a.search_point').length == 1)
    {
        $('#SearchMapResultsKW a.search_point').click();
    }

    $('#SearchMapResultsKW a.search_page').click(function(){
        $('#SearchMapResultsKW').html('<img src="/img/loading.gif" width="16" height="16" alt="" class="loading">').load($(this).attr('url'), null, function(){ initResultLinks(); });
        return false;
    });

}

