(function ($) {

Drupal.behaviors.quotebutton = {
  attach: function (context, settings) {
    // Don't even load unless there's a comment form.
    if ($('#comment-form').length > 0 || $('#privatemsg-new').length > 0) {

      /**
       * Get the selected text, as plain text. Mostly used to check if any text
       * (at all) is selected.
       */
      function getSelected() {
        var text;
        // Try several methods for different browsers/versions.
        if (window.getSelection) {
          text = window.getSelection();
        } else if (document.getSelection) {
          text = document.getSelection();
        } else if (document.selection) {
          text = document.selection.createRange().text;
        }
        return text.toString();
      }

      /**
       * Return the selected text, as HTML. This is a little more expensive, so
       * only use it when we need the formatting too.
       * TODO: jQuery-ify this.
       */
      function getSelectedHtml() {
        var html;
        // Couple methods to try, for different browsers.
        if (typeof window.getSelection != 'undefined') {
          var selection = window.getSelection();
          if (selection.rangeCount) {
            var container = document.createElement('div');
            for (var count = 0, length = selection.rangeCount; count < length; count++) {
              container.appendChild(selection.getRangeAt(count).cloneContents());
            }
            html = container.innerHTML;
          }
        } else if (typeof document.selection != 'undefined') {
          if (document.selection.type == 'Text') {
            html = document.selection.createRange().htmlText;
          }
        }
        return html;
      }

      /**
       * Hide the quote button, very simply.
       */
      function hideQuoteButton() {
        $('#quote-button-container').css('display', 'none');
      }

      /**
       * Voodoo to make the quote button appear on mouseup, only when something's
       * selected in the right area, but the mouse can end up anywhere.
       */
      function bindQuoteButton() {
        $('html').bind('mouseup.bindquote', function (event) {
          if (getSelected() != '') {
            // Don't want multiple buttons showing up ever.
            hideQuoteButton();
            $('#quote-button-container').css({'display': 'block', 'top': event.pageY - 30 + 'px', 'left': event.pageX + 'px'});
            // Don't get into any sort of infinite loops.
            $('html').unbind('mouseup.bindquote');
          }
        });
      }

      /**
       * Interact with ckeditor or just the comment textbox to paste the currently
       * selected text, with extra formatting for quotes/convenience.
       */
      function pasteText() {
        if (getSelected() != '') {
          Drupal.ckeditorInsertHtml('<blockquote><p>' + getSelectedHtml() + '</p></blockquote><p></p>');
          $('#edit-comment-body-und-0-value').val($('#edit-comment-body-und-0-value').val() + '<blockquote><p>' + getSelectedHtml() + '</p></blockquote><p></p>');
          $('#edit-body-value').val($('#edit-body-value').val() + '<blockquote><p>' + getSelectedHtml() + '</p></blockquote><p></p>');
        }
        hideQuoteButton();
      }

      // Bind all the mousedowns to the right actions, for every class/id we
  	// need to have quote buttons on.
      $('.forum-post-content').bind('mousedown', bindQuoteButton);
      $('.field-name-body').bind('mousedown', bindQuoteButton);
      $('.page-comment-reply .comment-text').bind('mousedown', bindQuoteButton);
      $('.privatemsg-message-body').bind('mousedown', bindQuoteButton);

      // Always hide the quote button on a click, when nothing's selected.
      $('html').bind('click', function () {
        if (getSelected() == '') {
          hideQuoteButton();
        }
      });

      // Finally, create the quote button and bind the paste to the click.
      $('body').append('<div id="quote-button-container" style="display: none; position: absolute;"><a id="quote-button" class="button" href="#comment-form">Quote</a></div>');
      $('#quote-button').bind('click', pasteText);
    }
  }
}

})(jQuery);;
(function ($) {

Drupal.behaviors.checksecure = {
  attach: function (context, settings) {
    // Don't even load unless there's a specific block found.
    if ($('#block-block-4').length > 0) {

      /**
	   * Test for HTTPS.
	   */
      function isSecure() {
        return window.location.protocol == 'https:';
      }

      // Make ourselves some text and ids to use.
      var secureid;
	  var securetext;
	  if (isSecure()) {
	    secureid = 'https-on';
		securetext = 'You\'re accessing this site securely!';
      } else {
        secureid = 'https-off';
		securetext = 'You\'re not currently accessing this site securely.';
      }
	  
      // And give the user some feedback about if they're using HTTPS or not.
	  if (window.secureAppended != true) {
        $('#block-block-4').append('<div id="' + secureid + '">' + securetext + '</div>');
				$("#block-block-4 a[href='https://wyrmrestaccord.net']").attr('href', 'https://' + window.location.hostname + window.location.pathname + window.location.search + window.location.hash);
      }
      window.secureAppended = true;
    }
  }
}

})(jQuery);;
(function ($) {

Drupal.behaviors.spoiler = {
  attach: function (context, settings) {
    $('.spoiler').wrap('<div class="spoiler-container-js" />');
    $('.spoiler').addClass('spoiler-js').removeClass('spoiler').hide();
    $('.spoiler-container-js').prepend(Drupal.t('<span class="spoiler-button"><span>Show</span><span class="spoiler-button-hide">Hide</span>.</span>')).children('.spoiler-button').click(function() {
      $(this).parent().children('.spoiler-js').toggle('normal');
			$('span', this).toggle();
    }).children('span.spoiler-button-hide').hide();
  }
}

})(jQuery);;

