(function($) {

	$.extend({
		
		status: function(msg, type, settings) {
					
			var options = {
				msg: '',
				type: 'green',
				parent: '#infopane',
				callback: function(){},
				firstClass: 'first'
			};
						
			if (typeof msg == 'object') $.extend(options, msg);
			if (typeof msg == 'string') options.msg = msg;
			if (typeof type == 'object') $.extend(options, type);
			if (typeof type == 'string') options.type = type;
			if (settings) $.extend(options, settings);
			
			// Create the status div
			var $box = $('<div class="status '+options.type+'"></div>');
			// Insert the status message
			$('<p>'+options.msg+'</p>').appendTo($box);
			// Insert the close button
			var close = $('<a href="#" class="status-close">x</a>').prependTo($box);
			
			var $status = $('.status:last');
						
			// Add the box to the DOM
			$parent = $(options.parent);
			
			if ($status.length) {
				$status.after($box);
			} else {
				$box.addClass(options.firstClass);
				$box.prependTo($parent);
				$parent.addClass('show-status').css('padding-top', 0);			
			}
			
			
			// Add click functionality to close button
			close.bind('click', function() {
				// Hide status and fix margin if necessary								
				if ($box.hasClass(options.firstClass)) {
					$box.animate({height: 0, opacity: 0, marginTop: -20}, {
						complete: function() {
							$(this).remove();
						}
					});
					
					// Animate infopane back to normal margin
					$parent.animate({
						paddingTop: 20
					}, {
						complete: function() {
							$(this).removeClass('show-status');
						}
					});
				} else {
					$box.slideUp(function(){
						$(this).remove();
					});
				}
					
				// Prevent normal click behavior
				return false;
			});
			
			return this;
		},
		
		action: function(settings) {
			
			var options = {
				id: '',
				name: '',
				action: '',
				liclass: ' active',
				active: false,
				line: false
			}
			
			$.extend(options, settings);
			
			if ( ! settings.active)
				options.liclass = '';
				
			function add_line(place, li) {
				var $line = $('<li class="line" />');
				
				if (place == 'before') $line.insertBefore(li);
				else $line.insertAfter(li);				
			}
				
			var $list = $('#actions_list');
			
			// Create the list ul if necessary
			var $li = $('<li class="ali" />');
			var $link = $('<a href="#" id="_action_'+options.id+'" class="ali'+options.liclass+'">'+options.name+'</a>');
			$link.data('action', options.action);
			
			$link.bind('activate', function(){
				$(this).addClass('active');
				$(this).click(function(){
					$(this).openform();
				});
			});
			
			$link.bind('disable', function(){
				$(this).unbind('click').removeClass('active');
			});
			
			if (options.active) $link.trigger('activate');
			
			$link.appendTo($li);
			
			if ( ! $list.length) {				
				
				var $div = $('<div id="actions" class="actions imgr">Actions</div>');
				var $ul = $('<ul id="actions_list" class="hidden" />');
				$ul.append($li);
				$div.appendTo('body');
				$ul.appendTo('body');
				
				if (options.line) add_line(options.line, $li);
				
				$('#actions').click(function() {
					$(this).toggleClass('clicked');
					$('#actions_list').toggleClass('hidden');
					
					if ($(this).hasClass('clicked')) {
						$(window).bind('click', {}, function(e) {
							if (e.target.id == 'actions_list' || $(e.target).hasClass('ali') || e.target.id == 'actions')
								return;
								
							$('#actions').click();
							$(this).unbind('click');						
						});
					} else {
						$(window).unbind('click');
					}
				});
			} else {
				$li.appendTo($list);
				if (options.line) add_line(options.line, $li);
			}
		},
		closeform: function() {
			$('#FormBox').trigger('loaded').remove();
			$.overlay('close');
			$(window).unbind('keyup');
		},
		progress: function() {
			var $progress = $('#FormBox_Progress');
			if ($progress.length)
				return $progress;
				
			var $progress = $('<div id="FormBox_Progress" />');
			
			$('#FormBox').loading('FormBox_Overlay');
			
			var $overlay = $('#FormBox_Overlay');
			var h = $overlay.outerHeight();
						
			$progress.css('left', $overlay.offset().left + 100);
			$progress.css('top', (h / 2) + 120);
			$progress.appendTo('body');
			
			return $progress;
		},
		overlay: function(action) {
			var $overlay = $('#overlay');
						
			// Open by default
			action = action ? action : 'open';
			
			if (action == 'close') {
				$overlay.remove();
			} else if ( ! $overlay.length) {
				$overlay = $('<div id="overlay" />');
				$overlay.height($(document).height()).width($(document).width());
				$overlay.appendTo('body');
			}
			
			return $overlay;
		}
		
	});
	
	$.fn.extend({
		center: function(side, settings) {
			var options = {
				side: 'both',
				'width': 600
			}
			
			if (typeof side == 'object') $.extend(options, settings);
			else if (side) options.side = side;
			$.extend(options, settings);
			
			$(this).each(function(){
				if (options.side == 'y' || options.side == 'vertical' || options.side == 'both') {
					$(this).css('top', ( $(window).height() - $(this).height() ) / 2 + $(window).scrollTop() + 'px');
				}
				
				if (options.side == 'x' || options.side == 'horizontal' || options.side == 'both') {
					$(this).css('left', ( $(window).width() - options.width ) / 2 + $(window).scrollLeft() + 'px');
				}
			});
			
			return this;
		},
		loading: function(id) {
			$(this).each(function(){
				var $overlay = $('<div class="overlay loading" />');
				if (id) $overlay.attr('id', id);
				var offset = $('#FormBox').offset();
				
				$overlay.css('left', offset.left);
				$overlay.width($(this).outerWidth());
				$overlay.height($(this).outerHeight());
				$overlay.css('top', offset.top);
				
				$overlay.appendTo('body');
				
				$(this).bind('loaded', function(){
					$overlay.remove();
				});
			});
			
			return this;
		},
		openform: function() {
			$(this).each(function() {
				var overlay = $.overlay();
				var action = $(this).data('action');
				$(overlay).addClass('loading');
				$(window).click();
				
				$.ajax({
					url: action,
					dataType: 'html',
					success: function(response) {
						$(overlay).removeClass('loading');
						var $formbox = $('<div id="FormBox" />');
						$formbox.data('action', action);
						$formbox.center('x');
						$formbox.css('top', $(window).scrollTop() + 50);
						$formbox.append(response);
						$formbox.appendTo('body');
						
						
						$('.button.action', $formbox).each(function(){
							$(this).click(function(){
								$('form', $formbox).submit();
							});
						});
						
						$(window).trigger('formloaded');
						$(window).bind('keyup', function(e){
							if (e.keyCode == 27) {
								$.closeform();
							}
							
							return true;
						});
						
						$first_input = $('input:not(:hidden)', $formbox);
						
						if ($first_input.length) {
							$first_input.get(0).focus();
						}
					}
				});
			});
			
			return this;
		}
	});
	
})(jQuery)