var TTWT_TARGET_WRAPPER_NAME = "div.jqlib-thumbTileWithTooltip";

var ttwt_wrapper;
var ttwt_tooltip;
var ttwt_wait = false;

$(document).ready(function(){
	// 対象のwrapperを取得して、存在しない場合は負荷軽減のためにreturnする。
	ttwt_wrapper = $(TTWT_TARGET_WRAPPER_NAME);
	if (ttwt_wrapper.size() <= 0) return;
	// if (ttwt_wrapper.find("ul").size() <= 0) return;
	if (ttwt_wrapper.find("li").size() <= 0) return;

	ttwt_tooltip_inner = $("<div>").html("&nbsp;");
	ttwt_tooltip = $("<div>").attr("id", "tooltip").addClass("pngfix").append(ttwt_tooltip_inner);
	ttwt_wrapper.prepend(ttwt_tooltip);

	ttwt_tooltip_w = getWidth(ttwt_tooltip);
	ttwt_tooltip_h = getHeight(ttwt_tooltip);

	ttwt_wrapper.find("li").each(function(){
		var hover = false;
		$(this).hover(function(e){
			if (ttwt_wait) return;
			ttwt_wait = true;
			var title = clone($(this).find("h3:first"));
			var catchCopy = clone($(this).find(".catchCopy:first"));
			ttwt_tooltip_inner.html("");
			ttwt_tooltip_inner.append(title);
			ttwt_tooltip_inner.append(catchCopy);
			// ttwt_tooltip.css("display", "block");
			var left = $(this).find(".cbox").position().left;
			var top = $(this).find(".cbox").position().top;
			ttwt_tooltip.css("left", left - ttwt_tooltip_w / 2 + $(this).find(".cbox").width() / 2);
			ttwt_tooltip.css("top", top - ttwt_tooltip_h);
			if ($.browser.msie) {
				ttwt_tooltip.css("display", "block");
				ttwt_wait = false;
			} else {
				ttwt_tooltip.fadeIn(400, function(){ ttwt_wait = false; });
			}
		}, function(){
			// ttwt_tooltip.css("display", "none");
			ttwt_tooltip.fadeOut(0);
		});
		
		
	});
});

function getInt(e) {
	if (e.indexOf("px") >= 0) {
		e = e.replace("px", "");
		return ~~e;
	} else {
		return 0;
	}
}

function getWidth(e) {
	return e.width()
		+ getInt(e.css("padding-left"))
		+ getInt(e.css("padding-right"))
		+ getInt(e.css("margin-left"))
		+ getInt(e.css("margin-right"));
}

function getHeight(e) {
	return e.height()
		+ getInt(e.css("padding-top"))
		+ getInt(e.css("padding-bottom"))
		+ getInt(e.css("margin-top"))
		+ getInt(e.css("margin-bottom"));
}
