/* Unfortunately there are few unique classes and no ids, which makes targeting specific info rather unwieldy and brittle. If any of the classes or structure changes this will have to be adjusted manually. */

function parseKana(el) {
  return (el.find('.f-dropdown li:nth-of-type(2)').text().match(/(?!\b)\W+$/) || [])[0];
}

function parseKanji(el) {
  return el.find('.text').text().trim();
}

function parseMeanings(el) {
  var defs = [];
  var defNodes = el.find('.meaning-wrapper').not('.meaning-tags:contains("Wikipedia") + .meaning-wrapper');

  defNodes.each(function(i, el) {
    // only numbered meanings have this class followed by meaning text
    var text = $(el).find('.meaning-definition-section_divider + span').text().trim();

    if (text.length) {
      defs.push(text + parseInfo(el));
    }
  });

  return defs;
}

function parseTags(el) {
  return el.find('.meaning-tags:first').text().split(', ').map(function(x) { return x.trim(); });
}

function parseSentences(el) {
  var sentences = [];
  el.find('.sentence').each(function(i, s) {
    sentences.push({
      "JA": $(s).find('.unlinked').text(),
      "EN": $(s).find('.english').text()
    });
  })
  return sentences;
}

function parseInfo(el) {
  var text = [];
  $(el).find('.supplemental_info .sense-tag').each(function(i, el) {
    return text.push($(el).text());
  });
  return text.length ? ' (' + text.join(', ') + ')' : '';
}

function buildJRE($entry) {
  var sentences = parseSentences($entry),
      tags = parseTags($entry),
      kana = parseKana($entry),
      ja = parseKanji($entry),
      en = parseMeanings($entry);

  // this is the format copied to clipboard
  var jre = {
    Tags: tags,
    JA: ja,
    Kana: kana || ja,
    EN: en,
    Sentences: sentences
  };

    /*
    // Alternatively you could do something like
       var jre = ja + (kana ? ' [' + kana + ']' : '') + ' - ' + en[0] + '\n';
    // which would output a text string (ending with a new line) :
    // '賞金 [しょうきん] - prize; monetary award'
    */

  return jre;
}

function sendText(text) {
  chrome.extension.sendMessage({ text: text });
}

var $entries = $('.concept_light');

function hoverOn() {
  $(this).css({
    boxShadow: '0 0 4px 0 tomato',
    cursor: 'pointer'
  });
}

function hoverOff() {
 $(this).css({
    boxShadow: 'none'
  });
}

$entries.hover(hoverOn, hoverOff);

// TODO: append a stylish looking box top right that fades in showing what was copied, and fades out again.
// box says when jisho2json is ready too

$('body').on('click', '.concept_light', function() {
  var o = buildJRE($(this));
  console.log('Copied:\n', o);
  sendText(JSON.stringify(o));
});

console.log('jisho2json loaded');

@@ -575,24 +575,55 @@
 on(i, el) %7B%0A
+    var tags = parseTags(el);%0A%0A
     // only 
@@ -812,61 +812,178 @@
 ush(
-text + parseInfo(el));%0A    %7D%0A  %7D);%0A%0A  return defs;%0A%7D%0A
+%7B%0A        meanings: text.split('; '),%0A        notes: parseInfo(el),%0A        tags: tags,%0A      %7D);%0A    %7D%0A  %7D);%0A%0A  return defs;%0A%7D%0A%0A// returns array -%3E %5B'Noun', 'Suru Verb'%5D
 %0Afun
@@ -1006,30 +1006,37 @@
 el) %7B%0A  
-return el.find
+var tags = $(el).prev
 ('.meani
@@ -1046,14 +1046,8 @@
 tags
-:first
 ').t
@@ -1051,16 +1051,81 @@
 ).text()
+;%0A  return (/Wiki%7COther/gi.test(tags) %7C%7C tags === '') ? %5B%5D : tags
 .split('
@@ -1579,51 +1579,8 @@
 text
-.length ? ' (' + text.join(', ') + ')' : ''
 ;%0A%7D%0A
@@ -1654,40 +1654,8 @@
 y),%0A
-      tags = parseTags($entry),%0A
     
@@ -1810,24 +1810,8 @@
 = %7B%0A
-    Tags: tags,%0A
     
@@ -2504,19 +2504,14 @@
 ish 
-looking box
+toastr
  top
@@ -2732,16 +2732,53 @@
 n', o);%0A
+  console.log(JSON.stringify(o.EN));%0A
   sendTe

add tags and info directly linked to meaning