/**
 *  This script is used on list view, table view, gallery view, and saved properties view
 */
// this is a crucial object and needs to be declared in global space
var searchForm;
var action = 'list';

function switchToLocationSearch() {
    //alert('switchToLocationSearch');
    $('#officesInput').hide();
    $('#locationInput').show();
    //$('#officeLocationSwitch').text('Switch to search by offices');
    searchForm.switchCenterTypeToGeoName();
}

function switchToOfficeSearch() {
    //alert('switchToOfficeSearch');
    $('#officesInput').show();
    $('#locationInput').hide();
    //$('#officeLocationSwitch').text('Switch to search by location');
    searchForm.switchCenterTypeToOffice();
}

ListSearch.prototype.initExtend = function() {
	HomeSearch.prototype.disableAutosuggest();
	
	$('#location option:first').attr('selected','selected');
	
    $("#location").focus(function () {
        if(typeof(searchForm) === 'object') {
            searchForm.disableAutosuggest();
        }
    });
    
    geoLocationType = 'customarea';
	geoLocationId = 3;
    $('#geoLocationType').val(geoLocationType);
    $('#geoLocationId').val(geoLocationId);
    
	 $("#location").change(function () {
        if ($(this).val() == 'All')
        {
          geoLocationType = 'customarea';
          geoLocationId = 3;
        } else if ($(this).val() == 'Clapham') {
          geoLocationType = 'customarea';
          geoLocationId = 2;
        } else if ($(this).val() == 'Brixton') {
          geoLocationType = 'customarea';
          geoLocationId = 1;
        } else if ($(this).val() == 'Stockwell') {
          geoLocationType = 'alias';
          geoLocationId = 24509;
        } else if ($(this).val() == 'Battersea') {
          geoLocationType = 'customarea';
          geoLocationId = 5;
        } else if ($(this).val() == 'Balham') {
          geoLocationType = 'alias';
          geoLocationId = 24532;
        } else if ($(this).val() == 'Camberwell') {
          geoLocationType = 'customarea';
          geoLocationId = 4;
        } else if ($(this).val() == 'East Dulwich') {
          geoLocationType = 'customarea';
          geoLocationId = 6;
        } else if ($(this).val() == 'West Dulwich') {
          geoLocationType = 'customarea';
          geoLocationId = 7;
        } else if ($(this).val() == 'Peckham') {
          geoLocationType = 'customarea';
          geoLocationId = 8;
        } else if ($(this).val() == 'Nunhead') {
          geoLocationType = 'customarea';
          geoLocationId = 9;
        } else if ($(this).val() == 'Brockley') {
          geoLocationType = 'customarea';
          geoLocationId = 10;
        } else if ($(this).val() == 'Forest Hill') {
          geoLocationType = 'customarea';
          geoLocationId = 11;
        } else if ($(this).val() == 'Camberwell') {
          geoLocationType = 'customarea';
          geoLocationId = 12;
        } else if ($(this).val() == 'Herne Hill') {
          geoLocationType = 'customarea';
          geoLocationId = 13;
        }
        $('#geoLocationType').val(geoLocationType);
        $('#geoLocationId').val(geoLocationId);
        
        searchForm.canSubmitRightAway = true;
        HomeSearch.prototype.criteriaChanged();
        
    });
};

/**
 * This is the method which needs to be called by Flash map to add office to the search
 */
function addOffice(officeId, officeName, dontReload) {
    $('#officesInput').append('Properties for ' + officeName + ' office');
}


$(document).ready(function() {
    searchForm = new ListSearch('homeSearch');

    var existingOffices = searchForm.getOffices();
    for(var i in existingOffices) {
        if (existingOffices[i] !== undefined) {
            addOffice(existingOffices[i].id, existingOffices[i].name, true);
        }
    }

    if (searchForm.getCenterType() == 'office') {
        switchToOfficeSearch();
    } else {
        switchToLocationSearch();
    }
    
    // change order results
    $('#order').change(function() {
       var url = searchForm.getSearchEngineUrl(action,'search');
       url = url + 'order/' + $('#order').val();
       window.location = url;
    });

    // change number of results per page
    $('#resultsPerPage').change(function() {
       var url = searchForm.getSearchEngineUrl(action,'search');
       url = url + 'resultsPerPage/' + $('#resultsPerPage').val();
       window.location = url;
    });

    $('#officeLocationSwitch').click(function() {
        if ($('#officesInput').is(":visible")) {
            switchToLocationSearch();
        } else {
            switchToOfficeSearch();
        }
        return (false);
    });

});

