//This script handles loading of the foobar from the very beginning, including checking jquery and jquery ui
//It will set no conflict mode if it detects prototype has also been loaded

(function(){
    var scriptsloaded = {};
    (function(){
        if (!window['jQuery']){
            if (!scriptsloaded['jquery']){
                var head = document.getElementsByTagName('head')[0];
                jquery = document.createElement('script');
                jquery.type = 'text/javascript';
                jquery.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js';
                head.appendChild(jquery);
                scriptsloaded['jquery'] = 1;
                
                var setnoconflict = function(){
                    if (window['Prototype'] && $ == jQuery){
                        jQuery.noConflict();
                    }
                }
                jquery.onreadystatechange = function() {
                    if (jquery.readyState == 'loaded' || jquery.readyState == 'complete') setnoconflict();
                }                            
                jquery.onload = function() {
                    setnoconflict();
                }
            }
            setTimeout(arguments.callee, 50);
            return;
        }
        if (!jQuery.ui){
            if (!scriptsloaded['jqueryui']){
                jQuery.ajax({
                    url: 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/jquery-ui.min.js',
                    dataType: 'script',
                    cache: true
                });
                scriptsloaded['jqueryui'] = 1;
            }
            setTimeout(arguments.callee, 50);
            return;
        }
        if (!window['FooBar']){
            if (!scriptsloaded['foobar']){
                jQuery.ajax({
                    url: 'http://static.usnews.com/scripts/jquery/foobarjs.js',
                    dataType: 'script',
                    cache: true
                });
                scriptsloaded['foobar'] = 1;
            }
            setTimeout(arguments.callee, 50);
            return;
        }
        //no conflict version of the placement script for beehive pages -- copied directly from colleges.js on 02/25/11
        (function($){
            $(document).ready(function(){
                if (window['FooBar']){
                    //ensure we have a placeholder.  do this before any foobar code so event handlers will be triggered.
                    var placeholder = $('#foobar');
                    if (!placeholder.length) placeholder = $('<div/>').attr('id', 'foobar').appendTo($('#footer'));
                    //logout/login links in the banner
                    var buttons = $('.foobar-login');
                    var greetings = $('.foobar-greeting');
                    /*$('.foobar-login').click(function(){
                        if (FooBar.initialized){
                            if (FooBar.isloggedin()) FooBar.dologout();
                            else FooBar.dologin();
                        }
                        return false;
                    });*/
                    //show the name, etc in the header
                    $('#foobar').bind('foobarinit', function(){
                        if (FooBar.isloggedin()){
                            greetings.show();
                            greetings.children('span').html(FooBar.getname());
                            buttons.hide().filter('.logout').show();
                        } else {
                            greetings.hide();
                            buttons.show().filter('.logout').hide();
                        }
                    });
                    //engage
                    FooBar.init(placeholder, '/toolbar');
                }
            });
        })(jQuery);
    })();
})();
