RT# 82949 - changes section name from fees to pricing, better opiton
[freeside.git] / httemplate / elements / xmenu.js
1 //<script>
2 /*
3  * This script was created by Erik Arvidsson (erik@eae.net)
4  * for WebFX (http://webfx.eae.net)
5  * Copyright 2001
6  * 
7  * For usage see license at http://webfx.eae.net/license.html   
8  *
9  * Created:             2001-01-12
10  * Updates:             2001-11-20      Added hover mode support and removed Opera focus hacks
11  *                              2001-12-20      Added auto positioning and some properties to support this
12  *                              2002-08-13      toString used ' for attributes. Changed to " to allow in args
13  */
14  
15 // check browsers
16 var ua = navigator.userAgent;
17 var opera = window.opera || /opera [56789]|opera\/[56789]/i.test(ua);
18 var ie = !opera && /MSIE/.test(ua);
19 var ie50 = ie && /MSIE 5\.[01234]/.test(ua);
20 var ie6 = ie && /MSIE [6789]/.test(ua);
21 var ieBox = ie && (document.compatMode == null || document.compatMode != "CSS1Compat");
22 var moz = !opera && /gecko/i.test(ua);
23 var nn6 = !opera && /netscape.*6\./i.test(ua);
24 var khtml = /KHTML/i.test(ua);
25
26 // define the default values
27
28 webfxMenuDefaultWidth                   = 154;
29
30 webfxMenuDefaultBorderLeft              = 1;
31 webfxMenuDefaultBorderRight             = 1;
32 webfxMenuDefaultBorderTop               = 1;
33 webfxMenuDefaultBorderBottom    = 1;
34
35 webfxMenuDefaultPaddingLeft             = 1;
36 webfxMenuDefaultPaddingRight    = 1;
37 webfxMenuDefaultPaddingTop              = 1;
38 webfxMenuDefaultPaddingBottom   = 1;
39
40 webfxMenuDefaultShadowLeft              = 0;
41 webfxMenuDefaultShadowRight             = ie && !ie50 && /win32/i.test(navigator.platform) ? 4 :0;
42 webfxMenuDefaultShadowTop               = 0;
43 webfxMenuDefaultShadowBottom    = ie && !ie50 && /win32/i.test(navigator.platform) ? 4 : 0;
44
45
46 webfxMenuItemDefaultHeight              = 18;
47 webfxMenuItemDefaultText                = "Untitled";
48 webfxMenuItemDefaultHref                = "javascript:void(0)";
49
50 webfxMenuSeparatorDefaultHeight = 6;
51
52 webfxMenuDefaultEmptyText               = "Empty";
53
54 webfxMenuDefaultUseAutoPosition = nn6 ? false : true;
55
56
57
58 // other global constants
59
60 webfxMenuImagePath                              = "";
61
62 webfxMenuUseHover                               = opera ? true : false;
63 webfxMenuHideTime                               = 500;
64 webfxMenuShowTime                               = 200;
65
66
67
68 var webFXMenuHandler = {
69         idCounter               :       0,
70         idPrefix                :       "webfx-menu-object-",
71         all                             :       {},
72         getId                   :       function () { return this.idPrefix + this.idCounter++; },
73         overMenuItem    :       function (oItem) {
74                 if (this.showTimeout != null)
75                         window.clearTimeout(this.showTimeout);
76                 if (this.hideTimeout != null)
77                         window.clearTimeout(this.hideTimeout);
78                 var jsItem = this.all[oItem.id];
79                 if (webfxMenuShowTime <= 0)
80                         this._over(jsItem);
81                 else if ( jsItem )
82                         //this.showTimeout = window.setTimeout(function () { webFXMenuHandler._over(jsItem) ; }, webfxMenuShowTime);
83                         // I hate IE5.0 because the piece of shit crashes when using setTimeout with a function object
84                         this.showTimeout = window.setTimeout("webFXMenuHandler._over(webFXMenuHandler.all['" + jsItem.id + "'])", webfxMenuShowTime);
85         },
86         outMenuItem     :       function (oItem) {
87                 if (this.showTimeout != null)
88                         window.clearTimeout(this.showTimeout);
89                 if (this.hideTimeout != null)
90                         window.clearTimeout(this.hideTimeout);
91                 var jsItem = this.all[oItem.id];
92                 if (webfxMenuHideTime <= 0)
93                         this._out(jsItem);
94                 else if ( jsItem ) 
95                         //this.hideTimeout = window.setTimeout(function () { webFXMenuHandler._out(jsItem) ; }, webfxMenuHideTime);
96                         this.hideTimeout = window.setTimeout("webFXMenuHandler._out(webFXMenuHandler.all['" + jsItem.id + "'])", webfxMenuHideTime);
97         },
98         blurMenu                :       function (oMenuItem) {
99                 window.setTimeout("webFXMenuHandler.all[\"" + oMenuItem.id + "\"].subMenu.hide();", webfxMenuHideTime);
100         },
101         _over   :       function (jsItem) {
102                 if (jsItem.subMenu) {
103                         jsItem.parentMenu.hideAllSubs();
104                         jsItem.subMenu.show();
105                 }
106                 else
107                         jsItem.parentMenu.hideAllSubs();
108         },
109         _out    :       function (jsItem) {
110                 // find top most menu
111                 var root = jsItem;
112                 var m;
113                 if (root instanceof WebFXMenuButton)
114                         m = root.subMenu;
115                 else {
116                         m = jsItem.parentMenu;
117                         while (m.parentMenu != null && !(m.parentMenu instanceof WebFXMenuBar))
118                                 m = m.parentMenu;
119                 }
120                 if (m != null)  
121                         m.hide();       
122         },
123         hideMenu        :       function (menu) {
124                 if (this.showTimeout != null)
125                         window.clearTimeout(this.showTimeout);
126                 if (this.hideTimeout != null)
127                         window.clearTimeout(this.hideTimeout);
128
129                 this.hideTimeout = window.setTimeout("webFXMenuHandler.all['" + menu.id + "'].hide()", webfxMenuHideTime);
130         },
131         showMenu        :       function (menu, src, dir) {
132                 if (this.showTimeout != null)
133                         window.clearTimeout(this.showTimeout);
134                 if (this.hideTimeout != null)
135                         window.clearTimeout(this.hideTimeout);
136
137                 if (arguments.length < 3)
138                         dir = "vertical";
139                 
140                 menu.show(src, dir);
141         }
142 };
143
144 function WebFXMenu() {
145         this._menuItems = [];
146         this._subMenus  = [];
147         this.id                 = webFXMenuHandler.getId();
148         this.top                = 0;
149         this.left               = 0;
150         this.shown              = false;
151         this.parentMenu = null;
152         webFXMenuHandler.all[this.id] = this;
153 }
154
155 WebFXMenu.prototype.width                       = webfxMenuDefaultWidth;
156 WebFXMenu.prototype.emptyText           = webfxMenuDefaultEmptyText;
157 WebFXMenu.prototype.useAutoPosition     = webfxMenuDefaultUseAutoPosition;
158
159 WebFXMenu.prototype.borderLeft          = webfxMenuDefaultBorderLeft;
160 WebFXMenu.prototype.borderRight         = webfxMenuDefaultBorderRight;
161 WebFXMenu.prototype.borderTop           = webfxMenuDefaultBorderTop;
162 WebFXMenu.prototype.borderBottom        = webfxMenuDefaultBorderBottom;
163
164 WebFXMenu.prototype.paddingLeft         = webfxMenuDefaultPaddingLeft;
165 WebFXMenu.prototype.paddingRight        = webfxMenuDefaultPaddingRight;
166 WebFXMenu.prototype.paddingTop          = webfxMenuDefaultPaddingTop;
167 WebFXMenu.prototype.paddingBottom       = webfxMenuDefaultPaddingBottom;
168
169 WebFXMenu.prototype.shadowLeft          = webfxMenuDefaultShadowLeft;
170 WebFXMenu.prototype.shadowRight         = webfxMenuDefaultShadowRight;
171 WebFXMenu.prototype.shadowTop           = webfxMenuDefaultShadowTop;
172 WebFXMenu.prototype.shadowBottom        = webfxMenuDefaultShadowBottom;
173
174
175
176 WebFXMenu.prototype.add = function (menuItem) {
177         this._menuItems[this._menuItems.length] = menuItem;
178         if (menuItem.subMenu) {
179                 this._subMenus[this._subMenus.length] = menuItem.subMenu;
180                 menuItem.subMenu.parentMenu = this;
181         }
182         
183         menuItem.parentMenu = this;
184 };
185
186 WebFXMenu.prototype.show = function (relObj, sDir) {
187         if (this.useAutoPosition)
188                 this.position(relObj, sDir);
189
190         var divElement = document.getElementById(this.id);
191         if ( divElement ) {
192
193           //divElement.style.left = opera ? this.left : this.left + "px";
194           //divElement.style.top = opera ? this.top : this.top + "px";
195           divElement.style.left = this.left + "px";
196           divElement.style.top = this.top + "px";
197           divElement.style.visibility = "visible";
198
199           if ( ie ) {
200             var shimElement = document.getElementById(this.id + "Shim");
201             if ( shimElement ) {
202               shimElement.style.width = divElement.offsetWidth;
203               shimElement.style.height = divElement.offsetHeight;
204               shimElement.style.top = divElement.style.top;
205               shimElement.style.left = divElement.style.left;
206               /*shimElement.style.zIndex = divElement.style.zIndex - 1; */
207               shimElement.style.display = "block";
208               shimElement.style.filter='progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';
209             }
210           }
211
212         }
213
214         this.shown = true;
215
216         if (this.parentMenu)
217                 this.parentMenu.show();
218 };
219
220 WebFXMenu.prototype.hide = function () {
221         this.hideAllSubs();
222         var divElement = document.getElementById(this.id);
223         if ( divElement ) {
224           divElement.style.visibility = "hidden";
225           if ( ie ) {
226             var shimElement = document.getElementById(this.id + "Shim");
227             if ( shimElement ) {
228               shimElement.style.display = "none";
229             }
230           }
231         }
232
233         this.shown = false;
234 };
235
236 WebFXMenu.prototype.hideAllSubs = function () {
237         for (var i = 0; i < this._subMenus.length; i++) {
238                 if (this._subMenus[i].shown)
239                         this._subMenus[i].hide();
240         }
241 };
242
243 WebFXMenu.prototype.toString = function () {
244         var top = this.top + this.borderTop + this.paddingTop;
245         var str = "<div id='" + this.id + "' class='webfx-menu' style='" + 
246         "width:" + (!ieBox  ?
247                 this.width - this.borderLeft - this.paddingLeft - this.borderRight - this.paddingRight  : 
248                 this.width) + "px;" +
249         (this.useAutoPosition ?
250                 "left:" + this.left + "px;" + "top:" + this.top + "px;" :
251                 "") +
252         (ie50 ? "filter: none;" : "") +
253         "'>";
254
255         if (this._menuItems.length == 0) {
256                 str +=  "<span class='webfx-menu-empty'>" + this.emptyText + "</span>";
257         }
258         else {  
259                 str += '<span class="webfx-menu-title" onmouseover="webFXMenuHandler.overMenuItem(this)"' +
260                         (webfxMenuUseHover ? " onmouseout='webFXMenuHandler.outMenuItem(this)'" : "") +
261                          '>' + this.emptyText + '</span>';
262                 // str += '<div id="' + this.id + '-title">' + this.emptyText + '</div>';
263                 // loop through all menuItems
264                 for (var i = 0; i < this._menuItems.length; i++) {
265                         var mi = this._menuItems[i];
266                         str += mi;
267                         if (!this.useAutoPosition) {
268                                 if (mi.subMenu && !mi.subMenu.useAutoPosition)
269                                         mi.subMenu.top = top - mi.subMenu.borderTop - mi.subMenu.paddingTop;
270                                 top += mi.height;
271                         }
272                 }
273
274         }
275         
276         str += "</div>";
277
278         if ( ie ) {
279           str += "<iframe id='" + this.id + "Shim' src='javascript:false;' scrolling='no' frameBorder='0' style='position:absolute; top:0px; left: 0px; display:none;'></iframe>";
280         }
281         
282         for (var i = 0; i < this._subMenus.length; i++) {
283                 this._subMenus[i].left = this.left + this.width - this._subMenus[i].borderLeft;
284                 str += this._subMenus[i];
285         }
286         
287         return str;
288 };
289 // WebFXMenu.prototype.position defined later
290
291 function WebFXMenuItem(sText, sHref, sToolTip, oSubMenu) {
292         this.text = sText || webfxMenuItemDefaultText;
293         this.href = (sHref == null || sHref == "") ? webfxMenuItemDefaultHref : sHref;
294         this.subMenu = oSubMenu;
295         if (oSubMenu)
296                 oSubMenu.parentMenuItem = this;
297         this.toolTip = sToolTip;
298         this.id = webFXMenuHandler.getId();
299         webFXMenuHandler.all[this.id] = this;
300 };
301 WebFXMenuItem.prototype.height = webfxMenuItemDefaultHeight;
302 WebFXMenuItem.prototype.toString = function () {
303         return  "<a" +
304                         " id='" + this.id + "'" +
305                         " href=\"" + this.href + "\"" +
306                         (this.toolTip ? " title=\"" + this.toolTip + "\"" : "") +
307                         " onmouseover='webFXMenuHandler.overMenuItem(this)'" +
308                         (webfxMenuUseHover ? " onmouseout='webFXMenuHandler.outMenuItem(this)'" : "") +
309                         (this.subMenu ? " unselectable='on' tabindex='-1'" : "") +
310                         ">" +
311                         (this.subMenu ? "<img class='arrow' src=\"" + webfxMenuImagePath + "arrow.right.black.png\">" : "") +
312                         this.text + 
313                         "</a>";
314 };
315
316
317 function WebFXMenuSeparator() {
318         this.id = webFXMenuHandler.getId();
319         webFXMenuHandler.all[this.id] = this;
320 };
321 WebFXMenuSeparator.prototype.height = webfxMenuSeparatorDefaultHeight;
322 WebFXMenuSeparator.prototype.toString = function () {
323         return  "<div" +
324                         " id='" + this.id + "'" +
325                         (webfxMenuUseHover ? 
326                         " onmouseover='webFXMenuHandler.overMenuItem(this)'" +
327                         " onmouseout='webFXMenuHandler.outMenuItem(this)'"
328                         :
329                         "") +
330                         "></div>"
331 };
332
333 function WebFXMenuBar() {
334         this._parentConstructor = WebFXMenu;
335         this._parentConstructor();
336 }
337 WebFXMenuBar.prototype = new WebFXMenu;
338 WebFXMenuBar.prototype.toString = function () {
339         var str = "<div id='" + this.id + "' class='webfx-menu-bar'>";
340         
341         // loop through all menuButtons
342         for (var i = 0; i < this._menuItems.length; i++)
343                 str += this._menuItems[i];
344         
345         str += "</div>";
346
347         for (var i = 0; i < this._subMenus.length; i++)
348                 str += this._subMenus[i];
349         
350         return str;
351 };
352
353 function WebFXMenuButton(sText, sHref, sToolTip, oSubMenu) {
354         this._parentConstructor = WebFXMenuItem;
355         this._parentConstructor(sText, sHref, sToolTip, oSubMenu);
356 }
357 WebFXMenuButton.prototype = new WebFXMenuItem;
358 WebFXMenuButton.prototype.toString = function () {
359         return  "<a" +
360                         " id='" + this.id + "'" +
361                         " href='" + this.href + "'" +
362                         (this.toolTip ? " title='" + this.toolTip + "'" : "") +
363                         (webfxMenuUseHover ?
364                                 (" onmouseover='webFXMenuHandler.overMenuItem(this)'" +
365                                 " onmouseout='webFXMenuHandler.outMenuItem(this)'") :
366                                 (
367                                         " onfocus='webFXMenuHandler.overMenuItem(this)'" +
368                                         (this.subMenu ?
369                                                 " onblur='webFXMenuHandler.blurMenu(this)'" :
370                                                 ""
371                                         )
372                                 )) +
373                         ">" +
374                         (this.subMenu ? "<img class='arrow' src='" + webfxMenuImagePath + "arrow.right.black.png'>" : "") +                             
375                         this.text + 
376                         "</a>";
377 };
378
379
380
381
382
383 /* Position functions */
384
385
386 function getInnerLeft(el) {
387
388         if (el == null) return 0;
389
390         if (ieBox && el == document.body || !ieBox && el == document.documentElement) return 0;
391
392         return parseInt( getLeft(el) + parseInt(getBorderLeft(el)) );
393
394 }
395
396
397
398 function getLeft(el, debug) {
399
400         if (el == null) return 0;
401
402         //if ( debug )
403         //  alert ( el.offsetLeft + ' - ' + getInnerLeft(el.offsetParent) );
404
405         return parseInt( el.offsetLeft + parseInt(getInnerLeft(el.offsetParent)) );
406
407 }
408
409
410
411 function getInnerTop(el) {
412
413         if (el == null) return 0;
414
415         if (ieBox && el == document.body || !ieBox && el == document.documentElement) return 0;
416
417         return parseInt( getTop(el) + parseInt(getBorderTop(el)) );
418
419 }
420
421
422
423 function getTop(el) {
424
425         if (el == null) return 0;
426
427         return parseInt( el.offsetTop + parseInt(getInnerTop(el.offsetParent)) );
428
429 }
430
431
432
433 function getBorderLeft(el) {
434
435         return ie ?
436
437                 el.clientLeft :
438
439                 ( khtml 
440                     ? parseInt(document.defaultView.getComputedStyle(el, null).getPropertyValue("border-left-width"))
441                     : parseInt(window.getComputedStyle(el, null).getPropertyValue("border-left-width")) 
442                 );
443
444 }
445
446
447
448 function getBorderTop(el) {
449
450         return ie ?
451
452                 el.clientTop :
453
454                 ( khtml 
455                     ? parseInt(document.defaultView.getComputedStyle(el, null).getPropertyValue("border-left-width"))
456                     : parseInt(window.getComputedStyle(el, null).getPropertyValue("border-top-width"))
457                 );
458
459 }
460
461
462
463 function opera_getLeft(el) {
464
465         if (el == null) return 0;
466
467         return el.offsetLeft + opera_getLeft(el.offsetParent);
468
469 }
470
471
472
473 function opera_getTop(el) {
474
475         if (el == null) return 0;
476
477         return el.offsetTop + opera_getTop(el.offsetParent);
478
479 }
480
481
482
483 function getOuterRect(el, debug) {
484
485         return {
486
487                 left:   (opera ? opera_getLeft(el) : getLeft(el, debug)),
488
489                 top:    (opera ? opera_getTop(el) : getTop(el)),
490
491                 width:  el.offsetWidth,
492
493                 height: el.offsetHeight
494
495         };
496
497 }
498
499
500
501 // mozilla bug! scrollbars not included in innerWidth/height
502
503 function getDocumentRect(el) {
504
505         return {
506
507                 left:   0,
508
509                 top:    0,
510
511                 width:  (ie ?
512
513                                         (ieBox ? document.body.clientWidth : document.documentElement.clientWidth) :
514
515                                         window.innerWidth
516
517                                 ),
518
519                 height: (ie ?
520
521                                         (ieBox ? document.body.clientHeight : document.documentElement.clientHeight) :
522
523                                         window.innerHeight
524
525                                 )
526
527         };
528
529 }
530
531
532
533 function getScrollPos(el) {
534
535         return {
536
537                 left:   (ie ?
538
539                                         (ieBox ? document.body.scrollLeft : document.documentElement.scrollLeft) :
540
541                                         window.pageXOffset
542
543                                 ),
544
545                 top:    (ie ?
546
547                                         (ieBox ? document.body.scrollTop : document.documentElement.scrollTop) :
548
549                                         window.pageYOffset
550
551                                 )
552
553         };
554
555 }
556
557
558 /* end position functions */
559
560 WebFXMenu.prototype.position = function (relEl, sDir) {
561         var dir = sDir;
562         // find parent item rectangle, piRect
563         var piRect;
564         if (!relEl) {
565                 var pi = this.parentMenuItem;
566                 if (!this.parentMenuItem)
567                         return;
568                 
569                 relEl = document.getElementById(pi.id);
570                 if (dir == null)
571                         dir = pi instanceof WebFXMenuButton ? "vertical" : "horizontal";
572                 //alert('created RelEl from parent: ' + pi.id);
573                 piRect = getOuterRect(relEl, 1);
574         }
575         else if (relEl.left != null && relEl.top != null && relEl.width != null && relEl.height != null) {      // got a rect
576                 //alert('passed a Rect as RelEl: ' + typeof(relEl));
577
578                 piRect = relEl;
579         }
580         else {
581                 //alert('passed an element as RelEl: ' + typeof(relEl));
582                 piRect = getOuterRect(relEl);
583         }
584
585         var menuEl = document.getElementById(this.id);
586         var menuRect = getOuterRect(menuEl);
587         var docRect = getDocumentRect();
588         var scrollPos = getScrollPos();
589         var pMenu = this.parentMenu;
590         
591         if (dir == "vertical") {
592                 if (piRect.left + menuRect.width - scrollPos.left <= docRect.width) {
593                         //alert('piRect.left: ' + piRect.left);
594                         this.left = piRect.left;
595                         if ( ! ie )
596                           this.left = this.left + 138;
597                 } else if (docRect.width >= menuRect.width) {
598                         //konq (not safari though) winds up here by accident and positions the menus all weird
599                         //alert('docRect.width + scrollPos.left - menuRect.width');
600
601                         this.left = docRect.width + scrollPos.left - menuRect.width;
602                 } else {
603                         //alert('scrollPos.left: ' + scrollPos.left);
604                         this.left = scrollPos.left;
605                 }
606                         
607                 if (piRect.top + piRect.height + menuRect.height <= docRect.height + scrollPos.top)
608
609                         this.top = piRect.top + piRect.height;
610
611                 else if (piRect.top - menuRect.height >= scrollPos.top)
612
613                         this.top = piRect.top - menuRect.height;
614
615                 else if (docRect.height >= menuRect.height)
616
617                         this.top = docRect.height + scrollPos.top - menuRect.height;
618
619                 else
620
621                         this.top = scrollPos.top;
622         }
623         else {
624                 if (piRect.top + menuRect.height - this.borderTop - this.paddingTop <= docRect.height + scrollPos.top)
625
626                         this.top = piRect.top - this.borderTop - this.paddingTop;
627
628                 else if (piRect.top + piRect.height - menuRect.height + this.borderTop + this.paddingTop >= 0)
629
630                         this.top = piRect.top + piRect.height - menuRect.height + this.borderBottom + this.paddingBottom + this.shadowBottom;
631
632                 else if (docRect.height >= menuRect.height)
633
634                         this.top = docRect.height + scrollPos.top - menuRect.height;
635
636                 else
637
638                         this.top = scrollPos.top;
639
640
641
642                 var pMenuPaddingLeft = pMenu ? pMenu.paddingLeft : 0;
643
644                 var pMenuBorderLeft = pMenu ? pMenu.borderLeft : 0;
645
646                 var pMenuPaddingRight = pMenu ? pMenu.paddingRight : 0;
647
648                 var pMenuBorderRight = pMenu ? pMenu.borderRight : 0;
649
650                 
651
652                 if (piRect.left + piRect.width + menuRect.width + pMenuPaddingRight +
653
654                         pMenuBorderRight - this.borderLeft + this.shadowRight <= docRect.width + scrollPos.left)
655
656                         this.left = piRect.left + piRect.width + pMenuPaddingRight + pMenuBorderRight - this.borderLeft;
657
658                 else if (piRect.left - menuRect.width - pMenuPaddingLeft - pMenuBorderLeft + this.borderRight + this.shadowRight >= 0)
659
660                         this.left = piRect.left - menuRect.width - pMenuPaddingLeft - pMenuBorderLeft + this.borderRight + this.shadowRight;
661
662                 else if (docRect.width >= menuRect.width)
663
664                         this.left = docRect.width  + scrollPos.left - menuRect.width;
665
666                 else
667
668                         this.left = scrollPos.left;
669         }
670 };