(function($){
	$.fn.tipTip = function(options) {
		var defaults = {
			activation: "hover",
			keepAlive: false,
			edgeOffset: 3,
			fadeIn: 200,
			fadeOut: 200,
			delay: 400,
			attribute: "title",
			content: false, // HTML or String to fill TipTIp with
			enter: function(){},
			exit: function(){},
			maxWidth: "200px"
	  	};
	 	
	 	var opts = $.extend(defaults, options);
		
		this.each(function() {
			var item = this;
			var fileNamePat = /([^\/]+)(?=\.\w+$)/;
			var fileNamePat1 = /(.*)[\/\\]([^\/\\]+\.\w+)$/;
			var matches = item.src.match(fileNamePat);
			var elSubClass = matches[0] ? matches[0] : 'none';
			$(item).attr('elsubclass', elSubClass);
			// Setup tip tip elements and render them to the DOM
			if($(".tiptip_holder." + elSubClass).length <= 0){
				var tiptip_holder = $('<div class="tiptip_holder ' + elSubClass + '" style="max-width:' + $(item).attr('tip-width') + ';"></div>');
				var tiptip_content = $('<div class="tiptip_content ' + elSubClass + '"></div>');
				$(item).parent().append(tiptip_holder.html(tiptip_content));
			} else {
				var tiptip_holder = $(".tiptip_holder." + elSubClass);
				var tiptip_content = $(".tiptip_content." + elSubClass);
			}
		});
	 	
		return this.each(function(){
			var org_elem = $(this);
			
			var tiptip_holder = $(".tiptip_holder." + org_elem.attr('elsubclass'));
			var tiptip_content = $(".tiptip_content." + org_elem.attr('elsubclass'));
			
			if(opts.content){
				var org_title = opts.content;
			} else {
				var org_title = org_elem.attr(opts.attribute);
			}
			
			if(org_title != ""){
				if(!opts.content){
					org_elem.removeAttr(opts.attribute); //remove original Attribute
				}
				var timeout = false;
				
				function active_tiptip(){
					opts.enter.call(this);
					tiptip_content.html(org_title);
					
					var parentTop = org_elem.offset()['top'] - 148;
					var top = parentTop + parseInt(org_elem.attr('tip-top'));
					var left = parseInt(org_elem.attr('tip-left'));
					
					tiptip_holder.css({"left": left+"px", "top": top+"px"});
					
					if (timeout) { clearTimeout(timeout); }
					timeout = setTimeout(function(){ tiptip_holder.stop(true,true).fadeIn(opts.fadeIn); }, opts.delay);
				}
				
				active_tiptip();
				
				function deactive_tiptip(){
					opts.exit.call(this);
					if (timeout){ clearTimeout(timeout); }
					tiptip_holder.fadeOut(opts.fadeOut);
				}
			}
		});
	}
})(jQuery);
