jQuery.fn.meBox = function()
{
	return this.each(function(params)
	{
		jQuery(this).bind('click', function(params) {
			params = jQuery.extend({
				href		: this.href,
				title		: (this.title) ? this.title : this.alt,
				rel		: this.rel,
				width		: 550,
				height	: 400,
				animation	: 'none'
			}, params);

			$._meBox(params);

			return false;
		});
	});
};

jQuery.meBox = function(obj, params)
{
	params = jQuery.extend({
		href		: obj.href,
		title		: (obj.title) ? obj.title : obj.alt,
		rel		: obj.rel,
		width		: 550,
		height	: 400,
		animation	: 'none'
	}, params);

	$._meBox(params);
};

jQuery._meBox = function(params) {
	var mb = this;

	mb.loadImage = function()
	{
		var meImage		= new Image();
		meImage.src		= mb.params.href;

		meImage.onload = function()
		{
			meImage.onload = null;

			mb.params.width	= parseInt(meImage.width);
			mb.params.height	= parseInt(meImage.height);

			x = $('body').width() - 80;
			y = $('body').height() - 100;

			if (mb.params.width > x)
			{
				mb.params.height	= mb.params.height * (x / mb.params.width);
				mb.params.width	= x;

				if (mb.params.height > y)
				{
					mb.params.width	= mb.params.width * (y / mb.params.height);
					mb.params.height	= y;
				}
			}
			else if (mb.params.height > y)
			{
				mb.params.width	= mb.params.width * (y / mb.params.height);
				mb.params.height	= y;

				if (mb.params.width > x)
				{
					mb.params.height	= mb.params.height * (x / mb.params.width);
					mb.params.width	= x;
				}
			}

			mb.expand('<img id="meBoxImage" src="' + mb.params.href + '" alt="' + mb.params.title + '" width="' + mb.params.width + '" height="' + mb.params.height + '" />');

			$('#meBoxImage').bind('click', function() { mb.close() });
		}
	}

	mb.loadPage = function()
	{
		jQuery.get(mb.params.href, function(meXML) {
			mb.expand(meXML);
		});
	}

	mb.expand = function(src)
	{
		switch (mb.params.animation)
		{
			case 'slide':
				mb.params.fade = 25;
				mb.update(src);
				break;

			case 'expand':
				mb.params.fade = 800;

				$('#meBox').animate({
					'height': height,
					'top': Math.floor(($('body').height() - height) / 2)
				}, 'slow', function() {
					$('#meBox').animate({
						'width': width,
						'left': Math.floor(($('body').width() - width) / 2)
					}, 'slow', function() {
						mb.update(src);
					});
				});
				break;

			case 'pop':
				mb.params.fade = 25;
				mb.update(src);
				break;

			default:
				mb.params.fade = 25;
				mb.update(src);
				break;
		}
	}

	mb.update = function(src)
	{
		if (mb.params.title)
		{
			src = '<div id="meBoxContents" style="display:none">' +
				'	<div id="meBoxTitle">' +
						mb.params.title +

						'<div id="meBoxClose">&nbsp;</div>' +
					'</div' +
					src +
				'</div>';

//			$('#meBox').append('<div id="meBoxContents" style="display:none">' + src + '</div>');
//			$('#meBox').append('<div id="meBoxTitle">' + mb.params.title + ' <div id="meBoxClose" onclick="meBox.close()">&nbsp;</div></div>');
		}
		else
		{
			src = '<div id="meBoxContents" style="display:none">' +
					'<div id="meBoxClose">&nbsp;</div>' +
					src +
				'</div>';
		}

		$('#meBox').append(src);

		$('#meBoxClose').bind('click', function() { mb.close() });
		$('#meBox .mb_close').bind('click', function() { mb.close() });

		$('#meBoxContents').fadeIn(mb.params.fade, function() {
			if (mb.params.rel)
			{
				list		= $('body a[@rel=' + mb.params.rel + ']');

				current	= false;

				previous	= false;
				next		= false;

				if (list)
				{
					$(list).each(function(i)
					{
						if (this.href == mb.params.href)
						{
							current	= i;

							previous	= ((i - 1) >= 0)			? list[i - 1] : false;
							next		= ((i + 1) <= list.length)	? list[i + 1] : false;
						}
					});

					if (previous)
					{
						$('#meBox').append('<div id="meBoxPrevious" title="previous">&nbsp;</div>');

						var meBoxPreviousBackground = $('#meBoxPrevious').css('background');

						$('#meBoxPrevious').css({
							'position': 'absolute',
							'background': 'none',
							'top': 0,
							'left': 0,
							'height': '100%'
						});

						$('#meBoxNext').bind('click', function() { mb.meBoxPrevious(previous) });

						$('#meBoxPrevious').hover(
							function() { $('#meBoxPrevious').css('background', meBoxPreviousBackground); },
							function() { $('#meBoxPrevious').css('background', 'none'); }
						);
					}

					if (next)
					{
						$('#meBox').append('<div id="meBoxNext" title="next">&nbsp;</div>');

						var meBoxNextBackground = $('#meBoxNext').css('background');

						$('#meBoxNext').css({
							'position': 'absolute',
							'background': 'none',
							'top': 0,
							'right': 0,
							'height': '100%'
						});

						$('#meBoxNext').bind('click', function() { mb.meBoxNext(next) });

						$('#meBoxNext').hover(
							function() { $('#meBoxNext').css('background', meBoxNextBackground); },
							function() { $('#meBoxNext').css('background', 'none'); }
						);
					}
				}

				list = null;
			}
		});
	}

	mb.close = function()
	{
		$('#meBox').remove();
		$('#meBoxShade').remove();
	}

	$('#meBoxContents, #meBoxPrevious, #meBoxNext').remove();

	mb.params = params;

	if (mb.params.href)
	{
		var windowWidth	= (document.body.clientWidth) ? document.body.clientWidth + 'px' : (window.innerWidth + window.scrollMaxX) + 'px';
		var windowHeight	= (document.body.clientHeight) ? document.body.clientHeight + 'px' : (window.innerHeight + window.scrollMaxY) + 'px';

		$('body').append('<div id="meBoxShade">&nbsp;</div>');

		$('#meBoxShade').bind('click', function() { mb.close(); });
		$('#meBoxShade').css({
			width: windowWidth,
			height: windowHeight
		});

		$('body').append('<div id="meBox"></div>');

		if (mb.params.animation == 'none')
		{
			$('#meBox').css({
				'width': mb.params.width + 'px',
				'height': mb.params.height + 'px'
			});
		}

		$('#meBox').css({
			'position': 'absolute',
			'top': Math.floor(($('body').height() - $('#meBox').height()) / 2) + 'px',
			'left': Math.floor(($('body').width() - $('#meBox').width()) / 2) + 'px'
		});

		if (mb.params.href.match(/(.+\.jpg|.+\.jpeg|.+\.gif|.+\.png|.+\.bmp|.+\.tiff)/i))
			mb.loadImage();

		if (mb.params.href.match(/(.+\.htm|.+\.html|.+\.php|.+\.php2|.+\.php3|.+\.php4|.+\.phtml|.+\.shtml|.+\.dhtml|.+\.asp|.+\.aspx|.+\.jsp|.+\.jst|.+\.rb|.+\.txt|.+\.cfm)/i))
			mb.loadPage();
	}

};

$(document).ready(function() {
	$('body a.mb').meBox();
});
