(function ($) {

  Drupal.HavenContent = {};

  /**
   * Translation, writing edit
   */
  Drupal.behaviors.creative_edit = {
    attach: function () {
      // shortcut to add acknowledgements
      if ($('#edit-hf-public-notes').length > 0) {
        if ($('#edit-hf-public-notes-und-0-format .admin-actions').length < 1) {
          $('#edit-hf-public-notes-und-0-format').append('<div class="admin-actions"><a href="#" class="action acknowledge">Add acknowledgement</a></div>');
        }
        $('a.action').click(function () {
          if ($(this).hasClass('acknowledge')) {
            var val = $('#edit-hf-public-notes-und-0-value').val();
            $('#edit-hf-public-notes-und-0-value').val(val + '<div class="acknowledgements"><span class="label">Acknowledgements:</span> <span class="value"></span></div>')
          }
          return false;
        });
      }
    }
  };

// Content node form functions
  Drupal.behaviors.translation_edit = {
    attach: function () {

      if ((!$('body').hasClass('page-node-edit') || !$('body').hasClass('node-type-translation')) &&
              (!$('body').hasClass('page-node-add') || !$('body').hasClass('page-node-add-translation'))) {
        return;
      }

      // initialize
      Drupal.HavenContent.translationTypeSelect($('#edit-hf-tax-translation-categories input:checked').siblings('label').text());

      // click actions
      $('#edit-hf-tax-translation-categories input').click(function () {
        Drupal.HavenContent.translationTypeSelect($(this).siblings('label').text());
      });

    }
  };

// Content node form functions
  Drupal.behaviors.writing_edit = {
    attach: function () {

      if ((!$('body').hasClass('page-node-edit') || !$('body').hasClass('node-type-writing')) &&
              (!$('body').hasClass('page-node-add') || !$('body').hasClass('page-node-add-writing'))) {
        return;
      }

      // initialize
      Drupal.HavenContent.writingTypeSelect($('#edit-hf-tax-writing-categories input:checked').siblings('label').text());

      // click actions
      $('#edit-hf-tax-writing-categories input').click(function () {
        Drupal.HavenContent.writingTypeSelect($(this).siblings('label').text());
      });

    }
  };

  // translation view behaviors
  Drupal.behaviors.creative_view = {
    attach: function () {
      if ($('body').hasClass('node-type-book') && $('h1#page-title').text().match('|')) {
        var parts = $('h1#page-title').text().split('|');
        $('h1#page-title').text(parts[0].trim()).after('<h2 class="chapter-title">' + parts[1] + '</h2>');
      }
    }
  };

  /**
   * Display/hide comments form
   */
  Drupal.behaviors.translation_comments = {
    attach: function () {
      if ($('#comments').length > 0) {
        // initialize
        if (!window.location.hash) {
          $('#comments form.comment-form').hide();
          $('#comments h2.comment-form').wrap('<a href="#" class="action show-comment-form"></a>');


          // click actions
          $('#comments a.show-comment-form').click(function () {
            $('#comments form.comment-form').show(400);
            return false;
          });

        }

      }
    }
  };

  /**
   * Glossary popup functionality
   */
  Drupal.behaviors.translation_glossary_popup = {
    attach: function () {
      if ($('#block-system-main .glossary-popup').length > 0) {
        // add block for glossary functions
        var block_html = '<section id="block-haven-content-translation-glossary" class="block block-haven-content contextual-links-region"><div class="block-inner clearfix"><h2 class="block-title">Glossary</h2><div class="block-content content"><div class="item-list"><ul><li class="even first"><a href="#" class="action popup-toggle popup-on">Turn glossary pop-ups off</a></li></ul></div></div></div></section>';
        $('.region-sidebar-first .region-inner').append(block_html);

        // click functionality
        $('#block-haven-content-translation-glossary a.popup-toggle').click(function () {
          if ($(this).hasClass('popup-on')) {
            $(this).removeClass('popup-on').addClass('popup-off');
            $('#block-system-main .glossary-popup').addClass('glossary-popup-off');
            $(this).text('Turn glossary pop-ups on');
          }
          else {
            $(this).removeClass('popup-off').addClass('popup-on');
            $('#block-system-main .glossary-popup').removeClass('glossary-popup-off');
            $(this).text('Turn glossary pop-ups off');
          }
          return false;
        });

      }

    }
  };


  /**
   * Helper function for Drupal.GetfitMinutesSingle.ajaxifyNav().
   *
   * @param {jQuery} $today
   *  The current selected form
   * @param {jQuery} $selected
   *  The new selected form
   * @returns {Boolean}
   */
  Drupal.HavenContent.translationTypeSelect = function (selected) {
    if (selected.trim() === 'Lyrics' || selected.trim() === 'Poetry') {
      $('#edit-hf-private-image').hide();
      $('#edit-hf-hlid').hide();
      $('#edit-hf-private-file').hide();
      $('#edit-hf-file-link').hide();
      $('#edit-hf-authors').show();
      $('#edit-hf-shorthand').show();
    }
    else {
      if (selected.trim() === 'Novel') {
        $('#edit-hf-private-file').hide();
        $('#edit-hf-file-link').hide();
        $('#edit-hf-private-image').hide();
      }
      else {
        $('#edit-hf-file-link').show();
        $('#edit-hf-private-file').show();
        $('#edit-hf-private-image').show();
      }
      $('#edit-hf-hlid').show();
      $('#edit-hf-shorthand').hide();
      $('#edit-hf-authors').hide();
    }
  };

  /**
   * Helper function for Drupal.GetfitMinutesSingle.ajaxifyNav().
   *
   * @param {jQuery} $today
   *  The current selected form
   * @param {jQuery} $selected
   *  The new selected form
   * @returns {Boolean}
   */
  Drupal.HavenContent.writingTypeSelect = function (selected) {
    if (selected.trim() === 'Fanfiction') {
      $('#edit-hf-tax-haven-fandoms').show();
      $('#edit-hf-characters').show();
      $('#edit-hf-pairings').show();
    }
    else {
      $('#edit-hf-tax-haven-fandoms').hide();
      $('#edit-hf-characters').hide();
      $('#edit-hf-pairings').hide();
    }
  };


})(jQuery);
;
window.CKEDITOR_BASEPATH = '/sites/all/libraries/ckeditor/';;
