/*jslint indent: 2 */
/*global $: false, togglbutton: false, createTag: false*/

'use strict';

// Jira 2017 board sidebar
togglbutton.render('#ghx-detail-view [spacing] h1:not(.toggl)', {observe: true}, function () {
  var link, description,
    rootElem = $('#ghx-detail-view'),
    container = createTag('div', 'jira-ghx-toggl-button'),
    titleElem = $('[spacing] h1', rootElem),
    numElem = $('[spacing] a', rootElem),
    projectElem = $('.bgdPDV');

  description = titleElem.textContent;
  if (numElem !== null) {
    description = numElem.textContent + " " + description;
  }

  link = togglbutton.createTimerLink({
    className: 'jira2017',
    description: description,
    buttonType: 'minimal',
    projectName: projectElem && projectElem.textContent
  });

  container.appendChild(link);
  numElem.parentNode.appendChild(container);
});

// Jira 2017 sprint modal
togglbutton.render('#ghx-issue-fragment [spacing] a img:not(.toggl)', {observe: true}, function () {
  var link, description,
    rootElem = $('#ghx-issue-fragment'),
    container = createTag('div', 'jira-ghx-toggl-button'),
    titleElem = $('[spacing] h1', rootElem),
    numElem = $('[spacing] a', rootElem),
    projectElem = $('.bgdPDV');

  description = titleElem.textContent;
  if (numElem !== null) {
    description = numElem.textContent + " " + description;
  }

  link = togglbutton.createTimerLink({
    className: 'jira2017',
    description: description,
    buttonType: 'minimal',
    projectName: projectElem && projectElem.textContent
  });

  container.appendChild(link);
  numElem.parentNode.appendChild(container);
});

// Jira 2017 issue page
togglbutton.render('.issue-header-content:not(.toggl)', {observe: true}, function (elem) {
  var link, description,
    numElem = $('#key-val', elem),
    titleElem = $('#summary-val', elem) || "",
    projectElem = $('.bgdPDV');

  if (!!titleElem) {
    description = titleElem.textContent;
  }

  if (numElem !== null) {
    if (!!description) {
      description = " " + description;
    }
    description = numElem.textContent + description;
  }

  link = togglbutton.createTimerLink({
    className: 'jira2017',
    description: description,
    projectName: projectElem && projectElem.textContent
  });

  $('.issue-link').parentElement.appendChild(link);
});

// Jira pre-2017
togglbutton.render('#ghx-detail-issue:not(.toggl)', {observe: true}, function (elem) {
  var link, description,
    container = createTag('div', 'ghx-toggl-button'),
    titleElem = $('[data-field-id="summary"]', elem),
    numElem = $('.ghx-fieldname-issuekey a'),
    projectElem = $('.ghx-project', elem);

  description = titleElem.textContent;
  if (numElem !== null) {
    description = numElem.textContent + " " + description;
  }

  link = togglbutton.createTimerLink({
    className: 'jira',
    description: description,
    projectName: projectElem && projectElem.textContent
  });

  container.appendChild(link);
  $('#ghx-detail-head').appendChild(container);
});

// Jira pre-2017
togglbutton.render('.issue-header-content:not(.toggl)', {observe: true}, function (elem) {
  var link, description, ul, li,
    numElem = $('#key-val', elem),
    titleElem = $('#summary-val', elem) || "",
    projectElem = $('#project-name-val', elem);

  if (!!titleElem) {
    description = titleElem.textContent;
  }

  if (numElem !== null) {
    if (!!description) {
      description = " " + description;
    }
    description = numElem.textContent + description;
  }

  link = togglbutton.createTimerLink({
    className: 'jira',
    description: description,
    projectName: projectElem && projectElem.textContent
  });

  ul = createTag('ul', 'toolbar-group');
  li = createTag('li', 'toolbar-item');
  li.appendChild(link);
  ul.appendChild(li);
  $('.toolbar-split-left').appendChild(ul);
});

//Confluence
togglbutton.render('#title-heading:not(.toggl)', {observe: true}, function (elem) {
  var link, description,
    titleElem = $('[id="title-text"]', elem);

  description = titleElem.textContent;

  link = togglbutton.createTimerLink({
    className: 'confluence',
    description: description
  });

  $('#title-text').appendChild(link);
});
@@ -841,32 +841,33 @@
 ontainer);%0A%7D);%0A%0A
+%0A
 // Jira 2017 spr
@@ -861,17 +861,17 @@
 Jira 201
-7
+8
  sprint 
@@ -880,175 +880,406 @@
 dal%0A
-togglbutton.render('#ghx-issue-fragment %5Bspacing%5D a img:not(.toggl)', %7Bobserve: true%7D, function () %7B%0A  var link, description,%0A    rootElem = $('#ghx-issue-fragment'),%0A
+// Using the h1 as selector to make sure that it will only try to render the button%0A// after Jira has fully rendered the modal content%0Atogglbutton.render('div%5Brole=%22dialog%22%5D h1:not(.toggl)', %7Bobserve: true%7D, function (titleElem) %7B%0A  // need to all the way up until we find the modal wrapper%0A  var rootElem = titleElem.offsetParent.offsetParent.offsetParent.offsetParent%0A%0A  var link, description,%0A  
     
@@ -1339,51 +1339,8 @@
 ,%0A  
-  titleElem = $('%5Bspacing%5D h1', rootElem),%0A
     
@@ -1369,32 +1369,34 @@
  a', rootElem),%0A
+  
     projectElem 
@@ -4353,28 +4353,29 @@
 ext').appendChild(link);%0A%7D);
+%0A

Fix toggl button inside the new jira modal