// http://stackoverflow.com/questions/4265316/auto-size-jquery-ui-dialog-in-ie
(function($) {
    var fixDialogAutoWidth = $.noop;
    // PINDAR: OM : added true condition to apply this to all browsers with as few changes as possible from the original.
    if ( true || $.browser.msie ) {
        fixDialogAutoWidth = function(content) {
        	// PINDAR : OM : dialog parent still suffers from width:auto; bug and is full width, so these two lines don't help.
            //var dialog = $(content).parent('.ui-dialog');
            //var width = dialog.innerWidth();
            
            var width = $(content).children('.ui-dialog-content').innerWidth();
            if ( width ) dialog.css('width', width);
        }
    }
    
    var _init = $.ui.dialog.prototype._init;
    $.ui.dialog.prototype._init = function() {
        //console.log('$.ui.dialog.prototype._init', this.options);
        // IE magick once again (width: 'auto' not working correctly) :
        // NOTE: persists in jQuery UI 1.8.5 http://dev.jqueryui.com/ticket/4437
        if ( this.options.width == 'auto' ) {
            var open = this.options.open;
            this.options.open = function() {
                fixDialogAutoWidth(this);
                if ( open ) open.apply(this);
            }
        }
        // yet another bug options.hide: 'drop'
        // does not work in IE http://dev.jqueryui.com/ticket/5615
        if ( $.browser.msie && this.options.hide == 'drop' ) {
            this.options.hide = 'fold';
        }
        return _init.apply(this); // calls open() if autoOpen
    };
})(jQuery);

$(document).ready(function() {
	$('.more-info-link').each(function() {
		var $link = $(this);
		var $dialog = $('<div></div>')
			.html($('#more-info'))
			//.load($link.attr('href')+'&title='+encodeURIComponent($link.attr('title')))
			.dialog({
				modal: true,
				buttons: {
					Ok: function() {
						$( this ).dialog( "close" );
					}
				},
				autoOpen: false,
				title: $link.attr('title'),
				// PINDAR : OM : width:auto; officially not supported due to the inept css handling in IE7--
				// Fix a semi-reasonable width to begin with, and then the code above modifies the jQuery dialog
				// code to calculate the dialog width on opening based on the actual content width (which shrink-wraps fine, in all browsers).
				//width: 'auto',
				width: 800,
				height: 'auto',
				minHeight: 200,
				minWidth: 300
			});

		$link.click(function() {
			$dialog.dialog('open');

			return false;
		});
	});
});
