// readitlatertooltips.js
// 2010.07.30
var KNEWS;

(function($) {
	if (typeof KNEWS == 'undefined') KNEWS = {};

	KNEWS.Tooltip = function() {
		this.init();
	};
	KNEWS.Tooltip.prototype = {
		WAITE_DISP_DTIME: 1000,
		WAITE_HIDE_DTIME: 200,
		TOOLTIP_DIV_ID: 'saveBtn',
		TOOLTIP_MOUSECURSOL_DX: 10,
		TOOLTIP_MOUSECURSOL_DY: 15,
		TOOLTIP_RIGHT_SPACE_MAX: 5,
		TOOLTIP_WIDTH: 137,
		TOOLTIP_HEIGHT: 33,
		init: function() {
			var self = this;
			$('.newsdata').each(function(){
				$(this)
					.mouseover(
						function(e){
							self.onNewsDataMouseOver.apply(self, [this, e, 'href']);
						}
					)
					.mouseout(
						function(e) {
							self.onNewsDataMouseOut.apply(self, [this, e]);
						}
					)
					.mousemove(
						function(e){
							self.onNewsDataMouseMove.apply(self, [this, e]);
						}
					);
			});
			// add
			$('.newsdata_btn').each(function(){
				$(this)
					.mouseover(
						function(e){
							self.targetId = self.getTargetId(this, 'href');
						}
					)
					.click(
						function() {
							// clickToolTip(targetId);
							self.isDispToolTip = false;
							KNEWS.readItLaterPanel.addReadItLaterIds([self.targetId]);
							return false;
						}
					);
			});
			// add 07.28
			$('.newsdata_rel').each(function(){
				$(this)
					.mouseover(
						function(e){
							self.onNewsDataMouseOver.apply(self, [this, e, 'rel']);
						}
					)
					.mouseout(
						function(e) {
							self.onNewsDataMouseOut.apply(self, [this, e]);
						}
					)
					.mousemove(
						function(e){
							self.onNewsDataMouseMove.apply(self, [this, e]);
						}
					);
			});

		},
		onNewsDataMouseOver: function(element, e, attr) {
			var self = this;
			clearTimeout(this.waiteHideTimerId);
			var currentId = this.getTargetId(element, attr);
			this.mouseX = e.pageX;
			this.mouseY = e.pageY;
			if ((this.isDispToolTip || this.isWriteReadItLater) && this.targetId == currentId) {
				return;
			}
			
			this.targetElem = $(element);
			this.targetId = currentId;
			this.waiteDispToolTip = true;
			this.waiteDispTimerId = setTimeout(function () {self.showToolTip()}, this.WAITE_DISP_DTIME);
		},
		onNewsDataMouseOut: function(element, e) {
			var self = this;
			this.waiteDispToolTip = false;
			clearTimeout(this.waiteDispTimerId);
			if ((this.toolTipX <= e.pageX) && (e.pageX <= this.toolTipX + this.TOOLTIP_WIDTH)
				&& (this.toolTipY <= e.pageY) && (e.pageY <= this.toolTipY + this.TOOLTIP_HEIGHT)) {
				return;
			}
			if (!this.isMouseCursolOnToolTip) {
				this.waiteHideTimerId = setTimeout(function () {self.hide()}, this.WAITE_HIDE_DTIME);
			}
		},
		onNewsDataMouseMove: function(element, e) {
			this.imouseX = e.pageX;
			this.imouseY = e.pageY;
		},
		showToolTip: function() {
			var self = this;
			clearTimeout(this.waiteDispTimerId);
			clearTimeout(this.waiteHideTimerId);
			this.hide();
			
			if (!this.waiteDispToolTip) {
				return;
			}
			if (!this.tooltip) {
				this.tooltip = $('<div/>')
					.attr('id', this.TOOLTIP_DIV_ID)
					.append($('<img/>')
						.attr({
							src: '/common/img/btn_save.gif',
							width: 89,
							height: 23,
							alt: 'Read It Later'
						}));
				this.tooltip.css({
						cursor: 'pointer',
						position: 'absolute'
					})
					.click(
						function() {
							self.clickToolTip(self.targetId);
						}
					)
					.hover(
						function() {
							self.isMouseCursolOnToolTip = true;
							clearTimeout(self.waiteHideTimerId);
						},
						function() {
							self.isMouseCursolOnToolTip = false;
							self.hide();
						}
				);
				$('body').append(this.tooltip);
			}
			var pos = this.targetElem.position();
			var left = pos.left + this.targetElem.width();
			this.toolTipX = Math.min(this.mouseX + this.TOOLTIP_MOUSECURSOL_DX, left + this.TOOLTIP_RIGHT_SPACE_MAX);
			this.toolTipY = this.mouseY - this.TOOLTIP_MOUSECURSOL_DY;
			this.tooltip.css({
					top: this.toolTipY,
					left: this.toolTipX
				});
			this.tooltip.show();
			this.isDispToolTip = true;
			this.isWriteReadItLater = false;
		},
		clickToolTip: function(id) {
			KNEWS.readItLaterPanel.addReadItLaterIds([id]);
			this.hide();
			this.isWriteReadItLater = true;
		},
		hide: function() {
			if (!this.tooltip) return;
			clearTimeout(this.waiteHideTimerId);
			this.tooltip.hide();
			this.isDispToolTip = false;
		},
		getTargetId: function(element, attr) {
			var targetId = $(element).attr(attr);
			targetId = targetId.split('#')[0].split('?')[0].split('/').reverse()[0];
			return targetId.split('.')[0];
		}
	};
/*
	$.fn.readItLaterToolTips = function () {
		KNEWS.tooltip = new KNEWS.Tooltip();
		jQuery.fn.newsReaderPanel = function (closeFunctionAry) {
			KNEWS.newsReaderPanel = new KNEWS.NewsReaderPanel({closeFuncAry:closeFunctionAry});
		}
		jQuery.fn.readItLaterPanel = function (closeFunctionAry) {
			KNEWS.readItLaterPanel = new KNEWS.ReadItLaterPanel({closeFuncAry:closeFunctionAry});
		}
	};
	if (typeof KNEWS.NewsReaderPanel == 'undefined') {
		document.write('<script language="JavaScript" type="text/javascript" src="/common/js/newsdatacontrol.js"></script>');
		document.write('<script language="JavaScript" type="text/javascript" src="/common/js/newsdatapanel.js"></script>');
	}
*/
})(jQuery);

