jQuery.fn.popup = function() {
	return this.each(function(o) {
        jQuery.popup(this, o);
    });
};

jQuery.popup = function(e, o) {
	var options = {
	    width: null,
	    height: null,
	    iframe: null,
	    close: true,
	    closeButton: 'x'
	};

	if (o)
        jQuery.extend(options, o);

	if (options.iframe == null) {
	    options.iframe = jQuery(e).size() == 0 || jQuery(e)[0].nodeName == 'HEAD' ? true : false;
	}

	if (options.width == null) {
	    options.width = 480;
	    if (!options.iframe) {
	        var w = toInt(jQuery(e).width());
	        if (w > 0)
	            options.width = w;
	    }
	}

	if (options.height == null) {
	    options.height = 480;
	    if (!options.iframe) {
	        var h = toInt(jQuery(e).height());
	        if (h > 0)
	            options.height = h;
	    }
	}

	if (document.getElementById('popup-hideselect') == null) {
		jQuery('html, body').css('height', '100%');
		jQuery('body').append("<iframe id='popup-hideselect'></iframe><div id='popup-overlay'></div><div id='popup-window'></div>");

		jQuery('#popup-hideselect').css({
            'zIndex': 10000000,
	        'position': 'absolute',
	        'top': 0,
	        'left': 0,
	        'backgroundColor': '#fff',
	        'border': 'none',
	        'filter': 'alpha(opacity=0)',
	        '-moz-opacity': 0,
	        'opacity': 0
        });

        jQuery('#popup-overlay').css({
            'position': 'fixed',
	        'z-index': 10000001,
	        'top': 0,
	        'left': 0,
            'width': '100%',
            'height': '100%',
	        'backgroundColor': '#000',
	        'filter': 'alpha(opacity=60)',
	        '-moz-opacity': 0.6,
	        'opacity': 0.6
        });

		jQuery('#popup-overlay').bind('click', jQuery.popdown);

		jQuery('#popup-window').css({
            'display': 'none',
            'padding': 0,
            'margin': 0,
            'position': 'fixed',
	        'overflow': 'hidden',
            'top': '50%',
            'left': '50%',
	        'background': '#000',
	        'zIndex': 10000002,
	        'color': '#000',
	        'textAlign': 'left'
        });

        if (jQuery.browser.msie) {
            jQuery('#popup-overlay')
            .css('position', 'absolute')
            .get(0)
            .style
            .setExpression(
                'height',
                "document.body.scrollHeight > document.body.offsetHeight ? document.body.scrollHeight : document.body.offsetHeight + 'px'"
            );

            jQuery('#popup-window')
            .css('position', 'absolute')
            .get(0)
            .style
            .setExpression(
                'marginTop',
                "0 - parseInt(this.offsetHeight / 2) + (PopupWindowMargin = document.documentElement && document.documentElement.scrollTop || document.body.scrollTop) + 'px'"
            );
        }
	}

	if (options.close) {
	    jQuery('#popup-window').append('<div id="popup-close"><a href="#" id="popup-closebutton" class="close">'+options.closeButton+'</a></div>');
	    jQuery('#popup-close')
	    .css({
	        'width': 'auto',
	        'text-align': 'right'
	    });
	}

	if (options.iframe) {
	    $('body').append('<iframe frameborder="0" hspace="0" src="" id="popup-fixendlessloadingframe" name="popup-fixendlessloadingframe" style="display:none;width:1px;height:1px;"></iframe>');
	    $('#popup-window').append('<iframe frameborder="0" hspace="0" src="'+e+'" id="popup-iframe" name="popup-iframe" onload="jQuery.popupFixEndlessLoading()"></iframe>');

	    jQuery('#popup-iframe')
	    .css({
	        'width': options.width + 'px',
	        'height': options.height + 'px',
	        'clear': 'both',
	        'margin': 0
	    });

	    if (frames['popup-iframe'] == undefined){ //be nice to safari
	        $(document).keyup( function(e){ var key = e.keyCode; if(key == 27){ jQuery.popdown()} });
	    }

	} else {
	    jQuery('#popup-window').append('<div id="popup-content"></div>');

	    jQuery('#popup-content')
	    .css({
	        'width': options.width + 'px',
	        'height': options.height + 'px',
	        'clear': 'both',
	        'overflow': 'auto'
	    }).html(jQuery(e).hide().html());
	}

	jQuery('.close', jQuery('#popup-window'))
	.unbind('click', jQuery.popdown)
	.bind('click', jQuery.popdown);

	jQuery('#popup-window').css({
		'marginLeft': '-' + toInt(options.width / 2) + 'px',
		'width': options.width + 'px'
	});

	if (!(jQuery.browser.msie && typeof XMLHttpRequest == 'function') ) { // take away IE6
		jQuery('#popup-window').css({
			marginTop: '-' + toInt(options.height / 2) + 'px'
		});
	}

	jQuery('#popup-window').fadeIn('fast');

	document.onkeyup = function(e) {
		if (e == null) { // ie
			var key = event.keyCode;
		} else { // mozilla
			var key = e.which;
		}

		if (key == 27){ // close
			jQuery.popdown();
		}
	};
};

jQuery.popupFixEndlessLoading = function() { // Fix endless loading in IE
    try {
        frames['popup-fixendlessloadingframe'].document.write("");
    } catch (e) {}

    try {
        frames['popup-fixendlessloadingframe'].close();
    } catch (e) {}
};

jQuery.popdown = function() {
	jQuery('#popup-overlay').unbind('click');
	jQuery('.close', jQuery('#popup-window')).unbind('click', jQuery.popdown);
	jQuery('#popup-window').fadeOut('fast', function() {
	    jQuery('#popup-iframe,#popup-fixendlessloadingframe,#popup-content,#popup-window,#popup-overlay,#popup-hideselect').remove();
	});

	return false;
};

if (toInt == undefined) {
	var toInt = function(n) {
		n = parseInt(n);
		return isNaN(n) ? 0 : n;
	};
}

