|
1 /* |
|
2 Author: mg12 |
|
3 Update: 2008/08/11 |
|
4 Author URI: http://www.neoease.com/ |
|
5 */ |
|
6 (function() { |
|
7 |
|
8 var Class = { |
|
9 create: function() { |
|
10 return function() { |
|
11 this.initialize.apply(this, arguments); |
|
12 } |
|
13 } |
|
14 } |
|
15 |
|
16 var GhostlyMenu = Class.create(); |
|
17 GhostlyMenu.prototype = { |
|
18 |
|
19 initialize: function(target, align, sub) { |
|
20 this.obj = cleanWhitespace(target); |
|
21 this.align = align || 'left'; |
|
22 this.sub = sub || -1; |
|
23 |
|
24 this.menu = this.obj.childNodes; |
|
25 if (this.menu.length < 2) { return; } |
|
26 |
|
27 this.title = this.menu[0]; |
|
28 this.body = this.menu[1]; |
|
29 |
|
30 cleanWhitespace(this.body).firstChild.getElementsByTagName('a')[0].className += 'first'; |
|
31 |
|
32 setStyle(this.body, 'visibility', 'hidden'); |
|
33 setStyle(this.body, 'display', 'block'); |
|
34 |
|
35 addListener(this.obj, 'mouseover', bind(this, this.activate), false); |
|
36 addListener(this.obj, 'mouseout', bind(this, this.deactivate), false); |
|
37 }, |
|
38 |
|
39 activate: function() { |
|
40 if(this.sub == 1) { |
|
41 var pos = currentOffset(this.title); |
|
42 var top = pos[1] + 3; |
|
43 var left = getWidth(this.body) - 6; |
|
44 } else { |
|
45 var pos = cumulativeOffset(this.title); |
|
46 var top = pos[1] + getHeight(this.title); |
|
47 var left = pos[0] - 1; |
|
48 } |
|
49 |
|
50 if(!/current/.test(this.title.className)) { |
|
51 this.title.className += ' current'; |
|
52 } |
|
53 |
|
54 setStyle(this.body, 'left', left + 'px'); |
|
55 setStyle(this.body, 'top', top + 'px'); |
|
56 setStyle(this.body, 'visibility', 'visible'); |
|
57 }, |
|
58 |
|
59 deactivate: function(){ |
|
60 this.title.className = this.title.className.replace('current', ''); |
|
61 var thismenu = this; |
|
62 var tid = setInterval( function() { |
|
63 clearInterval(tid); |
|
64 if (!/current/.test(thismenu.title.className)) { |
|
65 setStyle(thismenu.body, 'visibility', 'hidden'); |
|
66 } |
|
67 return false; |
|
68 }, 400); |
|
69 } |
|
70 } |
|
71 |
|
72 $A = function(iterable) { |
|
73 if(!iterable) { |
|
74 return []; |
|
75 } |
|
76 if(iterable.toArray) { |
|
77 return iterable.toArray(); |
|
78 } else { |
|
79 var results = []; |
|
80 for(var i = 0; i < iterable.length; i++) { |
|
81 results.push(iterable[i]); |
|
82 } |
|
83 return results; |
|
84 } |
|
85 } |
|
86 |
|
87 bind = function() { |
|
88 var array = this.$A(arguments); |
|
89 var func = array[array.length - 1]; |
|
90 var _method = func, args = array, object = args.shift(); |
|
91 return function() { |
|
92 return _method.apply(object, args.concat(array)); |
|
93 } |
|
94 } |
|
95 |
|
96 getHeight = function(element) { |
|
97 return element.offsetHeight; |
|
98 } |
|
99 |
|
100 getWidth = function(element) { |
|
101 return element.offsetWidth; |
|
102 } |
|
103 |
|
104 setStyle = function(element, key, value) { |
|
105 element.style[key] = value; |
|
106 } |
|
107 |
|
108 getStyle = function(element, key) { |
|
109 return element.style[key]; |
|
110 } |
|
111 |
|
112 cleanWhitespace = function(list) { |
|
113 var node = list.firstChild; |
|
114 while (node) { |
|
115 var nextNode = node.nextSibling; |
|
116 if(node.nodeType == 3 && !/\S/.test(node.nodeValue)) { |
|
117 list.removeChild(node); |
|
118 } |
|
119 node = nextNode; |
|
120 } |
|
121 return list; |
|
122 } |
|
123 |
|
124 currentOffset = function(element) { |
|
125 var valueT = element.offsetTop || 0; |
|
126 var valueL = element.offsetLeft || 0; |
|
127 return [valueL, valueT]; |
|
128 } |
|
129 |
|
130 cumulativeOffset = function(element) { |
|
131 var valueT = 0, valueL = 0; |
|
132 do { |
|
133 valueT += element.offsetTop || 0; |
|
134 valueL += element.offsetLeft || 0; |
|
135 element = element.offsetParent; |
|
136 } while (element); |
|
137 return [valueL, valueT]; |
|
138 } |
|
139 |
|
140 addListener = function(element, name, observer, useCapture) { |
|
141 if(element.addEventListener) { |
|
142 element.addEventListener(name, observer, useCapture); |
|
143 } else if(element.attachEvent) { |
|
144 element.attachEvent('on' + name, observer); |
|
145 } |
|
146 } |
|
147 |
|
148 function loadMenus() { |
|
149 var menubar = document.getElementById('navigation'); |
|
150 if (menubar) { |
|
151 var list = menubar.getElementsByTagName('ul'); |
|
152 for (var i = 0; i < list.length; i++) { |
|
153 var menu = list[i].parentNode; |
|
154 if(menu.parentNode === menubar) { |
|
155 new GhostlyMenu(menu, 'left'); |
|
156 } else { |
|
157 new GhostlyMenu(menu, 'left', 1); |
|
158 menu.firstChild.className += ' subtitle'; |
|
159 } |
|
160 } |
|
161 } |
|
162 } |
|
163 |
|
164 if (document.addEventListener) { |
|
165 document.addEventListener("DOMContentLoaded", loadMenus, false); |
|
166 |
|
167 } else if (/MSIE/i.test(navigator.userAgent)) { |
|
168 document.write('<script id="__ie_onload_for_elegantbox" defer src="javascript:void(0);"></script>'); |
|
169 var script = document.getElementById("__ie_onload_for_elegantbox"); |
|
170 script.onreadystatechange = function() { |
|
171 if (this.readyState == 'complete') { |
|
172 loadMenus(); |
|
173 } |
|
174 } |
|
175 |
|
176 } else if (/WebKit/i.test(navigator.userAgent)) { |
|
177 var _timer = setInterval( function() { |
|
178 if (/loaded|complete/.test(document.readyState)) { |
|
179 clearInterval(_timer); |
|
180 loadMenus(); |
|
181 } |
|
182 }, 10); |
|
183 |
|
184 } else { |
|
185 window.onload = function(e) { |
|
186 loadMenus(); |
|
187 } |
|
188 } |
|
189 |
|
190 })(); |