function popup(button,popup,overlay,closebtn) {
	var onload = false;
	var b = null;
	if (button == '') {
		onload = true;
	} else {
		b = $('#' + button + ', .' + button);
	}
	var p = $('#' + popup);
	var o = $('#' + overlay);
	var c = $('#' + closebtn + ', #' + popup + ' .' + closebtn);
	var hoverclass = "hover";
	var ie = false;
	var vers = 0;
	if(jQuery.browser.msie) {
		ie = true;
		vers = parseInt(jQuery.browser.version);
	}
			
	// Detect if cursor is over the popup
	var phover = false;
	p.hover(function(){ 
		phover=true; 
	}, function(){ 
		phover=false; 
	});
	
	if (onload === false) {
		// Attach onclick event listener to the activating element
		$(b).live('click', function(cl) {
			// prevent link from opening
			cl.preventDefault();
			loadPopup();
		});
	} else {
		loadPopup();	
	}
	
	function loadPopup() {
		// Get the window dimensions
		var wheight = $(window).height();
		var wwidth = $(window).width();
		
		// set the starting point for the CSS of the overlay
		o.css({"height":$(document).height()+"px", "width":"100%", "top":"0", "left":"0", "opacity":"0", "display":"block"});
		
		// Calculate lightbox top position
		var ptop = parseInt(($(window).height()/2) - (p.outerHeight()/2));
		
		// Reset the top position if the window is smaller than lightbox
		if ($(window).height() <= p.outerHeight()) ptop = 15;
		
		p.css({"top":parseInt(ptop + $(window).scrollTop()) + "px", "left":"50%", "margin-left":"-" + parseInt(p.outerWidth()/2) + "px"});
		if(ie === false) c.css({"opacity":0, "display":"block"});
		
		// Open the popup
		o.fadeTo("normal",.7,function(){
			if(ie === true) {
				p.add(c).css('display', 'block');
				if (vers == 6) $('select').css('display','none');
			} else {
				p.fadeIn("normal", function(){
					c.fadeTo("normal",1.0);
				});
			}
		});
	}
	
	// Attach onclick event to the document body to close lightbox (if popup not hovered) 
	$("body").mouseup(function() {
		if (!phover) {
			PClose();
		}
	});
	
	// Attach onclick event to the close button
	c.click(function (cl) {
		cl.preventDefault();
		PClose();
	});
	
	// Add hover state to the close button
	$('.close', p).hover(function() {
		$("*").removeClass(hoverclass);
		c.addClass(hoverclass);
	},
	function() {
		$("*").removeClass(hoverclass);
	});
	
	// Function for closing the popup
	function PClose() {
		c.fadeOut("fast", function(){
			p.fadeOut("fast",function(){
				o.fadeOut("fast");
				if (ie===true && vers == 6) $('select').css('display','inline');
			});
		});
	}
}
