var map_timer_id;

jQuery(document).ready(function($) {
    jQuery('.current_city span').click(function(event){
        event.preventDefault();
        var offset = jQuery(this).offset();
        jQuery('#select_city').trigger('click');
        jQuery('#city-selector-gl').css({'left':offset.left-38, 'top':offset.top-65});

        jQuery('#list_city td a').bind('click', function(event) {
            event.preventDefault();
            var el=jQuery(this);
            jQuery('.current_city span').text(el.text());
            var param=el.attr('href').split('=');
            jQuery.each(city_phones, function(index, value) {
                if (value.code==param[1]) {
                    sel_city=value.idcity;
                    jQuery('input#city').attr('value', sel_city);
                    jQuery('.search_result span.phone').text(value.phone);
                    //jQuery('.search_result a#dgis_link').attr('href', value.dgis);
                    jQuery('input#street').flushCache();
                    jQuery('input#street').setOptions({extraParams: {idcity:sel_city}});
                    SetCity(value.code);
                }
            });
            jQuery('#lcity_cbtn').trigger('click');
        });
    });

    jQuery("input#street").autocomplete('streets.php', {
        width: 338,
        multiple: false,
        dataType: "json",
        extraParams: {idcity:sel_city},
        parse: function(data) {
            return jQuery.map(data, function(row) {
                return {
                    data: row,
                    value: row,
                    result: row
                }
            });
        },
        formatItem: function(item) {
            return item;
        }
    });

    jQuery('.button_submit input').click(function(){
        if ($('#type_search').attr('value')!='3')
        {
            var len_street=jQuery('.input_street input').attr('value').length;
            if (len_street==0) {
                alert('Введите название вашей улицы');
                return false;
            }
            var len_home=jQuery('.input_home input').attr('value').length;
            if (len_home==0) {
                alert('Введите номер вашего дома');
                return false;
            }
        } else {
            var len_phone=jQuery('.input_phone input').attr('value').length;
            if (len_phone==0) {
                alert('Введите номер телефона');
                return false;
            }
        }
        return true;
    });

    map_timer_id=setTimeout(checkMapLoad, 2000);
});

function setRegion(code) {
    jQuery.each(city_phones, function(index, value) {
        if (value.code==code) {
            sel_city=value.idcity;
            jQuery('.current_city span').text(value.name);
            jQuery('input#city').attr('value', sel_city);
            jQuery('input#street').flushCache();
            jQuery('input#street').setOptions({extraParams: {idcity:sel_city}});
            jQuery('.search_result span.phone').text(value.phone);
            //jQuery('.search_result a#dgis_link').attr('href', value.dgis);
            if (value.typeconnect=='3')
            {
                $('#map_form_title').text('Введите ваш номер телефона');
                $('.search_form div.input_street').addClass('notshow');
                $('.search_form div.input_home').addClass('notshow');
                $('.search_form div.input_phone').removeClass('notshow');
                $('#how_connect_1').addClass('notshow');
                $('#how_connect_2').removeClass('notshow');
                $('#type_search').attr('value', '3');
            } else {
                $('#map_form_title').text('Введите название Вашей улицы и номер дома');
                $('.search_form div.input_street').removeClass('notshow');
                $('.search_form div.input_home').removeClass('notshow');
                $('.search_form div.input_phone').addClass('notshow');
                $('#how_connect_1').removeClass('notshow');
                $('#how_connect_2').addClass('notshow');
                $('#type_search').attr('value', '1');
            }
        }
    });
}


function checkMapLoad() {
    getMovie().SetVariable("check_ready","check");
    map_timer_id=setTimeout(checkMapLoad, 2000);
}
function mapReady() {
    clearTimeout(map_timer_id);
    jQuery.each(city_phones, function(index, value) {
        if (value.idcity==sel_city) {
            SetCity(value.code);
        }
    });
}
function getMovie() {
    var M$ =  navigator.appName.indexOf("Microsoft")!=-1
    return (M$ ? window : document)["ttkmap"]
}
function SetCity(code)
{
    getMovie().SetVariable("region_code",code);
    setRegion(code);
}

