/*
* blockquotes.js
*
* Simon Willison, 20th December 2002
*
* Explanation: 
*   http://simon.incutio.com/archive/2002/12/20/#blockquoteCitations
* Inspired by Adrian Holovaty: 
*   http://www.holovaty.com/blog/archive/2002/12/20/0454
* Alternative implementation of the same idea by Paul Hammond: 
*   http://www.paranoidfish.org/boxes/2002/12/20/
*/

function extractBlockquoteCitations() {
	var quotes = document.getElementsByTagName('blockquote');

	for (i = 0; i < quotes.length; i++) {
		var cite = quotes[i].getAttribute('cite');
		var paras = quotes[i].getElementsByTagName('p');

		if (paras.length > 0) {
			var l = paras.length - 1;

			if (paras[l].className != 'cite') {
				linkText = 'Source';
			} else {
				linkText = paras[l].firstChild.nodeValue;
			}

			if (paras[l].className == 'cite' && linkText != 'Source') { paras[l].className = 'cite' }

			if (cite) {
				if (linkText != 'Source' ) { quotes[i].removeChild(paras[l]); }

				newlink = document.createElement('a');
				newlink.setAttribute('href', cite);
				newlink.appendChild(document.createTextNode(linkText));
				newdiv = document.createElement('p');
				newdiv.className = 'cite';
				newdiv.appendChild(newlink);
				quotes[i].appendChild(newdiv);
			}
		}
	}
}

/* http://www.sovavsiti.cz/css/abbr.html */

function styleAbbr() {
	var oldBodyText, newBodyText, reg;
	var isIE = (document.all) ? true:false;

	if (isIE) {
		oldBodyText = document.body.innerHTML;
		reg = /<abbr([^>]*)>([^<]*)<\/abbr>/g;
		newBodyText = oldBodyText.replace(reg, '<abbr $1><span class=\"abbr\" $1>$2</span></abbr>');
		document.body.innerHTML = newBodyText;
	}
}

// Copyright (c) 1996-1997 Athenia Associates.
// http://www.webreference.com/js/
// License is granted if and only if this entire
// copyright notice is included. By Tomer Shiran.

function getCookie(name) {
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("&" + prefix);

	if (begin == -1) {
		begin = dc.indexOf(prefix);
	} else {
		begin += 1;
	}

	if (begin == -1) {
		return null;
	} else {
		var end = document.cookie.indexOf("&", begin);

		if (end == -1) { end = dc.length; }

		return unescape(dc.substring(begin + prefix.length, end));
	}
}

function formCookies() {
	var divses = document.getElementsByTagName('div');

	for (i = 0; i < divses.length; i++) {
		if(divses[i].getAttribute('id') == 'commentform') {
			var commentName = getCookie('CommentName');
			var commentEmail = getCookie('CommentEmail');
			var commentLink = getCookie('CommentLink');

			if (commentName) {
				document.getElementById('name').value = commentName;
				document.getElementById('cookieme').checked = true;
			}

			if (commentEmail) { document.getElementById('email').value = commentEmail; }

			if (commentLink) { document.getElementById('link').value = commentLink; }
		}
	}
}

window.onload = function() {
//	extractBlockquoteCitations();
	styleAbbr();
	formCookies();
};