$(document).ready(function() {

   var books = "Genesis :Exodus :Leviticus :Numbers :Deuteronomy :Joshua :Judges :Ruth :1 Samuel :2 Samuel :1 Kings :2 Kings :1 Chronicles :2 Chronicles :Ezra :Nehemiah :Esther :Job :Psalms :Proverbs :Ecclesiastes :Song of Songs :Isaiah :Jeremiah :Lamentations :Ezekiel :Daniel :Hosea :Joel :Amos :Obadiah :Jonah :Micah :Nahum :Habakkuk :Zephaniah :Haggai :Zechariah :Malachi :Matthew :Mark :Luke :John :Acts :Romans :1 Corinthians :2 Corinthians :Galatians :Ephesians :Philippians :Colossians :1 Thessalonians :2 Thessalonians :1 Timothy :2 Timothy :Titus :Philemon :Hebrews :James :1 Peter :2 Peter :1 John :2 John :3 John :Jude :Revelation ".split(":");
   $("#search").autocomplete(books,{matchContains:true});
   
   $("#search").focus();

   $("span.searchBtn").click(function (event) {
      event.preventDefault();
  
      var search = $("#search").val();
      doLookup(search);

  });
  
  $('a.verseLink').live('click', function(event) {
    event.preventDefault();
    var search = $(this).attr('id'); 
    doLookup(search);
  });
  
  $("#lookupForm").bind("submit", function(event) {
    event.preventDefault();
    
    var search = $("#search").val();
    doLookup(search);
    
  });

});

function doLookup(search) {

  //var search = $("#search").val();
  var bibleType = $("#bibleType").val();
  
  search = $.trim(search);
  bibleType = $.trim(bibleType);
      
  if (search != '')
  {
        $.get("/ajax/passage.php",
            { search: search, bibleType: bibleType },
              function(xml) {
                searchResponse(xml);
            },
            "xml");
  }
  
  $("#search").val("");

};

function searchResponse(xml) {
  
  var passageHTML = '';
  
  var version = $(xml).find('version').text().toLowerCase();
  var copyright = $(xml).find('copy').text();
  var timing = $(xml).find('time').text();

  $(xml).find("passage").each(function()
   {
            
    var book = $(this).find("book").text();
    var chapter = $(this).find("chapter").text();
    var bookUrl = book.replace(" ","-").toLowerCase();
    var startVerse = $(this).find("startverse").text();
    var endVerse = $(this).find("endverse").text();
    
    var chapterLink = '<a class="verseLink" id="' + book + ' ' + chapter + '" href="/'+version+'/'+bookUrl+'/'+chapter+'">chapter ' + chapter + '</a>';
    
    // AddThis Share Button --------------------------------------
    var shareButton = '';
    
    if (startVerse == endVerse)
    {
      shareButton = '<a href="http://www.addthis.com/bookmark.php?v=250&amp;username=verses" class="addthis_button" addthis:url="http://vers.es/'+version+'/'+bookUrl+'/'+chapter+'/'+startVerse+'" addthis:title="'+book+' '+chapter+':'+startVerse+' - Vers.es" addthis:description="Lookup any Bible Verses!"><img src="/img/share.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a>';
    }
    else
    {
      shareButton = '<a href="http://www.addthis.com/bookmark.php?v=250&amp;username=verses" class="addthis_button" addthis:url="http://vers.es/'+version+'/'+bookUrl+'/'+chapter+'/'+startVerse+'/'+endVerse+'" addthis:title="'+book+' '+chapter+':'+startVerse+'-'+endVerse+' - Vers.es" addthis:description="Lookup any Bible Verses!"><img src="/img/share.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a>';
    }
    // End AddThis Share Button -----------------------------------
    
    passageHTML += '<li><h1>' + book + ' ' + chapterLink + '</h1><p>'+shareButton+' ';
                
    $(this).find("verse").each(function()
    {
      var verseNum = $(this).attr('num');
      var verseLink = '<a class="verseLink" id="' + book + ' ' + chapter + ':' + verseNum + '" href="/'+version+'/'+bookUrl+'/'+chapter+'/'+verseNum+'">' + verseNum + '</a>';
      passageHTML += '<sup>[' + verseLink + ']</sup>' + $(this).text() + ' ';
                
    });
                
    passageHTML += '<br /><br /><span class="lookupSig">Took <b>' + timing + '</b> seconds! (<a href="/bible/'+version+'">'+version+'</a>)</span></p></li>';
            
   });
            
   $("#verseList").prepend(passageHTML);
   
   // Little Hack to Reload AddThis JS because it doesn't like
   //  to bind after AJAX requests... Reloading the script
   //  is pretty inefficient though, need a better solution than this...
   var atScript = 'http://s7.addthis.com/js/250/addthis_widget.js?domready=1#username=verses';
  
   if ( window.addthis ) {
        window.addthis = null;
   }
  
   $.getScript( atScript );
   //-------------------------------------------------------------

      
};


