$(function() {
	$.fn.lightBlock = function(settings) {
		settings = jQuery.extend({
			loadingImageSrc: "/common/image/loading.gif",
			hideImageSrc: "/common/image/button/hide.png",
			fadeSpeed: "normal"
		}, settings);

		function _show() {
			$('embed, object, select').css({ 'visibility' : 'hidden' });
			$('body').append('<div id="LightBlockOverlay"></div><div id="LightBlock"><img src="' + settings.loadingImageSrc + '" alt="..." id="LightBlockLoading" /><img src="' + settings.hideImageSrc + '" alt="X" id="LightBlockHide" class="LightBlockHide" /></div>');

			var pageSize = __getPageSize();
			$('#LightBlockOverlay').css({
				width: pageSize.pageWidth,
				height: pageSize.pageHeight
			}).fadeIn(settings.fadeSpeed);

			$('#LightBlockOverlay, .LightBlockHide').click(function() {
				_hide();
				return false;
			});

			var url = this.getAttribute('href');
			var baseUrl;
			if (url.indexOf('?') !== -1) baseUrl = url.substr(0, url.indexOf('?'));
			else baseUrl = url;
			if (baseUrl.toLowerCase().match(/\.jpg$|\.jpeg$|\.png$|\.gif$|\.bmp$/)) {
				contentsImage = new Image();
				$('#LightBlock').append(contentsImage);
				contentsImage.onload = function() {
					var ratio = 1;
					if (ratio > pageSize.windowWidth * 0.8 / this.width) ratio = pageSize.windowWidth * 0.8 / this.width;
					if (ratio > pageSize.windowHeight * 0.8 / this.height) ratio = pageSize.windowHeight * 0.8 / this.height;
					this.width = parseInt(this.width * ratio, 10);
					this.height = parseInt(this.height * ratio, 10);
					_set(this.width, this.height);
				};
				contentsImage.src = url;
			} else {
				$('#LightBlock').append('<div id="LightBlockContents"></div>');
				$('#LightBlockContents').load(url, function() { _set($('#LightBlock').width(), $('#LightBlock').height()); });
			}

			return false;
		}

		function _set(width, height) {
			var pageScroll = __getPageScroll();
			$('#LightBlock').css({
				width: width,
				height: height,
				marginTop: pageScroll.top - parseInt(height / 2, 10),
				marginLeft: parseInt(width / 2, 10) * -1
			});

			$('#LightBlockLoading').remove();
			$('#LightBlock').fadeIn(settings.fadeSpeed);

			$('#LightBlockHide').css({
				position: 'absolute',
				zIndex: 98,
				top: parseInt($('#LightBlockHide').height() / 2) * -1,
				left: width - parseInt($('#LightBlockHide').width() / 2)
			});
		}

		function _hide() {
			$('#LightBlock').fadeOut(function() { $('#LightBlock').remove(); });
			$('#LightBlockOverlay').fadeOut(function() { $('#LightBlockOverlay').remove(); });
			$('embed, object, select').css({ 'visibility' : 'visible' });
		}

		function __getPageSize() {
			var pageWidth = document.body.offsetWidth;
			var pageHeight = document.body.offsetHeight;
			var windowWidth, windowHeight;

			if (window.innerWidth && window.innerHeight) {
				windowWidth = window.innerWidth;
				windowHeight = window.innerHeight;
			} else {
				windowWidth = document.body.clientWidth || document.documentElement.clientWidth;
				windowHeight = document.body.clientHeight || document.documentElement.clientHeight;
			}

			if (pageWidth > windowWidth) pageWidth = windowWidth;
			if (pageHeight < windowHeight) pageHeight = windowHeight;

			return { pageWidth: pageWidth, pageHeight: pageHeight, windowWidth: windowWidth, windowHeight: windowHeight };
		}

		function __getPageScroll() {
			var top = document.body.scrollTop || document.documentElement.scrollTop;
			var left = document.body.scrollLeft || document.documentElement.scrollLeft;
			return { top: top, left: left };
		}

		return this.unbind('click').click(_show);
	};
});