(file) Return to nicetitle.js CVS log (file) (dir) Up to [RizwankCVS] / geekymedia_web

  1 rizwank 1.1 addEvent(window, "load", makeNiceTitles);
  2             
  3             var XHTMLNS = "http://www.w3.org/1999/xhtml";
  4             var CURRENT_NICE_TITLE;
  5             var browser = new Browser();
  6             
  7             function makeNiceTitles() {
  8                 if (!document.createElement || !document.getElementsByTagName) return;
  9                 // add namespace methods to HTML DOM; this makes the script work in both
 10                 // HTML and XML contexts.
 11                 if(!document.createElementNS)
 12                 {
 13                     document.createElementNS = function(ns,elt) {
 14                         return document.createElement(elt);
 15                     }
 16                 }
 17             
 18                 if( !document.links )
 19                 {
 20                     document.links = document.getElementsByTagName("a");
 21                 }
 22 rizwank 1.1     for (var ti=0;ti<document.links.length;ti++) {
 23                     var lnk = document.links[ti];
 24                     if (lnk.title) {
 25                         lnk.setAttribute("nicetitle",lnk.title);
 26                         lnk.removeAttribute("title");
 27                         addEvent(lnk,"mouseover",showNiceTitle);
 28                         addEvent(lnk,"mouseout",hideNiceTitle);
 29                         addEvent(lnk,"focus",showNiceTitle);
 30                         addEvent(lnk,"blur",hideNiceTitle);
 31                     }
 32                 }
 33                 var instags = document.getElementsByTagName("ins");
 34                 if (instags) {
 35                 for (var ti=0;ti<instags.length;ti++) {
 36                     var instag = instags[ti];
 37                     if (instag.dateTime) {
 38                         var strDate = instag.dateTime;
 39                         var dtIns = new Date(strDate.substring(0,4),parseInt(strDate.substring(4,6)-1),strDate.substring(6,8),strDate.substring(9,11),strDate.substring(11,13),strDate.substring(13,15));
 40                         instag.setAttribute("nicetitle","Added on "+dtIns.toString());
 41                         addEvent(instag,"mouseover",showNiceTitle);
 42                         addEvent(instag,"mouseout",hideNiceTitle);
 43 rizwank 1.1             addEvent(instag,"focus",showNiceTitle);
 44                         addEvent(instag,"blur",hideNiceTitle);
 45                     }
 46                 }
 47                 }
 48             }
 49             
 50             function findPosition( oLink ) {
 51               if( oLink.offsetParent ) {
 52                 for( var posX = 0, posY = 0; oLink.offsetParent; oLink = oLink.offsetParent ) {
 53                   posX += oLink.offsetLeft;
 54                   posY += oLink.offsetTop;
 55                 }
 56                 return [ posX, posY ];
 57               } else {
 58                 return [ oLink.x, oLink.y ];
 59               }
 60             }
 61             
 62             function showNiceTitle(e) {
 63                 if (!document.getElementsByTagName) return;
 64 rizwank 1.1     if (window.event && window.event.srcElement) {
 65                     lnk = window.event.srcElement
 66                 } else if (e && e.target) {
 67                     lnk = e.target
 68                 }
 69                 if (!lnk) return;
 70                 if (lnk.nodeType == 3) {
 71                     // lnk is a textnode -- ascend parents until we hit a link
 72                     lnk = getParent(lnk,"A");
 73                 }
 74                 if (!lnk) return;
 75                 nicetitle = lnk.getAttribute("nicetitle");
 76                 
 77                 var d = document.createElementNS(XHTMLNS,"div");
 78                 d.className = "nicetitle";
 79                 tnt = document.createTextNode(nicetitle);
 80                 pat = document.createElementNS(XHTMLNS,"p");
 81                 pat.className = "titletext";
 82                 pat.appendChild(tnt);
 83                 d.appendChild(pat);
 84                 if (lnk.href) {
 85 rizwank 1.1         tnd = document.createTextNode(lnk.href);
 86                     pad = document.createElementNS(XHTMLNS,"p");
 87                     pad.className = "destination";
 88                     pad.appendChild(tnd);
 89                     d.appendChild(pad);
 90                 }
 91                 
 92                 STD_WIDTH = 300;
 93                 if (lnk.href) {
 94                     h = lnk.href.length;
 95                 } else { h = nicetitle.length; }
 96                 if (nicetitle.length) {
 97                   t = nicetitle.length;
 98                 }
 99                 h_pixels = h*6; t_pixels = t*10;
100                 
101                 if (h_pixels > STD_WIDTH) {
102                     w = h_pixels;
103                 } else if ((STD_WIDTH>t_pixels) && (t_pixels>h_pixels)) {
104                     w = t_pixels;
105                 } else if ((STD_WIDTH>t_pixels) && (h_pixels>t_pixels)) {
106 rizwank 1.1         w = h_pixels;
107                 } else {
108                     w = STD_WIDTH;
109                 }
110                     
111                 d.style.width = w + 'px';    
112             
113                 /*
114                 mx = lnk.offsetLeft;
115                 my = lnk.offsetTop;
116                 */
117                 mpos = findPosition(lnk);
118                 mx = mpos[0];
119                 my = mpos[1];
120                 //xy = getMousePosition(e);
121                 //mx = xy[0]; my = xy[1];
122                 
123                 d.style.left = (mx+15) + 'px';
124                 d.style.top = (my+35) + 'px';
125             
126                 if (document.body && document.body.offsetWidth && ((mx+w) > document.body.offsetWidth)) {
127 rizwank 1.1         d.style.left = (document.body.offsetWidth - w - 20) + "px";
128                 }
129                 
130                 document.getElementsByTagName("body")[0].appendChild(d);
131                 
132                 CURRENT_NICE_TITLE = d;
133             }
134             
135             function hideNiceTitle(e) {
136                 if (!document.getElementsByTagName) return;
137                 if (CURRENT_NICE_TITLE) {
138                     document.getElementsByTagName("body")[0].removeChild(CURRENT_NICE_TITLE);
139                     CURRENT_NICE_TITLE = null;
140                 }
141             }
142             
143             // Add an eventListener to browsers that can do it somehow.
144             // Originally by the amazing Scott Andrew.
145             function addEvent(obj, evType, fn){
146               if (obj.addEventListener){
147                 obj.addEventListener(evType, fn, true);
148 rizwank 1.1     return true;
149               } else if (obj.attachEvent){
150             	var r = obj.attachEvent("on"+evType, fn);
151                 return r;
152               } else {
153             	return false;
154               }
155             }
156             
157             function getParent(el, pTagName) {
158             	if (el == null) return null;
159             	else if (el.nodeType == 1 && el.tagName.toLowerCase() == pTagName.toLowerCase())	// Gecko bug, supposed to be uppercase
160             		return el;
161             	else
162             		return getParent(el.parentNode, pTagName);
163             }
164             
165             function getMousePosition(event) {
166               if (browser.isIE) {
167                 x = window.event.clientX + document.documentElement.scrollLeft
168                   + document.body.scrollLeft;
169 rizwank 1.1     y = window.event.clientY + document.documentElement.scrollTop
170                   + document.body.scrollTop;
171               }
172               if (browser.isNS) {
173                 x = event.clientX + window.scrollX;
174                 y = event.clientY + window.scrollY;
175               }
176               return [x,y];
177             }
178             
179             // Determine browser and version.
180             
181             function Browser() {
182             // blah, browser detect, but mouse-position stuff doesn't work any other way
183               var ua, s, i;
184             
185               this.isIE    = false;
186               this.isNS    = false;
187               this.version = null;
188             
189               ua = navigator.userAgent;
190 rizwank 1.1 
191               s = "MSIE";
192               if ((i = ua.indexOf(s)) >= 0) {
193                 this.isIE = true;
194                 this.version = parseFloat(ua.substr(i + s.length));
195                 return;
196               }
197             
198               s = "Netscape6/";
199               if ((i = ua.indexOf(s)) >= 0) {
200                 this.isNS = true;
201                 this.version = parseFloat(ua.substr(i + s.length));
202                 return;
203               }
204             
205               // Treat any other "Gecko" browser as NS 6.1.
206             
207               s = "Gecko";
208               if ((i = ua.indexOf(s)) >= 0) {
209                 this.isNS = true;
210                 this.version = 6.1;
211 rizwank 1.1     return;
212               }
213             }
214             

Rizwan Kassim
Powered by
ViewCVS 0.9.2