/*
 * jQuery tool-source.js v0.5
 *
 * Copyright (c) 2009-10 Hans Chr. Reinl
 * Update: 01-01-2010
 * 
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 */
 
(function($){
	$.fn.toolsource = function(options) {
		
		var defaults = {
				action: 'move',
				color: '#444',
				rounded: false,
				opacity: 0.95
		  },
		  settings = $.extend({}, defaults, options);
		  
		  this.each(function() {
					var $this = $(this);
					var title = this.title;
					
						if ($this.is("a") && $this.attr("title") != "") {
							this.title = "";
						}
						$this.hover(function(e) {
							// mouseover
							// Moveable Tooltip
							if (settings.action == "move") {
								$('<div class="tooltip-source" />')
									.appendTo('body')
									.html(title)
									.hide()
									.css({
										color: settings.color,
										top: e.pageY + 20,
										left: e.pageX - (parseInt(settings.maxWidth ) / 2),
										opacity: settings.opacity
									}).fadeIn(300);
							}
							// Tooltip on top
							else if (settings.action == "set-top") {
								$this.find(".tooltip-source").hide().css({
										color: settings.color,
										marginTop: "-" + ($this.find(".tooltip-source").height() + 30) + "px",
										marginLeft: "-" + ($this.find(".tooltip-source").width() / 2 - $this.width() / 2 + 5) + "px",
										opacity: settings.opacity
									}).fadeIn(300);
							}
							// Tooltip on bottom
							else if (settings.action == "set-bottom") {
								$this.find(".tooltip-source").hide().css({
										color: settings.color,
										marginTop: ($this.height() + 13) + "px",
										marginLeft: "-" + ($this.find(".tooltip-source").width() / 2 - $this.width() / 2 + 5) + "px",
										opacity: settings.opacity
									}).fadeIn(300);
							}
								
							if (settings.rounded) { $(".tooltip-source").addClass("rounded"); }
							
							if (settings.action == "set-top") { $this.find(".tooltip-source").append('<div class="arrow-bottom"></div>'); }
							else if (settings.action == "set-bottom") { $this.find(".tooltip-source").append('<div class="arrow-top"></div>'); }
						},
						function() {
							// mouseout
							if (settings.action == "move") { $(".tooltip-source").remove(); }
							else if (settings.action == "set-top") {
								$(".tooltip-source").hide();
								$(".tooltip-source .arrow-bottom").hide().remove();
							}
							else if (settings.action == "set-bottom") {
								$(".tooltip-source").hide();
								$(".tooltip-source .arrow-top").hide().remove();
							}
						});	
					
					if (settings.action == "move") {
						$this.mousemove(function(e) {
							$(".tooltip-source").css({
									top: e.pageY + 20,
									left: e.pageX - (parseInt(settings.maxWidth) / 2)
								});
							});
					}
		  });
			
		  // returns the jQuery object to allow for chainability.
		  return this;
	}
})(jQuery);

