function initTransits() {

        var loader = new Loader({
            url: '/xml/routes',
            dataType: 'text',
            success: function(data){
                
                if ( $.browser.msie ) {
                    xml = new ActiveXObject("Microsoft.XMLDOM");
                    xml.async = false;
                    xml.loadXML(data);
                    
                } else {
                    xml = data;
                }

                var click = this.click;
                getMap().showPolyLines(data);
            }
        });

    $('a.transit').click(function() {
        $('a.transit').removeClass('selected');
        $(this).addClass('selected');
        loader.url = '/xml/routes/'+$(this).attr('id').substr(1);
        loader.show();
        loader.click = $(this);
        return false;
    });
}

function initStreets() {

     $('.buildings div').live('click', function(){
         getMap().showPoint($(this).attr('lat'), $(this).attr('lon'));
    });

    
    
        var loader = new Loader({
            url: '/xml/streets',
            dataType: 'text',
            success: function(data){
                
                if ( $.browser.msie ) {
                    xml = new ActiveXObject("Microsoft.XMLDOM");
                    xml.async = false;
                    xml.loadXML(data);
                    
                } else {
                    xml = data;
                }

                var click = this.click;
                getMap().showPolyLines(data);

                var block = $('<li class="buildings"></li>');
                var key = 0;
                
                $(xml).find('street').each(function(){
                    var number = $(this).find('title').text();
                    var lat = $(this).find('lat').text();
                    var lon = $(this).find('lon').text();
                    
                    if (number != ''){
                        //if (key) block.append(', ');
                        var numberLink = $('<div>' + number + '</div>');
                        numberLink.attr('lat', lat);
                        numberLink.attr('lon', lon);
                        block.append(numberLink);
                        key++;
                    }
                });
                if (key && !click.parent().find('ul').length) {
                    click.parent().append($('<ul></ul>').append(block));
                }
            }
        });

    $('a.street').click(function() {
        $('.buildings .selected').removeClass('selected');
        $('a.street').removeClass('selected');
        $(this).addClass('selected');
        loader.url = '/xml/streets/'+$(this).attr('id').substr(1);
        loader.show();
        loader.click = $(this);
        return false;
    });
}

(function($) {
$.fn.checkTree = function() {
    
    var $tree = this;

	$tree.find('input').attr('checked', '');

	$tree.find('li:has(li) > .arrow')
		.click(function() {
			var self = $(this);
			self
				.toggleClass('expanded')
				.toggleClass('collapsed')
                .siblings("ul:first")
                .toggleClass('hidden');
		});
    
    $tree.find('input').live('click', function() {
        var $this = $(this);
        
        var checked = $this.attr("checked");
        
        if (checked) {
            $this.siblings('ul:first').find('input:checkbox:not(:checked)').attr('checked', 'checked');
            getMap().onCheck($this.parent().attr('id').split('_')[1]);
        } else {
            $this.siblings('ul:first').find('input:checkbox:checked').attr('checked', '');
            getMap().onUnCheck($this.parent().attr('id').split('_')[1]);
        }

        for(var i=0; i<1; i++){
            $this = $this.parent().parent();
            if ($tree.get(0) == $this.get(0)) break;
            
            var parent = $this.parent().find('input:checkbox:first');
            var brothers = $this.find('input:checkbox');
            
            if (brothers.filter(':checked').length == brothers.length)
                parent.attr('checked', 'checked');
            else
                parent.attr('checked', '');
        }

        //return false;
    });

    initStreets();
    initTransits();
	
};
})($);

