jQuery(document).ready(function($) {

    var ie6 = $.browser.msie && parseInt($.browser.version) < 7;
    $('a.modal-open').click(function() {
        var id = this.href.replace(/.*#/, '');
        $('#' + id).modal({
            onShow: function(dialog) {
                $(dialog.wrap).css('overflow', 'hidden');
                $(dialog.container).css('height', 'auto');

                // if data is too large for window, change from fixed to absolute positioning
                $(window).unbind('resize.segregatedfunds').bind('resize.segregatedfunds', function() {
                    if ($(dialog.container).outerHeight() > $(window).height()) {
                        try { // removeExpression() is IE-specific, so wrap in try/catch to prevent failure in other browsers
                            $(dialog.container)[0].style.removeExpression('top');
                        } catch(e) {};
                        $(dialog.container).css({
                            position: 'absolute',
                            top: (document.documentElement.scrollTop || document.body.scrollTop) + 'px',
                            left: parseInt( ($(window).width() - $(dialog.container).outerWidth()) / 2 ),
                            marginTop: 0
                        });
                        
                        // disable simplemodal's auto positioning
                        $.modal.defaults.autoPosition = false;
                    } else if (!ie6) {
                        // revert to fixed position once data fits window again, except for IE6 since absolute is used to emulate fixed positioning in simplemodal
                        $(dialog.container).css('position', 'fixed');

                        // corrected centering
                        var top = parseInt( ($(window).height() - $(dialog.container).outerHeight()) / 2 );
                        var left = parseInt( ($(window).width() - $(dialog.container).outerWidth()) / 2 );
                        $(dialog.container).css({
                            top: top + 'px',
                            left: left + 'px'
                        });
                        
                        // re-enable simplemodal's auto positioning
                        $.modal.defaults.autoPosition = true;
                    } else {
                        // re-enable simplemodal's auto positioning
                        $.modal.defaults.autoPosition = true;
                    }
                }).trigger('resize.segregatedfunds');

                // in IE6, if modal extends beyond height of document content, extend overlay to match
                if (ie6 && $(dialog.overlay).height() < $(document).height()) {
                    $(dialog.overlay).height($(document).height());
                }
            }
        });
        return false;
    });

});

