/*!
* jquery.tinyTip. jQuery tiny tooltip plugin
*
* Copyright (c) 2009 Craig Thompson
* http://craigsworks.com
*
* Licensed under MIT
* http://www.opensource.org/licenses/mit-license.php
*/

"use strict"; // Enable ECMAScript "strict" operation for this function. See more: http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/
/*jslint browser: true, onevar: true, undef: true, nomen: true, bitwise: true, regexp: true, newcap: true, immed: true, strict: true */
/*global window: false, jQuery: false */
(function($) {
	function TinyTip(target, options) {
		var self = this,
			timer = false;

		$.extend(this, {
			'show': function() { $.fn.tinyTip.show(target, options); },
			'hide': function() { $.fn.tinyTip.hide(options); },
			'destroy': function() {
				target.attr('title', target.attr('oldtitle'))
				target.removeAttr('oldtitle')
					.unbind('mouseenter.tinytip mouseleave.tinytip')
					.removeData('TinyTip');

				$.fn.tinyTip.hide(options);
			}
		});

		if(options.content === false) {
			options.content = target.attr('title');
			target.attr('oldtitle', options.content);
			target.removeAttr('title');
		}

		target.bind('mouseenter.tinytip mouseleave.tinytip', function(e) {
			clearTimeout(timer);
			timer = setTimeout(function() {
				self[ e.type === 'mouseenter' ? 'show' : 'hide' ]();
			},
			options['delay' + (e.type === 'mouseenter' ? 'In' : 'Out')]);
		});
	}
	
	// Create tinyTip element
	$('<div />', { 'id': 'tinytip', 'class': 'tinytip' }).appendTo(document.body);

	// jQuery implementation
	$.fn.tinyTip = function(opts) {
		return $(this).each(function() {
			var api = $(this).data('TinyTip');

			if('string' === typeof opts) {
				if(api && 'object' === typeof api && api[opts]) {
					api[opts]();
				}
			}
			else if('undefined' === typeof api && !api) {
				var options = $.extend({}, $.fn.tinyTip.defaults, opts),
					obj = new TinyTip($(this), options);

				$(this).data('TinyTip', obj);
			}
		});
	};
	
	$.fn.tinyTip.show = function(elem, options) {
		if(!elem || !options) { return false; }

		var content = options.content,
			side = options.side,
			size = options.size,
			offset = options.offset,
			pos = elem.offset(),
			tinytip = $('#tinytip').html(content),
			tip = $('<div />', { 'class': 'tinytip-tip', 'height': 0, 'width': 0 }),
			border = '' + size + 'px solid ',
			borderDash = border.replace('solid', 'dashed'),
			color = tinytip.css('background-color'),
 
		// Determine properties to set based on chosen position
		toSet = (side === 'top' || side === 'bottom') ?
			['top', 'bottom', 'left', 'right', 'outerWidth', 'outerHeight', offset[0], offset[1]] :
			['left', 'right', 'top', 'bottom', 'outerHeight', 'outerWidth', offset[1], offset[0]];

		// Set tooltip position
		tinytip.css(toSet[2], pos[ toSet[2] ] + (elem[ toSet[4] ]() / 2) - (tinytip[ toSet[4] ]() / 2) + toSet[6])
			.css(toSet[0], pos[ toSet[0] ] + (side === toSet[1] ? elem[ toSet[5] ]() + size : -tinytip[ toSet[5] ]() - size) + toSet[7]);

		// Draw and position tip
		tip.css({ 'position': 'absolute', 'border': 0, 'line-height': 0, 'font-size': 0 })
			.css(toSet[2], (tinytip[ toSet[4] ]() / 2) -size)
			.css(side === toSet[0] ? toSet[1] : toSet[0], -size)
			.css('border-' + toSet[2], borderDash + 'transparent')
			.css('border-' + toSet[3], borderDash + 'transparent')
			.css('border-' + side, border + color)
			.prependTo(tinytip);

		// Fade the tooltip in
		tinytip.stop(1,0).fadeTo(options.duration, options.opacity, function() {
			try{ $(this).style.removeAttribute('filter');  }
			catch(e) { }
			finally{ $(this).css('opacity', ''); }
		});
	}

	$.fn.tinyTip.hide = function(options) {
		// Fade out tooltip
		$('#tinytip').stop(1,0).fadeTo(options.duration, 0, function() {
			$(this).empty().hide();
		});
	}
	
	// tinyTip defaults
	$.fn.tinyTip.defaults = {
		'content': false,
		'side': 'bottom',
		'size': 6,
		'delayIn': 150,
		'delayOut': 0,
		'opacity': 0.9,
		'duration': 50,
		'offset': [0, 0]
	}
}(jQuery));
