/* Copyright (c) 2008 Kean Loong Tan http://www.gimiti.com/kltan
 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * Copyright notice and license must remain intact for legal use
 * jHelpertip
 * Version: 1.0 (Jun 2, 2008)
 * Requires: jQuery 1.2+
 */

(function($) {
		  
	$.fn.ajax_tooltip = function(options) {
		// merge users option with default options
		var opts = $.extend({}, $.fn.ajax_tooltip.defaults, options);
		
		// default actions
		// create a container is not found
		if ($(opts.container).length == 0)
			$('<div id="'+opts.container.slice(1)+'"></div>').appendTo("body");
		
		// initialize our tooltip and our data container and also the close box
/*		$(opts.container).css({
			position: "absolute",
			display: "inline"
		}).hide();
*/
		// the sources of getting data
		var get_data = function(obj,e){
			get_position(obj, e);
			$(opts.container).html('<table class="Box" cellpadding="0" cellspacing="0" width="100%"><tr><td class="BoxTopLeft"></td><td class="BoxTop"></td><td class="BoxTopRight"></td></tr><tr><td class="BoxLeft"></td><td class="BoxContent"><div style="text-align:center;"><img src="'+opts.loading_image+'"/><br>'+opts.loading_text+'</div></td><td class="BoxRight"></td></tr><tr><td class="BoxBottomLeft"></td><td class="BoxBottom"></td><td class="BoxBottomRight"></td></tr></table><div class="BoxArrow"></div>').show();
			
			var url = (opts.url ? '/help/invest/invest_' + $(obj).attr('id') + '.html?isNaked=1' : '/index/vocabulary/vocabulary_' + $(obj).attr('id') + '.html?isNaked=1&direction=' + opts.direction);
			
			$.ajax({
				type: opts.type,
				url: url,
				data: opts.data,
				success: function(msg){
					$(opts.container).attr({
						openedid: $(obj).attr('id')
					}).html(msg);
				}
			});
		};
		
		
		// used to position the tooltip
		var get_position = function (obj, e){
			opts.direction == 'left' ? $(opts.container).removeClass('BoxArrowRight').addClass('BoxArrowLeft') : $(opts.container).removeClass('BoxArrowLeft').addClass('BoxArrowRight');
			$(opts.container).css({
				display: "inline",
				top: $(obj).offset().top + opts.top_offset,
				left: $(obj).offset().left + ((opts.direction == 'left') ? $(obj).attr("offsetWidth") : 0) + opts.left_offset
			}).show();
		};

		// just close tool tip when not needed usually trigger by anything outside out tooltip target
		$(this).bind("click", function(e){
			$(this).css({
				"text-decoration": "none",
				color: "#666"
			})
			if ($(this).attr('id') == $(opts.container).attr('openedid') && $(opts.container).css('display') == 'inline') {
				$(opts.container).hide();
			} else {
				get_data(this, e);
				get_position(this, e);
				$(document).bind("click", function(e){
					$(opts.container).hide().empty();
				});
			}
			return false;
		});

	};
	
	$.fn.ajax_tooltip.defaults = {
		direction: "left",
		top_offset: 10,
		left_offset: 30,
		container: "#BoxContainer", /* tooltip Container*/
		loading_image: "/i/ajax_loader.gif",
		loading_text: "Загрузка...",
		type: "GET", /* data can be inline or CSS selector */
		url: ''
		//data: '',
	};
		
	
		  

})(jQuery);