/**
 * Tooltip Object
 *
 */
(function($)
{
	$.fn.teliTooltip = function(settings)
	{
		settings = $.extend({}, $.fn.teliTooltip.defaults, settings);

		return this.each(function(){
			var $t=$(this);
			var $tc=$('#' + $t.attr('rel'));
			var m='';
			var h=0;
			var w=0;

			if((window.navigator.appVersion).search(/MSIE/) > 0) {
				if(!(document.documentElement.clientWidth == 0)) {
					h = document.documentElement.clientHeight;
					w = document.documentElement.clientWidth;
				} else {
					h = document.body.clientHeight;
					w = document.body.clientWidth;
				}
			} else {
				h = window.innerHeight;
				w = window.innerWidth;
			}

			if($tc[0].localName == 'span') {
				c='<div  id="'+$t.attr('rel')+'" class="dynamic_box_container" style="display: none; width: 210px;"><table class="dyn_box"><tr><td class="top_left">&nbsp;</td><td class="top_center">&nbsp;</td><td class="top_right">&nbsp;</td></tr><tr><td class="middle_left"></td><td class="middle_center"><strong class="orange">'+$tc.html()+'</strong></td><td class="middle_right"></td></tr><tr><td class="bottom_left"></td><td class="bottom_center"></td><td class="bottom_right"></td></tr></table></div>';
				$tc.remove();
				$('body').append(c);
				$tc=$('#' + $t.attr('rel'));
			} else {
				$tc.appendTo('body');
			}

			$t.bind('mouseenter', function(e) {

				m=setMode(e, settings.direction);

				var x	= calcX(e);
				var y	= calcY(e);

				$tc.css({
					"position": "absolute",
					"top": y + 'px',
					"left": x + 'px'
				});

				$tc.show(0);

				$('body').bind('mousemove', function(e) {
					var x = calcX(e);
					var y = calcY(e);

					$tc.css({
						"top": y,
						"left": x
					});
				});
			}).bind('mouseleave', function() {
				$tc.hide(0);
				$('body').unbind('mousemove');
			});

			var setMode = function(e, d){
				if(d&&d!='under') {
					if(d=='over'&&((e.clientY - ($tc.height() + 20)) > 0)) {
						return 'over'; 
					}
					if(d=='right'&&(e.clientX + $tc.width() + 20) < w) {
						return 'right'; 
					}
					if(d=='left') {
						return 'left';
					}
				}

				if((e.clientY + $tc.height() + 20) < h) {
					return 'under';
				}
				
				if(((e.clientY + $tc.height() + 20) >= h) && ((e.clientY - ($tc.height() + 20)) > 0)) {
					return 'over';
				}

				if((e.clientX + $tc.width() + 20) < w) {
					return 'right';
				}

				return 'left';
			};

			var calcX = function(e){
				if(m == 'right') {
					return (e.pageX + 20);
				}

				if(m == 'left') {
					return (e.pageX - ($tc.width() + 20));
				}

				if((e.pageX + ($tc.width() / 2)) > w) {
					return (e.pageX - (($tc.width() / 2) + ((e.pageX + ($tc.width() / 2)) - w + 30)));
				} else if((e.pageX - ($tc.width() / 2)) < 0) {
					return (e.pageX - (($tc.width() / 2) + (e.pageX - ($tc.width() / 2) - 5)));
				}

				return (e.pageX - ($tc.width() / 2));
			};

			var calcY = function(e){
				if(m == 'right' || m == 'left') {
					if((e.clientY - ($tc.height() / 2)) < 0) {
						return (e.pageY - (($tc.height() / 2) + (e.clientY - ($tc.height() / 2) + 5)));
					} else if((e.clientY + ($tc.height() / 2)) > h) {
						return (e.pageY - (($tc.height() / 2) + ((e.clientY + ($tc.height() / 2)) - h + 30)));
					}

					return (e.pageY - ($tc.height() / 2));
				}

				if(m == 'over') {
					return (e.pageY - ($tc.height() + 20));
				}

				return (e.pageY + 20);
			};
		});
	};

	$.fn.teliTooltip.defaults = {
		direction: null
	};

})(jQuery);
