window.onload = function() {
	addShadows();
}

function addShadows() {
	var tagNames = ["img", "div"];
	var tags = [];
	for (var i = 0 ; i < tagNames.length; i++) {
		var tagsC = document.getElementsByTagName(tagNames[i]);
		for (var j = 0; j < tagsC.length; j++) {
			tags.push(tagsC[j]);
		}
	}
	for (var i = 0; i < tags.length; i++) {
		if (hasClass(tags[i], 'shadow')) {
			createTags(tags[i]);
		}
	}
}
function hasClass(el, cl) {
	if (!el.className)
		return false;

	var c = el.className.split(' ');
	for (var i = 0; i < c.length; i++) {
		if (c[i] == cl)
			return true;
	}
	return false;
}
function createTags(el) {
	var shadowDiv = document.createElement('div');
	var width;
	var height;


	if (el.width) {
		width = el.width;
	} else {
		width = el.clientWidth;
	}
	if (el.height) {
		height = el.height
	} else {
		height = el.clientHeight;
	}
	shadowDiv.style.height = height + 16 + "px";
	shadowDiv.style.width = width + 16 + "px";
	shadowDiv.className = 'shadowDiv';


	var br = document.createElement('div');
	br.className = "br";
	shadowDiv.appendChild(br);

	var bl = document.createElement('div');
	bl.className = "bl";
	shadowDiv.appendChild(bl);

	var tl = document.createElement('div');
	tl.className = "tl";
	shadowDiv.appendChild(tl);

	var tr = document.createElement('div');
	tr.className = "tr";
	shadowDiv.appendChild(tr);

	var b = document.createElement('div');
	b.className = "b";
	b.style.width = width - 16 + "px";
	shadowDiv.appendChild(b);

	var t = document.createElement('div');
	t.className = "t";
	t.style.width = width - 16 + "px";
	shadowDiv.appendChild(t);

	var l = document.createElement('div');
	l.className = "l";
	l.style.height = height - 16 + "px";
	shadowDiv.appendChild(l);

	var r = document.createElement('div');
	r.className = "r";
	r.style.height = height - 16 + "px";
	shadowDiv.appendChild(r);

	if (el.parentNode.tagName == "A") {
		shadowDiv.appendChild(el.parentNode.cloneNode(true));
		el.parentNode.parentNode.replaceChild(shadowDiv, el.parentNode);
	} else {
		shadowDiv.appendChild(el.cloneNode(el.tagName != "IMG")); // Argument for cloneNode is deep copy.
		el.parentNode.replaceChild(shadowDiv, el);
	}
}
