function BookList(url)
{
     var imagepath = "/buythisbook/images";
     var xml = null;
     var isReady = false;
     var requestTimer = null;
     var books = new Array();
     var pane;
     var tmpAnchor;
     var tmpId;
     var lightboxback;

     this.GetBookList = function()
     {
          var xmlLoader = new Xml();
          xmlLoader.SendRequest(url, handleRequest);
          requestTimer = window.setTimeout(processXml, 1000);
     }

     function handleRequest(req)
     {
          xml = req.responseXML;
     }

     function processXml()
     {
          if (xml == undefined)
          {
               requestTimer = window.setTimeout(processXml, 100);
          }
          
          var bookNodes = xml.getElementsByTagName("book");
          for (x = 0; x < bookNodes.length; x++)
          {
               var book = new Book();
               var id = bookNodes[x].getAttribute("id");
               book.Id = id;
               book.Title = bookNodes[x].getAttribute("title");
               book.ThumbNail = bookNodes[x].getAttribute("thumbnail");
               books[id] = book;
               
               var linkNodes = bookNodes[x].getElementsByTagName("booklink");
               for (y = 0; y < linkNodes.length; y++)
               {
                    var link = new Link();
                    var linktxt = "";
                    link.Vendor = linkNodes[y].getAttribute("vendor");
                    link.Link = linkNodes[y].childNodes[0].nodeValue;

                    if (window.ActiveXObject)
                    {
                         linktxt = linkNodes[y].childNodes[0].nodeValue.Trim();
                    }
                    else
                    {
                         linktxt = linkNodes[y].childNodes[1].data.Trim();
                    }

                    link.Link = linktxt;
                    book.Links[link.Vendor] = link;
               }
          }
          isReady = true;
     }

     this.SubmitSearch = function(action)
     {
          var city = document.getElementById("city");
          var state = document.getElementById("state");
          var zip = document.getElementById("zip");

          if (city.value == "City") city.value = "";
          if (state.value == "State") state.value = "";
          if (zip.value == "Zip") zip.value = "";

          var url = action + "?q=christian+bookstore&near=" + city.value + "+" + state.value + "+" + zip.value;

          window.open(url, "", "");

          return false;
     }

     this.Blur = function(input)
     {
          if (input.value == "")
          {
               switch (input.id)
               {
                    case "city":
                         input.value = "City";
                         break;
                    case "state":
                         input.value = "State";
                         break;
                    case "zip":
                         input.value = "Zip";
                         break;
               }
          }
     }

     this.Focus = function(input)
     {
          switch (input.id)
          {
               case "city":
                    if (input.value == "City") input.value = "";
                    break;
               case "state":
                    if (input.value == "State") input.value = "";
                    break;
               case "zip":
                    if (input.value == "Zip") input.value = "";
                    break;
          }
     }

     this.PopValues = function(anchor, id)
     {
          if (!isReady || id != undefined)
          {
               if (pane == undefined)
               {
                    pane = document.getElementById("bookPaneContainer");
               }

               var bookTitle = "";
               var bookThumb = "";
               var bookAnchor_amazon_href = "";
               var bookAnchor_barnes_href = "";
               var bookPane_booksamillion_href = "";
               var bookPane_borders_href = "";
               var bookPane_christianbook_href = "";

               var book = books[id];
               if (book != undefined)
               {
                    bookTitle = book.Title;
                    bookThumb = book.ThumbNail;

                    var link_amazon = book.Links["amazon"];
                    var link_barnes = book.Links["barnes"];
                    var link_booksamillion = book.Links["booksamillion"];
                    var link_borders = book.Links["borders"];
                    var link_christianbook = book.Links["christianbook"];

                    if (link_amazon != undefined) bookAnchor_amazon_href = link_amazon.Link;
                    if (link_barnes != undefined) bookAnchor_barnes_href = link_barnes.Link;
                    if (link_booksamillion != undefined) bookPane_booksamillion_href = link_booksamillion.Link;
                    if (link_borders != undefined) bookPane_borders_href = link_borders.Link;
                    if (link_christianbook != undefined) bookPane_christianbook_href = link_christianbook.Link;

                    _getLightboxBack();
                    lightboxback.style.display = "block";
                    pane.style.display = "block";
               }
               else
               {
                    //alert("Book not found.");
               }
          }

          var htmlSource = '<b class="bookPane">' +
                    '<b class="bookPane1"><b></b></b>' +
                    '<b class="bookPane2"><b></b></b>' +
                    '<b class="bookPane3"></b>' +
                    '<b class="bookPane4"></b>' +
                    '<b class="bookPane5"></b></b>' +
                    '<div id="bookPaneBackground">' +
                         '<div id="bookPane">' +
                              '<div id="bookPane_header"><p>Buy This Book:</p><a href="javascript:buyBook.Close();" id="closebutton">X</a></div>' +
                              '<div id="bookPane_imgbox">' +
                                   '<div id="bookPane_thumbnail" style="float: left;">' +
                                        '<img src="' + bookThumb + '" id="bookPane_thumbnail_image" alt="' + bookTitle + '" />' +
                                   '</div>' +
                                   '<div id="bookPane_logos" style="float: left;">' +
                                        '<a href="' + bookAnchor_amazon_href + '" id="bookPane_amazon" target="_blank"><img src="' + imagepath + '/buythisbook_logo_amazon.gif" /></a>' +
                                        '<a href="' + bookAnchor_barnes_href + '" id="bookPane_barnes" target="_blank"><img src="' + imagepath + '/buythisbook_logo_barnsnoble.gif" /></a>' +
                                        '<br />' +
                                        '<a href="' + bookPane_booksamillion_href + '" id="bookPane_booksamillion" target="_blank"><img src="' + imagepath + '/buythisbook_logo_booksamillion.gif" /></a>' +
                                        '<a href="' + bookPane_borders_href + '" id="bookPane_borders" target="_blank"><img src="' + imagepath + '/buythisbook_logo_borders.gif" /></a>' +
                                        '<br />' +
                                        '<a href="' + bookPane_christianbook_href + '" id="bookPane_christianbook" target="_blank"><img src="' + imagepath + '/buythisbook_logo_christianbook.gif" /></a>' +
                                   '</div>' +
                                   '<div style="clear: all;"></div>' +
                              '</div>' +
                              '<div id="bookPane_searchheader"><p>Find a Local Christian Bookstore:</p></div>' +
                              '<form action="http://maps.google.com/maps" onsubmit="return buyBook.SubmitSearch(this.action);">' +
                                   '<div style="text-align: center;">' +
                                        '<input type="hidden" name="q" value="christian bookstore" />' +
                                        '<input type="text" id="city" value="City" name="city" size="20" onfocus="buyBook.Focus(this)" onblur="buyBook.Blur(this)" />,' +
                                        '<input type="text" id="state" value="State" name="state" size="2" onfocus="buyBook.Focus(this)" onblur="buyBook.Blur(this)" maxlength="2" />' +
                                        '<input type="text" id="zip" value="Zip" name="zip" size="5" onfocus="buyBook.Focus(this)" onblur="buyBook.Blur(this)" maxlength="5" /><br />' +
                                        '<input type="image" src="' + imagepath + '/buythisbook_button_findstore.gif" />&nbsp;&nbsp;<img src="' + imagepath + '/buythisbook_logo_google.gif" />' +
                                   '</div>' +
                              '</form>' +
                         '</div>' +
                    '</div>' +
                    '<b class="bookPane">' +
                    '<b class="bookPane5"></b>' +
                    '<b class="bookPane4"></b>' +
                    '<b class="bookPane3"></b>' +
                    '<b class="bookPane2"><b></b></b>' +
                    '<b class="bookPane1"><b></b></b></b>';

          pane.innerHTML = htmlSource;

          _setPaneLocation(anchor);
          return false;
     }
     
     this.Close = function()
     {
          if (pane == undefined)
          {
               pane = document.getElementById("bookPaneContainer");
          }
          _getLightboxBack();
          lightboxback.style.display = "none";
          pane.style.display = "none";
     }
     
     function _setPaneLocation(obj)
     {
          if (pane == undefined)
          {
               pane = document.getElementById("bookPaneContainer");
          }
          var coords = _getObjectPosition(obj);
          
          pane.style.left = coords.x + "px";
          pane.style.top = (coords.y + 20) + "px";
     }

     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 _getLightboxBack()
     {
          if (lightboxback == undefined)
          {
               lightboxback = document.createElement("div");
               lightboxback.setAttribute("id", "bookPaneLightboxBack");
               document.body.appendChild(lightboxback);
          }
          else
          {
               lightboxback = document.getElementById("bookPaneLightboxBack");
          }
     }
     
     function _getPageOffsetLeft(el)
     {
          var ol = el.offsetLeft;
          while ((el = el.offsetParent) != null)
          {
               ol += el.offsetLeft;
          }
          return ol;
     }

     function _getWindowOffsetLeft(el)
     {
          return _getPageOffsetLeft(el) - document.body.scrollLeft;
     }

     function _getPageOffsetTop(el)
     {
          var ot = el.offsetTop;
          while ((el = el.offsetParent) != null)
          {
               ot += el.offsetTop;
          }
          return ot;
     }

     function _getWindowOffsetTop(el)
     {
          return _getPageOffsetTop(el) - document.body.scrollTop;
     }
}

function Book()
{
     this.Id = 0;
     this.Title = "";
     this.ThumbNail = "";
     this.Links = new Array();
}

function Link()
{
     this.Vendor = "";
     this.Link = "";
}

String.prototype.Trim = function()
{
     return this.replace(/^\s+|\s+$/g, "");
}