(function($) {
	$.tiny = $.tiny || {};
	$.tiny.tooltip = {
		options : {
			direct : 'right',
			type : 'normal',
			hover : 'false',
			top : 0,
			left : 15
		}
	};
	$.fn.tinytooltip = function(oPopup, options) {
		var options = $.extend( {}, $.tiny.tooltip.options, options);
		if (options.hover == 'true') {
			new HoverTooltip($(this), $(oPopup), options);
		} else {
			new Tooltip($(this), $(oPopup), options);
		}
		return this;
	};

	function HoverTooltip(root, oPopup, options) {
		oPopup.appendTo('body');
		function initialize() {
			setUpdate();
			setEvent();
		}
		;

		function setUpdate() {
			oPopup.css( {
				'display' : 'none',
				'position' : 'absolute'
			});
		}

		function setEvent() {
			$(root).bind('mouseenter', start);
		}

		function start() {
			oPopup.css('display', 'block');
			$(document).bind('mousemove', hover);
			$(root).bind('mouseleave', end);
		}
		function end() {
			oPopup.css('display', 'none');
			$(document).unbind('mousemove');
			$(root).unbind('mouseleave');
		}

		function hover(oEvent) {
			oPopup.css( {
				'left' : oEvent.pageX + options.left + 'px',
				'top' : oEvent.pageY + options.top + 'px'
			});
		}

		return initialize();
	}

	function Tooltip(root, oPopup, options) {
		var oButton = $('.btnTooltip', root);

		var oPopupMain = $('.bgPopupTooltip', root);

		oPopup.appendTo('body');

		function initialize() {
			setUpdate();
			setEvent();
		}
		;

		function setUpdate() {
			oPopupMain.css( {
				"opacity" : "0",
				"background" : "white",
				"display" : "none",
				"position" : "fixed",
				"height" : "100%",
				"width" : "100%",
				"top" : "0px",
				"left" : "0px",
				"z-index" : "35"
			});
			oPopup.css( {
				"display" : "none",
				"position" : "absolute",
				"z-index" : "36"
			});
		}

		function setPosition() {
			var pos = getRealPosition(root.get(0));
			if (options.direct == 'right') {
				oPopup.left = pos.x + root.width();
				oPopup.top = pos.y;
			} else if (options.direct == 'left') {
				oPopup.left = pos.x - oPopup.width();
				oPopup.top = pos.y;
			} else if (options.direct == 'bottom') {
				oPopup.top = pos.y + root.height();
				oPopup.left = pos.x;
			} else if (options.direct == 'top') {
				oPopup.top = pos.y - oPopup.height();
				oPopup.left = pos.x;
			}

			oPopup.css( {
				"left" : oPopup.left + 'px',
				"top" : oPopup.top + 'px'
			});
		}

		function setEvent() {
			oButton.bind('click', start);
		}
		;

		function start() {
			setUpdate();
			setPosition();
			oPopupMain.bind('click', end);
			switch (options.type) {
			case 'normal':
				oPopupMain.css('display', 'block');
				oPopup.css('display', 'block');
				break;
			case 'fading':
				oPopupMain.css('display', 'block');
				oPopup.css( {
					'display' : 'block',
					'opacity' : '0'
				});
				oPopup.animate( {
					opacity : '1'
				}, 300);
				break;
			case 'moveright':
				oPopupMain.css('display', 'block');
				var widthPop = oPopup.width();
				oPopup.css( {
					'display' : 'block',
					'width' : '0px'
				});
				oPopup.animate( {
					width : widthPop + 'px'
				}, 300);
				break;
			case 'moveleft':
				oPopupMain.css('display', 'block');
				var widthPop = oPopup.width();
				var leftPop = oPopup.left + widthPop;
				oPopup.css( {
					'display' : 'block',
					'width' : '0px',
					'left' : leftPop + 'px'
				});
				oPopup.animate( {
					width : widthPop + 'px',
					left : oPopup.left + 'px'
				}, 300);
				break;
			default:
				oPopupMain.css('display', 'block');
				oPopup.css('display', 'block');
			}

			$(document).bind('keypress', keypress);
		}
		;

		function keypress(e) {
			if (e.keyCode == 27) {
				end();
			}
		}
		;

		function getRealPosition(el) {
			pos = {
				x : 0,
				y : 0
			};
			while (el) {
				pos.x += $(el).position().left;
				pos.y += $(el).position().top;
				el = el.offsetParent;
			}
			return pos;
		}

		function end() {
			$(document).unbind('keypress', keypress);
			oPopupMain.unbind('click', end);
			oPopupMain.attr('style', 'display:none;');
			oPopup.attr('style', 'display:none;');
		}
		;

		return initialize();
	}
	;
})(jQuery);
