(function($)
{
    $.fn.CreateScoreboard = function(settings)
    {       
        // Config Array
        var config = $.extend(
        {
            url: '',
            noGamesError: '',
            noMeetsError: ''
        }, settings || {});
        
    //  =============
    //  Global Object
        var global = 
        {
            widgetContent: "",
            btnNext: null,
            btnPrev: null 
            /*{
                "Date": "June 8, 2010",
                "PrevLink": "http://172.16.16.192:4502/content/dalhousie/en/home/campus_life/athletics_recreation/news_events/scoreboard.json.html?d=06_07_2010",
                "NextLink": "",
                "GamesError": true,
                "Games": [],
                "MeetsError": false,
                "Meets": [
                    {
                        "Sport": "Cross-Country",
                        "Events": [
                            {
                            "Title":"June - Test Meet"
                            }
                        ]
                    }
                ]
            }
            */
        };
        
    //  =================
    //  Initiate Function
        var jQueryUIScoreboard = function() 
        {
            $('#tab-Games').setTemplateURL('/etc/designs/dalhousie/js/templates/WidgetScoreboardGames.txt');
            $('#tab-Meets').setTemplateURL('/etc/designs/dalhousie/js/templates/WidgetScoreboardMeets.txt');
            
            //updateContent();
            initControls();
        };
        
        function getContent()
        {            
            if (config.url.indexOf('json') < 0) {
                config.url = config.url.replace(/html/, "json.html");
            }
            
            // Create AJAX call to retrieve widget content
            $.getJSON(config.url, function(data)
            {
                global.widgetContent = data;
                updateContent();
            });
            
            // Move this call inside the success function of the ajax call
            //updateContent();
        }
        
        function initControls()
        {
            global.btnNext = $('.ui-scoreboard-btnNext', '.ui-scoreboard-date');
            global.btnPrev = $('.ui-scoreboard-btnPrev', '.ui-scoreboard-date');
            
            setControls();
        };
        
        function setControls()
        {
            global.btnNext.bind("click", function() { return false; });
            global.btnPrev.bind("click", function() { return false; });
            
            if (global.btnNext.attr('href') == '')
            {
                global.btnNext.removeAttr('href').css({ opacity: .5 });
                global.btnNext.unbind("click");
            }
            else
            {
                global.btnNext.css({ opacity: 1 });
                global.btnNext.bind("click", btnClicked);
            }
            if (global.btnPrev.attr('href') == '')
            {
                global.btnPrev.removeAttr('href').css({ opacity: .5 });
                global.btnPrev.unbind("click");
            }
            else
            {
                global.btnPrev.css({ opacity: 1 });
                global.btnPrev.bind("click", btnClicked);
            }
        };
        
        function btnClicked()
        {
            config.url = $(this).attr('href');
            if (config.url.indexOf('json') < 0) {
                config.url = config.url.replace(/html/, "json.html");
            }
            
            // Unbind the click event to prevent repeated calls
            global.btnNext.unbind("click");
            global.btnPrev.unbind("click");
                        
            // Rebind it to do nothing (this is for when someone clicks really fast)
            global.btnNext.bind("click", function() { return false; });
            global.btnPrev.bind("click", function() { return false; });
            
            // Get the content using AJAX
            getContent();
            return false;
        };
        
        function updateContent()
        {
            $('.tabs-holder', this).each(function()
            {               
                var id = $(this).attr('id');
                $(this).CreateTabs({ tabsID: id });
            });
            
            // Update day text
            $('.ui-scoreboard-title', '.ui-scoreboard-date').html(global.widgetContent.Date);
            
            $('.ui-scoreboard-btnPrev', '.ui-scoreboard-date').attr('href', global.widgetContent.PrevLink);
            $('.ui-scoreboard-btnNext', '.ui-scoreboard-date').attr('href', global.widgetContent.NextLink);
            
            setControls();
            
            if (global.widgetContent != null)
            {
                if (global.widgetContent.GamesError) {
                    $('#tab-Games').html("<p>" + config.noGamesError + "</p>");
                }
                else {
                    $('#tab-Games').processTemplate(global.widgetContent);
                }
                
                if (global.widgetContent.MeetsError) {
                    $('#tab-Meets').html("<p>" + config.noMeetsError + "</p>");
                }
                else {
                    $('#tab-Meets').processTemplate(global.widgetContent);
                }
            }
        };
        
        // Initiate the scoreboard
        jQueryUIScoreboard();
    };
})(jQuery);
