/**
 * weetips.js
 *
 * weetips javascript
 *
 * @category   javascript
 * @package    jquery
 * @author     Jack <xiejinci@gmail.com>
 * @copyright  Copyright (c) 2006-2008 9wee Com. (http://www.9wee.com)
 * @license    http://www.9wee.com/license/
 * @version    
 */ 
(function(jQuery) {	
	var weetips = function() {
		this.init = function(options) {
			jQuery('<style type="text/css">'	+ 
				'#weetips{'	+ 
					'border:1px solid #000;' +
					'z-index:9999;'	+ 
					'position:absolute;' + 
					'background:#4c4c4c;' + 
					'color:#fff;' + 
					'padding:5px 10px;'	+ 
				'}'	+ 
			  '</style>').appendTo('body');
			jQuery('<div id="weetips"></div>').appendTo('body').hide();			
			jQuery(".weetips").mouseover(function(event) {
				var msg = jQuery(this).attr('title');
				if (jQuery.trim(msg) == '')  {
					return false;
				}		
				jQuery(this).attr('msg', msg);
				jQuery(this).removeAttr('title');				
				jQuery('#weetips').html(msg);
				jQuery.weetips.setPosition(event);		
				jQuery('#weetips').show();			
			}).mousemove(function(event){
				jQuery.weetips.setPosition(event);
			}).mouseout(function() {
				jQuery('#weetips').hide();		
				jQuery(this).attr('title',jQuery(this).attr('msg'));
				jQuery(this).removeAttr('msg');
			});	
		}
		
		this.setPosition = function(event) {
			var left = jQuery.weetips.getLeft(event);
			var top = jQuery.weetips.getTop(event);			
			jQuery('#weetips').css({left:left, top:top}).show();
		}
		
		this.getLeft = function(event) {
			var left = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft)+event.clientX;
			if (left+jQuery('#weetips').width()>=document.body.clientWidth) {
				left = event.clientX - 10 - jQuery('#weetips').width();
			} else {
				left = left + 10;
			}
			return left;
		}
		
		this.getTop = function(event) {
			var top = Math.max(document.documentElement.scrollTop, document.body.scrollTop)+event.clientY;
			if (top+jQuery('#weetips').height()>=document.body.clientHeight) {
				top = top - 10 - jQuery('#weetips').height();
			} else {
				top = top + 10;
			}		
			return top;
		}
	};	
	jQuery.extend({weetips: new weetips()});	
})(jQuery);