/*
 * Tooltip script
 * powered by jQuery (http://www.jquery.com)
 *
 * written by TJS (http://www.tjs.co.uk/)
 *
 */
(function($){
	$.fn.chapterNoteToolTip = function(options) {

		var defaults = {
			xOffset: 10,
			yOffset: 20,
			interval: 0,
			timer: '',
			idName: 'glossaryTerm'
		};
		var options = $.extend(defaults, options);

		function displayToolip(el, e){
			var note = el.data('note');

			if(note!=''){
				$("#"+options.idName).remove();
				$("body").append(
					"<div id='"+options.idName+"'>"
						+"<div class='left'>&nbsp;</div>"
						+"<div class='right'>"
							+note+
						"</div>"
					+"</div>"
				);

				$("#"+options.idName)
					.css("top",(e.pageY - options.xOffset) + "px")
					.css("left",(e.pageX + options.yOffset) + "px")
					.fadeIn("fast");
			}
		}

		$(window).bind('blur', function(){
			$("#"+options.idName).remove();
		});

		return this.each(function() {
			obj = $(this);
			if(obj.attr('rel')!=''){

				for(var x=0; x<chapterNotes.length; x++) {
					if(chapterNotes[x]['ID']==obj.attr('rel')) {
						obj.data('note', chapterNotes[x]['note']);
					}
				}
			}
			obj.attr('rel', '');

			obj.hover(
				function(e){
					el = $(this);
					var f = function() { displayToolip(el, e); };
					options.timer = setTimeout(f, options.interval);
				},
				function(e){
					el = $(this);
					clearTimeout(options.timer);
					$("#"+options.idName).remove();
				}
			);

			obj.bind('mousemove', function(e){
				el = $(this);
				if($("#"+options.idName).is(':hidden')){
					var f = function() { displayToolip(el, e); };
					timer = setTimeout(f, options.interval);
				}
				$("#"+options.idName)
					.css("top",(e.pageY - options.xOffset) + "px")
					.css("left",(e.pageX + options.yOffset) + "px");
			});
		});
	}
})(jQuery);