;(function($) {

	
	var helper = {},
		current,
		globalSettings,
		title,
		tID,
		IE = $.browser.msie && /MSIE\s(5\.5|6\.|7\.|8\.)/.test(navigator.userAgent),
		IE6 = $.browser.msie && /MSIE\s(6.)/.test(navigator.userAgent);
		hideDelayTimer = null;
	
	$.tooltip = {
		defaults: {
			delay: 250,
			fade: 200,
			hideDelay: 500,
			top: 5,
			left: 0,
			id: "tooltip"
		}
	};
	
	$.fn.extend({
		tooltip: function(settings) {
			globalSettings = $.extend({}, $.tooltip.defaults, settings);
			createHelper(globalSettings);
			return this.each(function() {
					this.tOpacity = helper.parent.css("opacity");
					this.tooltipText = this.title;
				
					$(this).removeAttr("title");
					this.alt = "";
				})
				.mouseover(save)
				.mouseout(hideDelayed)
				.click(hide);
		},
		fixPNG: IE ? function() {

			return $('td, div', this).each(function () {
		
				var image = $(this).css('backgroundImage');
				
				if (image.match(/^url\(["']?(.*\.png)["']?\)$/i)) {
					image = RegExp.$1;
					$(this).css({
						'backgroundImage': 'none',
						'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale, src='" + image + "')"
					}).each(function () {
						var position = $(this).css('position');
						if (position != 'absolute' && position != 'relative')
							$(this).css('position', 'relative');
					});
				}
			});
		} : function() { return this; },
		unfixPNG: IE ? function() {
			
			return this.each(function () {
				$('td, div', this).css({'filter': '', backgroundImage: ''});
			});
		} : function() { return this; }
	});
	
	function createHelper(settings) {
		try {
		if( helper.parent )
			return;

		helper.parent = $(
			'<div id="' + settings.id + '" class="popup"><table cellspacing="0" cellpadding="0"><tbody>' +
			'<tr><td id="topleft" class="corner"></td>' +
				'<td class="top"><div id="sp1" class="top_pstryk"></div></td>' +
			'<td id="topright" class="corner"></td></tr>' + 
				'<tr><td class="left"></td>' +
					'<td id="popup-content"></td>' +
				'<td class="right"></td></tr>' + 
			'<tr><td class="corner" id="bottomleft"></td>' +
				'<td class="bottom"><div id="sp2" class="bottom_pstryk"></div></td>' +
			'<td id="bottomright" class="corner"></td></tr>' +
			'</tbody></table></div>')
			.appendTo(document.body)
			.hide();
		
		if ( $.fn.bgiframe )
			helper.parent.bgiframe();
		
		
		helper.body = $('#popup-content', helper.parent);
		
		helper.sp1 = $('#sp1', helper.parent);
		helper.sp2 = $('#sp2', helper.parent);
		
		helper.parent.mouseover(function (event) { if (hideDelayTimer != null) clearTimeout(hideDelayTimer); });
		helper.parent.mouseout(hideDelayed);
		
		} catch (e) {}
	}

	function handle(event) {

		if( globalSettings.delay )
			tID = setTimeout(show, globalSettings.delay);
		else
			show();
		
		//update(event);
	}
	
	function save() {
		clearTimeout(hideDelayTimer);
		
		//if (current != null && current != this)
			//setTimeout(save, 100);

		current = this;
		title = this.tooltipText;
		
		helper.body.empty();
		helper.body.append(title);
		update();
		
		helper.parent.fixPNG();
		
		handle.apply(this, arguments);
	}
	
	function show() {
		tID = null;
		if (hideDelayTimer != null)
			clearTimeout(hideDelayTimer);
		update();
		if ((!IE || !$.fn.bgiframe) && globalSettings.fade) {
			if (helper.parent.is(":animated"))
				helper.parent.stop().show().fadeTo(globalSettings.fade, current.tOpacity);
			else
				helper.parent.is(':visible') ? helper.parent.fadeTo(globalSettings.fade, current.tOpacity) : helper.parent.fadeIn(globalSettings.fade);
		} else {
			helper.parent.show();
		}
		update();
	}
	
	function update(event)	{
		var left = helper.parent[0].offsetLeft;
		var top = helper.parent[0].offsetTop;
 		var cur = $(current);
		var v = viewport(),
		h = helper.parent[0];
		
		left = cur.offset().left;
		
		left += cur.width()/2;
		
		left -= helper.parent.width()/2;
		
		left -= globalSettings.left;
		
		
		if (v.y > cur.offset().top - helper.parent.height()) {
			top = cur.offset().top + cur.height() - globalSettings.top;
			helper.sp1.show();
			helper.sp2.hide();
		}
		else {
			top = cur.offset().top - helper.parent.height() + globalSettings.top;
			helper.sp2.show();
			helper.sp1.hide();
		}

		var right='auto';
		if (globalSettings.positionLeft) {
			right = $(window).width() - left;
			left = 'auto';
		}
		
		helper.parent.css({
			left: left,
			right: right,
			top: top
		});
		
		if (v.x + v.cx < h.offsetLeft + h.offsetWidth) {
			left -= h.offsetWidth + 20 + globalSettings.left;
			helper.parent.css({left: left + 'px'});
		}
	}
	
	function viewport() {
		return {
			x: $(window).scrollLeft(),
			y: $(window).scrollTop(),
			cx: $(window).width(),
			cy: $(window).height()
		};
	}

	function hideDelayed(event) {
	
		if (current) {
			if (tID) clearTimeout(tID);
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			
			hideDelayTimer = setTimeout(hide, globalSettings.hideDelay);
		}

	}
	
	function hide(event) {
			
		if (hideDelayTimer)
			clearTimeout(hideDelayTimer);

		if(tID)
			clearTimeout(tID);
		
		
		function complete() {
			helper.parent.hide().css("opacity", "");
			current = null;
		}
		
		if ((!IE || !$.fn.bgiframe) && globalSettings.fade) {
			if (IE) {
				complete();
			}
			else if (helper.parent.is(':animated')) {
				helper.parent.stop().fadeTo(globalSettings.fade, 0, complete);
			}
			else
				helper.parent.stop().fadeOut(globalSettings.fade, complete);
		} else
			complete();
			
		helper.parent.unfixPNG();
	}
})(jQuery);

