function openPopup(url, width, height, callback) {
	$('body#body').css({ height: '100%', overflow: 'hidden' });
	var html = '<div id="popup_background"></div><div id="popup"><div id="popup_close"><img src="images/cross.gif" /></div><div id="content"><img src="images/loader.gif" /></div></div>';
	$(html).appendTo("div#container");
	
	$('div#popup div#content').load(url, null, callback);
	$('div#popup_background').show();

	var margin_top = '-' + (height / 2) + 'px';
	var margin_left = '-' + (width / 2) + 'px';
	$('div#popup').css({
		margin: margin_top + ' 0px 0px ' + margin_left,
		width: width + 'px'
	});
	
	var content_height = height - 46;
	var content_width = width - 30;
	$('div#popup div#content').css({
		height: content_height + 'px',
		width: content_width + 'px'
	});
	
	$('div#popup').show();
	
	$('div#popup div#popup_close img').click(function() { closePopup(); });
}

function closePopup(callback) {
	$('div#popup').fadeOut('fast', function() {
		$('div#popup').remove();
		$('body#body').css({ height: 'auto', overflow: 'auto' });
		callback = callback || function() {};
		callback();
	});
	$('div#popup_background').fadeOut('fast', function() {
		$('div#popup_background').remove();
	});
}

function openEditor(url, validate_url, success_callback, size, width, height, callback) {
	callback = callback || function() {};
	$('div#msgs').remove();
	switch(size) {
		case 'small': 
			width = width || 300;
			height = height || 250;
			break;
		case 'medium': 
			width = width || 350;
			height = height || 400;
			break;			
		case 'large':
			width = width || 600;
			height = height || 500;
			break;
		default:
			width = width || 600;
			height = height || 500;
	}
	openPopup(url, width, height, function() {
							   
		$('div#popup form').submit(function() {								

			$(this).ajaxSubmit({
				beforeSubmit: function(form_array) {
					var valid = $.ajax({
						async: false,
						data: $('div#popup form').formSerialize(),
						type: 'POST',
						url: validate_url
					}).responseText;
					if(valid == '0') {
						showMsgsAbove('div#popup form');
						return false;
					}
				},
				success: function() { closePopup(success_callback); }
			});
			return false;
		});
		callback();
	});
}

function loadEditor(url, layer, validate_url, success_callback, callback) {
	callback = callback || function() {};
	success_callback = success_callback || function() {};
	$('div#msgs').remove();
	$(layer).load(url, null, function(){
		$(layer + ' form').submit(function() {
			$(this).ajaxSubmit({
				beforeSubmit: function(form_array) {
					var valid = $.ajax({
						async: false,
						data: $(layer + ' form').formSerialize(),
						type: 'POST',
						url: validate_url
					}).responseText;
					if(valid == '0') {
						showMsgsAbove(layer + ' form');
						return false;
					}
				},
				success: function() { success_callback(); }
			});
			return false;
		});
		callback();
	});
}

function showMsgsAbove(element) {
	$('div#msgs').remove();
	$(element).before('<div id="msgs"></div>');
	$('div#msgs').load('msgs.php', null, function() {
		if(!$('div#msgs').html()) {
			$('div#msgs').remove();
		}
	});
}