(function ($) {
// Implementation
$.fn.tooltip = function (options) {
var settings = $.extend({
content: '',
contentType: 'html', //html,text,page
destroy: false
}, options || {});
var hideDelay = 10;
var currentID;
var hideTimer = null;
var forAttr = this.attr('for') === undefined ? $(this).attr('id') : this.attr('for');
if ((settings.destroy == true) && ($('div.PopupContainer[for=' + forAttr + ']').length > 0)) {
$('div.PopupContainer[for=' + forAttr + ']').remove();
return;
}
var container = $('<div class="PopupContainer" style="text-align:center" for=' + forAttr + '>'
+ '<table border="0" cellspacing="0" cellpadding="0" align="center" class="Popup" for=' + forAttr + '>'
+ '<tr>'
+ ' <td class="corner topLeft"></td>'
+ ' <td class="top"></td>'
+ ' <td class="corner topRight"></td>'
+ '</tr>'
+ '<tr>'
+ ' <td class="left"> </td>'
+ ' <td><div class="PopupContent" for=' + forAttr + '></div></td>'
+ ' <td class="right"> </td>'
+ '</tr>'
+ '<tr>'
+ ' <td class="corner bottomLeft"> </td>'
+ ' <td class="bottom"> </td>'
+ ' <td class="corner bottomRight"></td>'
+ '</tr>'
+ '</table>'
+ '</div>');
$('body').append(container);
$(this).bind('mouseover', function () {
if (hideTimer)
clearTimeout(hideTimer);
var pos = $(this).offset();
var width = $(this).width();
var pageUrl = settings.content;
var elementHeight = $(this).height()
var containerTop = (pos.top + elementHeight) + 'px';
var containerLeft;
$('.PopupContent[for = ' + forAttr + ']').html(' ');
$('.PopupContent[for = ' + forAttr + ']').css('width', '190px');
$('.PopupContent[for = ' + forAttr + ']').css('text-align', 'center');
switch (settings.contentType) {
case 'page':
$('.PopupContent[for = ' + forAttr + ']').css('text-align', 'center');
$('.PopupContent[for = ' + forAttr + ']').css('padding', '0px');
if (!$('.PopupContent[for = ' + forAttr + ']').hasClass("LoadingImage"))
$('.PopupContent[for = ' + forAttr + ']').addClass("LoadingImage");
if (pos.left + width < Math.round($(document).width() * 0.55)) {
container.css
({
left: (Math.round(pos.left)) + 'px',
top: containerTop
});
}
else {
container.css
({
left: (pos.left + width - container.width()) + 'px',
top: containerTop
});
}
container.show();
var containerCoordinates = container.position().left + container.width();
hideTimer = setTimeout(function () {
$.ajax({
type: "POST",
url: pageUrl,
contentType: "text/html",
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
if ($('.PopupContent[for = ' + forAttr + ']').hasClass("LoadingImage"))
$('.PopupContent[for = ' + forAttr + ']').removeClass("LoadingImage");
},
success: function (result) {
if ($('.PopupContent[for = ' + forAttr + ']').hasClass("LoadingImage"))
$('.PopupContent[for = ' + forAttr + ']').removeClass("LoadingImage");
$('.PopupContent[for = ' + forAttr + ']').html(result);
$('.PopupContent[for = ' + forAttr + ']').css('width', 'auto');
$('.PopupContent[for = ' + forAttr + ']').css('display', 'block');
$('.PopupContent[for = ' + forAttr + ']').css('padding', '20px');
$('.PopupContent[for = ' + forAttr + ']').css('text-align', 'left');
if (pos.left + width < Math.round($(document).width() * 0.55))
containerLeft = (pos.left + Math.round(width / 2)) + 'px';
else
containerLeft = (Math.round(containerCoordinates - container.width()) - Math.round(width/2)) + 'px';
$('table[for = ' + forAttr + ']').width($('.PopupContent[for = ' + forAttr + ']').width + 50);
container.css
({
left: containerLeft,
top: containerTop,
width: ($('.PopupContent[for = ' + forAttr + ']').width + 50) + 'px'
});
}
});
}, 1000);
break;
case 'html':
$('.PopupContent[for = ' + forAttr + ']').html(settings.content);
container.css({
left: (pos.left + width) + 'px',
top: pos.top - 5 + 'px'
});
container.css('display', 'block');
break;
default:
$('.PopupContent[for = ' + forAttr + ']').text(settings.content);
//12 pixel is for the image paddingfor errors
container.css({
left: (pos.left + width + 12) + 'px',
top: pos.top - 5 + 'px'
});
container.css('display', 'block');
}
});
$(this).bind('mouseout', function () {
if (hideTimer)
clearTimeout(hideTimer);
hideTimer = setTimeout(function () {
container.css('display', 'none');
}, 1);
});
// Allow mouse over of details without hiding details
$('div.PopupContainer[for=' + forAttr + ']').mouseover(function () {
if (hideTimer)
clearTimeout(hideTimer);
if (settings.contentType == 'page' && $('.PopupContent[for = ' + forAttr + ']').hasClass("LoadingImage")) {
hideTimer = setTimeout(function () {
$('.PopupContent[for = ' + forAttr + ']').removeClass("LoadingImage");
container.css('display', 'none');
}, hideDelay);
}
});
// Hide after mouseout
$('div.PopupContainer[for=' + forAttr + ']').mouseout(function () {
if (hideTimer)
clearTimeout(hideTimer);
hideTimer = setTimeout(function () {
container.css('display', 'none');
}, hideDelay);
});
}
})(jQuery);