var noun_type_bible_passage = {
  _name: "Bible passage",

  buildBookSuggestion: function( book, suggestions ) {
    var favourites = {
      'Genesis': ['12:1-3'],
      'Isaiah': ['53'],
      'John': ['3:16'],
      'Romans': ['8:28'],
      'Colossians': ['1:28-29'],
      '1 Timothy': ['3:16'],
      '2 Timothy': ['3:16'],
      '1 Peter': ['3:18']
    };
    var chapter = '1';
    verses = favourites[book];
    if (verses) {
      for (var i = 0; i < verses.length; i ++) {
        suggestions.push( CmdUtils.makeSugg(book + ' ' + verses[i]) );
      }
    }
    suggestions.push( CmdUtils.makeSugg(book + ' 1') );  
  },

  suggest: function( text, html ) {
    
    var suggestions  = [];

    var bible_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 Solomon', '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']
    var multi_books = ['Samuel', 'Kings', 'Chronicles', 'Corinthians', 'Thessalonians', 'Timothy', 'Peter', 'John']    

    if (text == '') {
      // The golden oldie
      suggestions.push( CmdUtils.makeSugg('John 3:16') ); 
    } else {
      for (var i =0; i < bible_books.length; i ++) {
        var book = bible_books[i];
        if (book.indexOf(text) == 0) {          
          noun_type_bible_passage.buildBookSuggestion(book, suggestions); 
        }
      }
      for (var i = 0; i < multi_books.length; i ++) {
        if (multi_books[i].indexOf(text) == 0) {
          var book_count = (multi_books[i] == 'John' ? 3 : 2);
          for (var j = 1; j <= book_count; j ++) {
            noun_type_bible_passage.buildBookSuggestion(j + ' ' + multi_books[i], suggestions); 
          }
        }
      }
    }

    if (suggestions.length == 0) {
      // Offer the text itself
      suggestions.push( CmdUtils.makeSugg(text) );
    }

    // Return a list of input objects, limited to at most five:
    return suggestions.splice(0, 5);
  }
}

CmdUtils.CreateCommand({
  names: ["esv url", "bible url"],
  homepage: "http://www.geero.net/",
  author: { name: "Andy Geers", email: "andy@geero.net"},
  description: "Inserts URL of a Bible passage in the ESV.",
  help: "If you're in an editable text area, inserts the URL to the selected Bible passage in the ESV.",

  arguments: [{role: "object", nountype: noun_type_bible_passage, label: "passage"}],

  _url: function(passage){    
    var passageUrl = 'http://www.biblegateway.com/passage/?';
    var params = {
      search: passage,
      version: 47
    };

    return passageUrl + jQuery.param( params );
  },

  preview: function(pBlock, args) {
    if (args.object.text == '') {
      pBlock.innerHTML = _('Inserts Bible Gateway URL for a passage in the ESV');
      return;
    }

    var previewTemplate = "Searching Bible Gateway for <b>${query}</b> ...";
    var previewData = {query: args.object.text};
    pBlock.innerHTML = _(previewTemplate, previewData);

    var params = {
      search: args.object.text,
      interface: 'print',
      version: 47
    }
    var passageURL = 'http://www.biblegateway.com/passage/index.php?' + jQuery.param( params );    
 
    jQuery.ajax({
      type: "GET",
      url: passageURL,
      dataType: "html",
      error: function() {
        pBlock.innerHTML = _("<i>Error retreiving summary.</i>");
      },
      success: function(searchResponse) {
        startBlock = '<div class="publisher-info-inset">';
        if ((ind = searchResponse.indexOf(startBlock)) > -1) {
          middle = searchResponse.substr(ind + startBlock.length);
          endBlock = '<div id="result-options-info2"';
          if ((ind = middle.indexOf(endBlock)) > -1) {
            middle = middle.substr(0, ind);
          }
          pBlock.innerHTML = middle;
        } else {
          pBlock.innerHTML = _('Passage not found');
        }
      }
    });
   
  },

  execute: function( args ) {
    CmdUtils.setSelection( this._url(args.object.text) );
  }
})






