FACTOIDS = {};

FACTOIDS.app = function()
{
    function load()
    {
        var stamp = new Date();
        stamp = stamp.getTime();
        
        jQuery.ajax({
            type: "GET",
            url: "/Portals/0/factoids.xml?stamp=" + stamp,
            dataType: "xml",
            success: function(xml)
            {
                display(xml);
            }
        });
    }
    
    function display(xml)
    {
        var factoid = '';
        var factoids = [], displayFactoids = [];
        
        var howMany = jQuery(xml).find('numberToDisplay').text();
        
        //load factoids into an array
        jQuery(xml).find('facts > factoid').each(function(i)
        {
            factoid = jQuery(this).text();
            factoids[i] = factoid;
        });
        
        //randomize the order of factoids in the array
        factoids.sort(function()
        {
            return 0.5 - Math.random()
        });
        
        //write the factoids to the screen
        for (var j = 0; j < howMany; j++) {
            jQuery('#factoids').append(factoids[j] + '<br /><br />');
        }
    }
    
    return {
        init: function()
        {
            load();
            display();
        }
    };
}();

jQuery(document).ready(function()
{
    FACTOIDS.app.init();
});

