/*
	ポップアップ表示
	
	Yuichi Takeuchi
	http://takeyu-web.com/
*/

var InfoBox = new Object;

InfoBox.width = 140;

(function($){
	
	InfoBox.append = function(attrName, parentId){
		attrName = attrName || 'title';
		var parent = parentId ? $('#'+parentId) : $(document.body);
		
		parent.find('a['+attrName+'],label['+attrName+']').each(function(){
			var target = $(this);
			target.attr('infobox:message', target.attr(attrName));
			target.removeAttr(attrName);
			target.removeAttr('title'); // titleツールチップが邪魔になるので
			target.children('img[alt]').removeAttr('alt');
			//target.mousemove(InfoBox.show(target));
			target.hover(InfoBox.show(target), InfoBox.hide(target));
		});
	}
	
	/* 実際にポップアップさせるクロージャを返す */
	InfoBox.show = function(target){
		var func = function(event){
			var div = $('#InfoBox-popup-window');
			var shim = $('#InfoBox-popup-window-SHIM');
			var flagDiv = false;
			var flagShim = false;
			if(div.length == 0){
				div = $('<div id="InfoBox-popup-window"></div>');
				div.css('position', 'absolute');
				div.css('padding', '3px 4px');
				div.css('border', '1px solid #FFBB00');
				div.css('background-color', '#FDFDD4');
				div.css('font-size', '10px');
				div.css('z-index', '2');
				
				flagDiv = true;
			}
			if(shim.length == 0){
				// IFRAME SHIM
				shim = $('<iframe id="InfoBox-popup-window-SHIM" frameborder="0" src="about:blank" scrolling="no"></iframe>');
				shim.css('position', 'absolute');
				shim.css('display', 'block');
				shim.css('z-index', '1');
				
				flagShim = true;
			}
			div.hide();
			shim.hide();
			
			//top = event.clientY + $(document.body).scrollTop() - 20;
			var top = event.pageY + 10;
			if(top < 0)
				top = 0;
			//left = event.clientX + $(document.body).scrollLeft() + 5;
			var left = event.pageX + 15;
			var htmlWidth = $('html').width();
			if(jQuery.browser.msie){
				if(left + InfoBox.width + 40 > htmlWidth)
					left = htmlWidth - InfoBox.width - 40;
			} else {
				if(left + InfoBox.width + 20 > htmlWidth)
					left = htmlWidth - InfoBox.width - 20;
			}
			
			div.css('top', top);
			div.css('left', left);
			shim.css('top', top);
			shim.css('left', left);
			
			div.text(target.attr('infobox:message'));
			
			shim.width(div.width() + parseInt(div.css('padding-left') || '0') + parseInt(div.css('padding-right') || '0'));
			shim.height(div.height() + parseInt(div.css('padding-top') || '0') + parseInt(div.css('padding-bottom') || '0'));
				
			if(flagDiv) div.appendTo(document.body);
			if(flagShim) shim.appendTo(document.body);
			
			div.width(InfoBox.width);
			shim.width(InfoBox.width);
			
			div.fadeIn("500", function(){
				shim.show();
				var hideFunc = InfoBox.hide(target);
				$(document).one('mousemove', function(){
					hideFunc();
				});	
			});
			
			return false;
		}
		
		return func;
	}
	
	InfoBox.hide = function(target){
		var func = function(){
			$('#InfoBox-popup-window').hide();
			$('#InfoBox-popup-window-SHIM').hide();
			return false;
		}
		return func;
	}

})(jQuery);
