;(function($){
	$.fn.tooltype = function( type, text, css , click) {
		var settings = {};
		return $(this).each(function() {
			
			this.__tooltype=new Object();
			this.__tooltype.shown=false;
			this.__tooltype.builded=false;
			this.__tooltype.element=$(this);
			this.__tooltype.mouseMoveHand=$.proxy(this.__tooltype.element.mouseXY,this);
			this.__tooltype.tt_container;
			this.__tooltype.text=text;
			this.__tooltype.click=typeof click =='undefined'?false:true;
			this.__tooltype.tt_container_css={width:'auto', height:'auto',background:'#fff',border:"1px solid #ccc",'border-radius':'5px','-webkit-border-radius':'5px','-moz-border-radius':'5px','position':'absolute'};
			$.extend(this.__tooltype.tt_container_css,css);
			this.__tooltype._show_f=$.proxy(this.__tooltype.element.tt_show,this);
			this.__tooltype._hide_f=$.proxy(this.__tooltype.element.tt_hide,this);
			
			if(this.__tooltype.click){
				this.__tooltype.element.click($.proxy(this.__tooltype.element.tt_show,this));
				this.__tooltype._hide_f=$.proxy(function(){
					this.__tooltype.element.tt_hide.call(this);
					/*if(jQuery.browser.msie){
						$('body').unbind('click',this.__tooltype._hide_f);
						
					}else{
						$(window).unbind('click',this.__tooltype._hide_f);
					}*/
				},this);
			}else{ 
				this.__tooltype.element.mouseenter($.proxy(this.__tooltype.element.tt_show,this));
				this.__tooltype.element.mouseleave($.proxy(this.__tooltype.element.tt_hide,this));
			}
			
			types = type.split(' ');
			
			for(var i=0;i<types.length ;i++){
				switch(types[i]){
					case 'top'		:this.__tooltype.vtype=0; break;
					case 'middle'	:this.__tooltype.vtype=1; break;
					case 'bottom'	:this.__tooltype.vtype=2; break;
					case 'left'		:this.__tooltype.htype=0; break;
					case 'center'	:this.__tooltype.htype=1; break;
					case 'right'	:this.__tooltype.htype=2; break;
				}
			}
		});
	};
	$.fn.tt_build = function() {
		var settings = {};
		return $(this).each(function() {
			if(!this.__tooltype.builded){
				this.__tooltype.tt_container=$(document.createElement('div'))
				.css(this.__tooltype.tt_container_css)
				.hide()
				.appendTo($('body'))
				.html(this.__tooltype.text);
				//this.__tooltype.tt_container.show();
				this.__tooltype.builded=true;
			}
		});
	};
	
	$.fn.tt_show = function(event) {
		var settings = {};
		return (function(event) { 
			if(!this.__tooltype.shown){
				if(!this.__tooltype.builded){ this.__tooltype.element.tt_build();}
				this.__tooltype.shown=true; 
				if(this.__tooltype.click){
					if(jQuery.browser.msie){
						this.__tooltype.tt_container.show();
						this.__tooltype.mouseMoveHand(event);
						$('body').bind('click',this.__tooltype._hide_f);
					}else{
						this.__tooltype.mouseMoveHand(event);
						this.__tooltype.tt_container.fadeIn(200);
						$(window).bind('click',this.__tooltype._hide_f);
					}
				}else{ 
					if(jQuery.browser.msie){
						this.__tooltype.tt_container.show();
						$('body').bind('mousemove',this.__tooltype.mouseMoveHand);
						$('body').mousemove(event);
						
					}else{
						$(window).bind('mousemove',this.__tooltype.mouseMoveHand);
						//$(window).mousemove(event);
						//this.__tooltype.mouseMoveHand();
						this.__tooltype.tt_container.fadeIn(200);
					}
				}
			}
			return false;
		}).call(this,[event]);
	};
	$.fn.tt_hide = function() {
		var settings = {};
		return $(this).each(function() {
			if(this.__tooltype.shown){
				this.__tooltype.shown=false;
				if(this.__tooltype.click){
					if(jQuery.browser.msie){
						this.__tooltype.tt_container.hide();
					}else{
						this.__tooltype.tt_container.fadeOut(200);
					}
				}else{
					if(jQuery.browser.msie){
						this.__tooltype.tt_container.hide();
						$('body').unbind('mousemove',this.__tooltype.mouseMoveHand);
					}else{
						this.__tooltype.tt_container.fadeOut(200);
						$(window).unbind('mousemove',this.__tooltype.mouseMoveHand);
					}
				}
			}
		});
	};
	$.fn.mouseXY = function(event) { 
		/*if(navigator.appName.substring(0,4) == "Micr"){ 
			// ;
			//mx=(navigator.appName=="Netscape")
			mx=event.clientX;
			my=event.clientY+Math.round(document.documentElement.scrollTop);
		}else if(navigator.appName.substring(0,4) == "Nets"){
			mx=e.pageX;
			my=e.pageY;
		}else{
			mx=event.pageX;
			my=event.pageY;
		}*/
		if(this.__tooltype.click){
			d=this.__tooltype.element.offset();
			mx=d.left;
			my=d.top;
		}else{
			mx=event.pageX;
			my=event.pageY;
		}
		body=$('body')[0];
		if(this.clientWidth+mx>body.clientWidth-20){
			mx=body.clientWidth-this.clientWidth-20;
		}
		//alert(this);
		width=this.__tooltype.tt_container.outerWidth();
		height=this.__tooltype.tt_container.outerHeight();
		switch(this.__tooltype.htype){
			case 0: x=mx-width; break;
			case 1: x=Math.round(mx-width/2); break;
			case 2: x=mx; break;
		}
		switch(this.__tooltype.vtype){
			case 0: y = my-height-15; break;
			case 1: y = Math.round(my-height/2); break;
			case 2: y = my+20; break;
		}
		this.__tooltype.tt_container.css({'left':x+"px"});
		this.__tooltype.tt_container.css({'top':y+"px"});
	};
})(jQuery);
