var trim = function (str) {
    str = str.replace(/&nbsp;/g, ' ');
    return str.replace(/(^\s+)|(\s+$)/g, '');
}

var finded_max = 10;

    // подсказка - регионы
        // подсказка - регионы
    var city_core = function() {
        var start_win;
        var submit_btn;
        var initialize = false;

        var init = function() {
            var user_region = $.cookie('region_id')? $.cookie('region_id') : user_region;
            set_region(user_region);

            load_tpl('region_change', function() {
                main_block = $('#mesech1');
                submit_btn = $('#submit_btn');
                submit_btn.click(submit);

                $('#cname').focus(function(){
                    $(this).val(($(this).val() == 'Введите названия города')? '': $(this).val());
                }).blur(function(){
                    $(this).val(($(this).val() == '')? 'Введите названия города': $(this).val());
                }).keyup(function() { search(this.value) });

                start_win = new abstr_window($('#tooltip_changer'));
                var title = start_win.get_win().find('h1.title').html();
                start_win.get_win().find('h1.title').replaceWith('<h1 class="title">' + title + '</h1>Для правильного отображения страницы укажите ваш регион.');
                start_win.get_win().find('.close').click(function(){ 
                    $.cookie("no_more_choose_region", 1, {expires: 365})
                    start_win.hide(true, getSplScreen().hide(true));
                });
                search('');
                initialize = true;
                start();
            });
        }

        var start = function() {
            getSplScreen().show(true, function(){
                if (!initialize) {
                    getSplScreen().get_win().html('<div style="padding: 20px;">Загрузка...</div>');
                    setTimeout(start, 200);
                    return;
                }
                getSplScreen().get_win().html('');
                start_win.show(true);
                start_win.to_center();
            });
        }

        var set_region = function(id, hndl) {
            user_region = id;
            id? $.cookie("region_id", id, {expires: 365}) : '';
            $('.cname').html(cities[id]? cities[id] : 'неопределен');
            $('.close').trigger('click');
            if (hndl) hndl();
        }

        var search = function(text) {
            if (!trim(text).length) $('#cities').html('');
            var re = new RegExp(".*" + text + ".*", 'i')
            var ids = [];
            for (id in cities)
                if (re.test(cities[id]))
                    ids[ids.length] = id;
            show_cities(ids);
        }

        var show_cities = function(ids) {
            $('#cities').html('').removeClass('nofilter');
            var num = 0, city, el;
            if (!ids.length) {
                $('#cities').addClass('nofilter');
                $('#cities').html('<li>Подходящих городов не найдено.</li>');
            }
            for (var i in ids) {
                city = cities[ids[i]];
                el = $("<li><a href='#' class='dashlink' style='margin-bottom: 6px;' id='" + ids[i] + "' onclick='return false;'>" + city + '</a>\
                <span class="graytli-top-l"></span>\
                    <span class="graytli-botton-l"></span>\
                    <span class="graytli-top-r"></span>\
                    <span class="graytli-botton-r"></span>\
                </li>');
                el.find('a').click(function() { set_region($(this).attr('id'), function(){ window.location.reload() }) })
                num++;
                $('#cities').append(el);
            }
            $('#cities').find('li:last').css('border', 'none');
        }

        var submit = function() {
            $.cookie('region_id', user_region, { expires: 365 });
            js_api.change_loc(user_region, function(){});
        }

        var error = function(text) {
            alert(text);
        }

        loadCities(init);
    }

    var city_core = new city_core();
    
