var searchForm;

/**
 * called by flash map when is loaded
 */
function flashLoaded() {
    var existingOffices = searchForm.getOffices();
    for(var i in existingOffices) {
        if (existingOffices[i] !== undefined) {
            //addOffice(existingOffices[i].id, existingOffices[i].name, true);
            thisMovie('officeMap').sendToActionScript(existingOffices[i].id);
        }
    }
}

function thisMovie(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName];
    } else {
        return document[movieName];
    }
}

function switchToLocationSearch() {
    $('#showOfficesMap').hide();
    $('#officesInput').hide();
    $('#showFeaturedProperty').show();
    $('#locationInput').show();
    $('#locationInput').show();
    $('#submitButtonSection').addClass('negativeTopForBetterBlending');
    searchForm.switchCenterTypeToGeoName();
}

function switchToOfficeSearch() {
    $('#showOfficesMap').show();
    $('#officesInput').show();
    $('#showFeaturedProperty').hide();
    $('#locationInput').hide();
    $('#submitButtonSection').removeClass('negativeTopForBetterBlending');
    searchForm.switchCenterTypeToOffice();
}

/**
 * This is the method which needs to be called by Flash map to remove already added office
 */
function removeOffice(officeId, dontReload) {
    searchForm.removeOffice(officeId, dontReload);
    $('#officeLiId_' + officeId).remove();
}

/**
 * This is the method which needs to be called by Flash map to add office to the search
 */
function addOffice(officeId, officeName, dontReload) {
    removeOffice(officeId, true);
    $('#officesInput ul').append('<li id="officeLiId_'+officeId+'">'+officeName+'</li>');
    searchForm.addOffice(officeId, officeName, dontReload);
}

function removeAllOffices() {
    var existingOffices = searchForm.getOffices();
    for(var i in existingOffices) {
        removeOffice(existingOffices[i].id, true);
    }
}

function switchOfficesTo(searchType) {
    removeAllOffices();
    try {
        thisMovie('officeMap').switchOfficesTo(searchType);
        //console.info('switching to '+searchType);
    } catch (e) {
        //console.info('switching to '+searchType+' failed');
    }
}

HomeSearch.prototype.switchSearchTypeToBuy = function() {
    this.switchSearchType(minimumPricesBuy, maximumPricesBuy);
    switchOfficesTo('sales');
};

HomeSearch.prototype.switchSearchTypeToRent = function() {
    this.switchSearchType(minimumPricesRent, maximumPricesRent);
    switchOfficesTo('lettings');
};


$(document).ready(function() {
    searchForm = new HomeSearch('homeSearch');
    if (searchForm.getCenterType() == 'office') {
        switchToOfficeSearch();
    } else {
        switchToLocationSearch();
    }

    $('.switchToOfficeSearch').click(function() {
        switchToOfficeSearch();
        if (searchForm.getSearchType() == 'buy') {
            setTimeout("switchOfficesTo('sales')",1000);
        } else {
            setTimeout("switchOfficesTo('lettings')",1000);
        }
       return (false);
    });
    $('#switchToLocationSearch').click(function() {
       switchToLocationSearch();
       return (false);
    });

    // load offices flash map
    var flashvars = {xmlLocation: '/office/officemapxml'};
    var params = {wmode: 'transparent'};
    var attributes = {};
    swfobject.embedSWF("/flash/mapSearch.swf", "officeMap", "646", "419", "9.0.0","/flash/expressInstall.swf", flashvars, params, attributes);

});

