/**
 * @example
 *	$('#username').hover(function(){
 *		$(this).arrowTip('Please enter your username', {
 *			arrow: {direction: 'top'},
 *			position: 8
 *		});
 *	}, function(){
 *		$(this).data('arrowTip').remove();
 *	});
 */
$.fn.arrowTip = function(text, opts) {

	var s = $.extend(true, {}, $.fn.arrowTip.defaults, opts);

	return this.each(function(){
		$(this).data('arrowTip', new _arrowTip(this, text, s));
	});
};

$.fn.arrowTip.defaults = {
	// direction, height, width, side, offset
	arrow: {
		direction: 'bottom',
		height: 6
	},
	// 是否考虑出界
	strict: false,
	// 1-9,
	//	left top, center top, right top,
	//	left middle, center middle, right middle,
	//	left bottom, center bottom, right bottom
	position: 2,
	offsetX: 0,
	offsetY: 0,
	distance: 6,
	closeable: true,
	minWidth: 50,
	maxWidth: 400,
	opacity: 1,
	className: 'jcan-arrowTip'
};

var _arrowTip = function(o, text, opts) {
	this.settings = opts;
	this.tip = null;
	this.remove = function() {
		var _s = {opacity: 0};
		switch (this.settings.arrow.direction) {
			case 'top':
				_s.top ='-=' + this.settings.distance;
				break;
			case 'bottom':
				_s.top = '+=' + this.settings.distance;
				break;
			case 'left':
				_s.left = '-=' + this.settings.distance;
				break;
			case 'right':
				_s.left = '+=' + this.settings.distance;
				break;
		}
		this.tip.animate(_s, 'fast', function(){
			$(this).remove();
		});
	};

	var self = this, $$ = $(o), s = opts, tip;
	var offset = $$.getPosOffset(s.position);

	if (typeof text == 'string') {
		tip = $('<div\/>').html(text);
	} else {
		tip = text.clone();
	}
	this.tip = tip.addClass(s.className);

	tip.css({
		position: $$.css('absolute') == 'fixed' ? 'fixed' : 'absolute',
		visibility: 'hidden',
		opacity: 0,
		display: 'block'
	})
	.appendTo('body')
	.addCssArrow(s.arrow, offset.left, offset.top)
	.css('visibility', 'visible');

	var _s = {opacity: s.opacity};
	switch (s.arrow.direction) {
		case 'top':
			_s.top ='+=' + s.distance;
			break;
		case 'bottom':
			_s.top = '-=' + s.distance;
			break;
		case 'left':
			_s.left = '+=' + s.distance;
			break;
		case 'right':
			_s.left = '-=' + s.distance;
			break;
	}
	tip.animate(_s, 'fast', function(){
		$.browser.msie && $(this).css('filter', 'none');
		$.browser.opera && $(this).css('opacity', 1);
	});
};