Chris@0
|
1 <attach event="ondocumentready" handler="parseStylesheets" />
|
Chris@0
|
2 <script>
|
Chris@0
|
3 /**
|
Chris@0
|
4 * Whatever:hover - V1.42.060206 - hover & active
|
Chris@0
|
5 * ------------------------------------------------------------
|
Chris@0
|
6 * (c) 2005 - Peter Nederlof
|
Chris@0
|
7 * Peterned - http://www.xs4all.nl/~peterned/
|
Chris@0
|
8 * License - http://creativecommons.org/licenses/LGPL/2.1/
|
Chris@0
|
9 *
|
Chris@0
|
10 * Whatever:hover is free software; you can redistribute it and/or
|
Chris@0
|
11 * modify it under the terms of the GNU Lesser General Public
|
Chris@0
|
12 * License as published by the Free Software Foundation; either
|
Chris@0
|
13 * version 2.1 of the License, or (at your option) any later version.
|
Chris@0
|
14 *
|
Chris@0
|
15 * Whatever:hover is distributed in the hope that it will be useful,
|
Chris@0
|
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@0
|
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
Chris@0
|
18 * Lesser General Public License for more details.
|
Chris@0
|
19 *
|
Chris@0
|
20 * Credits and thanks to:
|
Chris@0
|
21 * Arnoud Berendsen, Martin Reurings, Robert Hanson
|
Chris@0
|
22 *
|
Chris@0
|
23 * howto: body { behavior:url("csshover.htc"); }
|
Chris@0
|
24 * ------------------------------------------------------------
|
Chris@0
|
25 */
|
Chris@0
|
26
|
Chris@0
|
27 var csshoverReg = /(^|\s)(([^a]([^ ]+)?)|(a([^#.][^ ]+)+)):(hover|active)/i,
|
Chris@0
|
28 currentSheet, doc = window.document, hoverEvents = [], activators = {
|
Chris@0
|
29 onhover:{on:'onmouseover', off:'onmouseout'},
|
Chris@0
|
30 onactive:{on:'onmousedown', off:'onmouseup'}
|
Chris@0
|
31 }
|
Chris@0
|
32
|
Chris@0
|
33 function parseStylesheets() {
|
Chris@0
|
34 if(!/MSIE (5|6)/.test(navigator.userAgent)) return;
|
Chris@0
|
35 window.attachEvent('onunload', unhookHoverEvents);
|
Chris@0
|
36 var sheets = doc.styleSheets, l = sheets.length;
|
Chris@0
|
37 for(var i=0; i<l; i++)
|
Chris@0
|
38 parseStylesheet(sheets[i]);
|
Chris@0
|
39 }
|
Chris@0
|
40 function parseStylesheet(sheet) {
|
Chris@0
|
41 if(sheet.imports) {
|
Chris@0
|
42 try {
|
Chris@0
|
43 var imports = sheet.imports, l = imports.length;
|
Chris@0
|
44 for(var i=0; i<l; i++) parseStylesheet(sheet.imports[i]);
|
Chris@0
|
45 } catch(securityException){}
|
Chris@0
|
46 }
|
Chris@0
|
47
|
Chris@0
|
48 try {
|
Chris@0
|
49 var rules = (currentSheet = sheet).rules, l = rules.length;
|
Chris@0
|
50 for(var j=0; j<l; j++) parseCSSRule(rules[j]);
|
Chris@0
|
51 } catch(securityException){}
|
Chris@0
|
52 }
|
Chris@0
|
53
|
Chris@0
|
54 function parseCSSRule(rule) {
|
Chris@0
|
55 var select = rule.selectorText, style = rule.style.cssText;
|
Chris@0
|
56 if(!csshoverReg.test(select) || !style) return;
|
Chris@0
|
57
|
Chris@0
|
58 var pseudo = select.replace(/[^:]+:([a-z-]+).*/i, 'on$1');
|
Chris@0
|
59 var newSelect = select.replace(/(\.([a-z0-9_-]+):[a-z]+)|(:[a-z]+)/gi, '.$2' + pseudo);
|
Chris@0
|
60 var className = (/\.([a-z0-9_-]*on(hover|active))/i).exec(newSelect)[1];
|
Chris@0
|
61 var affected = select.replace(/:(hover|active).*$/, '');
|
Chris@0
|
62 var elements = getElementsBySelect(affected);
|
Chris@0
|
63 if(elements.length == 0) return;
|
Chris@0
|
64
|
Chris@0
|
65 currentSheet.addRule(newSelect, style);
|
Chris@0
|
66 for(var i=0; i<elements.length; i++)
|
Chris@0
|
67 new HoverElement(elements[i], className, activators[pseudo]);
|
Chris@0
|
68 }
|
Chris@0
|
69
|
Chris@0
|
70 function HoverElement(node, className, events) {
|
Chris@0
|
71 if(!node.hovers) node.hovers = {};
|
Chris@0
|
72 if(node.hovers[className]) return;
|
Chris@0
|
73 node.hovers[className] = true;
|
Chris@0
|
74 hookHoverEvent(node, events.on, function() { node.className += ' ' + className; });
|
Chris@0
|
75 hookHoverEvent(node, events.off, function() { node.className = node.className.replace(new RegExp('\\s+'+className, 'g'),''); });
|
Chris@0
|
76 }
|
Chris@0
|
77 function hookHoverEvent(node, type, handler) {
|
Chris@0
|
78 node.attachEvent(type, handler);
|
Chris@0
|
79 hoverEvents[hoverEvents.length] = {
|
Chris@0
|
80 node:node, type:type, handler:handler
|
Chris@0
|
81 };
|
Chris@0
|
82 }
|
Chris@0
|
83
|
Chris@0
|
84 function unhookHoverEvents() {
|
Chris@0
|
85 for(var e,i=0; i<hoverEvents.length; i++) {
|
Chris@0
|
86 e = hoverEvents[i];
|
Chris@0
|
87 e.node.detachEvent(e.type, e.handler);
|
Chris@0
|
88 }
|
Chris@0
|
89 }
|
Chris@0
|
90
|
Chris@0
|
91 function getElementsBySelect(rule) {
|
Chris@0
|
92 var parts, nodes = [doc];
|
Chris@0
|
93 parts = rule.split(' ');
|
Chris@0
|
94 for(var i=0; i<parts.length; i++) {
|
Chris@0
|
95 nodes = getSelectedNodes(parts[i], nodes);
|
Chris@0
|
96 } return nodes;
|
Chris@0
|
97 }
|
Chris@0
|
98 function getSelectedNodes(select, elements) {
|
Chris@0
|
99 var result, node, nodes = [];
|
Chris@0
|
100 var identify = (/\#([a-z0-9_-]+)/i).exec(select);
|
Chris@0
|
101 if(identify) {
|
Chris@0
|
102 var element = doc.getElementById(identify[1]);
|
Chris@0
|
103 return element? [element]:nodes;
|
Chris@0
|
104 }
|
Chris@0
|
105
|
Chris@0
|
106 var classname = (/\.([a-z0-9_-]+)/i).exec(select);
|
Chris@0
|
107 var tagName = select.replace(/(\.|\#|\:)[a-z0-9_-]+/i, '');
|
Chris@0
|
108 var classReg = classname? new RegExp('\\b' + classname[1] + '\\b'):false;
|
Chris@0
|
109 for(var i=0; i<elements.length; i++) {
|
Chris@0
|
110 result = tagName? elements[i].all.tags(tagName):elements[i].all;
|
Chris@0
|
111 for(var j=0; j<result.length; j++) {
|
Chris@0
|
112 node = result[j];
|
Chris@0
|
113 if(classReg && !classReg.test(node.className)) continue;
|
Chris@0
|
114 nodes[nodes.length] = node;
|
Chris@0
|
115 }
|
Chris@0
|
116 }
|
Chris@0
|
117
|
Chris@0
|
118 return nodes;
|
Chris@0
|
119 }
|
Chris@0
|
120
|
Chris@0
|
121 window.parseStylesheets = parseStylesheets;
|
Chris@0
|
122 </script> |