// planetcrap greasemonkey helper
// version 0.3 
// 2007-01-14
//
// --------------------------------------------------------------------
//
// This is a Greasemonkey user script.
//
// To install, you need Greasemonkey: http://greasemonkey.mozdev.org/
// Then restart Firefox and revisit this script.
// Under Tools, there will be a new menu item to "Install User Script".
// Accept the default configuration and install.
//
// To uninstall, go to Tools/Manage User Scripts,
// select "planetcrap", and click Uninstall.
//
// --------------------------------------------------------------------
//
// ==UserScript==
// @name          planetcrap
// @namespace     http://www.planetcrap.com/greasemonkey/
// @description   some helper functions for planetcrap
// @include       http://www.planetcrap.com/*
// @include       http://planetcrap.com/*
// @include       http://girlskissing.planetcrap.com/*
// ==/UserScript==

window.addEventListener("load", entryPoint(), false);

// script entry point
function entryPoint() {
  comments = getElementsByClass('comment-info');
  var numComments = comments.length;

  // install a quote button under each post info block
  for (i = 0; i < numComments; i++) {
    var quoteDiv = document.createElement("div");
    quoteDiv.setAttribute("class", "quote");
    quoteDiv.setAttribute("post_id", i);
    var quoteInnerBits = '<a href="#" onclick="return false;">quote</a>';
    quoteDiv.innerHTML = quoteInnerBits;
    comments[i].appendChild(quoteDiv);
  }

  // LESLIE'S CHANGES START
  var previewWindow = document.createElement("div");
  previewWindow.className = 'preview-window';
  previewWindow.style.padding = '6px';
  previewWindow.style.height = '200px';
  previewWindow.style.border = '1px solid #ccc';
  previewWindow.style.backgroundColor = '#eee';
  previewWindow.style.overflow = 'auto';
  previewWindow.innerHTML = '';
  document.forms[0].elements.namedItem('comment[body]').parentNode.appendChild(previewWindow);
	document.addEventListener('keyup', function(event) {
		if ('comment[body]' == event.target.name) {
			updatePreview(event.target.value);
		}
	}, true);
	document.addEventListener('focus', function(event) {
		if ('comment[body]' == event.target.name) {
			updatePreview(event.target.value);
		}
	}, true);
	// LESLIE'S CHANGES FINISH

  addGlobalStyle('.preview-window blockquote { color: #666 ! important; }');
  
  // register a click event handler to do the work of quoting when it happens
  document.addEventListener('click', function(event) {
    if (event.target.parentNode.className == "quote") {
      // find the clicked post id and name
      var postId = event.target.parentNode.getAttribute('post_id');
      var posterInfoChunk = event.target.parentNode.parentNode.textContent;
      processQuoteClick(postId, posterInfoChunk);
    }
  }, true);
}

// a click was triggered on a quote link, so take the text of that post and
// put it into the comment box
function processQuoteClick (postId, posterInfoChunk) {
  var splitChunk = posterInfoChunk.split('\n');
          
  var realPostId = splitChunk[1];
  var posterName = splitChunk[2];

  // grab the clicked posts comment body
  var posts = getElementsByClass('comment');
  var quotedPostBody = posts[postId].innerHTML;

  // change the html to craptags and strip sig etc..
  var cleanedQuoteBody = cleanQuote(quotedPostBody);
  
  // add the post number and name
  cleanedQuoteBody = realPostId + ' [b]' + posterName + '[/b]\n\n' + cleanedQuoteBody;
   
  // put the cleaned comment body into the comment field
  document.forms[0].elements.namedItem('comment[body]').value = cleanedQuoteBody;
  document.forms[0].elements.namedItem('comment[body]').focus();
}

function cleanQuote(text) {
  // strip the sig if there
  sigIndex = text.lastIndexOf('<div class="sign'); 
  if (sigIndex != -1) {
    text = text.substring(0, sigIndex);
  }

  // replace nbsp's
  text = text.replace(/&nbsp;/g, ' ');

  // replace <br>'s
  text = text.replace(/<br>/g, '\n');
  
  // handle inner quotes and pesky newlines
  text = text.replace(/<div class="quote">/g, '\[quote\]');
  text = text.replace(/<\/div>\n/g, '\[/quote\]');

  // replace links and emails with planetcrap markup
  text = text.replace(/<a href="/g, '\[url=');
  text = text.replace(/" target="_blank">/g, '\]');
  text = text.replace(/">/g, '\]');
  text = text.replace(/<\/a>/g, '\[/url\]');

  // replace bolds, italics, underlines and strikethrough
  text = text.replace(/<b>/g, '\[b\]');
  text = text.replace(/<\/b>/g, '\[/b\]');
  text = text.replace(/<i>/g, '\[i\]');
  text = text.replace(/<\/i>/g, '\[/i\]');
  text = text.replace(/<u>/g, '\[u\]');
  text = text.replace(/<\/u>/g, '\[/u\]');
  text = text.replace(/<s>/g, '\[s\]');
  text = text.replace(/<\/s>/g, '\[/s\]');

  // trim
  text = text.replace(/^\s+|\s+$/, '');

  // add some clean start and end quotes
  text = '[quote]' + text + '[/quote]\n';
  text = text.replace(/\n\[\/quote\]/g, '\[/quote\]');

  text = text.replace(/\n\n\n/g, '\n');

  GM_log(text);
  
  return text;
}

function getElementsByClass(searchClass,node,tag) {

	var classElements = new Array();
	
	if ( node == null ) {
	  node = document;
	}
	
	if ( tag == null ) {
		tag = '*';
	}

	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');

	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}


// LESLIE'S CHANGES START
function updatePreview(v)
{
	var preview = getElementsByClass('preview-window', null, 'div');
	if (preview) {
		var post = v;
		post = post.replace(/\n/g, '<br />');
		post = post.replace(/\[quote\]/g, '<blockquote>');
		post = post.replace(/\[\/quote\]/g, '</blockquote>');
		post = post.replace(/\[b\]/g, '<b>');
		post = post.replace(/\[\/b\]/g, '</b>');
		post = post.replace(/\[i\]/g, '<i>');
		post = post.replace(/\[\/i\]/g, '</i>');
		post = post.replace(/\[u\]/g, '<u>');
		post = post.replace(/\[\/u\]/g, '</u>');
		post = post.replace(/\[s\]/g, '<s>');
		post = post.replace(/\[\/s\]/g, '</s>');
		post = post.replace(/\[url=(.*?)\]/g, '<a href="$1" target="_blank">');
		post = post.replace(/\[\/url]/g, '</a>');
		preview[0].innerHTML = post;
	}
}
// LESLIE'S CHANGES FINISH

function addGlobalStyle(css) {
    var head, style;
    head = document.getElementsByTagName('head')[0];
    if (!head) { return; }
    style = document.createElement('style');
    style.type = 'text/css';
    style.innerHTML = css;
    head.appendChild(style);
}