﻿/*
LearnCenter specific plugins and extensions for jQuery
*/

(function($) {
    // Override modal behavior in jQuery UI dialog box to utilize the expose plugin instead to mitigate memory leaks discovered in modal version of the jQuery UI dialog
    var _init = $.ui.dialog.prototype._init;
    $.ui.dialog.prototype._init = function() {
        var self = this;
        _init.apply(this, arguments);

        // Remove the default modal behavior and exhibit the new one
        if (self.options.modal) {
            self.options.modal = false;
            self.options.isModal = true;
        }

        if (self.options.stretchIFrame) {
            var iFrames = $('iframe', this.uiDialog);
            iFrames.load(sizeIFrame);
        }

        this.uiDialog.bind('dialogopen', function(event, ui) {
            if (self.options.isModal) {
                if ($(this).expose == null)
                    window.alert("Dialog box depends on the expose plugin to be modal. Please include the jquery tools Javascript include.");
                else {
                    $(this).expose({ opacity: 0.3
                                , color: '#CCCCCC'
                                , loadSpeed: 0
                                , closeSpeed: 0
                                , closeOnClick: false
                                , closeOnEsc: false
                                , api: true
                    }).load();
                }
            }
        });

        this.uiDialog.bind('dialogfocus', function(event, ui) {
            if (self.options.isModal) {
                $(this).css('z-index', '9999');
            }
        });

        this.uiDialog.bind('dialogclose', function(event, ui) {
            if (self.options.isModal) {
                if ($(this).expose != null) {
                    $(this).expose({ api: true }).close();
                }
            }
        });
    };

    $.ui.dialog.defaults.isModal = false;
    $.ui.dialog.defaults.stretchIFrame = false;
})(jQuery);

function sizeIFrame() {
    var helpFrame = jQuery(this);
    var innerDoc = (helpFrame.get(0).contentDocument) ? helpFrame.get(0).contentDocument : helpFrame.get(0).contentWindow.document;
    helpFrame.height(innerDoc.body.scrollHeight + 35);
}
