/*
 * Title:
 * Description:
 * Aucthor: ÔøÏÜ±ó
 * Email: zengxianbin@gmail.com
 * Create Date:2009-06-18
 * Copyright 2009
 */ 
(function($){
	$.fn.tip = function(msg,clock){
		this.msg = msg;
		this.clock = $(this).attr('clock')||clock||1;

		this._init = function(){
			if(this._tipPanel)
				this._tipPanel.remove();
			this.data("_tip", this);  
			return this;
		};
		this.show = function(){
			var str = [
				"<div id=abc class='z-tip z-tip-callout",this.clock,"' >",
					"<table border='0' cellspacing='0' cellpadding='0' class='z-table'>",
						"<tr><td class='z-corner z-topleft'> </td><td class='z-topcenter'> </td><td class='corner z-topright'> </td></tr>",
						"<tr><td class='z-bodyleft'> </td><td class='z-content'>"+this.msg+"</td><td class='z-bodyright'> </td></tr>",
						"<tr><td class='z-corner z-footerleft'> </td><td class='z-footercenter'> </td><td class='z-corner z-footerright'> </td></tr>",
					"</table>",
					"<div class='z-fang'></div>",
				"</div>"
			];
			var _tipPanel = this._tipPanel = $(str.join(''));

			$(document.body).append(_tipPanel);

			var c = this.clock;
			var pos = this.position();
			var w = this.width();
			var h = this.height();

			var x=pos.left, y=pos.top;

			var dw = _tipPanel.width();
			var dh = _tipPanel.height();

			if(c==2||c==3||c==4){
				x-=dw;
			}
			else if(c==8||c==9||c==10){
				x += w;
			}
			else if(c==12 || c == 6){
				x = x-dw/2+w/2;
			}
			else if(c==5 || c==1){
				x -= dw/2
			}
			else if(c==7 || c==11){
				x += w-dw/2
			}
			
			if(c==5||c==6||c==7){
				y -= dh;
			}
			else if(c==11||c==12||c==1){
				y += h;
			}
			else if(c==9||c==3){
				y = y-dh/2+h/2;
			}
			else if(c==2||c==10){
				y = y-dh/2+h;
			}
			else if(c==4||c==8){
				y = y-dh/2;
			}
			_tipPanel.css('left',x);
			_tipPanel.css('top',y);
			//_tipPanel.remove();
		}
		this.close = function(){
			if(this._tipPanel)
				this._tipPanel.remove();
			this.data("_tip", null); 
		}

		var tip = $(this).data("_tip");
		if(tip){
			return tip;
		}else{
			tip = this._init();
		};
		return tip;
	};
	$.initTip = function(){
		$('$[tip]').each(function(){
			$(this).hover(function(){
				$(this).tip($(this).attr('tip'),7).show();
			},function(){
				$(this).tip().close();
			})
		});
	}
	$($.initTip)
})(jQuery);	