$(document).ready(function(){

    $("p.title").click(function(){
        $("div.mtitle").hide();
        $("div.filial_news").hide();
        $(this).next("div.filialList").toggle();
        var des_list = $(this).next("div.filialList").children("div.descr");
        var news_list = $("div.filial_news");
        var iteration = 0;
        for (var k=0; k<news_list.length;k++) {
            var realid = news_list[k].id.substr(5);
            for (var i=0; i<des_list.length;i++) {
                var newid = des_list[i].id.substr(4);
                if (realid == newid) {
                    if (iteration == 0) {
                        $("div.mtitle").show();
                    }
                    newid = 'fnws_'+newid;
                    $("div#"+newid).show();
                    //console.log(newid);
                    iteration++;
                }
                // console.log(newid);
                if (iteration == 3){
                    return;
                }
            }
        }
    });

    hideAllRegions();
    /******функция для исправления бага в ie с z-index*****/
    $(function() {
        var zIndexNumber = 1000;
        $('div.map div').each(function() {
            $(this).css('zIndex', zIndexNumber);
            zIndexNumber -= 10;
        });
    });

    var hl = 0,hg = 0,old_city=0;
    var selRegionMsg = '';

    jQuery(function($){

        hlA = function(id)
        {
            if (id == hl)
                return;

            oldhl = hl;
            if (oldhl !=0)
                {
                if (oldhl != 'area_0')
                    {
                    $("#"+oldhl).hide();
                }
            }
            hl = id;
        }

        sA = function(id) {
            $("#"+id).show();
        }

        hA = function(id) {
            if (hl != id) {
                $("#"+id).hide();
            }
        }
    })

})

var Region = undefined;

function setRegion(id){
    //   console.log(id);
    $("div.filial_news").hide();
    $("div.mtitle").hide();
    hideAllRegions();
    sA('area_'+id);
    hlA('area_'+id);
    var sel = 'selCity_'+id;
    var div = 'region_'+id;
    var li = $("#"+sel).children().get(0);
    //  console.log(li);
    $(li).attr("selected","selected");
    $("#"+sel).fadeIn("fast", function(){
        $("#"+div).fadeIn("fast");
    });



    Region = $('#'+div);
    showAllCities();
}

function hideAllRegions() {
    $("div.dep_info").hide();
    $("div.mapCity select").hide();
}

function setCity(id) {
    if (id > 0) {
        hideCities(id);
        var city = $("#city_"+id)
        if (checkCity(city)) {
            $(city).show();
        }
    } else {
        showAllCities();
    }
}

function checkCity(el) {
    var list = $("div.filialList",el).children();
    var length = list.length;
    var empty = 0;
    //  console.log(list);
    for (var i =0; i < length; i++) {
        //		console.log(list[i].style.display);
        if (list[i].style.display == 'block' || list[i].style.display == '') {
            empty++;
        }
    }
    //    console.log(empty);
    return empty;
}

function hideCities (id) {
    //  console.log(id);
    var parent = $("#city_"+id).parent().get(0);
    //	$(parent).children().hide();
    $("div.cities",parent).hide();
}

function showAllCities() {
    // console.log(Region);
    $(Region).show();
    var list = $("div.cities",Region);
    //  console.log(list);
    var length = list.length;
    for (var i =0; i < length; i++) {
        var empty = checkCity(list[i]);
        //		console.log(empty);
        if (empty == 0) {
            $(list[i]).hide();
        } else {
            //    console.log(list[i]);
            $(list[i]).show();
        }
    }
}

function checkCities(R) {
    var empty = 0;
    cityList = $("div.dep_info",R);
    cityLength = cityList.length;
    for (var l = 0; l < cityLength; l++) {
        empty = empty + checkCity(cityList[l]);
    }
    return empty;
}

function setType() {
    var type = $("div.mapTrade select").attr('value');
    if (type != '0') {
        hideAllPoints();
        // console.log(type);
        $("div."+type+'Block').show();
        var list = $("div.cities");
        var length = list.length;
        for (var i =0; i < length; i++) {
            var selector = 'div.'+type+'Block';
            var points = $(selector,list[i]);
            if (points.length < 1) {
                $(list[i]).hide();
            } else {
                $(list[i]).show();
            }
        }
    } else {
        showAllPoints();
    }

    list = $("div.cities");
    length = list.length;
    for (i = 0; i < length; i++) {
        //  console.log(list[i]);
        var empty = checkCities(list[i]);

        if (empty == 0) {
            createEmptyBox(list[i]);
            //			disable;
        } else {
            removeEmptyBox(list[i]);
        }
    }
}

function hideAllPoints() {
    $("div.azs").hide();
    $("div.agnks").hide();
    $("div.partners").hide();
}

function showAllPoints() {
    $("div.azs").show();
    $("div.agnks").show();
    $("div.partners").show();
    $("div.noElements").remove();
    $("div.cities").show();
}

function createEmptyBox(el){
    //	$(el).append('<div class="noElements">'+noElements+'</div>');
    //	$(el).clone('<div.noElements');
    $("div.noElements",el).remove();
    var div = document.createElement('div');
    div.appendChild(document.createTextNode(noElements));
    div.className = 'noElements';
    el.appendChild(div);
}

function removeEmptyBox(el){
    //	$("div.noElements",el).remove();
}
