(function($){ 
	$.fn.extend({
		bstip: function(options) {
				var opts = $.extend({}, $.fn.bstip.defaults, options);
			return this.each(function(i){						
				var nowid=$(this).id, nowtitle=$(this).attr("title"),here=$(this);
				var nowclass=here.attr("class"),nowrel=here.attr("rel");
				here.addClass("souniqueithurts");
				if(nowclass==""){
					newclass="";
				}else{
					newclass=nowclass;
					newclass=newclass.replace(/ /,"");
				}
				if(nowid==""){
					newid="";
				}else{
					newid=nowid;
				}
				if(nowrel==""){
					newrel="";
				}else{
					newrel=nowrel;
				}
				if(opts.type=="tip"){
					$("body").append("<div class='"+opts.color+"' id='bs"+i+newclass+newid+newrel+"'>"+opts.forewrap+nowtitle+opts.backwrap+"</div>"); 
				}else if(opts.type=="menu"){
					nextitem=$('#'+nowtitle);
					nextcont=nextitem.html();
					$("body").append("<div class='"+opts.color+"' id='bs"+i+newclass+newid+newrel+"'>"+opts.forewrap+nextcont+opts.backwrap+"</div>");
					nextitem.remove();
				}
				var bs_tooltip = $("#bs"+i+newclass+newid+newrel);
				tipwidth=bs_tooltip.width();
				tipheight=bs_tooltip.height();
						offset = here.offset();
						left=offset.left;
						right=offset.right;
						nwidth=$(this).width();
						nheight=$(this).height();
							if(opts.hook=="bottom-right" || opts.hook=="mid-right" || opts.hook=="top-right"){
								bleft=offset.left+nwidth+opts.xnudge;
							}else if(opts.hook=="bottom-mid" || opts.hook=="mid-mid" || opts.hook=="top-mid"){
								bleft=offset.left+(nwidth/2)-(tipwidth/2)+opts.xnudge;
							}else if(opts.hook=="bottom-left" || opts.hook=="mid-left" || opts.hook=="top-left"){
								bleft=offset.left-opts.xnudge-tipwidth;
							}
							 if(opts.hook=="top-right" || opts.hook=="top-mid" || opts.hook == "top-left"){
								btop=offset.top-opts.ynudge-tipheight;
							 }else if(opts.hook=="mid-right" || opts.hook=="mid-mid" || opts.hook == "mid-left"){
								btop=offset.top+opts.ynudge;
							 }else if(opts.hook=="bottom-right" || opts.hook=="bottom-mid" || opts.hook=="bottom-left"){
								btop=offset.top+nheight+opts.ynudge;
							 }
					bs_tooltip.css({top:btop,left:bleft,position:"absolute",display:"none"});
		if(opts.sticky=="none"){
			$(this).removeAttr("title").hover(function(){
				bs_tooltip.css({"opacity":opts.opacity}).fadeIn(opts.speed);
			},function(){
				t = setTimeout(function(){
					bs_tooltip.fadeOut(opts.speed);
				}, opts.tack);
			});
					bs_tooltip.hover(function(){
						clearTimeout(t);
					},function(){
						t = setTimeout(function(){
							bs_tooltip.fadeOut(opts.speed);						
						}, opts.tack);
					});
		}else if(opts.sticky=="move"){
			$(this).removeAttr("title").mouseover(function(){
					bs_tooltip.css({opacity:opts.opacity, display:"none"}).fadeIn(opts.speed);
			}).mousemove(function(kmouse){
						tipwidth=bs_tooltip.width();
						tipheight=bs_tooltip.height();
							if(opts.hook=="bottom-right" || opts.hook=="mid-right" || opts.hook=="top-right"){
								bleft=kmouse.pageX+opts.xnudge;
							}else if(opts.hook=="bottom-mid" || opts.hook=="mid-mid" || opts.hook=="top-mid"){
								bleft=kmouse.pageX-(tipwidth/2)+opts.xnudge;
							}else if(opts.hook=="bottom-left" || opts.hook=="mid-left" || opts.hook=="top-left"){
								bleft=kmouse.pageX-opts.xnudge-tipwidth;
							}
							 //Position Up and Down
							 if(opts.hook=="top-right" || opts.hook=="top-mid" || opts.hook == "top-left"){
								btop=kmouse.pageY-tipheight-opts.ynudge;
							 }else if(opts.hook=="mid-right" || opts.hook=="mid-mid" || opts.hook == "mid-left"){
								btop=kmouse.pageY-(tipheight/2)+opts.ynudge;
							 }else if(opts.hook=="bottom-right" || opts.hook=="bottom-mid" || opts.hook=="bottom-left"){
								btop=kmouse.pageY+opts.ynudge;
							 }
					bs_tooltip.css({left:bleft, top:btop});
			}).mouseout(function(){
					bs_tooltip.fadeOut(opts.speed);
			});	
		}else if(opts.sticky=="slide"){
			$(this).removeAttr("title").mouseover(function(){
					bs_tooltip.css({opacity:opts.opacity, display:"none"}).fadeIn(opts.speed);
			}).mousemove(function(kmouse){
							if(opts.hook=="bottom-right" || opts.hook=="mid-right" || opts.hook=="top-right"){
								bleft=kmouse.pageX+opts.xnudge;
							}else if(opts.hook=="bottom-mid" || opts.hook=="mid-mid" || opts.hook=="top-mid"){
								bleft=kmouse.pageX-(tipwidth/2)+opts.xnudge;
							}else if(opts.hook=="bottom-left" || opts.hook=="mid-left" || opts.hook=="top-left"){
								bleft=kmouse.pageX-opts.xnudge-tipwidth;
							}
							 if(opts.hook=="top-right" || opts.hook=="top-mid" || opts.hook == "top-left"){
								btop=top-opts.ynudge-tipheight;
							 }else if(opts.hook=="mid-right" || opts.hook=="mid-mid" || opts.hook == "mid-left"){
								btop=top+opts.ynudge;
							 }else if(opts.hook=="bottom-right" || opts.hook=="bottom-mid" || opts.hook=="bottom-left"){
								btop=top+nheight+opts.ynudge;
							 }
					bs_tooltip.css({left:bleft, top:btop});
			}).mouseout(function(){
				t = setTimeout(function(){
					bs_tooltip.fadeOut(opts.speed);
				}, opts.tack);
			});	
					bs_tooltip.hover(function(){
						clearTimeout(t);
					},function(){
						t = setTimeout(function(){
							bs_tooltip.fadeOut(opts.speed);						
						}, opts.tack);
					});
		}
	});
	}
});
  $.fn.bstip.defaults = {
   		sticky:'move',
		forewrap:'<div><p>',
		backwrap:'</p></div>',
		hook:'bottom-right',
		color:'TipCSS1',
		speed:'fast',
		type:'tip',
		tack:0,
		keep:2000,
		ynudge:15,
		xnudge:15,
		opacity:.8
  };
})(jQuery);
