/* Class for the Google +1 Button module */
var google_plusone = $.inherit(Module, {
    __constructor: function(moduleDivObject) { 
        this.__base(moduleDivObject);
    },

    // When loading the published page, insert the <g:plusone> tag so the 
    // google-provided script can turn it into a real button
    initialize: function() {
        // Get the options for this button from the database
        this.getButtonOptions(createRef(this, this.addGTags));
    },

    // Add the <g:plusone> tags with size and url data
    addGTags: function() {
        // IE Has a REALLY hard time with Google's code, so let's bypass it with something
        // that works.
        if ($.support.leadingWhitespace) {
            this.container.empty();
            $('<g:plusone href="' + this.options.url + '" size="' + this.options.size + '"></g:plusone>').appendTo(this.container);
            $.getScript('https://apis.google.com/js/plusone.js', function() {
                // Manually render the button, since it only gets automatically rendered the first time the page loads
                gapi.plusone.go();
            });
        }
    },

    getButtonOptions: function(callbackFunc) {
        var _this = this;
        this.ajaxPost('getOptions', {},
            function(data, textSuccess) {
                // Save products and then possibly perform a callback function
                _this.options = data.response;
                (callbackFunc || $.noop)();
            }
        );
    },

    loadModuleCallback: function(data,textStatus) {

        // We need to use innerHTML here because of the (highly probable)
        // chance that the html we get back has <script> in it.  jQuery 1.4
        // tries to be smart and not insert non-standard code.
        this.container[0].innerHTML = data.html;

        // Get this module's options and then add the <g:plusone> tags and get the Google script
        // that turns this into a real button
        this.getButtonOptions(createRef(this, this.addGTags));

        this.addDragHandle(data);
        this.appropriatelyResize();
        this.manageModuleMargins();
    }
});

