
;(function($){

	$.fn.extend({
		svgtip: function(options) {
			options = $.extend(true, {}, $.Svgtip.defaults, options);
			return this.each(function(){
				new $.Svgtip(this, options);
			});
		}
	});

	$.Svgtip = function(element, options) {
		var $element=$(element),
		target=options.target || $element.attr(options.targetAttribute),
		tipId='svgtip_'+(((typeof MD5 != 'undefined') && $.isFunction(MD5) && target)?MD5(target):($.Svgtip.idGenerator++)),
		tipSelector="#"+tipId;
		if(!$(target).length){return null}
		var dim={
			width:$(target).width(),
			height:$(target).height()
		};
		options.border = (options.tipAttr['stroke-width'])?Number(options.tipAttr['stroke-width']):0;
		$element.hover(function(){
			var $tip = $(tipSelector);
			if(!$tip.length){
				var cvsWidth=(((options.margin+options.padding+options.border)*2)+dim.width),
				cvsHeight=(((options.margin+options.padding+options.border)*2)+dim.height+options.arrow),
				zIndex=options.zIndex;
				$paper = $("<div class=\""+options.prefixClass+"-draw\"/>").width(cvsWidth).height(cvsHeight).css({
					position:'absolute',
					zIndex:zIndex
				});
				$tip = $("<div id=\""+tipId+"\" class=\""+options.prefixClass+"\"><div class=\""+options.prefixClass+"-inner\"></div></div>").addClass(options.addClass).css({
					position:'absolute',
					zIndex:(zIndex++),
					marginTop:'-'+(cvsHeight+2)+'px',
					marginLeft:'-'+(Math.floor(cvsWidth/2))+'px'
				});
				var $tipContent = $tip.find("."+options.prefixClass+"-inner").width(cvsWidth).height(cvsHeight).css({position:'relative'});
				$tipContent.append($paper);
				var paper = Raphael($paper.get(0), cvsWidth, cvsHeight);
				if(options.externalStyles){
					paper.externalStyles=options.externalStyles;
				}
				var tip = paper.tooltip(dim.width, dim.height, options),blurtip=null;
				tip.translate(options.margin, options.margin);
				tip.attr(options.tipAttr);
				if(options.glow){
					tip.glow(options.glowAttr);
				}
				setTimeout(function(){paper.safari();});
				$tipContent.append($("<div class=\""+options.prefixClass+"-content\"/>").css({
					position:'absolute',
					zIndex:(zIndex++),
					top:(options.margin+options.padding)+'px',
					left:(options.margin+options.padding)+'px'
				}).append($(target).find("> *")));
				$("body").append($tip);
				trigger('Create', tipSelector, $tip, tip, paper);
				$(target).remove();
			}
			var offset=$(this).offset();
			$tip.css({
				top:(offset.top)+'px',
				left:(offset.left+(Math.floor($(this).width()/2)))+'px'
			}).show();
			trigger('Complete', tipSelector);
		}, function(){
			$(tipSelector).hide();
			trigger('Close', tipSelector);
		});

		var trigger = function(){
			var name='on'+arguments[0];
			if(options[name] && $.isFunction(options[name])){
				(options[name]).apply(element, arguments);
			}
		};

	};

	$.Svgtip.idGenerator = 1;

	$.Svgtip.defaults = {
		target:'',
		targetAttribute:'href',
		zIndex: 10,
		prefixClass: 'svg-tooltip',
		addClass: '',
		margin: 6,
		padding: 8,
		radius: 8,
		arrow: 18,
		glow: true,
		tipAttr: {
			fill:'90-#fefefe-#cdced0',
			stroke:'#a69db0',
			'stroke-width':1
		},
		glowAttr: {
			width:6,
			opacity:0.2,
			color:'#666'
		},
		externalStyles:null,
		onCreate:null,
		onComplete:null,
		onClose:null
	};

})(jQuery);
