

var DTBSimpleMenu = {
        upArrowElm   :  function(theMenu) {
                            var idName = "mnu_"+theMenu.name+"_up";
                            var elm = document.getElementById(idName);
                            return elm;
                     },
        downArrowElm :  function(theMenu) {
                            var idName = "mnu_"+theMenu.name+"_down";
                            var elm = document.getElementById(idName);
                            return elm;
                     },
        setArrowImg  :  function(arrowElm, imageSrc) {
                         var elm = this.isAnchor(arrowElm)?arrowElm.firstChild : arrowElm;
                         elm.src = imageSrc;
                  },
        bindScroll : function(theMenu) {
                       var elm = this.upArrowElm(theMenu);
                       if (elm) {
                           if (this.isAnchor(elm)) {
                               elm.setAttribute("href",null);
                           }
                           var thisMenuLib = this;
                           elm.onclick = function() {
                                   try {
                                           thisMenuLib.scrollUp(theMenu.name);
                                           return false;
                                   } catch (e) {
                                           alert("Exception "+e);
                                           return false;
                                   }
                           };
                       }              
                       elm = this.downArrowElm(theMenu);
                       if (elm) {
                           if (this.isAnchor(elm)) {
                              elm.setAttribute("href",null);
                           }
                           elm.onclick = function() {
                                    try {
                                           thisMenuLib.scrollDown(theMenu.name);
                                           return false;
                                    } catch (e) {
                                           alert("Exception "+e);
                                           return false;
                                        }
                                     };
                       }              
                   },
       register      : function(menuData) {
                           this.menu[menuData.name] = menuData;
                           var theMenu = this.menu[menuData.name];
                           this.bindScroll(theMenu);
                        },
        menu          : {},
        canScrollDown : function(menuName) {
                            var theMenu = this.menu[menuName];
                            return ((theMenu.offset+theMenu.itemsPerPage)<theMenu.items.length);
                        },
        scrollDown    : function(menuName) {
                          var theMenu = this.menu[menuName];
                          if ( ! this.canScrollDown(menuName)) {
                             return;
                          }
                          theMenu.offset++;
                          this.draw(theMenu);
                        },
        canScrollUp   : function(menuName) {
                           var theMenu = this.menu[menuName];
                           return (theMenu.offset>=1);
                        },
        scrollUp      : function(menuName) {
                          var theMenu = this.menu[menuName];
                          if ( ! this.canScrollUp(menuName)) {
                             return;
                          }
                          theMenu.offset--;
                          this.draw(theMenu);
                      },
        draw     : function(theMenu) {
                      var displayedItems = ((theMenu.offset+theMenu.itemsPerPage)>theMenu.items.length) ? theMenu.itemsPerPage - (theMenu.items.length-theMenu.offset) : theMenu.itemsPerPage;
                      var idName;
                      var elm;
                      for (var i=0; i<displayedItems; i++) {
                         idName = "mnu_"+theMenu.name+"_"+i;
                         elm = document.getElementById(idName);
                         elm.href=theMenu.items[i+theMenu.offset].url;
                         elm.firstChild.data=theMenu.items[i+theMenu.offset].text;
                      }
                      for (;i<theMenu.itemsPerPage; i++) {
                         idName = "mnu_"+theMenu.name+"_"+i;
                         elm = document.getElementById(idName);
                         elm.href="";
                         elm.firstChild.data=" ";
                      }
                      var enabled = this.canScrollDown(theMenu.name) ? "enabled" : "disabled"; 
                      this.setArrowImg(this.downArrowElm(theMenu), theMenu.downArrow[enabled]);
                      enabled = this.canScrollUp(theMenu.name) ? "enabled" : "disabled"; 
                      this.setArrowImg(this.upArrowElm(theMenu), theMenu.upArrow[enabled]);
                      this.updateOffsetWhenNotBusy(theMenu, theMenu.offset);
                   },
        timerOffset   : -1,           
        updateOffsetWhenNotBusy : function(theMenu, offset) {
                       if (this.timerOffset > -1) {
                           this.timerOffset = offset;
                           return;
                       }
                       this.timerOffset = offset;
                       var thisMenuLib = this;
                       var timerFunction = function() {
                                               if (theMenu.offset==thisMenuLib.timerOffset || thisMenuLib.timerOffset<0) {
                                                   if (thisMenuLib.timerOffset>-1) {
                                                       thisMenuLib.updateOffset(theMenu);
                                                   }
                                                   thisMenuLib.timerOffset=-1;
                                               } else {
                                                   setTimeout(timerFunction, 2000);
                                               }
                                          };
                       setTimeout(timerFunction, 2000);
                   },
        updateOffset : function(theMenu) {
                           var thisMenuLib = this;
                           var pageViewerUpdaterUrl = theMenu.updateOffsetUrl;
                           pageViewerUpdaterUrl = pageViewerUpdaterUrl.replace("--REPLACE-WITH-OFFSET--",theMenu.offset);
                           var now = "uid=" + new Date().getTime();
                           pageViewerUpdaterUrl += "&" + now;  // Stopping browser from caching result of request
                           (
                               new DtbXmlHttpRequest(
                                      function(domObj,url, responseText) {
                                          //\n's in JSON string, when evaluated will create errors in IE
                                          var result = null;
                                          if (responseText) {
                                              result = responseText.replace(/[\n\r]/g,"");
                                          }
                                          if (result && result.trim()!="") {
                                             result = eval('('+result+')');
                                             var returnedOffset = result.pageViewerForm.offset;
                                             if (returnedOffset != theMenu.offset) {
                                                 theMenu.offset = returnedOffset;
                                                 thisMenuLib.draw(theMenu);
                                             }
                                          }
                                            ;
                                      }
                           )).load(pageViewerUpdaterUrl);
                   },
        isAnchor : function(elm) {
                      return (undefined!==elm) && (elm!==null) && (elm.tagName=="A");
                   }
};
