Destinations.gigya = function() {
    Destinations.register("gigya");
    
    var textareaID = "gigya_embed_code";
    
    var config={
        widgetTitle: "My Cooliris Wall",
        widgetDescription: "A 3D wall of rich media brought to you by Cooliris",
        useFacebookMystuff: 'false', 
        // facebookURL: 'http://www.cooliris.com/',
        defaultContent: textareaID, 
        UIConfig: '<config baseTheme="v2"><display showEmail="false" useTransitions="false" showPost="true" showBookmark="false" networksToHide="igoogle" networksToShow="facebook, twitter, myspace, blogger, typepad, wordpress, yahoo, livejournal, livespaces, friendster, orkut, bebo, tagged, hi5, piczo, freewebs, blackplanet, myyearbook, vox, xanga, multiply, netvibes, pageflakes, migente, ameba, liverdoor, eons, overblog, *"></display><body><background frame-color="#000000" background-color="Transparent" frame-thickness="0" gradient-color-begin="Transparent" gradient-color-end="Transparent" corner-roundness="1;1;1;1"></background><controls color="#F4F4F4" font="Arial" size="12"><snbuttons frame-color="#666666" over-frame-color="#dbdbdb" color="#F4F4F4" gradient-color-begin="#6A6A6A" gradient-color-end="#2B2B2B" bold="true" down-frame-color="#074F7F" down-gradient-color-begin="#000000" over-gradient-color-end="#606060" down-gradient-color-end="#4A4A4A" over-color="#074F7F" down-color="#074F7F" over-bold="true" over-gradient-color-begin="#8A8A8A" down-bold="true"><more frame-color="#666666" over-frame-color="#dbdbdb" gradient-color-begin="#6A6A6A" gradient-color-end="#2B2B2B" over-gradient-color-begin="#8A8A8A" over-gradient-color-end="#606060" down-frame-color="#074F7F" down-gradient-color-begin="#000000" down-gradient-color-end="#4A4A4A"></more><previous frame-color="#666666" over-frame-color="#dbdbdb" gradient-color-begin="#6A6A6A" gradient-color-end="#2B2B2B" over-gradient-color-begin="#8A8A8A" over-gradient-color-end="#606060" down-frame-color="#074F7F" down-gradient-color-begin="#000000" down-gradient-color-end="#4A4A4A"></previous></snbuttons><servicemarker gradient-color-begin="#074F7F" gradient-color-end="#074F7F"></servicemarker><textboxes><codeboxes color="#CCCCCC" frame-color="#58595B" background-color="#333333" gradient-color-begin="#333333" gradient-color-end="#333333" corner-roundness="0;0;0;0"></codeboxes></textboxes></controls><texts color="#F4F4F4" bold="true"><messages color="#FFFFFF" background-color="#666666"></messages><links color="#F4F4F4"></links></texts></body></config>'
    };
    
    return {
        id: 'gigya',
        displayName: 'Blog Post',
        select: function() {
            if (!Sources.current) {
              return;
            }
            
            Wall.setProp('width', 400);
            Wall.setProp('height', 248);
            
            $("#" + textareaID).text(Wall.getEmbedCode());
            
            $('#website-content').show();
            var source = Sources.current;
            
            var thumb = Wall.getFlashvar("theme").replace("theme-", "");
            if (thumb == 'none') {
                thumb = 'black';
            }
            thumb += Wall.getFlashvar("numrows");
            
            var wallTitle = Wall.getProp("title");
            var wallUrl;
            var params;
            if (Wall.getProp("id")) {
                params = {
                    z: Wall.getProp("id"),
                    t: new Date().getTime()
                };
                wallUrl = getPath() + "gallery.php" + implodeQueryParameters(params);
            }
            else {
                params = {
                    t: new Date().getTime(),
                    type: source.type,
                    source: source.displayName,
                    thumb: thumb,
                    flashvars: Base64.encode(Wall.getQueryString())
                };
                wallUrl = getPath() + "wall.php" + implodeQueryParameters(params);
            }
            
            var BITLY_USER = "coolirisholidays";
            var BITLY_API_KEY = "R_a30e568944aea98952c16e6aaff13487";

            var reqUrl = "http://api.bit.ly/shorten?version=2.0.1&login=" + BITLY_USER + "&apiKey=" + BITLY_API_KEY + "&callback=?&longUrl=" + encodeURIComponent(wallUrl);

            // FIXME: This does a superfluous bitly request on page load.
            $.getJSON(reqUrl, function(response){
                var shortUrl;
                if (response && response.errorCode == 0) {
                  shortUrl = response.results[wallUrl].shortUrl; 
                }
                else {
                  shortUrl = wallUrl; 
                }
                config.widgetTitle = wallTitle;
                config.widgetDescription = "A " + source.displayName + " 3D wall brought to you by Cooliris";
                config.defaultBookmarkURL = wallUrl;
                    config.twitterBookmarkURL = shortUrl;
                config.facebookURL = 'http://www.facebook.com/sharer.php?u='+encodeURIComponent(wallUrl)+'&t='+encodeURIComponent(wallTitle);
            
                Wildfire.divWildfirePost.applyConfig(config);
            });
        },
        
        init: function() {
            Wildfire.initPost('902051', 'divWildfirePost', 255, 300, config);
        }
    };
}();
