// 指定要素のrel属性をツールチップ表示
// http://www.queness.com/post/92/create-a-simple-cssjavascript-tooltip-with-jquery
$.fn.tooltip = function(){
	//if(this.length==0) return this;
	
	// Cookie functions
	var Cookie = {
		key    : 'wordhelp',
		path   : '/',
		def    : 1,
		create : function(val){
			var d = new Date();
			d.setTime(d.getTime()+(365*24*60*60*1000));
			var c = [];
			c.push(this.key);
			c.push('=');
			c.push(val);
			c.push(';');
			c.push('expires=');
			c.push(d.toGMTString());
			c.push(';');
			c.push('path=');
			c.push(this.path);
			c.push(';');
			document.cookie = c.join('');
		},
		get : function(){
			var n = this.key + '=';
			var c = document.cookie.split(';');
			var l = c.length;
			for(var i=0; i<l; i++){
				var d = c[i];
				while (d.charAt(0)==' ') d = d.substring(1, d.length);
				if(d.indexOf(n)==0) return d.substring(n.length, d.length);
			}
			this.create(this.def);
			return this.def;
		}
	};
	
	// Constructor
	if(Cookie.get()==1){
		$('#wordhelp_switcher li.wordhelp_ON').css({'visibility':'visible'});
		$('#wordhelp_switcher li.wordhelp_OFF').hide().css({'visibility':'visible'});
	}else{
		$('#wordhelp_switcher li.wordhelp_OFF').css({'visibility':'visible'});
		$('#wordhelp_switcher li.wordhelp_ON').hide().css({'visibility':'visible'});
	}
	
	// Switcher
	$('#wordhelp_switcher a').click(function(){
		//if($('#wordhelp_switcher li.wordhelp_ON:visible').length>0){
		if(Cookie.get()==1){
			$('#wordhelp_switcher li.wordhelp_ON').hide();
			$('#wordhelp_switcher li.wordhelp_OFF').show();
			Cookie.create(0);
		}else{
			$('#wordhelp_switcher li.wordhelp_OFF').hide();
			$('#wordhelp_switcher li.wordhelp_ON').show();
			Cookie.create(1);
		}
		$(this).blur();
		return false;
	});
	
	// Tooltip functions
	return this.each(function(){
		var flg = 1;
		$(this)
			.mouseover(function(e) {
				flg = Cookie.get();
				if(flg==0) return;
				var tip = $(this).attr('rel');
				$(this).append('<div id="tooltip">' + tip + '</div>');
				$('#tooltip').css('top', e.pageY + 10 );
				$('#tooltip').css('left', e.pageX + 20 );
				$('#tooltip').fadeTo('fast', 1);
			})
			.mousemove(function(e) {
				if(flg==0) return;
				$('#tooltip').css('top', e.pageY + 10 );
				$('#tooltip').css('left', e.pageX + 20 );
			})
			.mouseout(function() {
				if(flg==0) return;
				$(this).children('div#tooltip').remove();
			});
	});
};
$(function() {
	$('#main a.tooltip').tooltip();
});

