var _shoretelBorderRadius = new Object({
	rules:[],
	init:function () {
		var a = _shoretelBorderRadius;
		if (navigator.userAgent.match(/MSIE [78]\.0/)) {
			for (var i = 0; i < document.styleSheets.length; i++) {
				try {
					for (var j = 0; j < document.styleSheets[i].rules.length; j++) {

						var rule = document.styleSheets[i].rules[j];
						var isRound = rule.style['border-radius'] || 0;
						if (isRound) {
							var tR = tL = bR = bL = false;

							var t = isRound.split(/\s+/);
							if (t[t.length - 1] === '') t.pop();
							switch (t.length) {
								case 1:
									tL = tR = bR = bL = t[0];
									break;
								case 2:
									tL = bR = t[0];	tR = bL = t[1];
									break;
								case 3:
									tL = t[0]; tR = bL = t[1]; bR = t[2];
									break;
								case 4:
									tL = t[0]; tR = t[1]; bR = t[2]; bL = t[3];
									break;
								default:
									break;
							}
							if (tR || tL || bR || bL) {
								var selectors = rule.selectorText.replace(/\s+$/,'').split(/,\s*/);
								for (var k = 0; k < selectors.length; k++) {
									a.rules[selectors[k]] = [tL,tR,bR,bL];
								}
							}
						}
					}
				} catch (e) {
					alert("Error: " + e);
				}
			}
			if (a.rules) {
				document.getElementById("body").style.position = "relative";
				a.findRoundedObjects();
			}
		}
	},
	findRoundedObjects:function () {
		var a = this;

		for (var i in a.rules) {
			var getBy = i.split(/\s+/);
			var found = [document];
			for (var j = 0; j < getBy.length; j++) {
				var newFound = [];
				for (var f in found) {
					if (getBy[j].charAt(0) == "#") {
						newFound.push(document.getElementById(getBy[j].substr(1)));
					} else {
						var searchTag = getBy[j], searchClass = "";
						if (searchTag.indexOf(".") !== -1) {
							var s = searchTag.split(".");
							searchTag = s[0];
							searchClass = s[1];
						}
						var tags = found[f].getElementsByTagName(searchTag);
						for (var k = 0; k < tags.length; k++) {
							if (!searchClass || tags[k].className.indexOf(searchClass) !== -1) newFound.push(tags[k]);
						}
					}
				}
				found = newFound;
			}
			for (var b in found) {
				if (found[b]) a.makeRound(found[b], a.rules[i]);
			}
		}
	},
	makeRound:function (obj,rule) {
		var a = this, parent = obj, pBackground = "transparent";

		// Find Parent's background to use as outside filler
		while (pBackground == "transparent") {
			try {
				parent = parent.parentNode;
				pBackground = parent.currentStyle["backgroundColor"];
			} catch (e) {
				pBackground = "#ffffff";
			}
		}

		// Move all the contents of the existing box into a fragment
		var fragment = document.createDocumentFragment();
		while (obj.firstChild) fragment.appendChild(obj.removeChild(obj.firstChild));

		// Meat of rounded hamburger
		var newMid = document.createElement("div");

		newMid.style.overflow = "hidden";
		newMid.style.paddingTop = obj.currentStyle["paddingTop"];
		newMid.style.paddingRight = obj.currentStyle["paddingRight"];
		newMid.style.paddingBottom = obj.currentStyle["paddingBottom"];
		newMid.style.paddingLeft = obj.currentStyle["paddingLeft"];
		newMid.style.borderTopWidth = obj.currentStyle["borderTopWidth"];
		newMid.style.borderTopStyle = obj.currentStyle["borderTopStyle"];
		newMid.style.borderTopColor = obj.currentStyle["borderTopColor"];
		newMid.style.borderRightWidth = obj.currentStyle["borderRightWidth"];
		newMid.style.borderRightStyle = obj.currentStyle["borderRightStyle"];
		newMid.style.borderRightColor = obj.currentStyle["borderRightColor"];
		newMid.style.borderBottomWidth = obj.currentStyle["borderBottomWidth"];
		newMid.style.borderBottomStyle = obj.currentStyle["borderBottomStyle"];
		newMid.style.borderBottomColor = obj.currentStyle["borderBottomColor"];
		newMid.style.borderLeftWidth = obj.currentStyle["borderLeftWidth"];
		newMid.style.borderLeftStyle = obj.currentStyle["borderLeftStyle"];
		newMid.style.borderLeftColor = obj.currentStyle["borderLeftColor"];
		
		newMid.appendChild(fragment);

		obj.style.position = "relative";
//		obj.style.zIndex = 0;
		obj.style.paddingTop = obj.style.paddingRight = obj.style.paddingBottom = obj.style.paddingLeft = "0";
		obj.style.borderTopWidth = obj.style.borderRightWidth = obj.style.borderBottomWidth = obj.style.borderLeftWidth = "0";
		obj.appendChild(newMid);

		// Top of rounded hamburger
		if (rule[0] != 0) {
			var topLeftCorner = document.createElement("div");
			topLeftCorner.style.position = "absolute";
			topLeftCorner.style.overflow = "hidden";
			topLeftCorner.style.top = topLeftCorner.style.left = 0;
			topLeftCorner.style.width = topLeftCorner.style.height = rule[0];
			topLeftCorner.appendChild(a.getCorner("tl", rule[0], pBackground, newMid.currentStyle["borderRightWidth"], newMid.currentStyle["borderRightColor"]));
			obj.appendChild(topLeftCorner);
		}
		if (rule[1] != 0) {
			var topRightCorner = document.createElement("div");
			topRightCorner.style.position = "absolute";
			topRightCorner.style.overflow = "hidden";
			topRightCorner.style.top = topRightCorner.style.right = 0;
			topRightCorner.style.width = topRightCorner.style.height = rule[1];
			topRightCorner.appendChild(a.getCorner("tr", rule[1], pBackground, newMid.currentStyle["borderLeftWidth"], newMid.currentStyle["borderLeftColor"]));
			obj.appendChild(topRightCorner);
		}

		// Bottom of rounded hamburger
		if (rule[2] != 0) {
			var bottomRightCorner = document.createElement("div");
			bottomRightCorner.style.position = "absolute";
			bottomRightCorner.style.overflow = "hidden";
			bottomRightCorner.style.bottom = bottomRightCorner.style.right = 0;
			bottomRightCorner.style.width = bottomRightCorner.style.height = rule[2];
			bottomRightCorner.appendChild(a.getCorner("br", rule[2], pBackground, newMid.currentStyle["borderLeftWidth"], newMid.currentStyle["borderLeftColor"]));
			obj.appendChild(bottomRightCorner);
		}
		if (rule[3] != 0) {
			var bottomLeftCorner = document.createElement("div");
			bottomLeftCorner.style.position = "absolute";
			bottomLeftCorner.style.overflow = "hidden";
			bottomLeftCorner.style.bottom = bottomLeftCorner.style.left = 0;
			bottomLeftCorner.style.width = bottomLeftCorner.style.height = rule[3];
			bottomLeftCorner.appendChild(a.getCorner("bl", rule[3], pBackground, newMid.currentStyle["borderRightWidth"], newMid.currentStyle["borderRightColor"]));
			obj.appendChild(bottomLeftCorner);
		}
	},
	getCorner:function (which, ru, c, bw, bc) {
		var a = this, r = parseFloat(ru) * 10;

		var newCorner = document.createDocumentFragment();

		if (bw == "medium") bw = 0;
		var b = r - parseInt(bw);
		for (y = 0; y < r; y++) {

			var y1 = (y + 1 >= b) ? -1 : Math.floor(Math.sqrt(Math.pow(b, 2) - Math.pow(y + 1, 2))) - 1;
			var y3 = (y + 1 >= r) ? -1 : Math.floor(Math.sqrt(Math.pow(r, 2) - Math.pow(y + 1, 2))) - 1;
			var iWide = y1 + 1;
			var bWide = y3 - y1;
			bw = bWide + "px";

			var pixel = document.createElement("div");
			pixel.style.overflow = "hidden";
			pixel.style.backgroundColor = c; //colors[y];
			pixel.style.width = (r - iWide - bWide) + "px";
			pixel.style.height = "1px";
			pixel.style.position = "absolute";
			pixel.style.borderRightStyle = pixel.style.borderLeftStyle = "solid";
			pixel.style.borderTopWidth = pixel.style.borderRightWidth = pixel.style.borderBottomWidth = pixel.style.borderLeftWidth = 0;

			if (which == "tl") {
				pixel.style.bottom = y;
				pixel.style.left = 0;
				pixel.style.borderRightWidth = bw;
				pixel.style.borderRightColor = bc;
			} else if (which == "tr") {
				pixel.style.bottom = y;
				pixel.style.right = 0;
				pixel.style.borderLeftWidth = bw;
				pixel.style.borderLeftColor = bc;
			} else if (which == "br") {
				pixel.style.top = y;
				pixel.style.right = 0;
				pixel.style.borderLeftWidth = bw;
				pixel.style.borderLeftColor = bc;
			} else if (which == "bl") {
				pixel.style.top = y;
				pixel.style.left = 0;
				pixel.style.borderRightWidth = bw;
				pixel.style.borderRightColor = bc;
			}
			newCorner.appendChild(pixel);
		}
		return newCorner;
	}
});
startListening(window,"load",_shoretelBorderRadius.init);
