jQuery.extend({
	/**
	 * Creates a custom popup window capable of displaying a specified message and unlimited buttons with custom events
	 * attached
	 *
	 * @author		Alex Kaye 13/05/09
	 * @param		object		config		Config object to change performance of popup. See defaults for available methods
	 * @param		mixed		buttons		Object of array of objects with button information. See defaults for available methods
	 */

	popup : function(config, buttons) {
		var config_defaults={
			'title'			:	'',
			'content'		:	'Are you sure you want to do this?',
			'speed'			:	'fast',
			'popupClass'	:	'normal',
			'draggable'		:	false,
			'closeable'		:	true,
			'closeButton'	:	'<img src="/graphics/popups/close-button.png" alt="Close" />',
			'maskOpacity'	:	0.4,
			'popupOpacity'	:	1,
			'onStart'		:	null,
			'onLoad'		:	null,
			'onClose'		:	null,
			'onComplete'	:	null,
			'onUnload'		:	null
		};
		var button_defaults={
			'label'			:	'OK',
			'buttonClass'	:	'small_button',
			'action'		:	null,
			'closePopup'	:	true
		};
	
		// Apply defaults to any unset properties for this button
		var config=$.extend({}, config_defaults, config);

		function closePopup(callback) {
			$('#popup').fadeOut(config.speed);
			$('#mask').fadeOut(config.speed, function() {
				$('#popup, #mask').remove();
				if($.isFunction(callback)) {
					callback();
				}
				if($.isFunction(config.onUnload)) {
					config.onUnload();
				}					
			});
		}
		
		var closeButton=''
		if(config.closeable) {
			closeButton	='<a class="close_popup">'+config.closeButton+'</a>';
		}
		
		// Call any start events
		if($.isFunction(config.onStart)) {
			config.onStart();
		}
		// Append our mask and popup structure
		$('body').append('<div id="mask"></div><div id="popup" class="' + config.popupClass + '"><div id="popup_title">' + closeButton + config.title + '</div><div id="popup_inner">' + config.content + '</div></div>');
		
		//var popupHeight = $('#popup').outerHeight();
		//$('#popup').css('marginTop','-' + (popupHeight / 2) + 'px');

		var centerDiv = $('#popup');
		centerDiv.css('top', ($(window).height()/2-centerDiv.height()/2) - 30);
		centerDiv.css('left', $(window).width()/2-centerDiv.width()/2);

		// Fade in our mask and popup 
		$('#mask, #popup').fadeTo(0,0);
		$('#mask').fadeTo(config.speed, config.maskOpacity);
		$('#popup').fadeTo(config.speed, config.popupOpacity, config.onLoad);
		
		// Add events to any close buttons
		$('#popup .close_popup').click(function() {
			closePopup(config.onClose);	
		});
		
		// Make popup draggable if specified
		if(config.draggable) {
			$("#popup").draggable({ handle : '#popup_title' });
		}
		
		// Check whether we need to add any buttons
		if(buttons) {
			// Force our buttons value into an array 
			if(!$.isArray(buttons)) {
				// If so make it into an array containing the single button object
				buttons=[buttons];
			}
			// Loop through each button
			$.each(buttons, function(i) {
				// Apply defaults to any unset properties for this button
				var thisButton=$.extend({}, button_defaults, buttons[i]);
				
				// Create button container for first button
				if(i==0) {
					$('#popup_inner').append('<div id="popup_buttons" class="clearfix"></div>');	
				}
				if(thisButton.buttonClass) {
					var buttonClass=' class="'+thisButton.buttonClass+'"';	
				} else {
					var buttonClass='';
				}
				// Append the button to our popup
				$('#popup_buttons').append('<button'+buttonClass+'><span>' + thisButton.label + '</span></button>');
				// If an action has been associated with the buttons then create the event
				if(thisButton.action) {
					$('#popup button:last').click(thisButton.action);
				}
				// If the button closes the popup then create that event
				if(thisButton.closePopup) {
					$('#popup button:last').click(function() {
						closePopup();
						// Call our complete event function if one exists
						if($.isFunction(config.onComplete)) {
							// Pass the index of the button clicked
							config.onComplete(i);
						}
					});
				}
			});
		}
		
	}
});
