/* =============================================================================
	jQuery LightWindow 1.0


	■ 追加・修正履歴：
		【2010.4.9】
			・プロトタイプ完成（jQuery1.4.2にて作成）
		【2010.4.12】
			・jQuery 1.2.6および1.3.2に対応するため、スクリプトの見直し・修正
			・オーバーレイの範囲（大きさ）および位置をHTMLがロードされたとき
			　のみだったものを、リサイズ時も対応
			・ウィンドウがスクロールされた状態で立ち上げた場合の表示位置を調整
		【2010.4.19】
			・画像の場合にshow()ではなくfadeIn()に修正
			　これにより画像が瞬時ではなくフェードインして表示されるようになる
			・立ち上がった際にブラウザによりウィンドウ内の高さが伸びてしまう
			　不具合を修正
			・Chromeにて、スクロールした場合の表示位置が正常に動作していない
			　不具合を修正

============================================================================= */
(function($) {

	$.fn.jlightWindow = function(settings) {
		settings = jQuery.extend({
			overlay_bgcolor	:"#000",
			overlay_speed	: 300,
			overlay_opacity	: 0.8,
			overlay_fadein	: 0.8,
			window_width	: 480,
			window_height	: 320,
			window_speed	: 300,
			window_bgcolor	: "#fff",
			image_close		: "/lib/images/closelabel.gif",
			image_prev		: "/lib/images/prev.gif",
			image_next		: "/lib/images/next.gif",
			image_loading	: "/lib/images/loading.gif"
		}, settings);

		var en = {
			overlay	: "div#overlay",
			mainbox	: "div#jlightwindow",
			content	: "div#jlw_contents",
			footer	: "div#jlw_footer",
			iframe	: "iframe",
			ohtml	: "div#htmlcontent",
			image	: "img",
			flash	: "object"
		}

		var _root = this;
		var imageSettings = {
			active	: "",
			count	: "",
			href	: {},
			title	: {}
		};
		var _target;

		var current_w, current_h, all_w, all_h;
		_getSize();

		// click
		this.click(function() {
			clicked(this, _root);
			return false;
		});

		// open window
		function clicked(_this, _root) {
			if(_root.length > 1) {
				var tcount = 0;
				var icount = 0;
				for(var t = 0;t < _root.length;t++) {
					var h = _root[t].getAttribute('href');
					if(h.search(/\.+(png|jpg|jpeg|gif)$/) != -1) {
						if(icount == 0) {
							imageSettings.active = tcount;
						}
						imageSettings.href[tcount]	= h;
						imageSettings.title[tcount]	= _root[t].getAttribute('title');
						icount++;
					}
					tcount++;
				}
			}
			var href	= _this.getAttribute('href');
			var title	= _this.getAttribute('title');

			$('embed, object, select').css({
				'visibility':'hidden'
			});

			template(href);

			$(en.mainbox).width(settings.window_width);
			$(en.mainbox).height(settings.window_height);
			if(title) {
				$(en.footer).append('<p class="jlw_title">' + title + '</p>');
			}
			_showbox();
		}

		// css
		function app_css() {
			var topposition = document.body.scrollTop;
			if(document.documentElement.scrollTop) {
				topposition = document.documentElement.scrollTop;
			}
			$(en.overlay).css("background-color",	settings.overlay_bgcolor);
			$(en.overlay).css("filter",				"alpha(opacity=" + (settings.overlay_fadein * 100) + ")");
			$(en.overlay).css("-moz-opacity",		settings.overlay_fadein);
			$(en.overlay).css("opacity",			settings.overlay_fadein);
			$(en.mainbox).css("top",				topposition + 100);
			$(en.content).css("background",			settings.window_bgcolor + " url(" + settings.image_loading + ") no-repeat 50% 50%");
			$(en.content).css("filter",				"alpha(opacity=0)");
			$(en.content).css("-moz-opacity",		0);
			$(en.content).css("opacity",			0);
			$(en.footer).css("background-color",	settings.window_bgcolor);
		}

		// resize of browzer
		$(window).resize(function() {
			_resize();
		});

		// open mainbox
		function _showbox() {
			$(en.footer + " img").click(finish);
			var image_loader = new Image();
			image_loader.onload = function() {
				switch(_target) {
					case"id":
						$(en.content + " " + en.ohtml).height(settings.window_height);
						break;
					case"iframe":
						$(en.content).width($(en.mainbox).width());
						$(en.content).height(settings.window_height);
						$(en.content + " " + en.iframe).width(settings.window_width);
						$(en.content + " " + en.iframe).height(settings.window_height);
						$(en.content + " " + en.iframe).hide();
						break;
					case"image":
						$(en.mainbox).width($(en.content + " img").width() + 20);
						$(en.mainbox).height($(en.content + " img").height() + 20);
						$(en.content).width($(en.content + " img").width());
						$(en.content).height($(en.content + " img").height());
						$(en.content + " img").hide();
						break;
					case"flash":
						break;
					default:
				}

				_resize();
				var fheight = $(en.footer).height();
				$(en.footer).hide();
				$(en.footer).height(0);
				app_css();

				$(en.overlay).fadeTo(settings.overlay_speed, settings.overlay_opacity, function() {
					$(en.content).fadeTo(settings.overlay_speed, 1, function() {
						if(fheight < image_loader.height) {
							fheight = image_loader.height;
						}
						switch(_target) {
							case"id":
								break;
							case"iframe":
								$(en.content + " " + en.iframe).show();
								break;
							case"image":
								$(en.content + " img").fadeIn();
								break;
							case"flash":
								break;
							default:
						}
						$(en.footer).animate(
							{
								height: fheight,
								opacity: 1
							},
							200
						);
					});
				});

				$(en.overlay).click(finish);
			}
			image_loader.src = settings.image_close;
		}

		function _resize() {
			_getSize();

			if(current_w > document.body.scrollWidth) {
				$(en.overlay).width(current_w);
			} else {
				$(en.overlay).width(all_w);
			}
			if(current_h > document.body.scrollHeight) {
				$(en.overlay).height(current_h);
			} else {
				$(en.overlay).height(all_h);
			}

			$(en.footer).width($(en.content).width());
			$(en.mainbox).css("left",	(current_w - $(en.mainbox).width()) / 2);

			return false;
		}

		// finish
		function finish() {
			$(en.mainbox).remove();
			$(en.overlay).fadeTo(settings.overlay_speed, 0, function() {
				$(en.overlay).remove();
			});
			$('embed, object, select').css({
				'visibility':'visible'
			});

			return false;
		}

		// get window size
		function _getSize() {
			current_w = document.documentElement.clientWidth;
			current_h = document.documentElement.clientHeight;
			if(current_w < document.body.scrollWidth) {
				all_w = document.body.scrollWidth;
			} else {
				all_w = current_w;
			}
			if(current_h < document.body.scrollHeight) {
				all_h = document.body.scrollHeight;
			} else {
				all_h = current_h;
			}
			if(all_h > document.documentElement.scrollHeight) {
				all_h = document.documentElement.scrollHeight;
			}
		}

		// template
		function template(target) {
			tmp  = '<div id="overlay"></div>';
			tmp += '<div id="jlightwindow">';
			tmp += '<div id="jlw_contents">';
			if(target.search(/(#+.+)$/) != -1) {
				_target = "id";
				var id = target.substring(target.search(/(#+.+)$/));
				tmp += '<div id="htmlcontent">' + $(id).html() + '</div>';
			} else if(target.search(/\.+(png|jpg|jpeg|gif)$/) != -1) {
				_target = "image";
				tmp += '<img src="' + target + '" />';
			} else if(target.search(/\.+(swf)$/) != -1) {
				_target = "flash";
			} else {
				_target = "iframe";
				tmp += '<iframe frameborder="0" src="' + target + '"></iframe>';
			}
			tmp += '</div>';
			tmp += '<div id="jlw_footer"><img src="' + settings.image_close + '" /></div>';
			tmp += '</div>';
			$('body').append(tmp);
		}

	};
})(jQuery);


