var isIE     = navigator.userAgent.toLowerCase().indexOf("msie 6") > -1;
var isMoz    = document.implementation && document.implementation.createDocument;
var isSafari = ((navigator.userAgent.toLowerCase().indexOf('safari')!=-1)&&(navigator.userAgent.toLowerCase().indexOf('mac')!=-1))?true:false;

function getElementsByClass(searchClass, node, tag){
     var classElements = new Array();

     if(node == null) node = document.body;
     if(tag == null) tag = '*';

     var els = node.getElementsByTagName(tag);
     var elsLen = els.length;
     var pattern = new RegExp(searchClass);

     for (i = 0, j = 0; i < elsLen; i++){
        if(pattern.test(els[i].className)){
           classElements[j] = els[i];
           j++;
        }
     }

     return classElements;
}

function get_style(oElm, strCssRule){
	var strValue = "";
	if(document.defaultView && document.defaultView.getComputedStyle){
		strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
	}
	else if(oElm.currentStyle){
		strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
			return p1.toUpperCase();
		});
		strValue = oElm.currentStyle[strCssRule];
	}
	if(strValue.indexOf("px") != -1){ //stripping away 'px'
		strValue = parseInt( strValue.slice(0, strValue.indexOf("px")) );
	}
	return strValue;
}

function getBgColor(box){
	
	if(get_style(box, 'background-color') == 'transparent'){ //if color not set in css

		while(get_style(box, 'background-color') == 'transparent' && box.nodeName != 'BODY'){//loop and check if color is set in css
			box = box.parentNode; // if not found then go higher

			if(get_style(box, 'background-color') != 'transparent'){ //check if box's color is set and return if so
				return get_style(box, 'background-color');
			}//if not ... go on ...
		}
		
		if(box.nodeName == 'BODY') var color = '#ffffff';//if we reached body ... there is no point in continuiting
	}else{
		var color = get_style(box, 'background-color');
	}
	return color;
}

function setOpacity(obj, opacity){
	if(typeof(obj.style.opacity) != "undefined"){
		// W3C
		obj.style.opacity = opacity/100;
	}else if(typeof(obj.style.MozOpacity) != "undefined"){
		// Older Mozilla
	    obj.style.MozOpacity = opacity/100;
	}else if(typeof(obj.style.filter) != "undefined"){
		// IE
	    obj.style.filter = "alpha(opacity:" + opacity + ")";
	}else if(typeof(obj.style.KHTMLOpacity) != "undefined"){
		// Older KHTML Based Browsers
	    obj.style.KHTMLOpacity = opacity/100;
	}
}

function drawPixel(color, container, opacity){

	if(opacity == null) var opacity = "99.999";
	var pixel = document.createElement("DIV");
	pixel.style.fontSize = "1px";
	pixel.style.overflow = "hidden";
	pixel.style.width = "1px";
	pixel.style.height = "1px";
	pixel.style.backgroundColor = color;
	pixel.style.position = "absolute";
	setOpacity(pixel, opacity);

	container.appendChild(pixel);
}

function minVal(min, val){
	if(val < min) return min;
	return val;
}

function drawCorners(box, radius){

box.style.position = "relative"; //set position relative to eb able to position the containers correctly

//set TopLeft corner
for(var i = 0; i < 4; i++){ //we have 4 corners

	for(var x = 1; x <= radius; x++){ //we have x lines to draw
		var barWidth = minVal(1, Math.round(radius - Math.sqrt( Math.pow(radius, '2') - Math.pow(x,'2') ))); // each has the width of barWidth
		for(var y = 0; y < barWidth; y++){

			//antialiasing ...
			if(x == 1){
				drawPixel(getBgColor(box.parentNode), box, 20);
			}else if(x == 2){
				drawPixel(getBgColor(box.parentNode), box, 40);
			}else if(x == 3){
				drawPixel(getBgColor(box.parentNode), box, 70);
			}else if(x == radius){
				if(y == (barWidth-1)){
					drawPixel(getBgColor(box.parentNode), box, 20);
				}else if(y == (barWidth-2)){
					drawPixel(getBgColor(box.parentNode), box, 40);
				}else{
					drawPixel(getBgColor(box.parentNode), box);
				}
			}else if(x == (radius-1) && y == (barWidth-1)){
				drawPixel(getBgColor(box.parentNode), box, 70);
			}else{
				drawPixel(getBgColor(box.parentNode), box);
			}

			//positioning
			if(i == 0){ //TopLeft
				box.lastChild.style.top = (radius - x) + "px"; //flipping the cruve over
				box.lastChild.style.left = y + "px";
			}else if(i == 1){ //TopRight
				box.lastChild.style.top = (radius - x) + "px";
				box.lastChild.style.right = (isIE)? (y-1) + "px" : y + "px";
			}else if(i == 2){ //BottomRight
				box.lastChild.style.bottom = (isIE)? ((radius - x)-1) + "px" : (radius - x) + "px";
				box.lastChild.style.right = (isIE)? (y-1) + "px" : y + "px";
			}else if(i == 3){ //BottomLeft
				box.lastChild.style.bottom = (isIE)? ((radius - x)-1) + "px" : (radius - x) + "px"; //set pixels bottom position (moving down 1px in case it's ie ...)
				box.lastChild.style.left = y + "px"; //set pixels left position
			}
		}
	}

}

}

function applyCorners(objClass, radius){
	var box = getElementsByClass(objClass);
	for(var x = 0; x < box.length; x++){
		drawCorners(box[x], radius);
	}
}