 /*
 * JavaScript for a SharePopup class and functions to show the share popup
 *
 * Dependencies:
 *    share.css
 *    jquery-latest.js
 *    thickbox
 *    addressbook.js
 *    open_window.js
 *    clipboard.js
 *    swfobject.js
 *
 */

if (typeof(snapvine) == "undefined") snapvine = function() {};

function safe_share_track(state) { safeUrchinTracker("/share/share_popup_state_" + state); }


/*
 * Constructor
 */
snapvine.SharePopup = function(obj, options) {
    this.initialize(obj, options);
};

snapvine.SharePopup.prototype = {
    
    initialize: function(obj, options)
    {
        this.options = { title: "",
                         embed_urn: "",
                         show_urn: "",
                         post_urn: "",
                         show_codes: true,
                         ab_share_urn: "",
                         fb_share_urn: "",
                         popup_dom: "/share/popup",
                         bookmark_urn: "/share/bookmark_popup"};

        Object.extend(this.options, options || {})        

        document.body.sv_sharepopup = this;
    },


    show: function()
    {
        if (!this.is_dom_loaded()) {
            $("body").append("<div id='share_popup_container'></div>");
            $("body #share_popup_container").load(this.options.popup_dom, "", this.dom_loaded);
        } else  {
            this.dom_ready();
        }
    },

    dom_loaded: function()
    {
        var popup = document.body.sv_sharepopup;
        popup.bind_dom();
        popup.dom_ready();
    },
    
    dom_ready: function()
    {
        $('#share_content').show();
        $('#share_loading').hide();
        $('#bookmark_popup').hide();        

        $("#share_content .the_title").html(document.body.sv_sharepopup.options.title);        
        $("#share_content #url_section #url")[0].value = document.body.sv_sharepopup.options.show_urn;

        if (document.body.sv_sharepopup.options.show_codes) {
            $('#share_content #url_section .count').html("2.");
            $('#share_content #email_section .count').html("3.");
            $('#share_content #embed_section').show();
        } else {
            $('#share_content #url_section .count').html("1.");
            $('#share_content #email_section .count').html("2.");
            $('#share_content #embed_section').hide();
        }
        
        this.load_embed_codes();
        this.draw();
    },

    draw: function()
    {
        var height = 425;
        var title = '<h3>Share A ' + this.options.title + '</h3>';
        var tb_options = '#TB_inline?height=' + height + '&width=450&inlineId=share_main&overlayBG=lt'
        tb_show(title, tb_options, null);
    },

    
    bind_dom: function()
    {
        // - Bind post/ab links
        
        snapvine.SharePopup.bindEmailLink(function() {
                document.body.sv_sharepopup.share_ab_popup(this); return false;
            });
        snapvine.SharePopup.bindPostCodesLink(function() {
                document.body.sv_sharepopup.post_codes_popup(this, null); return false;
            });
        snapvine.SharePopup.bindCopyPaste();
        
        // Bind bookmarks
        $("#share_content #bookmark_section a").click(function() {
                document.body.sv_sharepopup.bookmark_popup(this); return false;
            });

        $("#share_content #fb_share_link").click(function(){
                document.body.sv_sharepopup.fbs_click(this); return false;
            });
    },

    load_embed_codes: function()
    {
        $('#share_content #embed_section #embed').val("");
        $("#share_content #embed_section .codes_area").hide();
        $('#share_content #embed_section .codes_loading').show();
        
        $.ajax( { type: 'GET',
                    url: this.options.embed_urn,
                    dataType: "html",
                    error: function() { alert('Error loading embed codes'); },
                    success: function(data, status) {
                        $('#share_content #embed_section #embed').val(data);
                        $('#share_content #embed_section .codes_loading').hide();
                        $("#share_content #embed_section .codes_area").show();
                }
            });
    },
    
    close_all_popups: function() { tb_remove(); },

    post_codes_popup: function(obj, site)
    {
        safe_share_track("post_codes");
        
        var the_url = this.options.post_urn + "?popup=1&w=890";
        if (site != null)
            the_url += "&site=" + site;
        
        open_popup_window(the_url, 910, 670);
        this.close_all_popups();
    },

    share_ab_popup: function(obj)
    {
        var the_url = this.options.ab_share_urn + "?popup=1";
        ab_popup_url(the_url);

        this.close_all_popups();
    },

    fbs_click : function(obj)
    {
        safe_share_track("facebook_link");
        
        var share_url= this.options.fb_share_urn;
        var the_url = 'http://www.facebook.com/sharer.php?u='+encodeURIComponent(share_url);
        open_popup_window(the_url, 626, 436);

        this.close_all_popups();
    },
    
    
    bookmark_popup : function(obj)
    {
        var bookmark_site = obj.className;
        
        safe_share_track("bookmark_"+bookmark_site);
        
        var the_url = "";
        the_url += this.options.bookmark_urn + "?popup=1&w=600";
        the_url += "&site="+bookmark_site;
        the_url += "&share_urn="+this.options.show_urn;
        
        open_popup_window(the_url, 620, 500);
        this.close_all_popups();

        /*
        $.ajax( { type: 'GET',
                    url: this.options.bookmark_dom,
                    data: {site: bookmark_site, bookmark_url: this.options.show_urn},
                    dataType: "html",
                    error: function(xml, status, e) {},
                    success: function(data, status) {}
            );
        */    
    },
    
    
    is_dom_loaded: function()
    {
        return $("body #share_popup_container #share_main").length > 0;
    }
};


/*
 * Static helper method which binds the copy and paste sections
 * to the clipboard functionality
 */
snapvine.SharePopup.bindCopyPaste = function(embed_callback, url_callback) {
    var embed_cb = embed_callback || function() {safe_share_track('copypaste_embed');}
    var url_cb = url_callback || function() {safe_share_track('copypaste_url');};
    
    // Bind the copy&paste links
    $('#share_content #embed_section #embed').click(function() {
            do_copy_jquery('#share_content #embed', '#share_content #copy_status_embed');
            embed_cb();
        });  
    $('#share_content #url_section #url').click(function() {
            do_copy_jquery('#share_content #url', '#share_content #copy_status_url');
            url_cb();
        });
};


/*
 * Static helper to bind a function to the "share by email" link
 */
snapvine.SharePopup.bindEmailLink = function(func) {
    $("#share_content a.send").click(func);
};


/*
 * Static helper to bind the "Autoinsert" link
 */
snapvine.SharePopup.bindPostCodesLink = function(func) {
    $("#share_content a.post").click(func);
};


/*
 * Static helper to directly set the content within the share ui
 */
snapvine.SharePopup.setContent = function(title, embed_codes, url) {
    $("#share_content .the_title").html(title);

    $('#share_content #embed_section #embed').val(embed_codes);
    $("#share_content #url_section #url").val(url);
    $("#share_content #embed_section .codes_area").show();
};


/*
 * show share popup
 *
 *  click_obj     is typically the "this" object
 *  options
 *    title         Title to show in the popup
 *    embed_urn     is the URL you visit with an Ajax call to return the embed codes
 *    show_urn      is the actual URL that users will share
 *    post_urn      URN to direct the user to for the "post this" action
 *    ab_share_urn  URN to direct the addressbook response to for batch sharing.
 *    fb_share_urn  URN to the facebook xml data
 *    show_codes    Boolean to turn off posting & embed
 */
snapvine.show_share = function(click_obj, options)
{
    safeUrchinTracker("/share/share_popup_show");
    
    var p = new snapvine.SharePopup(click_obj, options)
    p.show();
}

