var PokkariCaptcha = Class.create();
PokkariCaptcha.prototype = {
	initialize: function() {
		this.uuid = new UUID();
	},

	getHTML: function() {
		return "<input type='hidden' name='captchas_guid' value='" + this.uuid + "' />\n" +
			"<img src='/captchas/" + this.uuid + "' />\n";
	},
	
	write: function() {
		var html = this.getHTML();
		document.write(html);
	},

	captchaIsAppended: function(element) {
		// TODO FIXME Naive approach, use a className to make sure.
		var imgs = element.getElementsByTagName("img");
		return (imgs && imgs.length);
	},

	appendChildTo: function(element) {
		if (element && element.appendChild) {
			if (!this.captchaIsAppended(element)) {
				var hidden = document.createElement("input");
				hidden.type = "hidden";
				hidden.name = "captchas_guid";
				hidden.value = this.uuid;
				element.appendChild(hidden);

				var img = document.createElement('img');
				img.style.width = "172px";
				img.style.height = "64px";

				// Give IE time to figure out its DOM handling before showing the image.
				if (document.all) {
					img.src = "/blank.gif";
					window.captchaImage = img;
					window.setTimeout("window.captchaImage.src = '/captchas/"+this.uuid+"'; window.captchaImage = null;",50);
				}
				else {
					img.src = "/captchas/" + this.uuid;
				}

				element.appendChild(img);
			}
		}
	}
}

PokkariCaptcha.include = function() {
	var captcha = new PokkariCaptcha();
	captcha.write();
}

PokkariCaptcha.appendChildTo = function(element) {
	var captcha = new PokkariCaptcha();
	captcha.appendChildTo(element);
}
