var isMSIE = /*@cc_on!@*/0;
var isMSIE6 = isMSIE && (typeof document.documentElement.style.msInterpolationMode == 'undefined');

var compact = function(s) {
	var p0 = s.lastIndexOf('<p>');
	var p1 = s.lastIndexOf('</p>');
	var br = s.substr(0, p1).lastIndexOf('<br/>');
	if(p0 < br && br < p1) {
		s = s.substr(0, br) + s.substr(p1);
	} else if(br < p0 && p0 < p1) {
		s = s.substr(0, p0) + s.substr(p1+4);
	} else {
		return false;
	}
	return s;
};

var converter = function(s) {
	// 1100 00XX 10XX YYYY -> (0000) XXXX YYYY
	var hex2int = function(s) {
		return parseInt(s, 16);
	};
	var int2hex = function(n) {
		return ((n < 16) ? '0' : '') + n.toString(16);
	};
	var dst = "";
	for(var p=0 ; p<s.length ; p+=6) {
		if(s.substr(p, 1)=='%' && s.substr(p+3, 1)=='%') {
			var a = (hex2int(s.substr(p+1, 2)) << 8) | hex2int(s.substr(p+4, 2));
			dst += '%' + int2hex( ((a&0x3f00)>>2) | (a&0x3f) );
		}
	}
	return dst;
};

var encoder = isMSIE6 ? function(s) {
	return converter(encodeURIComponent(s));
} : function(s) {
	return encodeURIComponent(s);
};

function popupDict(param) {
	var eword;
	var a = this;
	var handler = function(r) {
		var title = $(r).find('title').text();
		var content = $(r).find('content').text();
		var image = $(r).find('image').text();
		
		if(!document.getElementById('#hiddenModalContent')) {
			$('body').append('<div id="hiddenModalContent" style="display: none;"></div>');
		}
		var target = $('#hiddenModalContent');
		var html = '';
		html += '<div style="float: right;"><a href="#" id="close_thickbox">Close</a></div>';
		html += '<div id="word_content" style="padding: 0 20px 0 20px;">';
		if(image) {
			html += '<div style="text-align: center;"><a href="#" class="go_word"><img id="word_image" src="'+image+'"/></a></div>';
		}
		html += '<h4 style="margin-top: 10px; font-size: 150%;"><a href="#" class="go_word"></a></h4>';
		html += '<div id="word_text"></div>';
		html += '</div>';
		target.html(html);
		$('#hiddenModalContent h4 a').html(title);
		$('a.go_word')
		.attr('href', '/words/'+eword);
		$('#close_thickbox').click(tb_remove);
		(a.tagName == 'A') && a.blur();
		var show = function() {
			tb_show(null, "#TB_inline?height=510&width=550&inlineId=hiddenModalContent&modal=true", false);
			$('#TB_ajaxContent').css('overflow-y', 'hidden');
			$('#TB_overlay').click(tb_remove);
			$('#word_text').html(content);
			var more_text = '<a href="/words/'+eword+'">続きを読む</a>';
			while(true) {
				var h = $('#word_content').height();
				if(h < 490) {
					break;
				}
				var compacted = compact(content);
				if(compacted) {
					if(content == compacted) {
						break;
					}
					content = compacted;
					$('#word_text').html(content + more_text);
				} else {
					$('#word_text').html();
					break;
				}
			}
		};
		if(image) {
			$('#word_image').load(show);
		} else {
			show();
		}
	};
	
	var word;
	var errorHandler = function(r, status) {
		window.open('http://www.google.com/search?q=' + eword, '');
	};
	if(a.tagName == 'A') {
		eword = a.href.replace(/^.+\/words\//, '');
		word = decodeURIComponent(eword);
	} else {
		eword = param;
		word = decodeURIComponent(eword);
	}
	
	jQuery.ajax({
		url: '/words/'+eword+'.xml',
		success: handler,
		error: errorHandler
	});
	return false;
}

this.vtip = function(_target) {
	var vtip = this;
	vtip.xOffset = -10; // x distance from mouse
	vtip.yOffset = 20; // y distance from mouse
	vtip.shown = false;
	
	var target = _target || $(".vtip");
	var close = function(_this) {
		_this.title = _this.t;
		vtip.shown = false;
		$("#vtip").fadeOut("slow", function() {
			if(!vtip.shown) {
				$(this).remove(); 
			}
		});
		$(document).unbind('click');
		return false;
	};
	target.unbind().hover(function(e) {
		if(vtip.shown) {
			$('#vtip').show();
			return false;
		}
		var a = this;
		var eword = a.href.replace(/^.+\/words\//, '');
		if(isMSIE6) {
			eword = encoder(eword);
		}
		var handler = function(r) {
			var title = $(r).find('title').text();
			var content = $(r).find('heading').text();
			var image = $(r).find('thumb').text();
			
			a.t = a.title || '';
			a.title = '';
			
			var html = '';
			html += '<div style="width: 2em; float: right; text-align: right;"><a href="" id="closeIcon">×</a></div>';
			html += '<span style="float: left; text-align: left; margin-bottom: 10px;"><a href="" class="go_word">' + title + '</a></span>';
			if(image) {
				html += '<div style="clear: left; text-align: center; padding-bottom: 5px;"><a href="" class="go_word"><img src="'+image+'"/></a></div>';
			}
			html += "<p>" + content + '...</p>';
			$('#vtipcontent').html(html);
			$('#closeIcon').attr('href', '#').css('text-decoration', 'none').bind('click', close);
			$('#vtipcontent a.go_word')
			.attr('href', '/words/'+eword)
			.click(function(e) {
				e.stopPropagation();
				return true;
			});
			if(typeof DD_belatedPNG != 'undefined') {
				DD_belatedPNG.fix("#vtipArrow");
			}
			$('#vtip').fadeIn("fast");
			vtip.shown = true;
			
			$(document).bind('click', close);
		};
		var errorHandler = function(r, status) {
			window.open('http://www.google.com/search?q=' + eword, '');
		};
		{
			var top = (e.pageY + vtip.yOffset);
			var left = (e.pageX + vtip.xOffset);
			$('body').append('<div id="vtip" style="display: none;"><img id="vtipArrow" /><div id="vtipcontent"></div></div>');
			$('#vtipArrow')
			.attr("src", '/img/common/vtip_arrow.png')
			.css("position", "absolute")
			.css("top", "-10px")
			.css("left", "5px");
			$('#vtip').css("width", "320px")
			.css("text-align", "left")
			.css("position", "absolute")
			.css("padding", "10px")
			.css("left", "5px")
			.css("font-size", "12px")
			.css("background-color", "white")
			.css("border", "1px solid #a6c9e2")
			.css("-moz-border-radius", "5px")
			.css("-webkit-border-radius:", "5px;")
			.css("z-index", 9999);
			$('#vtip').css("top", top+"px").css("left", left+"px");
		}
		jQuery.ajax({
			url: '/words/'+eword+'.xml',
			success: handler,
			error: errorHandler
		});
		return false;
	}, function(e) {
//		vtip.shown = false;
//		close(this);
		return false;
	}).mousemove(
		function(e) {
//			this.top = (e.pageY + vtip.yOffset);
//			this.left = (e.pageX + vtip.xOffset);
//
//			$("#vtip").css("top", this.top+"px").css("left", this.left+"px");
		}
	);
};

// for news
$(function() {
	if(document.getElementById('article')) {
		$("#article a").each(function() {
			if(0 <= this.href.indexOf('/words/')) {
				vtip($(this));
			}
		});
	}
});

if(isMSIE && isMSIE6) {
	var h = document.getElementsByTagName('HEAD').item(0);
	var s = document.createElement('SCRIPT');
	s.type = 'text/javascript';
	s.src = '/js/belatedpng.js';
	h.appendChild(s);
}

