 /*
 * 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)
 * 
 * DESTINATION DEFINITION
 *
 * A destination should be a JS module attached to the Sources 'namespace' like:
 * Destinations.blogger = function() { ... }();
 * 
 * A destination must register itself with Destinatons using the name of the 
 * destination:
 * Destinations.register("blogger");
 *
 * 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 destination UI setup
 * 
 * select()
 * Called when the destination is selected from the selector widget.
*/

var Destinations = function(){
    var registeredDests = [];
    
    return {
        init: function() {
            $("#dest-type").change(function(){
                $(".dest-type").hide();
                $("#" + $(this).val()).show();
                
                if ($(this).val() == "dest-type-post" && Destinations['gigya']) {
                    Destinations.current = Destinations['gigya'];
                    Destinations.current.select();
                }
                
                if ($(this).val() == "dest-type-widget") {
                    $("#layout").removeClass().addClass("layout_side");
                }
                else {
                    $("#layout").removeClass().addClass("layout_main");
                }
            });
            $("#dest-type").change();
            
            $.each(registeredDests, function(i) {
                if (Destinations[this].init) {
                    Destinations[this].init();
                }
            });
            
            if (registeredDests.length == 1) {
                Destinations.current = Destinations[registeredDests[0]];
                $(".layout").hide();
                Destinations.current.select();
            }
            
            $("#dest-select div.accord-button").click(function(event) {
                event.preventDefault();
                $(this).siblings().removeClass("ui-state-active");
                $(this).addClass("ui-state-active");
                $("#dest-type-widget>div.accord-panel").hide();
                var destName = $(this).attr('id').replace("item-tab-where-", "");
                $("#tab-where-" + destName).show();
                Destinations.current = Destinations[destName];
                Wall.setFlashvar('dest', destName);
                Destinations.current.select();
                return false;
            });
            $("#dest-select div.accord-button:first").click();
        },
        
        getRegistered: function() {
            return registeredDests;
        },
        
        register: function(destName) {
            registeredDests.push(destName);
        },
        
        setState: function() {
            var dest = Wall.getFlashvar("dest");
            if (!dest) {
                dest = Destinations.getRegistered()[0];
            }
            $("#item-tab-where-" + dest).click();
            // $("#dest-select").accordion('activate', 0);
        }
    };
}();

