jQuery( function($) 
{
    $('#add_topic').addClass('hidden');
    var topic_tree = [];
    var country_name; 
    // this function is not longer needed here
    var ParseTopicTreeJsonToHtml = function(json_resp) 
    {
        var i = 0;
        var j = 0;
        //flev is sort of "first level" 
        //slev is short for "second level"  ? 
        for(var flev_topic in json_resp) {
            if(flev_topic === "All Topics") {continue;}
            i = topic_tree.length;
            topic_tree[i] = { 'name':flev_topic, 'children':[] };
            for(var slev_topic in json_resp[flev_topic]) {
                j = topic_tree[i].children.length;
                topic_tree[i].children[j] = { 
                    'name':slev_topic, 
                    'children':json_resp[flev_topic][slev_topic] 
                };
            }
        }
        // run the tree through the trimpath template
        var data = {
            'path':location.pathname, 
            'country':country_name,
            'topic_tree':topic_tree
        };
        var overlay = TrimPath.processDOMTemplate("region_selector", data);
    }
    var makeOverlay = function()
    {
        var ol = $('<div class="hidden" id="overlay"></div>');
        ol.append($('#nav_topic_list_inner').clone().get(0));
        // remove that extra little div that does the visual connection
        $('#nav_topic_list_connector',ol).remove();
        // get ready to make the urls of each cross page
        // we want urls like this:
        //  http://roubinibeta.rgemonitor.com/topic/government-bonds.php?bg=Government%20Bonds&rg=Estonia 
        var base_url = window.location.href;
        var this_page = $('#main_head H1').text().replace(/ /g,'+'); 
        // set up the links
        $('A[href]',ol).each( function() { 
            if (this.href.match(/topic/) ) {
                if (typeof(this.text) == 'undefined') { this.text = this.innerHTML; }
                var link_part = this.text.replace(/ /g,'+');
                this.href = base_url + "?bg=" + this_page + "&tp=" + link_part;
            } else { 
                $(this).before('<div>' + this.text + '</div>').remove();
            }
        });
        // make the subtopics headers not linkable
        $('A[href].subtopic',ol).each( function() { 
            $(this).css({'font-weight': 'bold'});
        });
        // make the top level supertopic headers not linkable
        $('#nav_topic_list_inner>TABLE.nav_topic_list>TBODY>TR>TD',ol).each( function() {
            $('A:first',this).css({'font-weight': 'bold','font-size':'11px'});
        });
        return ol;
    }

    var setupOverlay=  function() {

		var preventScroll = function(off) {
			if (off) {
				$('#all_content').css('height', 'auto');
			} else {
				$('#all_content').css('height', $(window).height());
			}
		};

        var overlay = makeOverlay();
        var showing_cross = false;
        var scrim = $('<div id="cross-scrim">&nbsp;</div>');
        var clone_button = $('<div id="clone_add_topic" class="button">  ADD TOPIC &nbsp; <span class="close">[ CLOSE X ]</span></div>');
        $('body').append(scrim);
        $('body').append(overlay);
        clone_button.addClass('active');
        clone_button.removeClass('hidden');
        $('#overlay').prepend(clone_button);
        $('#add_topic').removeClass('hidden');
        $('#clone_add_topic').click(function(event){hideRegionCross();});
        var closeOnEsc = function(e) {
            // close form if ESC is pressed
            if (e.keyCode == 27) {
                hideRegionCross();
            }
        }
        var hideRegionCross = function() {
			preventScroll('off');
            scrim.removeClass('visible');
            $('#overlay').addClass('hidden');       
            $('#add_topic').removeClass('hidden');  
            showing_cross=false;
        }
        var showRegionCross = function()
        {
			preventScroll();
            scrim.addClass('visible');
            $('#overlay').removeClass('hidden');
            $('#add_topic').addClass('hidden');     
            $(document).bind('keyup', closeOnEsc);
            showing_cross=true;
        }
        $('#add_topic').click(function(event) {
            if (showing_cross) { 
                hideRegionCross(); 
            } else { 
                showRegionCross();
            }
            return false;
        });
    }
        var init_region_cross = function()
        {
            country_name = $('#main_head > h1').text();
            setupOverlay();
        };
        init_region_cross();
});