/***************************************************************
	Developed by Websplash IS
	Copyright 2008
	
	Info: ronald@websplash.nl

****************************************************************/

var ttset = false;
$.fn.jwebtooltip = function(options) {
	var defaults = {	'html' 		: '<div id="tooltip"><div class="top"></div><div id="tooltipmsg"></div><div class="bottom"></div></div>',
						'position'	: 'auto',
						'top'		: 3,
						'bottom'	: 15,
						'right'		: 0,
						'left'		: 0,
						'defaulttxt': 'false',
						'pre'		: false,
						're'		: false,
						'id'		: 'tooltip',
						'msg'		: 'tooltipmsg',
						'animate'	: false
					};

	var opts = $.extend(defaults, options);

	// Creating the tooltip
	$(document).ready(function(){
		if(!ttset)
			$('body').append(opts.html);
			ttset = true;
	});

	$(this).each(function(){
		var title;
		var bydefault;
		$(this).mouseover(function(){
			if($.browser.msie && ($.browser.version <= 6)){
				$('#'+opts.id).remove();
				$('body').append(opts.html)
				$('#'+opts.id).hide();
			}
			var zw = (document.documentElement.clientWidth/2)+document.documentElement.scrollLeft;
			var zh = (document.documentElement.clientHeight/2)+document.documentElement.scrollTop;

			var xrel = (xpos() > zw) ? 'l' : 'r';
			var yrel = (ypos() > zh) ? 't' : 'b';

			var pos = xrel+yrel;
			
			title = $(this).attr('title');

			$(this).removeAttr('title')
			if(opts.position == 'auto')
				$('#'+opts.id).attr({'class' : pos})

		//	if($.browser.msie && ($.browser.version <= 6)){
		//		$('#'+opts.id+'.'+pos+' div').pngfix();
		//	}

			if(!title){
				bydefault = true;
				title = opts.defaulttxt ? opts.defaulttxt : false
			}
			if(title){
				ctitle = opts.pre ? opts.pre+''+title : title;
				ctitle = opts.re ? ctitle+''+opts.re : ctitle;
				$('#'+opts.msg).html(ctitle);
				if(opts.animate)
					$('#'+opts.id).show(opts.animate)
				else
					$('#'+opts.id).show();
			}

			var toppos = 	{
								't' : 0-(document.getElementById(opts.id).scrollHeight+opts.top),
								'b' : opts.bottom
							}

			var leftpos = 	{
								'r' : opts.right,
								'l' : 0-(document.getElementById(opts.id).scrollWidth+opts.left)
							}	

			$(this).mousemove(function(){
				var e = window.event || arguments.callee.caller.arguments[0];
				document.getElementById(opts.id).style.left= xpos(e)+leftpos[xrel] + "px";
				document.getElementById(opts.id).style.top= ypos(e)+toppos[yrel]+ "px";
			});
		});

		$(this).mouseout(function(){
			if(title && !bydefault)
				$(this).attr({'title' : title})
			$('#'+opts.id).hide();
		});
	});
}

function xpos(e){
	e = e ? e : window.event || arguments.callee.caller.arguments[0];
	var scrollX = window.innerHeight ? 0 : ((document.documentElement.scrollLeft > document.body.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft);
	return e.pageX ? e.pageX : (e.clientX + scrollX);}

function ypos(e){
	e = e ? e : window.event || arguments.callee.caller.arguments[0];
	var scrollY = window.innerHeight ? 0 : ((document.documentElement.scrollTop > document.body.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop);
	return e.pageY ? e.pageY : (e.clientY + scrollY);}