jQuery.extend(
{
  context: function (context)
  {
    var co =
    {
      callback: function (method)
      {
        if (typeof method == 'string') method = context[method];
        var cb = function () { method.apply(context, arguments); }
        return cb;
      }
    };
    return co;
  }
});

/**
 * Constructor of a new Immobrowse object.
 * @param ajaxUrl - the URL of the Immobrowse JSON action
 * @param searchKey - the searchKey to be used
 * @param baseUrl - base URL of the main servlet
 * @param resultPage - base URL of the result page
 * @param cookieName - name of the cookie to store a choosen county ID
 * @param cookieMaxAge - max age (in seconds) for the county ID cookie
 */
function Immobrowse (ajaxUrl, searchKey, baseUrl, resultPage, cookieName, cookieCityName, cookieSubCityName, cookieMaxAge) {
    this.ajaxUrl = ajaxUrl;
    this.searchKey = searchKey;
    this.baseUrl = baseUrl;
    this.resultPage = resultPage;
    this.cookieName = cookieName;
    this.cookieCityName = cookieCityName;
    this.cookieSubCityName = cookieSubCityName;
    this.cookieMaxAge = cookieMaxAge;
    this.colClass = 'realestaterow';
    this.colOptimize = 3;
    this.choosenCounty = '';
    this.choosenCity = '';
    this.searchButton = '';
    this.allCountiesNavigator = 'geoid1nav';
    this.countyNavigator = 'admin2idnav';
}

Immobrowse.prototype.setColClass = function(clazz) {
    this.colClass = clazz;
}
Immobrowse.prototype.setColOptimize = function(numCols) {
    this.colOptimize = numCols;
}
Immobrowse.prototype.fixBaseUrl = function() {
    //baseUrl might be end with two slashes ('/iad//'), but just one is needed
    this.baseUrl = this.baseUrl.replace(/\/\//,'/');
}
Immobrowse.prototype.setSearchButton = function(button) {
    this.searchButton = button;
}

Immobrowse.prototype.setBaseUrl = function(bUrl) {
    this.baseUrl = bUrl;
}
Immobrowse.prototype.getBaseUrl = function() {
    return this.baseUrl;
}
Immobrowse.prototype.setChoosenCounty = function(id) {
    this.choosenCounty = id;
    this.setCookie(id, this.cookieName);
}
Immobrowse.prototype.setChoosenCity = function(id) {
    this.choosenCity = id;
    this.setCookie(id, this.cookieCityName);
}
Immobrowse.prototype.getChoosenCounty = function() {
    return this.choosenCounty;
}
Immobrowse.prototype.getChoosenCity = function() {
    return this.choosenCity;
}
Immobrowse.prototype.isSubCityChecked = function() {
    if(jQuery("input[@type=checkbox][@checked][name='areaId']").size() > 0) {
        return true;
    }
    return false;
}

Immobrowse.prototype.setSubCityHeader = function() {
    var header = jQuery('#subcity_header_choosen_caption');
    header.empty();
    var checkedList = jQuery("input[@type=checkbox][@checked][name='areaId']");
    if(checkedList.size() > 0) {
        checkedList.each(function() {
            var label = jQuery("label[for='" + this.id + "']").text();
            if (header.text().length > 0) {
                header.append(', ');
            }
            header.append(label);
        });
    } else {
        header.append('Stadtteil oder Gemeinde wählen ');
    }

}

Immobrowse.prototype.submitPopup = function() {
    var message = 'county: ' + this.choosenCounty + ' city: ' + this.choosenCity + ' subcities choosen: ' + this.isSubCityChecked();
    //alert (message);
    jQuery("#form_parent_areaid").val('');
    jQuery("#form_cityId").val('');
    jQuery("#form_areaId").val('');
    if (this.choosenCity != "") {
        jQuery("#form_cityId").val(this.choosenCity);
        if (this.isSubCityChecked()) {
            jQuery("#form_parent_areaid").val(this.choosenCity);
        } else {
            if (this.choosenCounty != 'all_counties') {
                jQuery("#form_parent_areaid").val(this.choosenCounty);
            }
            jQuery("#form_areaId").val(this.choosenCity);
        }
    } else {
        if (this.choosenCounty != 'all_counties') {
            jQuery("#form_areaId").val(this.choosenCounty);
        }
    }
}

Immobrowse.prototype.setHistory = function() {
    var hash = '#' + this.getChoosenCounty() + ';' + this.getChoosenCity();
    var checkedList = jQuery("input:checked[name='areaId']");
    if(checkedList.size() > 0) {
        checkedList.each(function() {
            hash += '|' + this.value;
        });
    }
    /*location.replace(location.href.replace(/#.*$/,'') + hash);*/
    window.location.href = window.location.href.replace(/#.*$/,'') + hash;
}

Immobrowse.prototype.restoreHistory = function() {
    var county = window.location.hash.replace(/#(\d+);.*$/,'$1');
    var city = window.location.hash.replace(/#\d+;(\d+)\|.*/,'$1');
    var checkboxes = window.location.hash.replace(/#\d+;\d+\|(.*)$/,'$1')
    if (county && city && checkboxes) {
        browse.showCitiesForCounty(county,'prepareCitySelection',true);
        browse.showSubCities(city,'displaySubCitySelection');
    }
}

/**
 * Iterates all points in the JSON object an renders the locations accordingly.
 * @param data - the JSON object containing the location information
 * @param container - the DOM element to render the locations to
 * @param hideContainer - the DOM element to hide after the locations are rendered (normally the container of the parent locations)
 * @param createLink - a function that renders a link of a location
 * @param prepend - contains a HTML string to be rendered before the actual locations are rendered
 */
Immobrowse.prototype.renderLocations = function(data, container, hideContainer, createLink, prepend, hidden) {
    var colClass = this.colClass;
    var colOptimize = this.colOptimize;
    hideContainer.slideUp(800, function () {
        container.empty();
        if(data != null) {
            var points = data['POINTS'];
            if(points != null) {
                var html = prepend;
                html += '<div class="' + colClass + '"><ul>';
                var numRows = Math.ceil(points.length/colOptimize);
                for (var i = 0; i < points.length; i++) {
                    var point = points[i];
                    if (i > 0 && i % numRows == 0) {
                        html += '</ul></div><div class="' + colClass + '"><ul>';
                    }
                    html += '<li>';
                    html += createLink(point);
                    html += point['NAME'];
                    html += '</a>';
                    html += '<span class="count">' + ' (' + point['COUNT'] + ')</span>';
                    html += '</li>';
                }
                html += '</ul></div><div style="clear:both;"></div>';
                container.append(html);
            }
        }
        if (!hidden) {
            container.slideDown('slow');
        }
    });
}

/**
 * Iterates all points in the JSON object an renders the locations accordingly.
 * @param data - the JSON object containing the location information
 * @param container - the DOM element to render the locations to
 * @param hideContainer - the DOM element to hide after the locations are rendered (normally the container of the parent locations)
 * @param createLink - a function that renders a link of a location
 * @param prepend - contains a HTML string to be rendered before the actual locations are rendered
 */
Immobrowse.prototype.renderCities4Popup = function(data, container, hideContainer, createLink, prepend) {
    hideContainer.slideUp(800, function () {
        container.empty();
        if(data != null) {
            var points = data['POINTS'];
            if(points != null) {
                var html = prepend;
                html += '<ul>';
                for (var i = 0; i < points.length; i++) {
                    var point = points[i];
                    html += '<div class="geo_layer_li" id="city_' + point['ID'] + '"><li>';
                    html += createLink(point);
                    html += point['NAME'];
                    html += '</a></li></div>';
                }
                html += '</ul>';
                container.append(html);
            }
        }
        container.slideDown('slow');
    });
}


/**
 * Iterates all points in the JSON object an renders the locations accordingly.
 * @param data - the JSON object containing the location information
 * @param container - the DOM element to render the locations to
 * @param hideContainer - the DOM element to hide after the locations are rendered (normally the container of the parent locations)
 * @param createLink - a function that renders a link of a location
 * @param prepend - contains a HTML string to be rendered before the actual locations are rendered
 */
Immobrowse.prototype.renderSubCities4Popup = function(data, container, hideContainer, prepend) {
    hideContainer.slideUp(800, function () {
        container.empty();
        if(data != null) {
            var points = data['POINTS'];
            if(points != null) {
                var html = prepend;
                if (points.length > 0) {
                    html += '<div class="geo_chkarea_all">';
                    html += '<div class="geo_layer_input">';
                    html += '<input type="checkbox" id="chkarea_all">';
                    html += '<label for="chkarea_all">Alle Stadtteile/Gemeinden auswählen</label>';
                    html += '</div></div>';
                }
                for (var i = 0; i < points.length; i++) {
                    var point = points[i];
                    html += '<div class="geo_layer_input">';
                    html += '<input type="checkbox" id="chkarea_' + point['ID'] +'" value="' + point['ID'] +'" name="areaId">';
                    html += '<label for="chkarea_' + point['ID'] + '">' + point['NAME'] + '</label>';
                    html += '</div>';
                }
                container.append(html);
            }
        }
        jQuery("input[@type=checkbox][name='areaId']").click(function() {
            if(this.checked) {
                jQuery(this).parent(".geo_layer_input").addClass('geo_layer_choosen_item');
                jQuery("#chkarea_all").parent(".geo_layer_input").removeClass('geo_layer_choosen_item');
                jQuery("#chkarea_all").removeAttr('checked');
            } else {
                jQuery(this).parent(".geo_layer_input").removeClass('geo_layer_choosen_item');
            }
        });
        jQuery("#chkarea_all").click(function() {
            if(this.checked) {
                jQuery(this).parent(".geo_layer_input").addClass('geo_layer_choosen_item');
                jQuery("input[@type=checkbox][name='areaId']").parent(".geo_layer_input").removeClass('geo_layer_choosen_item');
                jQuery("input[@type=checkbox][name='areaId']").removeAttr('checked');
            } else {
                jQuery(this).parent(".geo_layer_input").removeClass('geo_layer_choosen_item');
            }
        });
        container.slideDown('slow');
    });
}


/**
 * Iterates all points in the JSON object an renders the locations accordingly.
 * @param data - the JSON object containing the location information
 * @param container - the DOM element to render the locations to
 * @param hideContainer - the DOM element to hide after the locations are rendered (normally the container of the parent locations)
 * @param createLink - a function that renders a link of a location
 * @param prepend - contains a HTML string to be rendered before the actual locations are rendered
 */
Immobrowse.prototype.renderSubCities = function(data, container, hideContainer, allLabel, actionUrl, searchButton) {
    var colClass = this.colClass;
    var colOptimize = this.colOptimize;
    hideContainer.slideUp(800, function () {
        container.empty();
        var points = data['POINTS'];
        if(points != null) {
            var submitButton = '<input onclick="browse.setHistory(); return true;" type="submit" class="searchButtonFreitext" value=" " src="' + searchButton + '"/>';
            var parentId = data['PARENTID'];
            var html = '';
            html += '<form action="' + actionUrl + '">';
            html += '<input type="hidden" name="parent_areaid" value="' + parentId + '"/>';
            html += '<input type="hidden" name="cityId" value="' + parentId + '"/>';
            if (points.length > 0) {
                html += '<div class="' + colClass + '">';

                html += '<div class="geo_browse_input geo_browse_choosen_item">';
                html += '<input type="checkbox" checked="checked" id="chkarea_all" value="' + parentId + '" name="areaId" />';
                html += '<label class="label" for="chkarea_all">' + allLabel + '</label>';
                html += '</div></div>';
                html += '<div class="geo_browse_submit">' + submitButton + '</div>';
                html += '<div class="geo_browse_clear"></div>';
            }
            html += '<div class="' + colClass + '">';
            var numRows = Math.ceil(points.length/colOptimize);
            for (var i = 0; i < points.length; i++) {
                var point = points[i];
                if (i > 0 && i % numRows == 0) {
                    html += '</div><div class="' + colClass + '">';
                }
                html += '<div class="geo_browse_input">';
                html += '<input type="checkbox" id="chkarea_' + point['ID'] +'" value="' + point['ID'] +'" name="areaId" />';
                html += '<label class="label" for="chkarea_' + point['ID'] + '">' + point['NAME'] + ' (' + point['COUNT'] + ')';
                html += '</label>';
                html += '</div>';
            }
            html += '</div><div style="clear:both">'
            html += '<div class="geo_browse_submit">' + submitButton + '</div>';
            html += '</div>';
            html += '<div class="geo_browse_clear"></div>';
            html += '</form>';
            container.append(html);
        }

        jQuery("input[@type=checkbox][name='areaId'][id!='chkarea_all']").click(function() {
            if(this.checked) {
                jQuery(this).parent(".geo_browse_input").addClass('geo_browse_choosen_item');
                jQuery("#chkarea_all").parent(".geo_browse_input").removeClass('geo_browse_choosen_item');
                jQuery("#chkarea_all").removeAttr('checked');
            } else {
                jQuery(this).parent(".geo_browse_input").removeClass('geo_browse_choosen_item');
                if(jQuery("input:checked[name='areaId']").size() == 0) {
                    // no checkbox checked: check the "all" checkbox
                    jQuery("#chkarea_all").attr('checked', 'checked');
                    jQuery("#chkarea_all").parent(".geo_browse_input").addClass('geo_browse_choosen_item');
                }
            }
        });
        jQuery("#chkarea_all").click(function() {
            if(this.checked) {
                jQuery(this).parent(".geo_browse_input").addClass('geo_browse_choosen_item');
                jQuery("input[@type=checkbox][name='areaId'][id!='chkarea_all']").parent(".geo_browse_input").removeClass('geo_browse_choosen_item');
                jQuery("input[@type=checkbox][name='areaId'][id!='chkarea_all']").removeAttr('checked');
            } else {
                this.checked=true;  // don't allow unchecking of the "all" checkbox; unchecking is done by clicking on a specific subcity
            }
        });

        var preset = window.location.hash.replace(/#\d+;\d+\|(.*)$/,'$1')
        if (preset) {
            var toCheck = preset.split('|');
            for (var i=0; i<toCheck.length; i++) {
                //alert(toCheck[i]);
                cb = jQuery("input[@type=checkbox][name='areaId'][value='"+toCheck[i]+"']");
                if (toCheck.length > 0 || cb.id!='chkarea_all') {
                    jQuery("#chkarea_all").parent(".geo_browse_input").removeClass('geo_browse_choosen_item');
                    jQuery("#chkarea_all").removeAttr('checked');
                }
                cb.attr('checked', 'checked');
                cb.parent(".geo_browse_input").addClass('geo_browse_choosen_item');
            }
        }

        container.slideDown('slow');
    });
}

/**
 * Sets the header text.
 * @param data - a JSON object containing geo locations
 * @param header - the DOM element of the header
 * @param changeLink - a HTML string containg a link for changing the choosen location
 */
Immobrowse.prototype.setHeader = function(data, header, changeLink) {
    if (data != null) {
        var name = data['PARENT'];
        if ('276' == data['PARENTID']) {
            name = 'alle Bundesländer';
        }
    }
    header.empty();
    header.append('<div>' + name + ' ' + changeLink);
}

/**
 * Sets a choosen county ID as a cookie for preselecting the county on the next visit.
 * @param id the GEO ID of the choosen county
 */
Immobrowse.prototype.setCookie = function(id, _cookieName) {
    if (id != undefined && this.cookieName != '') {
        if (id == 'all_counties' || id == '276') {
            id = '276';
        }
        var path = '/';
        var expires = new Date();
        expires.setTime(expires.getTime() + (this.cookieMaxAge*1000));
        document.cookie = _cookieName+'='+id+'; path=/; expires='+expires.toGMTString();
    }
}

/**
 * Renders a 2nd level link which normally does just DHTML.
 * @param point - a JSON object holding data for a concrete location
 */
Immobrowse.prototype.intermediatedLink = function (point) {
    return '<a href="#" onClick="browse.showSubCities(' + point['ID'] + ',\'displaySubCitySelection\'); return false;">';
}

/**
 * Renders a 2nd level link which normally does just DHTML.
 * @param point - a JSON object holding data for a concrete location
 */
Immobrowse.prototype.intermediatedLink4Popup = function (point) {
    return '<a href="#" onClick="browse.showSubCities(' + point['ID'] + ',\'displaySubCitySelection4Popup\'); return false;">';
}

/**
 * Renders a 3rd level (final) link which normally links to another page.
 * @param point - a JSON object holding data for a concrete location
 */
Immobrowse.prototype.finalLink = function(point) {
    return '<a href="' + appendPathToUrl(browse.baseUrl, point['PATH'])+ '" onclick="browse.setCookie('+point['ID']+', \''+browse.cookieSubCityName+'\');" >';
}

/**
 * Shows the county selection.
 */
Immobrowse.prototype.expandCounties = function() {
    jQuery('#county_header_choose').show();
    jQuery('#county_header_choosen').hide();
    if (jQuery('#city_header_choose').css('display') == 'block') {
        jQuery('#city_header_choose').hide();
        if (this.choosenCounty != '') {
            jQuery('#city_header_choosen').show();
        }
    }
    if (jQuery('#subcity_header').css('display') == 'block') {
        jQuery('#subcity_header').hide();
        if (this.getChoosenCity() != undefined && this.getChoosenCity() != '') {
            this.setSubCityHeader();
            jQuery('#subcity_header_choosen').show();
            jQuery('#subcity_list').slideUp('slow');
        }
    }

    if (jQuery('#subcity_header_choose').css('display') == 'block') {
        jQuery('#subcity_header_choose').hide();
        if (this.getChoosenCity() != undefined && this.getChoosenCity() != '') {
            this.setSubCityHeader();
            jQuery('#subcity_header_choosen').show();
            jQuery('#subcity_list').slideUp('slow');
        }
    }
    
    jQuery('#city_list').slideUp(800, function () {
        jQuery('#county_list').slideDown('slow');
    });

    /*reset the url-hash for browser history*/
    window.location.hash = '';
}

/**
 * Shows the city selection.
 */
Immobrowse.prototype.expandCities = function() {
    jQuery('#city_header_choose').show();
    jQuery('#city_header_choosen').hide();
    jQuery('#county_header_choose').hide();
    jQuery('#county_header_choosen').show();
    if(this.getChoosenCity() != undefined && this.getChoosenCity() != ''){
        jQuery('#subcity_header').hide();
        jQuery('#subcity_header_choose').hide();
        this.setSubCityHeader();
        jQuery('#subcity_header_choosen').show();
        jQuery('#subcity_list').slideUp('slow');
    }else{
        if (jQuery('#subcity_header_choose').css('display') == 'block') {
            jQuery('#subcity_header_choose').hide();
            this.setSubCityHeader();
            jQuery('#subcity_header_choosen').show();
            jQuery('#subcity_list').slideUp('slow');
        }
    }
    jQuery('#county_list').slideUp(800, function () {
        jQuery('#city_list').slideDown('slow');
    });

    /*reset the url-hash for browser history*/
    window.location.hash = '';
}

/**
 * Shows the subcity selection.
 */
Immobrowse.prototype.expandSubCities = function() {
    jQuery('#subcity_header_choose').show();
    jQuery('#subcity_header_choosen').hide();
    jQuery('#county_header_choose').hide();
    jQuery('#county_header_choosen').show();
    if (jQuery('#city_header_choose').css('display') == 'block') {
        jQuery('#city_header_choose').hide();
        jQuery('#city_header_choosen').show();
    }
    jQuery('#county_list').slideUp('slow');
    jQuery('#city_list').slideUp(800, function () {
        jQuery('#subcity_list').slideDown('slow');
    });
}

/**
 * Makes an AJAX request to retrieve and render the list of cities for a choosen county.
 * @param id - the county ID
 */
Immobrowse.prototype.showCitiesForCounty = function(id, callback, hidden) {
    this.setCookie(id, this.cookieName);
    this.setCookie('', this.cookieCityName);
    this.setChoosenCounty(id);
    this.setChoosenCity('');
    if (!hidden) {
        jQuery('#city_header_choosen').hide();
        jQuery('#city_header_choose').show();
        jQuery('#subcity_header_choose').hide();
        jQuery('#subcity_header_choosen').hide();
        jQuery('#subcity_list').hide();
    }
    var searchParams='';
    var navigator='';
    if (id == 'all_counties') {
        navigator = this.allCountiesNavigator;
    } else {
        searchParams = 'areaId=' + id;
        navigator = this.countyNavigator;
    }
    jQuery('#city_list').hide();
    jQuery.getJSON(this.ajaxUrl,
        {ajax:true,searchKey:this.searchKey,searchParams:searchParams,navigator:navigator,parentId:id},
        jQuery.context(this).callback(callback)
    )
}

/**
 * Makes an AJAX request to retrieve and render the list of subcities for a choosen city.
 * @param id - the city ID
 */
Immobrowse.prototype.showSubCities = function (id, callback) {
    this.setCookie(id, this.cookieCityName);
    this.setChoosenCity(id);
    var searchParams = 'areaId=' + id;
    jQuery('#subcity_list').hide();
    jQuery.getJSON(this.ajaxUrl,
        {ajax:true,searchKey:this.searchKey,searchParams:searchParams,navigator:'countryidnav,geoid4nav',navigatorString:'countryidnav%2cgeoid4nav(filter%3d301%2f0%2f*)',parentId:id},
        jQuery.context(this).callback(callback)
    )

}

/**
 * The callback function to the AJAX request for displaying the city list.
 * @param data - the JSON response
 */
Immobrowse.prototype.displayCitySelection = function(data){
    this.setHeader(data, jQuery('#county_header_choosen'), '<a href="#" onClick="browse.expandCounties(); return false;">(ändern)</a></div>');
    jQuery('#city_header_choosen').empty();
    jQuery('#city_header_choosen').append('<div>Stadt oder Landkreis wählen <a href="#" onClick="browse.expandCities(); return false;">(ändern)</a></div>');
    jQuery('#county_header_choose').hide();
    jQuery('#county_header_choosen').show();
    this.renderLocations(data, jQuery('#city_list'), jQuery('#county_list'), this.intermediatedLink, '');
}

/**
 * The callback function to the AJAX request for preparing (but not displaying) the city list.
 * @param data - the JSON response
 */
Immobrowse.prototype.prepareCitySelection = function(data){
    this.setHeader(data, jQuery('#county_header_choosen'), '<a href="#" onClick="browse.expandCounties(); return false;">(ändern)</a></div>');
    //jQuery('#city_header_choosen').empty();
    //jQuery('#city_header_choosen').append('<div>Stadt oder Landkreis wählen <a href="#" onClick="browse.expandCities(); return false;">(ändern)</a></div>');
    jQuery('#county_header_choose').hide();
    jQuery('#county_header_choosen').show();
    this.renderLocations(data, jQuery('#city_list'), jQuery('#county_list'), this.intermediatedLink, '', true);
}

/**
 * The callback function to the AJAX request for displaying the city list.
 * @param data - the JSON response
 */
Immobrowse.prototype.displayCitySelection4Popup = function(data){
    this.setHeader(data, jQuery('#county_header_choosen'), '<a href="#" onClick="browse.expandCounties(); return false;">(ändern)</a></div>');
    var parentId = data['PARENTID'];
    jQuery('#city_header_choosen').empty();
    jQuery('#city_header_choosen').append('<div>Stadt oder Landkreis wählen <a href="#" onClick="browse.expandCities(); return false;">(ändern)</a></div>');
    jQuery('#county_header_choose').hide();
    jQuery('#county_header_choosen').show();
    this.renderCities4Popup(data, jQuery('#city_list'), jQuery('#county_list'), this.intermediatedLink4Popup, '');
    jQuery('#county_list div').removeClass('geo_layer_choosen_item');
    jQuery('#county_' + parentId).addClass('geo_layer_choosen_item');
}

/**
 * The callback function to the AJAX request for displaying the subcity list.
 * @param data - the JSON response
 */
Immobrowse.prototype.displaySubCitySelection = function(data){
    jQuery('#city_header_choose').hide();
    var hideContainer;
    var subcityHeader = jQuery('#subcity_header_choose');
    subcityHeader.empty();
    var allResultCaption = '';
    var parentId = data['PARENTID'];
    if (parentId == '998') {
        this.setHeader(data, jQuery('#county_header_choosen'), '<a href="#" onClick="browse.expandCounties(); return false;">(ändern)</a></div>');
        jQuery('#subcity_header_choosen').empty();
        jQuery('#subcity_header_choosen').append('<div>Land wählen <a onClick="browse.expandSubCities(); return false;" href="">(ändern)</a></div>');
        jQuery('#city_header_choose').hide();
        jQuery('#city_header_choosen').hide();
        jQuery('#city_list').hide();
        jQuery('#county_header_choose').hide();
        jQuery('#county_header_choosen').show();
        subcityHeader.append('<div>Land wählen</div>');
        hideContainer = jQuery('#county_list');
        allResultCaption = 'alle Länder';
    } else {
        this.setHeader(data, jQuery('#city_header_choosen'), '<a href="#" onClick="browse.expandCities(); return false;">(ändern)</a></div>');
        jQuery('#subcity_header_choosen').empty();
        jQuery('#subcity_header_choosen').append('<div>Stadtteil oder Gemeinde wählen <a onClick="browse.expandSubCities(); return false;" href="">(ändern)</a></div>');
        jQuery('#city_header_choosen').show();
        hideContainer = jQuery('#city_list');
        subcityHeader.append('<div>Stadtteil oder Gemeinde wählen</div>');
        allResultCaption = data['PARENT'] + ', alle';
    }
    jQuery('#subcity_header_choosen').hide();
    subcityHeader.show();
    var allResults = allResultCaption + ' ('+ data['DOCCOUNT'] +')';
    this.renderSubCities(data, jQuery('#subcity_list'), hideContainer, allResults, this.resultPage, this.searchButton);
}

/**
 * The callback function to the AJAX request for displaying the subcity list.
 * @param data - the JSON response
 */
Immobrowse.prototype.displaySubCitySelection4Popup = function(data){
    jQuery('#city_header_choose').hide();
    var hideContainer;
    var subcityHeader = jQuery('#subcity_header_choose');
    subcityHeader.empty();
    var parentId = data['PARENTID'];
    if (parentId == '998') {
        this.setHeader(data, jQuery('#county_header_choosen'), '<a href="#" onClick="browse.expandCounties(); return false;">(ändern)</a></div>');
        jQuery('#city_header_choose').hide();
        jQuery('#city_header_choosen').hide();
        jQuery('#city_list').hide();
        jQuery('#county_header_choose').hide();
        jQuery('#county_header_choosen').show();
        subcityHeader.append('<div>Land wählen</div>');
        hideContainer = jQuery('#county_list');
    } else {
        this.setHeader(data, jQuery('#city_header_choosen'), '<a href="#" onClick="browse.expandCities(); return false;">(ändern)</a></div>');
        jQuery('#city_header_choosen').show();
        hideContainer = jQuery('#city_list');
        subcityHeader.append('<div>Stadtteil oder Gemeinde wählen</div>');
    }
    jQuery('#subcity_header_choosen').hide();
    subcityHeader.show();
    var allResults = '';
    this.renderSubCities4Popup(data, jQuery('#subcity_list'), hideContainer, allResults);
    jQuery('#city_list div').removeClass('geo_layer_choosen_item');
    jQuery('#city_' + parentId).addClass('geo_layer_choosen_item');

}

Immobrowse.prototype.showEuropeInPopup = function() {
    jQuery('#county_list div').removeClass('geo_layer_choosen_item');
    jQuery('#county_998').addClass('geo_layer_choosen_item');
    this.showSubCities(998,'displaySubCitySelection4Popup');
}
