// Class for the Facebook Like Box
var fbookbox = $.inherit(Module, {
    __constructor: function(div) {
        this.__base(div);
        this.type = "fbookbox";
    },

    // This is called as a callback from loadModule.
    // It should also be called directly by modules that overload loadModule and avoid calls to frame2 on init (like customHtml).
    loadModuleCallback: function(data, textStatus) {
        // Store the returned data so we can access it later on
        this.moduleData = data;

        // 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;

        // Handle actions to be taken after the saving of this module's settings
        if (this.postSaveData) {
            this.handleModuleSaveResult(this.postSaveData);
            this.postSaveData = '';
        }

        // Since the HTML we just inserted could contain <script>,
        // move them to a place where they'll get executed.
        $("script", this.container).each(function() {
            if (typeof $(this).attr("src") != "undefined") {
                $.getScript($(this).attr("src"));
            }
            else {
                $("body").append(this);
            }
        });

        this.addDragHandle(data);

        // Resize module container to match the size of the like box
        this.appropriatelyResize();
    },

    /* Returns the object of the region that contains this module */
    getRegion: function() {
        return this.element.parent();
    },

    appropriatelyResize: function() {

        // Get the iFrame from Facebook inside the module container
        var thisIframe = this.container.find('iframe');

        // In the case that we're in the sidebar, shrink the module iframe to fit properly inside it
        var thisRegion = this.getRegion();

        if (thisRegion.attr('id') == 'sidebar') {
            var regionWidth = thisRegion.width();

            // Rewrite the iframe URL to reflect the new narrower module width
            var iframeURL = thisIframe.attr('src');

            var regex = new RegExp("[\\?&]width=([^&#]*)");
            var regexResult = regex.exec(iframeURL);
            var newIframeURL = iframeURL.replace(regexResult[0], '&width=' + regionWidth);

            thisIframe.css({ 'width': regionWidth }).attr('src', newIframeURL);

            // Update the width options in ly:options so this size change is actually recorded
            if (this.getOptions()) {
                var lyOptionsAsObject = eval('(' + this.getOptions() + ')');
                var lyOptionsAsText = new Array();

                lyOptionsAsObject.width = regionWidth;

                for(i in lyOptionsAsObject) {
                    lyOptionsAsText.push('"' + i + '": "' + lyOptionsAsObject[i] + '"');
                }

                lyOptionsAsText = '{'  +lyOptionsAsText.join(',') + '}';

                this.element.attr('ly:options', lyOptionsAsText);
            }
        }

        // Set the size of the module container to that of the iframe within it, but only
        // if the iFrame is visible, and thus, has relevant contents to display
        if (thisIframe.css('display') == 'block') {
            var iframeHeight = thisIframe.height();
            var iframeWidth = thisIframe.width();
            this.container.height(iframeHeight).width(iframeWidth);
        }
    }
});

