function Menu(id)
{
     var domain = "http://www.traciepetersonbooks.com";
     var imagepath = "images";
     var minOpacity = 0;
     var maxOpacity = 85;
     
     var menuwidth = 0;
     var paneId = "menuPane_" + id;
     var fadeTimer = null;
     var hideTimer = null;
     var pane;
     var linkArray = new Array();
     var FADE_INACTIVE = 0;
     var FADE_IN = 1;
     var FADE_OUT = 2;
     var fadeMode = 0;
     
     this.AnimateFade = function(dir)
     {
          _animateFade(dir);
     }
     
     this.AddLink = function(href, text)
     {
          var link = new Link();
          link.Href = domain + "/" + href;
          link.Text = text;
          _addLink(link);
     }
     
     this.AddLinkObject = function(link)
     {
          _addLink(link);
     }
     
     this.Hide = function()
     {
          _hide();
     }
     
     this.Show = function(anchor, x, y)
     {
          _show(anchor, x, y);
     }

     this.Width = function(width)
     {
          _width(width);
     }
     
     function _addHandler(target, eventName, handler)
     {
          if (target.addEventListener) target.addEventListener(eventName, handler, false);
          else if (target.attachEvent) target.attachEvent("on" + eventName, handler);
          else target["on" + eventName] = handler;
     }
     
     function _addLink(link)
     {
          linkArray[linkArray.length] = link;
     }
     
     function _animateFade(dir)
     {
          var isMinMax = false;
          
          if (pane == undefined) pane = _getPane();
          if (pane.style.opacity == undefined)
          {
               pane.style.opacity = 0;
          }
          
          var op = (pane.style.opacity * 100);

          if(dir == FADE_IN) 
          {
               op += 10;
               if(op >= maxOpacity)
               {
                    op = maxOpacity;
                    isMinMax = true;
               }
          }
          else if(dir == FADE_OUT)
          {
               op -= 10;
               if(op <= minOpacity)
               {
                    op = minOpacity;
                    isMinMax = true;
               }
          }
          
          pane.style.opacity = (op/100);
          pane.style.filter = "alpha(opacity='" + op.toFixed() + "')";
          pane.style.display = (op != 0) ? "block" : "none";

          if(isMinMax)
          {
               clearTimeout(fadeTimer);
          }
          else
          {
               fadeTimer = setTimeout(id + ".AnimateFade(" + dir + ")", 20);
          }
     }

     function _getObjectPosition(obj)
     {
          var useWindow = false;
          var coordinates = new Object();
          var x = 0;
          var y = 0;

          x = _getPageOffsetLeft(obj);
          y = _getPageOffsetTop(obj);
          
          coordinates.x = x;
          coordinates.y = y; 
          return coordinates;
     }

     function _getObjectWindowPosition(obj)
     {
          var coordinates = _getObjectPosition(obj);
          var x = 0;
          var y = 0;
          if (document.getElementById)
          {
               if (isNaN(window.screenX))
               {
                    x = coordinates.x - document.body.scrollLeft + window.screenLeft;
                    y = coordinates.y - document.body.scrollTop + window.screenTop;
               }
               else
               {
                    x = coordinates.x + window.screenX + (window.outerWidth - window.innerWidth) - window.pageXOffset;
                    y = coordinates.y + window.screenY + (window.outerHeight - 24 - window.innerHeight) - window.pageYOffset;
               }
          }
          else if (document.all)
          {
               x = coordinates.x - document.body.scrollLeft + window.screenLeft;
               y = coordinates.y - document.body.scrollTop + window.screenTop;
          }
          coordinates.x = x;
          coordinates.y = y;
          return coordinates; 
     }
     
     function _getPageOffsetLeft(obj)
     {
          var ol = obj.offsetLeft;
          while ((obj = obj.offsetParent) != null)
          {
               ol += obj.offsetLeft;
          }
          return ol;
     }

     function _getPageOffsetTop(obj)
     {
          var ot = obj.offsetTop;
          while ((obj = obj.offsetParent) != null)
          {
               ot += obj.offsetTop;
          }
          return ot;
     }
     
     function _getPane()
     {
          var pane = document.getElementById(paneId);
          if (pane == undefined)
          {
               pane = document.createElement("div");
               pane.setAttribute("id", paneId);
               pane.className = "menuPane";
               pane.style.width = menuwidth + "px";
               _addHandler(pane, "mouseover", _resetHideTimer);
               _addHandler(pane, "mouseout", _hide);
               document.body.appendChild(pane);
          }
          return pane;
     }

     function _getWindowOffsetLeft(obj)
     {
          return _getPageOffsetLeft(obj) - document.body.scrollLeft;
     }

     function _getWindowOffsetTop(obj)
     {
          return _getPageOffsetTop(obj) - document.body.scrollTop;
     }
     
     function _hide()
     {
          if (hideTimer != undefined) clearTimeout(hideTimer);
          if (fadeTimer != undefined) clearTimeout(fadeTimer);
          
          if (pane == undefined) pane = _getPane();
          hideTimer = setTimeout(id + ".AnimateFade(" + FADE_OUT + ")", 500);
     }
     
     function _resetHideTimer()
     {
          if (hideTimer != undefined) clearTimeout(hideTimer);
          _animateFade(FADE_IN);
     }
     
     function _show(anchor, offx, offy)
     {
          if (hideTimer != undefined) clearTimeout(hideTimer);
          if (fadeTimer != undefined) clearTimeout(fadeTimer);

          if (pane == undefined) pane = _getPane();
          if (!offx) offx = 0;
          if (!offy) offy = 0;

          var htmlSource = '';
          for(var x = 0; x < linkArray.length; x++)
          {
               htmlSource += '<a href="' + linkArray[x].Href + '">' + linkArray[x].Text + '</a>';
          }
          pane.innerHTML = htmlSource;

          _setPaneLocation(anchor, offx, offy);
          
          _animateFade(FADE_IN);
          return false;
     }
     
     function _setPaneLocation(obj, x, y)
     {
          if (pane == undefined) pane = _getPane();
          var coords = _getObjectPosition(obj);
          
          pane.style.left = (coords.x + x) + "px";
          pane.style.top = (coords.y + y) + "px";
     }
     
     function _width(width)
     {
          menuwidth = width;
     }
}

function Link()
{
     this.Href = "";
     this.Text = "";
}

String.prototype.Trim = function()
{
     return this.replace(/^\s+|\s+$/g, "");
}

