/**
 *
 */

$(document).ready(function() {

    $('.nearby_remove').live('click', function() {
        $(this).parent().remove();
        return false;
    });

	var chosen_list = new Array();

	function appendChosenList(str) {

        var html = $("#chosenList").html();
		
        if (!$("#chosenList").html()) {
            //chosenListTitle
            $("#chosenListTitle").slideDown("slow");
            $(".cityBoxHidden").fadeIn("slow");
            //searchButton
            $("#searchButton").attr("disabled",false);
        }

        $("#chosenList").html(html + '<li id="'+ str +
                              '"><input type="hidden" name="chosenCities[]" value="' + str +
                              '"> &raquo; ' + str + '<a href="#" class="remove nearby_remove">&nbsp;</a><br /><a href="#" class="show_nearby">Show nearby cities</a></li>');
        $("#CityAjax").attr("value",'');
        $("#CityAjax").focus();

        
        return;
	}
	
	//
	$("#CityAjax").autocomplete("/search/addCities/cities.php", {
		delay: 10,
		minChars: 3,
		matchSubset: 1,
		matchContains: 1,
		cacheLength: 999,
		autoFill: false
	});
    $("#CityAjax").result(function (event, data, formatted) {
        if (chosen_list[data[0]] !== undefined) {
            $(this).val('');
            return false;
        }
        chosen_list[data[0]] = 1;
        
        appendChosenList(data[0]);
    });
	
  
    //--------------- show nearby cities by ajax -------------------
    $('a.show_nearby').live('click', function() {

        if ($(this).parent().find('span.nearby_cities').html()) {
            if ($(this).parent().find('span.nearby_cities').is(':visible')) {
                $(this).parent().find('span.nearby_cities').hide('slow');
                $(this).html('Show nearby cities');
            }
            else {
                $(this).parent().find('span.nearby_cities').show('slow');
                $(this).html('Hide nearby cities');
            }


            return false;
        }

        var city_str = $(this).parent().find('input').val();

        $(this).parent().append('<span style="display:none" class="nearby_cities"><img src="/images/loader.gif" /></span>');

        var span = $(this).parent().find('span.nearby_cities');

        span.show('slow');

        $.ajax({
          url: '/search/addNearby/index.hmtl?q='+city_str,
          success: function(data) {
            if (data) span.html(data);
            else span.html('<i>No cities found in 5 miles radius</i>');
          }
        });

        $(this).html('Hide nearby cities');

        return false;
    });

    /* ------------ open area div ---------------*/
    $('.show_region').click( function() {

        var group_id = $(this).attr('href').substring(1,3);
        if (group_id=='sd' || group_id=='sf')
            $('div#main_area_'+group_id).show();
        else {
            $('div#main_area_la').show();
            $('div#main_area_a1').show();
            $('div#main_area_a2').show();
        }

        return true;
     });

});
