(function($){
	
	$.fn.ctips = function(options) {
		
		var defaults = {
			click: false,
			speed: 200,
			easing: 'linear',
			position: 'top',
			delay: 200
		},
		settings = $.extend({}, defaults, options);
		
		this.each(function() {
			
			var $this = $(this);
			var pos = null;
			var content = $this.attr('title').split('\\n').join('<br/>');
			var tipWidth = 0;
			var tipHeight = 0;

			function over()
			{
				pos = $.extend({}, $this.offset(), {width: $this.outerWidth(), height: $this.outerHeight()});
				
				if($this.tip){ out(); return false; }
				
				$this.attr('title', '').attr('alt', '');
				$this.tip = $('<div class="ctip '+settings.position+'"><div>'+content+'</div></div>');
				
				if(settings.click)
				{
					var close_btn = $('<a href="javascript:void(0);" class="close">x</a>').css('float', 'right');
					close_btn.prependTo($this.tip);
					close_btn.click(function(){
						out();
						return false;
					})
				}
				
				$this.tip.css({
					position: 'absolute', 'z-index': 1000000, opacity: 0
				}).prependTo("body");
				
				tipWidth = $this.tip.outerWidth();
				tipHeight = $this.tip.outerHeight();
				
				switch (settings.position) {
	                case 'top':
	                   $this.tip.css({ top: pos.top - (tipHeight-5), left: pos.left + (pos.width/2) - (tipWidth/2) }).animate({
							top: pos.top - tipHeight, opacity: 1
						}, settings.speed, settings.easing);
	                    break;
	                case 'right':
	                	$this.tip.css({ top: pos.top + (pos.height/2) - (tipHeight/2), left: pos.left + pos.width }).animate({
							left: pos.left + (pos.width+5), opacity: 1
						}, settings.speed, settings.easing);
	                    break;
	                case 'bottom':
	                    $this.tip.css({ top: pos.top + (pos.height), left: pos.left + (pos.width/2) - (tipWidth/2) }).animate({
							top: pos.top + (pos.height+5), opacity: 1
						}, settings.speed, settings.easing);
	                    break;
	                case 'left':
	                    $this.tip.css({ top: pos.top + (pos.height/2) - (tipHeight/2), left: pos.left - (tipWidth) }).animate({
							left: pos.left - (tipWidth+5), opacity: 1
						}, settings.speed, settings.easing);
	                    break;
	            }
			}
			
			function out()
			{
				$this.attr('title', content.split('<br/>').join('\\n')).attr('alt', content.split('<br/>').join('\\n'));
				switch (settings.position) {
	                case 'top':
	                    $this.tip.animate({
							top: pos.top-(tipHeight+15), opacity: 0
						}, settings.speed, settings.easing, function(){$this.tip.remove();$this.tip = null;});
	                    break;
	                case 'right':
	                    $this.tip.animate({
							left: pos.left+(pos.width+15), opacity: 0
						}, settings.speed, settings.easing, function(){$this.tip.remove();$this.tip = null;});
	                    break;
	                case 'bottom':
	                	$this.tip.animate({
							top: pos.top+(tipHeight+15), opacity: 0
						}, settings.speed, settings.easing, function(){$this.tip.remove();$this.tip = null;});
	                    break;
	                case 'left':
	                    $this.tip.animate({
							left: pos.left-(tipWidth+15), opacity: 0
						}, settings.speed, settings.easing, function(){$this.tip.remove();$this.tip = null;});
	                    break;
	            }
			}
				
			if(!settings.click)
			{
				hiConfig = {
			        sensitivity: 1,
			        interval: settings.delay,
			        timeout: settings.delay,
			        over: over,
			        out: out
			    }
			    
			    $this.hoverIntent(hiConfig);
			}else{
				$this.click(function(){
					over();
				})
			}
		
		});
		
	}
	
})(jQuery);

