// Simple tooltip for Duells-product catalogue (function($){ var self; var tooltipDivId; $.fn.Tooltip = function(options){ self = this; var opt = { tooltipDivId:'SimpleTooltipMain', readFromTitle:true, headerText:'', bodyText:'', bodyUrl:'', headerCss:'SimpleTooltipHeader', bodyCss:'SimpleTooltipBody', fadeSpeed:350, offset: {x:12, y:18} } $.extend(opt, options); tooltipDivId = opt.tooltipDivId; //Create base elements if needed if($("#" + tooltipDivId).size() == 0){ $('
') .appendTo(document.body) .attr('id', tooltipDivId) .css({display:'none', position: 'absolute', left:'-1000px', top: '-1000px', 'z-index':'4000'}); $("#" + tooltipDivId).append('
'); } self.each(function(){ new tip(this, opt); }); return self; } var tip = function(obj, opt){ obj = $(obj); var $main = $("#" + tooltipDivId); var $header = $main.children(":first"); var $body = $main.children(":last"); var headerTxt, bodyTxt; //Get tooltip from object title if(opt.readFromTitle && obj.attr("title") != ""){ var titleStr = obj.attr("title"); //Remove title obj.attr("title", ""); var regExp = /^([^:]+)?:\s*([\w\W]*)/; var matchCol = regExp.exec(titleStr); if(matchCol){ headerTxt = matchCol[1]; bodyTxt = matchCol[2].replace(/^\s*([\w\W]+?)\s*$/, "$1").replace(/\r?\n/g, '
'); } }else{ headerTxt = opt.headerText; bodyTxt = opt.bodyText; if(opt.bodyUrl != ''){ $.ajax({ url: opt.bodyUrl, dataType: 'html', success: function(data) { $body.html(data); } }); } } //Only show tooltip if contents is available if(headerTxt != '' || bodyTxt != ''){ obj.bind("mouseover", function(){ if(headerTxt != ''){ $header.attr('class', opt.headerCss); $header.html(headerTxt); $header.show(); }else $header.hide(); if(bodyTxt != ''){ $body.attr('class', opt.bodyCss); $body.html(bodyTxt); $body.show(); }else $body.hide(); $main.fadeIn(opt.fadeSpeed, function(){ if($header.html()=='' && $body.html()=='') $main.hide(); }); obj.bind("mousemove", move); return false; }); obj.bind("mousedown mouseout", function(){ $main.hide(); obj.unbind("mousemove"); $header.html(""); $body.html(""); }); $(document.body).bind("mousemove", function(){ if($main.is(":visible")){ $main.hide(); obj.unbind("mousemove"); $header.html(""); $body.html(""); } }); } /* Moves the tooltip to follow the cursor */ function move(ev){ var w = window; var $main = $("#" + tooltipDivId); if(!ev){ ev = window.ev } else { window.ev = ev} if(!$main) { return } var p = { x: ev.pageX + opt.offset.x, y: ev.pageY + opt.offset.y, w: $main.width(), h: $main.height() } var v = { x: Number(w.scrollX?w.scrollX:document.body.scrollLeft), y: Number(w.scrollY?w.scrollY:document.body.scrollTop), w: Number(w.innerWidth?w.innerWidth:document.body.clientWidth) - 20/*(w.scrollMaxY > 0 ? 30 : 20)*/, h: Number(w.innerHeight?w.innerHeight:document.body.clientHeight) - 20/*(w.scrollMaxX > 0 ? 20 : 10)*/ }; /* Better not to go offscreen */ if(p.x + p.w > v.x + v.w) { p.x = v.x + v.w - p.w } if(p.y + p.h > v.y + v.h) { p.y = (v.y+v.h-p.h)}//{ p.y = ev.pageY - p.h - opt.offset.y } if(p.x < v.x){ p.x = v.x } if(p.y < v.y){ p.y = v.y } $main.css({top:p.y, left:p.x}); return false; } } })(jQuery);