 /*
 * This is the container and handler for builder sources.  A builder sorue is
 * a JS file (and the associated HTML snippet) that creates the feed flashvar
 * for a given data soruce (Flickr, Picasa, Media RSS, etc)
 * 
 * SOURCE DEFINITION
 * 
 * A source should be a JS module attached to the Sources 'namespace' like:
 * Sources.flickr = function() { ... }();
 * 
 * A source must register itself with Sources using the name of the source
 * Sources.register("flickr");
 *
 * Sources should have the following public methods:
 * 
 * getFeedURL()
 * Should return the URL (http or api) for the feed flashvar based on the 
 * UI selections for the source.
 * 
 * setState(feed)
 * Given the feed flashvar, set up the fields for the source appropriately
 * in the UI.  Basically the inverse of getFeedURL()
 * 
 * The following methods are optional.  If they are defined, will be called at 
 * particular times in the program flow:
 * 
 * init()
 * Called during (near the end of) the JQuery 'ready' stage.  Can be used for
 * any special source UI setup
 * 
 * preEmbed(embedFunc)
 * Called right before the builder tries to embed the SWF based on the Wall
 * object.  Any last-minute flashvar massaging or API calls should be done here
 * A callback to the function that does the embedding should be provided.
 * If the preEmbed function starts an asynchronous call, it should return true.
 * This tells Sources.preEmbed to not try to call the embedFunc itsef.
*/

var Sources = function(){
    var registeredSources = [];
    
    return {
        init: function() {
            $.each(registeredSources, function(i) {
                if (Sources[this].init) {
                    Sources[this].init();
                }
            });
            
            $("#source-select div.accord-button").click(function(event) {
                event.preventDefault();
                $(this).siblings().removeClass("ui-state-active");
                $(this).addClass("ui-state-active");
                $("#tab-what div.accord-panel").hide();
                var sourceName = $(this).attr('id').replace("item-tab-what-", "");
                $("#source-header div").text($(this).text()).removeClass().addClass(sourceName + "-title");
                $("#tab-what-" + sourceName).show();
                $("#source-select").accordion('activate', false);                
                $("#tips-text").html($(".tip-" + sourceName).html());
                $("#tips-text").show();
                Sources.current = Sources[sourceName];
                Wall.setFlashvar("source", sourceName);
                return false;
            });
        },
        
        getRegistered: function() {
            return registeredSources;
        },
        
        register: function(sourceName) {
            registeredSources.push(sourceName);
        },
        
        setState: function() {
            var sourceName = Wall.getFlashvar('source');
            if (!sourceName) {
                sourceName = Sources.getRegistered()[0];
            }
            Sources.current = Sources[sourceName];
            if (Sources.current.setState) {
                Sources.current.setState(Wall.getFlashvar('feed'));
            }
            if (!this.startClosed) {
                $("#source-select").accordion('activate', 0);                
            }
        }
    };
}();


