/**

Documentation can be found at http://www.dudeami.com/tooltips/

Licensing:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
	
 */

jQuery.fn.addTooltip = function(options) {
	settings = jQuery.extend({
		text: "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aliquam tincidunt mauris eu risus.",
		width: 200,
		side: 'top',
		imgdir: './images',
		removeOn: null,
		useArrow: true
	}, options);
	
	if ($(this).length == 0 && settings.position == null) return;
	
	internal = {
		pointPosition: {x: 0, y: 0}
	};

	// Create a new element
	var uid = "cmt" + String((new Date()).getTime()).replace(/\D/gi,'').substr(0,6);
	var content = $(
			'<table id="' + uid + '" cellpadding="0" cellspacing="0" border="0" class="jq-comments">' +
			'<tr>' +
			'<td style="background: repeat-x bottom left url(' + settings.imgdir + '/comment-multi-cornerul.png);" class="jq-row1 jq-row-image1">&nbsp;</td>' +
			'<td style="width: ' + settings.width + 'px; background: repeat-x bottom left url(' + settings.imgdir + '/comment-multi-top.png);" class="jq-row2 jq-row-image1">&nbsp;</td>' +
			'<td style="background: repeat-x bottom left url(' + settings.imgdir + '/comment-multi-cornerur.png);" class="jq-row3 jq-row-image1">&nbsp;</td>' +
			'</tr>' +
			'<tr>' +
			'<td class="jq-row1 jq-row-woimage"></td>' +
			'<td style="width: ' + settings.width + 'px;" class="jq-row2 jq-row-woimage">' + settings.text + '</td>' +
			'<td class="jq-row2 jq-row-image" style="background: url(' + settings.imgdir + '/comment-multi-right.png);"></td>' +
			'</tr>' +
			'<tr>' +
			'<td style="background: repeat-x top left url(' + settings.imgdir + '/comment-multi-cornerbl.png);" class="jq-row1 jq-row-image2">&nbsp;</td>' +
			'<td class="jq-row2 jq-row-image2" style="height: 11px; overflow: hidden; width: ' + settings.width + 'px; background: repeat-x top left url(' + settings.imgdir + '/comment-multi-bottom.png);">&nbsp;</td>' +
			'<td style="background: repeat-x top left url(' + settings.imgdir + '/comment-multi-cornerbr.png);" class="jq-row3 jq-row-image2">&nbsp;</td>' +
			'</tr>' +
			'</table>'
	);
	// Due to some settings, we need to add the comment into the page ASAP
	// We will attempt to hide it by putting a top: -500px on it.
	$(content).css('position', 'absolute');
	$(content).css('top', -500);
	$('body').append($(content));
	if (settings.useArrow) {
		var pointer = $('<img id="' + uid + 'i" src="#" alt="" />');
		pointer.css('position', 'absolute');
		$('body').append($(pointer));
	}
	internal.position = {x: 0, y: 0};
	// Align based on the element!
	switch (settings.side) {
		case 'exact':
			internal.position.x = $(this).offset().left;
			internal.position.y = $(this).offset().top;
			break;
		case 'top':
			internal.position.x = Math.floor($(this).offset().left + (($(this).outerWidth()-$(content).outerWidth())/2) );
			internal.position.y = Math.floor($(this).offset().top - $(content).outerHeight() -(!settings.useArrow ? 0 : 6));
			if (settings.useArrow) {
				$(pointer).attr('src', settings.imgdir + '/comment-multi-pointd.png');
				$(pointer).css('top', Math.floor($(this).offset().top - 15));
				$(pointer).css('left', Math.floor($(this).offset().left + ($(this).outerWidth()/2) - 15));
			}
			break;
		case 'right':
			internal.position.y = Math.floor($(this).offset().top + (($(this).outerHeight()-$(content).outerHeight())/2) + (!settings.useArrow ? 0 : 6));
			internal.position.x = Math.floor($(this).offset().left + $(this).outerWidth() + (!settings.useArrow ? 0 : 11));
			if (settings.useArrow) {
				$(pointer).attr('src', settings.imgdir + '/comment-multi-pointl.png');
				$(pointer).css('left', Math.floor($(this).offset().left + $(this).outerWidth()));
				$(pointer).css('top', Math.floor($(this).offset().top + ($(this).outerHeight()/2) - 7));
			}
			break;
		case 'left':
			internal.position.y = Math.floor($(this).offset().top + (($(this).outerHeight()-$(content).outerHeight())/2) + (!settings.useArrow ? 0 : 6));
			internal.position.x = Math.floor($(this).offset().left - content.width() - (!settings.useArrow ? 0 : 11));
			if (settings.useArrow) {
				$(pointer).attr('src', settings.imgdir + '/comment-multi-pointr.png');
				$(pointer).css('left', Math.floor($(this).offset().left - 14));
				$(pointer).css('top', Math.floor($(this).offset().top + ($(this).outerHeight()/2) - 7));
			}
			break;
		case 'bottom':
		default:
			internal.position.x = Math.floor($(this).offset().left + (($(this).outerWidth()-$(content).outerWidth())/2));
			internal.position.y = Math.floor($(this).offset().top + $(this).outerHeight() + (!settings.useArrow ? 0 : 11));
			if (settings.useArrow) {
				$(pointer).attr('src', settings.imgdir + '/comment-multi-pointu.png');
				$(pointer).css('top', Math.floor($(this).offset().top + $(this).outerHeight()));
				$(pointer).css('left', Math.floor($(this).offset().left + ($(this).outerWidth()/2) - 10));
			}
			break;
	}
	if (settings.removeOn != null && settings.removeOn.toString().match(/^[0-9]+$/)) {
		setTimeout(function () {
			if (!$(content).next().hasClass('jq-comments')) $(content).next().remove();
			$(content).remove();
		}, settings.removeOn);
	} else if (settings.removeOn != null) {
		$(this).bind(settings.removeOn, function (e) {
			if (!$(content).next().hasClass('jq-comments')) $(content).next().remove();
			$(content).remove();
		});
	}
	if (internal.position.x < 0) {
		internal.position.x = 0;
	} else if (internal.position.x > $(document).width() - content.width()) {
		internal.position.x = $(document).width() - content.width();
	}
	$(content).css('top', internal.position.y);
	$(content).css('left', internal.position.x);
}

jQuery.addTooltip = function (options) {
	try {
		var newDiv = $('<div style="position: absolute; left: ' + options.position.x + 'px; top: ' + options.position.y + 'px;"></div>');
		$('body').append(newDiv);
		newDiv.addTooltip(jQuery.extend(options, { position: null, side: 'exact', useArrow: false }));
	} catch (e) { }
}
