/**
 * @requires js/libs/jQuery/plugins/jquery.tooltip.js
 */

if (typeof(m) == "undefined" || !m) {
	var m = {};
}

(function($) {
	var cache = {};

	m.productsList = {
		setup : function() {
			var self = this;

			$('.good-150 .image-wrapper').tooltip({
				track : true,
				delay : 0,
				showURL : false,
				bodyHandler : function() {
					return self.getTooltipContent(this.parentNode.id);
				}
			});
		},

		getTooltipContent : function(htmlId) {
			$('#tooltip').attr('title', htmlId);

			if (typeof(cache[htmlId]) != 'undefined') {
				return cache[htmlId];
			} else {
				var idPrefix = 'product-id-';
				var id = htmlId.substr(idPrefix.length);
				$.post('/catalog/tooltip/', {id:id}, function(data) {
					cache[htmlId] = data.html;

					if ($('#tooltip').attr('title') == htmlId) {
						$('#tooltip .body').html(data.html);
					}
				}, 'json');

				return 'Загрузка...';
			}
		}
	}
}) (jQuery);
