// Costik Comments
// version 0.3
// 28 May 2005
// Copyright (c) 2005, Adam Vandenberg
// Released under the GPL license
// http://www.gnu.org/copyleft/gpl.html
//
// --------------------------------------------------------------------
//
// 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 "Costik Comments", and click Uninstall.
//
// ==UserScript==
// @name		Costik Comments
// @namespace	http://adamv.com/greases/
// @description 	Guerilla usability for Greg Costik's weblog
// @include	http://www.costik.com/*
// @include	http://costik.com/*
// ==/UserScript==

(function(){
function fix_links(){
	//	javascript:viewComments('111676872382092469')
	var pattern = /javascript\:viewComments\(\'(\d+)/;
	
	var links = document.getElementsByTagName("a")
	for (var i=0; i < links.length; i++){
		var link = links[i];
		
		// Get post ID
		var match = link.href.match(pattern);
		
		if (match){
			link.href = "http://www.costik.com/cgi-bin/sc2/comment.pl?action=start&id=01&item=" + match[1];
		}
	}
}

function fix_comments(){
	var body = document.getElementsByTagName("body")[0];
	var html = body.innerHTML;	

	var pField = /\n(\w*\:)/g;
	html = html.replace(pField, "<b>$1</b>")

	var pName = /\<b\>Name\:/g;
	html = html.replace(pName, "</div><div class='comment'><b>Name:")
	
	var pEnd = /\<br\>-----\<br\>\<br\>/g;
	html = html.replace(pEnd, "<br><hr size=1><br>")
	
	body.innerHTML = html;
	
	document.forms[0].elements["remember"].checked=true;
	var textarea = document.forms[0].elements["comment"];
	textarea.cols=80;
	textarea.rows=20;
}

if (location.href.indexOf("comment.pl") > -1)
	fix_comments()
else
	fix_links()
})();
