	function clone(what) {
	    var img = document.createElement('IMG');
	    img.src = what.src;
	    img.title = what.title;
	    img.className = what.className;
	    img.alt = what.alt;

	    // Copy the style.
	    for(keyVar in what.style) {
	    	// Skip numeric styles ... thats something new :)
	    	if(isNaN(keyVar)) {
	    		// Some styles can't be set, only read so we wrap the whole thing in a try-catch block
	    		try {
	    			eval('img.style.'+keyVar+' = what.style.'+keyVar+';');
	    		}
	    		catch(e) {

	    		}
	    	}
	    }
	    return img;
	}


	function Captionize() {

		this.init = function() {
			var obj = new Captionize();
			var images = document.getElementsByTagName('IMG');
			for(var p = 0; p < images.length ; p++) {
				if(images[p].className.indexOf('captionize') > -1) {
					obj.addCaption(images[p]);
				}
			}
		}

		this.addCaption = function(obj) {
			var div = document.createElement('DIV');

			var img = clone(obj);

			// Opera fix
			img.style.height = obj.height + 'px';
			img.style.width = obj.width + 'px';
			// End Opera fix
			div.style.height = obj.height + 'px';
			div.style.width = img.style.width;

			if(img.style.cssFloat) {
				div.style.cssFloat = img.style.cssFloat;
				img.style.cssFloat = '';
			}
			if(img.style.styleFloat) {
				div.style.styleFloat = img.style.styleFloat;
				img.style.styleFloat = '';
			}

			// IE 6 fix ... for some reasons it adds 3px to the rendered margin?
			if(navigator.userAgent.indexOf('MSIE') > -1) {
				div.style.marginLeft = ((parseInt(obj.style.marginLeft) > 0) ? parseInt(obj.style.marginLeft) - 3 : 0) + 'px';
				div.style.marginRight = ((parseInt(obj.style.marginRight) > 0) ? parseInt(obj.style.marginRight) - 3 : 0) + 'px';

				div.style.marginTop = obj.style.marginTop;
				div.style.marginBottom = obj.style.marginBottom;
			}
			// End IF fix
			else div.style.margin = obj.style.margin;

			img.style.margin = '';

			div.appendChild(img);

			var caption = document.createElement('DIV');
			caption.className = 'caption';
			var p = document.createElement('P');
			p.className = 'caption';

			p.innerHTML = obj.title;

			caption.style.width = obj.width + 'px';


			div.style.position = 'relative';
			div.appendChild(caption);
			div.appendChild(p);
			obj.parentNode.replaceChild(div, obj);

			// For the width = width + padding difference in FF / IE
			var diff = p.clientWidth - img.clientWidth;
			if(diff > 0) p.style.width = p.clientWidth - (diff*2) + 'px';

			caption.style.height = p.clientHeight + 'px';
		}
	}

	var captionize = new Captionize();
	if(window.addEventListener) window.addEventListener('load', captionize.init, true)
	else window.attachEvent('onload', captionize.init, true)
