$(function() {
	product_tooltips();
	tooltip_from_title();
});

function product_tooltips() {
	var $tooltips = $('div.tooltip_product');

	$tooltips.each(function() {
		var $tooltip = $(this);
		var $trigger = $tooltip.parent().removeAttr('title');

		$tooltip.appendTo('body');
		$trigger.hover(
			function(e) {
				$tooltip.css({
					position: 'absolute',
					top: e.pageY+2,
					left: e.pageX-182
				}).fadeIn('fast');
			},
			function() {
				$tooltip.fadeOut('fast');
			}
		).mousemove(function(e) {
			$tooltip.css({
				top: e.pageY+2,
				left: e.pageX-182
			});
		});
	});
};

function tooltip_from_title() {
	var $tooltip_candidates = $('.tt_from_title');
	var tooltip_cache = [];

	$tooltip_candidates.each(function() {
		var $this = $(this);
		var title = $this.attr('title'); $this.removeAttr('title');
		var cache_index = $.inArray(title, tooltip_cache);
		var $tooltip;

		if (cache_index == -1) {
			cache_index = tooltip_cache.push(title) - 1;
			title = title.split('|');
			var heading = title[0];
			var content = title[1] || '';
			$tooltip = $('<div class="tooltip_product from_title index_'+cache_index+'"><div class="top"></div><div class="wrap"><strong>'+heading+'</strong><p>'+content+'</p></div><div class="bottom"></div></div>').appendTo('body');
		} else {
			$tooltip = $('.tooltip_product.index_'+cache_index);
		}

		$this.hover(
			function(e) {
				$tooltip.css({
					position: 'absolute',
					top: e.pageY+2,
					left: e.pageX-126
				}).fadeIn('fast');
			},
			function() {
				$tooltip.fadeOut('fast');
			}
		).mousemove(function(e) {
			$tooltip.css({
				top: e.pageY+2,
				left: e.pageX-126
			});
		});
	});
}
