﻿// todo: doc

(function($) {
    // -- FFDIALOG --
    $.fn.ffdialog = function(options) {
    
        var defaults = {
            imgPath: Config.Paths.Img + 'ffdialog/',
            title: '',
            close: true,    // whether or not the X button is added
            innerWidth: '400px',
            builtCallback: null, // called once the dialog is built (only the first time); useful for binding controls
            hideBackground: false   // the background is automatically displayed if the inner width is at least 400 px; this option forces it hidden
        };
        
        var options = $.extend(defaults, options);
        
        return this.each(function() {
            var obj = $(this);
            
            // get id; clone content (previous bindings are lost)
            var objId = this.id;
            var objHtml = obj.html();
            var titleWidth = parseInt(options.innerWidth) - 32 - (options.close ? 26 : 0);
            
            if (obj.hasClass('ffdialog'))
                return; // ffdialog already applied
            
            obj.remove();
            
            var html =             
                '   <div id="' + objId + '" class="ffdialog" style="display: none; width: ' + parseInt(options.innerWidth) + 'px;">'
                + '     <div class="header">'
                + '         <div class="title" style="width: ' + titleWidth + 'px;">' + options.title + '</div>';
            
            if (options.close)
                html +=
                    '       <a class="close simplemodal-close" href="#"><img src="' + options.imgPath + 'lb-close.png" style="width: 26px; height: 25px;" alt="Close" /></a>';

            html +=
                '       </div>'
                + '     <div class="body' + (((parseInt(options.innerWidth) < 400) || options.hideBackground) ? ' compact' : '') + '">'
                + '         <div class="html">' + objHtml + '</div>'
                + '     </div>'
                + ' </div>';

            $('body').append(html);
            
            if (options.builtCallback != null)
                options.builtCallback();
            
        });
    }
})(jQuery);

