// UTF-8
/**
 * lockup.tooltip.js
 * Copyright (c) 2010 LockUP
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 * 
 *
*/
$.fn.toolTip = function(obj){
	//@obj
	//	imgPath			:	画像フォルダのパス　例 './images/'
	//	titleColor		:	タイトルテキスト色
	//	titleColor_H	:	タイトルテキスト色(Hover)
	//	textDecoration 	: 	アンダーラインの有無
	//	isUnderline		:	下線の有無
	//	time			:	ツールチップが表示されるインターバル

	var obj = (!!obj)?obj:{};
	$('<div id="ToolBoxContainer_js"></div>').appendTo($('body'));
	
	
	//▼CSS設定
	var toolStyle = {
		title : {
			"color":(obj.titleColor)?obj.titleColor:"#000",
			"borderBottom":(obj.isUnderline)?obj.isUnderline:"1px dashed "+((obj.titleColor)?obj.titleColor:"#000")+"",
			"cursor":"pointer"
		},
		titleHover : {
			"color":(obj.titleColor_H)?obj.titleColor_H:"#666"
		},
		toolTip : {
			"position":"absolute",
			"color":"#FFF",
			"display":"none"
		}
	};
	
	
	//IE PNG用スタイルの追加
	var iecss = '';
	iecss += '* html .tooltipPng{';
	iecss += 'behavior: expression(';
	iecss += 'this.style.behavior || (';
	iecss += 'this.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=crop,src=\'"+this.currentStyle.getAttribute(\"backgroundImage\").slice(5,-2)+"\')",';
	iecss += 'this.style.backgroundImage = "none",';
	iecss += 'this.style.behavior = "none"';
	iecss += ')';
	iecss += ');';
	iecss += '}';
	//$('<style type="text/css"></style>').appendTo('head').text(iecss);
	$('<style type="text/css">'+iecss+'</style>').appendTo('head'); 
	
	//▼ToolTipのHTML
	var tooltipSrc = '';
	tooltipSrc+='<table cellpadding="0" cellpadding="0" border="0" style="width:260px">';
	tooltipSrc+='<tr><td colspan="3" style="width:260px;height:15px;background:url('+((obj.imgPath)?obj.imgPath:'')+'tooltip_top.png) no-repeat left bottom;" class="tooltipPng"></td></tr>';
	tooltipSrc+='<tr>';
	tooltipSrc+='<td style="width:19px;background:url('+((obj.imgPath)?obj.imgPath:'')+'tooltip_left.png) no-repeat left top;" class="tooltipPng">&nbsp;</td>';
	tooltipSrc+='<td style="width:222px;background:url('+((obj.imgPath)?obj.imgPath:'')+'tooltip_bg.png) no-repeat left top;" class="tooltipPng" id="ToolBox_textImport_js"></td>';
	tooltipSrc+='<td style="width:19px;background:url('+((obj.imgPath)?obj.imgPath:'')+'tooltip_right.png) no-repeat left top;" class="tooltipPng">&nbsp;</td>';
	tooltipSrc+='</tr>';
	tooltipSrc+='<tr><td colspan="3"style="width:260px;height:30px;background:url('+((obj.imgPath)?obj.imgPath:'')+'tooltip_bottom.png) no-repeat left top;" class="tooltipPng">&nbsp;</td></tr>';


	
	var intervalTimer;
	var isRollover=false;
	var emptyTimer;
	this.each(function(i){
		$(this).css(toolStyle.title).mouseover(function(e){
			e.stopPropagation();
			var that = this;
			intervalTimer = setTimeout(function(){
				isRollover = true;
				$('<div id="ToolBox_js"></div>').
				html(tooltipSrc).appendTo($('#ToolBoxContainer_js')).css(toolStyle.toolTip).fadeIn("fast");
				$("#ToolBox_textImport_js").text($(that).next().text());

				var w = $("#ToolBox_js").width();
				var h = $("#ToolBox_js").height();
				var left = Math.floor($(e.target).offset().left)-$(e.target).width()/2;
				var top = Math.floor($(e.target).offset().top)-h+10;
				
				
				if(left+w>=$("html").width()){
					var p = left+w -$("html").width()+20;
					$("#ToolBox_js").css({'top':top,'left':left-p});
				}else if(left<=0){
					$("#ToolBox_js").css({'top':top,'left':20});
				}else{
					$("#ToolBox_js").css({'top':top,'left':left});
				}

				
			},(obj.time)?obj.time:100);
		}).mouseout(function(){
			clearInterval(intervalTimer);
			emptyTimer = setTimeout(function(){
				if($("ToolBox_js")){
					$("#ToolBoxContainer_js").empty();
					isRollover=false;
				}
			},100)
		})
	});
	
	$("#ToolBoxContainer_js").mouseover(function(){
		clearInterval(emptyTimer);
	}).mouseout(function(){
		$("#ToolBoxContainer_js").empty();
		isRollover=false;
	},true)
	
	return this;
}