/*
 * jQuery Flexible Tooltip
 *
 * By Manuel Boy (http://www.polargold.de)
 * Copyright (c) 2010 polargold
*/
(function($){
	
	$.fn.flexibleTooltip = function(options) {
		
		// base settings
		var settings = {
			defaultStyle: '', // or: black, important
			defaultArrow: 'none', // or: right, left, top, bottom, leftbottom
			hideSpeed: 500,
			appearSpeed: 500
		};
		
		var log = function(msg) {
			if (typeof console == "object") {
				console.log(msg);
			}
		}
		
		return this.each(function() {

			// handle plugin settings
			if (options) { 
				$.extend(settings, options);
			}

			// ie is not capable of displaying correct fading
			if($.browser.msie) {
				settings.hideSpeed = 0;
				settings.appearSpeed = 0;
			}
			
			var item = $(this);
			
			var translateToArrowClass = function(id) {
				var arrowClass;
				switch (id) {
					case 'left':
						arrowClass = 'arrowleft';
						break;
					case 'right':
						arrowClass = 'arrowright';
						break;
					case 'top':
						arrowClass = 'arrowtop';
						break;
					case 'bottom':
						arrowClass = 'arrowbottom';
						break;
					case 'bottomleft':
						arrowClass = 'arrowbottomleft';
						break;
					case 'none':
					default:
						arrowClass = 'noarrow';
						break;
				}
				return arrowClass;
			}

			var translateToStyleClass = function(style) {
				var styleClass;
				switch (style) {
					case 'important':
						styleClass = 'important';
						break;
					case 'black':
						styleClass = 'blacktip';
						break;
					case 'default':
					default:
						styleClass = '';
						break;
				}
				return styleClass;
			}
			
			// initialize tooltip
			var style = $(item).attr('data-tooltip-style') || settings.defaultStyle;
			var styleClass = translateToStyleClass(style);
			var arrowId = $(item).attr('data-tooltip-arrow') || settings.defaultArrow;
			var arrowClass = translateToArrowClass(arrowId);
			var title = $(item).attr('data-tooltip-title');
			var content = $(item).attr('data-tooltip-content');
			var tooltipMarkup = '<div class="tooltip"><div class="tooltip-content"></div></div>';
			
			var tooltip = $(tooltipMarkup).hide().appendTo('body').addClass(arrowClass).addClass(styleClass);
			if (title !== undefined && title != '') {
				$('<h3>'+title+'</h3>').appendTo($('.tooltip-content', tooltip));
			}
			if (content !== undefined && content != '') {
				$('<p>'+content+'</p>').appendTo($('.tooltip-content', tooltip));
			}
			$(item).data('tooltip', tooltip);
			
			// tooltip event: set close button
			$(tooltip).bind('setCloseButton', function(event) {
				var tooltip = $(this);
				if($('.close', tooltip).length == 0) {
					$('<div class="close"><a href="#">Close</a></div>').appendTo($('.tooltip-content', tooltip));
					$('.close a', tooltip).bind('click', function(evt) {
						evt.preventDefault();
						$(tooltip).trigger('hide');
					});	
				}
			});

			// tooltip event: update title
			$(tooltip).bind('updateTitle', function(event, title) {
				var tooltip = $(this);
				if (title !== undefined && title != '') {
					$('.tooltip-content h3', tooltip).remove();
					$('<h3>'+title+'</h3>').prependTo($('.tooltip-content', tooltip));
				}
			});

			// tooltip event: update content
			$(tooltip).bind('updateContent', function(event, content) {
				var tooltip = $(this);
				if (content !== undefined && content != '') {
					$('.tooltip-content p', tooltip).remove();
					$('<p>'+content+'</p>').appendTo($('.tooltip-content', tooltip));
				}
			});
			
			// tooltip event: appear
			$(tooltip).bind('appear', function(event, speed) {
				if (speed == null) {
					speed = settings.appearSpeed;
				}
				// ie is not capable of displaying correct fading
				if($.browser.msie) {
					speed = 0;
				}
				$(this).fadeIn(speed);
			});
			
			// tooltip event: hide
			$(tooltip).bind('hide', function(event, speed) {
				if (speed == null) {
					speed = settings.hideSpeed;
				}
				// ie is not capable of displaying correct fading
				if($.browser.msie) {
					speed = 0;
				}
				$(this).fadeOut(speed);
			});
			
			// tooltip event: update position
			$(tooltip).bind('setPosition', function(event, position) {
				$(this).css({
					left: position.x,
					top: position.y
				});
			});

			// tooltip event: make collapsible
			$(tooltip).bind('makeCollapsible', function(event) {
				$('p', tooltip).hide();
				$(tooltip).css('cursor', 'pointer').hover(function() {
					if($('p:hidden', tooltip).length > 0) {
						$('p', tooltip).slideDown(500, function() {
						});
					}
				}, function() {
					$('p', tooltip).slideUp(500, function() {	
					});
				});
			});	
			
		});
	};
	
})(jQuery);
