$(document).ready(function(){
    $("a.external").append("<span>&nbsp;</span>");

    /**********************
     *THIS CODE FIXES A Z-INDEX BUG IN IE7
     *THAT PLACES THE TOOLTIPS FOR THE SERVICES
     *IN THE HEADER, BEHIND THE BACKGROUND FOR THE TEMPDIV
    ***********************/
    $(function() {
        var zIndexNumber = 1000;
        $('#header div').each(function() {
            $(this).css('zIndex', zIndexNumber);
            zIndexNumber -= 10;
        });
    });

});  // END OF DOCUMENT READY



//
//FUNCTIONS
//

$.fn.blockTabs = function(callback) {
    var block = $(this);
    block.find(".block_tabs a").click(function(){
        if ($(this).hasClass("tab_active")){return;}

        // Remove the tab_active class from any other tabs, the add it to this
        block.find(".block_tabs .tab_active").removeClass("tab_active");
        $(this).addClass("tab_active");

        // Get the class name of the target from href, remove the anchor #
        //NOTE IE changes the href value to full url after.  Sometimes
        href = $(this).attr("href").split('#');
        content_class_name = href[1];

        // Find the content div
        block.find(".tab_content_active").removeClass("tab_content_active").hide();
        block.find("."+content_class_name).addClass("tab_content_active").show().focus();
    });

    //Parse out an anchor from the URL, and open that tab
    autoselect = true;
    if(window.location.href.indexOf('#') > 0) {
        anchor = window.location.href.split('#')
        if(anchor[1].length > 0){
            selector = '.block_tabs a[href="#'+anchor[1]+'"]';
            if ($(selector).is("a")){
                autoselect = false;
                $(selector).click();
            }
        }
    }
    if(autoselect){block.find(".block_tabs li a").last().click();}
    if(callback != undefined){callback();}
}

$.fn.newsFeed = function(id,name,url){
    var feedBlock = $(this);
    if(!feedBlock.hasClass('feed')){
        feedBlock.addClass('feed');
    }
    $.getFeed({
        'url':'/rss_proxy.php?url='+url,
        success: function(feed){
            //BUILD CONTENT
            // Add to the source list
            feedBlock.find(".news_navbar").append(
                '<li><a href="#article_'+id+'" onclick="return false;">'+name+'</a></li>');
    
            for(var i = 0; i < feed.items.length && i < 6; i++) {
                var item = feed.items[i];
    
                // Add to the article list
                feedBlock.find(".nf_list").append(
                    '<li class="article_'+id+'"><a href="#story_'+ id + '_' + i +'" onclick="return false;">'+ item.title +'</a></li>');
    
                // Add to the story list
                feedBlock.find(".nf_story ul").append(
                    '<li class="story_'+ id + '_' + i +'">'
                    + '<div class="story_title"><a href="'+item.link+'" target="_blank">'+item.title+'</a></div>'
                    + item.description + '</li>');
    
            }
            feedBlock.find(".nf_list").append(
                '<li class="article_'+id+'"><a href="'+ feed.link +'" class="site_link" target="_blank">All News Articles</a></li>'
            );
    
            //SETUP SOURCE -> ARTICLE LINKS
            feedBlock.find(".news_navbar a").click(function(){
                href = $(this).attr("href").split('#');
                article_name = href[1];
                feedBlock.find(".nf_list .article_active").hide();
                feedBlock.find(".nf_list ."+article_name).addClass('article_active').show();
                feedBlock.find(".news_navbar .nf_source_active").removeClass('nf_source_active');
                $(this).addClass('nf_source_active');
                feedBlock.find(".nf_story").hide();
            });
    
            //SETUP ARTICLE -> STORY LINKS
            feedBlock.find(".nf_list a").click(function(){
                href = $(this).attr("href").split('#');
                news_class_name = href[1];
                feedBlock.find(".nf_story").show();
                feedBlock.find(".story_active").hide();
                feedBlock.find(".nf_story ."+news_class_name).addClass('story_active').show();
            });
            feedBlock.find(".nf_story .nf_story_hide").click(function(){
                feedBlock.find(".nf_story").hide();
            });
    
            // Load First News 
            feedBlock.find(".news_navbar a").first().click();

    }})
};
