/**
 * makes rounded corners
 * 
 * issues: for ie, floated elements need to have a specific width, defined in style-attribute
 */
function getCurrentStyle(element, property) {
  var exception, value;
  try {
    var cs=document.defaultView.getComputedStyle(element,'');
    value=cs.getPropertyValue(property);
  }
  catch(exception) {
	  if(element.currentStyle) {
		  property=property.split("-");
		  for(i=1; i<property.length; i++) property[i]=property[i].toUpperCase();
		  property=property.join("");
		  value=element.currentStyle.getAttribute(property);
	   }
    }
  
  return value;
}

function roundCorners() {
	var divs = document.getElementsByTagName("div");
	for (var i=0; i<divs.length; i++) {
		if (divs[i].className.indexOf('rounded') != -1) {
			var newDiv = document.createElement("div");
			newDiv.style.marginRight = getCurrentStyle(divs[i], "margin-right");
			newDiv.style.marginLeft = getCurrentStyle(divs[i], "margin-left");
			newDiv.style.marginTop = getCurrentStyle(divs[i], "margin-top");
			newDiv.style.marginBottom = getCurrentStyle(divs[i], "margin-bottom");
			if (divs[i].style.width) newDiv.style.width = divs[i].style.width; // ie messes up on floated elements without specific width
			if (newDiv.style.cssFloat != undefined) {
				newDiv.style.cssFloat = getCurrentStyle(divs[i], "float");				
			}
			else {
				newDiv.style.styleFloat = getCurrentStyle(divs[i], "style-float");
			}
								
			var backgroundImage = getCurrentStyle(divs[i], "background-image").replace(/"/g,"");
			var backgroundColor = getCurrentStyle(divs[i], "background-color");
			var opacity = getCurrentStyle(divs[i], "opacity");
			var filter = getCurrentStyle(divs[i], "filter");
			if (filter != "") filter = "filter:" + filter;
			newDiv.innerHTML = 
				"<span style=\"display:block;overflow:hidden;margin:0 6px;height:1px;background:"+backgroundColor+" "+backgroundImage+";opacity:"+opacity+";"+filter+"\"></span>" +
				"<span style=\"display:block;overflow:hidden;margin:0 4px;height:1px;background:"+backgroundColor+" "+backgroundImage+";opacity:"+opacity+";"+filter+"\"></span>" +
				"<span style=\"display:block;overflow:hidden;margin:0 3px;height:1px;background:"+backgroundColor+" "+backgroundImage+";opacity:"+opacity+";"+filter+"\"></span>" +
				"<span style=\"display:block;overflow:hidden;margin:0 0px;height:1px;background:"+backgroundColor+" "+backgroundImage+";opacity:"+opacity+";"+filter+"\"></span>" +
				"<span style=\"display:block;overflow:hidden;margin:0 0px;height:1px;background:"+backgroundColor+" "+backgroundImage+";opacity:"+opacity+";"+filter+"\"></span>" +
				"<span style=\"display:block;overflow:hidden;margin:0 3px;height:1px;background:"+backgroundColor+" "+backgroundImage+";opacity:"+opacity+";"+filter+"\"></span>" +
				"<span style=\"display:block;overflow:hidden;margin:0 4px;height:1px;background:"+backgroundColor+" "+backgroundImage+";opacity:"+opacity+";"+filter+"\"></span>" +
				"<span style=\"display:block;overflow:hidden;margin:0 6px;height:1px;background:"+backgroundColor+" "+backgroundImage+";opacity:"+opacity+";"+filter+"\"></span>";

			var contentDiv = divs[i].cloneNode(true);
			contentDiv.className = "";
			contentDiv.style.margin = 0;
			contentDiv.style.cssFloat = "none";
			contentDiv.style.styleFloat = "none";
			contentDiv.style.width = "";
			divs[i].parentNode.replaceChild(newDiv, divs[i]);
			newDiv.insertBefore(contentDiv, newDiv.childNodes[4]);			
		}
	}
}


if (window.addEventListener){
	window.addEventListener('load', roundCorners, false); 
} else if (window.attachEvent){
	window.attachEvent('onload', roundCorners);
}
