;(function($) {
	var helper ={};
	
	var shelper = {};
	
	var data = {}, top, left, current, clin;
	
	$.ttip = {
		defaults: {
			position:	'bottom-left',
			id:			'tooltip',
			tclass:		'',
			clin:		'mouse'
		}
	};
	
	$.fn.extend({
		ttip: function(settings) {
			settings = $.extend({}, $.ttip.defaults, settings);
			createHelper(settings);
			return this.each(function() {
				$(this).removeAttr('title');
				if ($(this).attr('ttip')) {
					settings.position = $(this).attr('ttip');
				}
				if ($(this).attr('ttip_clin')) {
					settings.clin = $(this).attr('ttip_clin');
				} else {
					settings.clin = 'mouse';
				}
				if (/from_[A-Za-z0-9_]+/.test($(this).attr('id'))) {
					settings.type = 'small';
					settings.title = $('#' + $(this).attr('id').replace('from_', '')).html();
				} else {
					settings.type = 'normal';
					settings.title = '';
				}
				$(this).attr('ttip_position', settings.position);
				$(this).attr('ttip_type', settings.type);
				$(this).attr('ttip_title', settings.title);
				$(this).attr('ttip_class', settings.tclass);
				$(this).attr('ttip_clin', settings.clin);
				this.alt = '';
			}).hover(show, hide).click(hide);
		},
		url: function() {
			var src = this.attr('href') || this.attr('src');
			return src.replace('/catalog', '/blocks/catalog');
		},
		get_position: function() {
			return this.attr('ttip_position');
		},
		type: function() {
			return this.attr('ttip_type');
		},
		text: function() {
			return this.attr('ttip_title');
		},
		get_class: function() {
			return this.attr('ttip_class');
		},
		clin: function() {
			return this.attr('ttip_clin');
		}
	});
	
	function createHelper(settings) {
		if (helper.parent || shelper.parent) return;
		helper.parent = $('<div id="' + settings.id + '"><div class="header"></div><div class="body"><div class="shadow"><div class="light"><h3></h3><div class="image-header"></div><div class="image"></div><div class="image-footer"></div><div class="description"></div></div></div></div><div class="footer"></div></div>')
			.appendTo(document.body)
			.hide();
		helper.title = $('h3', helper.parent);
		helper.image = $('div.image', helper.parent);
		helper.description = $('div.description', helper.parent);
		shelper.parent = $('<div id="' + settings.id + '-small"><h3></h3><div class="description"></div></div>')
			.appendTo(document.body)
			.hide();
		shelper.title = $('h3', shelper.parent);
		shelper.description = $('div.description', shelper.parent);
	};
	
	function settings(element) {
		return $.data(element, "ttip-settings");
	};
	
	function handle(event) {
		show();
		update(event);
	};
	
	function show(event) {
		if ($(this).clin() == 'mouse') {
			position = $(this).get_position();
			switch(position) {
				case 'bottom-left':
				case 'left-bottom':
					top = 15;
					left = 15;
					break;
				case 'bottom-right':
				case 'right-bottom':
					top = 15;
					left = -350;
					break;
				default:
					top = 0;
					left = 0;
			}
		} else {
			var clinEl = $('#' + $(this).clin());
			var offset = clinEl.offset();
			top = offset.top + clinEl.height();
			left = offset.left;
		}
		clin = $(this).clin();
		
		if ($(this).get_class() != '') {
			$(this).addClass($(this).get_class());
		}
		
		current = this.id;
		switch($(this).type()) {
			case 'normal':
				if (!data[current]) {
					helper.parent.addClass('empty');
					helper.description.html('');
					data[current] = 'empty';
					$.getJSON($(this).url(), {id: current}, onLoad);
				}
				helper.parent.show();
				break;
			case 'small':
				shelper.description.html($(this).text());
				if (clin != 'mouse') {
					shelper.parent.appendTo(this);
				}
				shelper.parent.show();
				break;
		}
		update(event);
		$(document.body).bind('mousemove', update);
	};
	
	function onLoad(json) {
		data[json.id] = json;
		update();
		$(document.body).bind('mousemove', update);
	};
	
	function update(event) {
		if (event) {
			_left = left;
			_top = top;
			if (clin == 'mouse') {
				_left += event.pageX;
				_top += event.pageY;
			}
			if (helper.parent.css('display') == 'block') {
				helper.parent.css({
					left:	_left + 'px',
					top:	_top + 'px'
				});
			} else {
				shelper.parent.css({
					left:	_left + 'px',
					top:	_top + 'px'
				});
			}
		}
		if (helper.parent.css('display') == 'block' && data[current] && data[current] != 'empty') {
			helper.parent.removeClass('empty');
			helper.title.html(data[current].title);
			helper.description.html(data[current].description);
			helper.image.html('<img src="' + data[current].image + '" alt="' + data[current].title + '" />');
		}
	};
	
	function hide(event) {
		helper.parent.hide();
		shelper.parent.hide();
//		helper.parent.fadeOut('fast');
		$(document.body).unbind('mousemove', update)
	};
})(jQuery);
