/**
 * @author ast
 * 
 */

/**
 * происходит, в часности, когда переключается js-tab
 */
function updateAllFilters(){
        
        Common.Class.remove(elMap.infoContaner, 'indexSelected');
        var IsSearch = isSearching();
        
        //if(!IsSearch){
                menu(targetCollection, function(node){
                        Common.Class.remove(node, 'hide-point');
                });
        //}
        
        if($('branches-search'))
        {
                if($('branches-search').value.length != 0)
                        if(IsSearch)  ps.startSearch($('branches-search').value);
                if(!IsSearch) elMap.showElementsInIndex(elMap.indexWithItems); // покажем весь индекс
    }
}

/**
 * 
 * @param {Object} ref
 */
function updateBySearchInput(ref){
        
}


function isSearching(){
        var input = $('branches-search');
        if(input){
                if(Common.Class.match(input, 'empty') || input.value.length == 0 || input.value == searchKeyWord){
                        Common.Class.remove($('Branches'), 'filtered')
                        return false;
                }
                else{
                        return true;
                }
        }
        else
        {
                return false;
        }
}


function getTargetCollection(){
        return getElementsByFilter($('Branches').getElementsByTagName('tr'), function(node){
    if(Common.Class.match(node, 'point'))
      if(!Common.Class.match(node, 'table_hidden'))
        return true;
    return false;
  });
}

var ps = {};

function initInit(targetCollection){
        elMap.getElementsCollection(targetCollection);
        elMap.setNotNullDependencies();
        
        elMap.searchInput = $('branches-search');
        
        if(document.getElementById('branches-search')){
          ps=new PageSearch('Branches', 'branches-search');
          ps.setParam('found_elements', ['table','tr']);
        }
        
        ps.onSearch=function(){
          if($('branches-search').value.length != 0)
            ps.startSearch($('branches-search').value)
                        
                if($('branches-search').value.length == 0)
                        Common.Class.remove($('Branches', 'filtered')) // убираем filtered если пустой поиск
        }
        
        ps.onSearchEnd=function(){
          if($('branches-search').value == '' || Common.Class.match($('branches-search'), 'empty')) elMap.showElementsInIndex(elMap.indexWithItems);
          if(ps.lastText == '') elMap.showElementsInIndex(elMap.indexWithItems);
          
          if($('branches-search').value.length != 0) elMap.FilterRefs();
          if($('branches-search').value.length == 0) Common.Class.remove($('Branches'), 'filtered')
        }
        
        var oSearchInput = $('branches-search');
        if (oSearchInput)
        
        menu($('f_examples').childNodes, function(span){
                if(Common.Class.match(span, 'pseudo-href'))
                {
                        
                        Common.Event.add(span, 'click', function(){
                                Common.Class.remove(oSearchInput, 'empty');
                                oSearchInput.value = span.innerHTML;
                                ps.startSearch(span.innerHTML);
                        });
                }
        });
}               

function ToggleOn()
{
        var fmap = $('f_map');
        var eatlasmap = $('CityMap');
        var branchesfilter = $('branches-filter');
        var fexamples = $('f_examples');
        
        console.log(branchesfilter);
        
        Common.Class.remove(eatlasmap, 'non-visible');
        if(branchesfilter) Common.Class.add(branchesfilter, 'disabled-element');
        if(fexamples) Common.Class.add(fexamples, 'disabled-element');
        
        //vtb24.tabs.switchTabItem($('f_map')); // @ast
        tabs.OnTabLabelClick($('f_map'))
}

function ToggleOff()
{
        var branchesfilter = $('branches-filter');
        var fexamples = $('f_examples');
        
        if(branchesfilter) Common.Class.remove(branchesfilter, 'disabled-element');
        if(fexamples) Common.Class.remove(fexamples, 'disabled-element');
        
        var flist = $('f-list');
        //var eatlasmap = $('CityMap');
          
        //Common.Class.add(eatlasmap, 'non-visible');
        
        //vtb24.tabs.switchTabItem($('f-list')); // @ast
        tabs.OnTabLabelClick($('f-list'));
        isSearching();
}





function ToggleServiceGroup(id)
{
        var node = document.getElementById(id);

        var i = 0;
        while(node = node.nextSibling)
        {
                i++;
                        
                if(classIs (node, 'service'))
                {
                        break;
                }
                else
                {
                        if(classIs (node, 'table_hidden'))
                                removeClass(node, 'table_hidden', '');
                        else
                                addClass(node, 'table_hidden');
                }
        }
}

function ToggleTypeGroup(id) {
	var node = document.getElementById(id);
	if (classIs(node, 'hidden')) {
		removeClass(node, 'hidden');
	}
	else {
		addClass(node, 'hidden');
	}
}

function classIs(node, name)
{
        if(node.className.indexOf(name) == -1)
                return false;
        else
                return true;
}

function removeClass(node, name)
{
        var posStart = node.className.indexOf(name);
        if(posStart != -1)
        {
                var classLeft = node.className.substring(0,posStart) + node.className.substring((posStart + name.length), node.className.length);
                node.className = classLeft;
        }
}

function addClass(node, name)
{
        node.className += ' ' +name;
}




