var roundCorners = new Class({
	initialize: function contructor(options) {
		this.options		= typeof(options) != 'undefined' ? options : '';
		this.radius			= (typeof(options) != 'undefined' && !isNaN(parseInt(options.radius))) ? options.radius : 10;
		this.corners		= (typeof(options) != 'undefined' && typeof(options.corners != 'undefined')) ? options.corners : false;
		this.background		= (typeof(options) != 'undefined' && typeof(options.background) != 'undefined') ? options.background : '#FFF';
		this.antiAliase		= (typeof(options) != 'undefined' && typeof(options.antiAliase) != 'undefined') ? options.antiAliase : false;

		this.borderSize		= null;
		this.borderColor	= null;
		this.isIE			= navigator.userAgent.toLowerCase().indexOf("msie") > -1;
		this.isMoz			= document.implementation && document.implementation.createDocument;
		this.isSafari		= ((navigator.userAgent.toLowerCase().indexOf('safari')!=-1)&&(navigator.userAgent.toLowerCase().indexOf('mac')!=-1))?true:false;
	},
	getStyle: function(element) {
		try {
			this.borderSize = parseInt(this.get_style(element, 'borderTopWidth', 'border-top-width'));
			this.borderColor = this.get_style(element, 'borderTopColor', 'border-top-color');
			return true;
		}
		catch (e) {
			return false;
		}
	},
	getParentBackground: function(element) {
		var bgColor = this.get_style(element.parentNode, 'backgroundColor', 'background-color');
		if (bgColor == 'transparent' && typeof(element.parentNode) != 'undefined' && element.parentNode.nodeName != 'HTML') {
			bgColor = this.getParentBackground(element.parentNode);
		}
		return (bgColor != 'transparent' ? bgColor : this.background);
	},
	generateCorners: function(element) {

		var radius = this.radius;
		var nocorner = new Array();
		nocorner['tl'] = {'set':false};
		nocorner['tr'] = {'set':false};
		nocorner['bl'] = {'set':false};
		nocorner['br'] = {'set':false};
		element.style.position = 'relative';

		if (typeof(this.options) != 'undefined' && typeof(this.options.background) == 'undefined') {
			this.background = this.getParentBackground(element);
		}
		
		element.style.border = '1px solid ' + this.background;

		for (var i = 0; i < 4; i++) {

			radius = this.radius;
			switch (i) {
				case 0:
					if (typeof(this.corners) != 'undefined' && typeof(this.corners.tl) != 'undefined' && typeof(this.corners.tl.radius) != 'undefined' && parseInt(this.corners.tl.radius) >= 0) {
						radius = parseInt(this.corners.tl.radius);
					}
					break;
				case 1:
					if (typeof(this.corners) != 'undefined' && typeof(this.corners.tr) != 'undefined' && typeof(this.corners.tr.radius) != 'undefined' && parseInt(this.corners.tr.radius) >= 0) {
						radius = parseInt(this.corners.tr.radius);
					}
					break;
				case 2:
					if (typeof(this.corners) != 'undefined' && typeof(this.corners.bl) != 'undefined' && typeof(this.corners.bl.radius) != 'undefined' && parseInt(this.corners.bl.radius) >= 0) {
						radius = parseInt(this.corners.bl.radius);
					}
					break;
				case 3:
					if (typeof(this.corners) != 'undefined' && typeof(this.corners.br) != 'undefined' && typeof(this.corners.br.radius) != 'undefined' && parseInt(this.corners.br.radius) >= 0) {
						radius = parseInt(this.corners.br.radius);
					}
					break;
			}

			for (var j = parseInt(radius); j > 0; j--) {

				if (typeof(this.corners) != 'undefined') {
					if (typeof(this.corners.tl) != 'undefined' && typeof(this.corners.tl.radius) == 'undefined' && Boolean(this.corners.tl) != true && i == 0) {
						nocorner['tl'] = {'set':true, 'radius':radius};
						continue;
					} else if (typeof(this.corners.tr) != 'undefined' && typeof(this.corners.tr.radius) == 'undefined' && Boolean(this.corners.tr) != true && i == 1) {
						nocorner['tr'] = {'set':true, 'radius':radius};
						continue;
					} else if (typeof(this.corners.bl) != 'undefined' && typeof(this.corners.bl.radius) == 'undefined' && Boolean(this.corners.bl) != true && i == 2) {
						nocorner['bl'] = {'set':true, 'radius':radius};
						continue;
					} else if (typeof(this.corners.br) != 'undefined' && typeof(this.corners.br.radius) == 'undefined' && Boolean(this.corners.br) != true && i == 3) {
						nocorner['br'] = {'set':true, 'radius':radius};
						continue;
					}
				}

				var rounded = Math.round(radius - (Math.floor(Math.sqrt(Math.pow(radius, 2) - Math.pow((j-1), 2)))) - 1);

				if (parseInt(radius) - j >= element.offsetHeight) {
					break;
				} else if (rounded >= element.offsetWidth) {
					break;
				}

				corner = document.createElement("DIV");
				corner.style.width = (rounded < 1 ? '0' : rounded) + 'px';
				corner.style.height = '1px';
				corner.style.fontSize = '1px';
				corner.style.position = 'absolute';
				corner.style.zIndex = 899;
				corner.style.backgroundColor = this.background;

				switch (i) {
					case 0:
					case 2:
						corner.style.borderRight = this.borderSize + 'px solid ' + this.borderColor;
						corner.style.left = '0px';
						//corner.style.marginLeft = this.borderSize + 'px';
						break;
					case 1:
					case 3:
						corner.style.borderLeft = this.borderSize + 'px solid ' + this.borderColor;
						corner.style.right = '0px';
						//corner.style.marginRight = this.borderSize + 1 + 'px';
						break;
				}

				switch (i) {
					case 0:
					case 1:
						corner.style.top = parseInt(radius) - j + 'px';
						//corner.style.marginTop = this.borderSize + 'px';
						break;
					case 2:
					case 3:
						//corner.style.marginBottom = this.borderSize + 'px';
						corner.style.bottom = parseInt(radius) - j + 'px';
						break;
				}

				if (this.antiAliase) {
					var aA_corner = this.generateAliasedCorners(corner, i);
					if (typeof(aA_corner) == 'object') {
						corner.appendChild(aA_corner);
					}
				}

				element.appendChild(corner);
			}	
		}
		
		var nocorners = {0:nocorner['tl'], 1:nocorner['tr'], 2:nocorner['br'], 3:nocorner['bl'], 4:nocorner['tl']};
		
		for (var k = 0; k <= 3; k++) {
			var cornerwidth = Math.round((typeof(nocorners[k].radius) != 'undefined' ? nocorners[k].radius : radius) - (Math.floor(Math.sqrt(Math.pow((typeof(nocorners[k].radius) != 'undefined' ? nocorners[k].radius : radius), 2) - Math.pow(((typeof(nocorners[k].radius) != 'undefined' ? nocorners[k].radius : radius)-1), 2)))) - 1);
			
			if(nocorners[k].set == true && nocorners[k+1].set == true) {
				eval('element.style.border' + (k==0 ? 'Top' : (k==1 ? 'Right' : (k==2 ? 'Bottom' : 'Left'))) + ' = this.borderSize + \'px solid \' + this.borderColor;');
			} else {
				borderline = document.createElement("DIV");
				borderline.style.fontSize = '1px';
				borderline.style.lineHeight = '1px';
				borderline.style.position = 'absolute';
				borderline.style.zIndex = 899;
				borderline.style.backgroundColor = (this.borderColor == 'rgb(0, 0, 0)' || this.borderColor == '#000' ? this.background : this.borderColor);

				if (element.offsetWidth > 0)
				{
					borderline.style.width = (k==1 || k==3 ? '1' : element.offsetWidth-((nocorners[k].set != true && nocorners[k+1].set == true) || (nocorners[k].set == true && nocorners[k+1].set != true) ? 1 : 2)*cornerwidth) + 'px';
					borderline.style.height = (k==0 || k==2 ? '1' : element.offsetHeight-((nocorners[k].set != true && nocorners[k+1].set == true) || (nocorners[k].set == true && nocorners[k+1].set != true) ? 1 : 2)*cornerwidth) + 'px';
					eval('borderline.style.' + (k==0 ? 'top' : (k==1 ? 'right' : (k==2 ? 'bottom' : 'left'))) + ' = \'0px\';');			
					
					if(nocorners[0].set == true && (k==0 || k==3)) {
						eval('borderline.style.' + (k==0 ? 'left' : 'top') + ' = \'0px\';');
					}
					else if(nocorners[1].set == true && (k==0 || k==1)) {
						eval('borderline.style.' + (k==0 ? 'right' : 'top') + ' = \'0px\';');
					}
					else if(nocorners[2].set == true && (k==1 || k==2)) {
						eval('borderline.style.' + (k==1 ? 'bottom' : 'right') + ' = \'0px\';');
					}
					else if(nocorners[3].set == true && (k==2 || k==3)) {
						eval('borderline.style.' + (k==2 ? 'left' : 'bottom') + ' = \'0px\';');
					}
					else {
						eval('borderline.style.' + (k==0 || k==2 ? 'left' : 'top') + ' = \'' + cornerwidth + 'px\';');
					}
					
					element.appendChild(borderline);
					borderline.innerHTML = '&nbsp;';
				}
			}
		}
		
	},
	generateAliasedCorners: function(corner, pos) {

		if (parseInt(corner.style.width) > 0)
		{
			var aA_corner = document.createElement("DIV");
			aA_corner.style.position = 'absolute';
			aA_corner.style.height = '1px';
			aA_corner.style.fontSize = '1px';
			aA_corner.style.width = parseInt(corner.style.width) - 1 + 'px';
			aA_corner.style.left = corner.style.left;
			aA_corner.style.right = corner.style.right;

			switch (pos) {
					case 0:
					case 2:
						aA_corner.style.borderRight = this.borderSize + 'px solid ' + this.borderColor;
						break;
					case 1:
					case 3:
						aA_corner.style.borderLeft = this.borderSize + 'px solid ' + this.borderColor;
						break;
			}

			var opacity = 25;
			aA_corner.style.opacity = (opacity / 100);
			aA_corner.style.MozOpacity = (opacity / 100);
			aA_corner.style.KhtmlOpacity = (opacity / 100);
			aA_corner.style.filter = "alpha(opacity=" + opacity + ")"; 

			return aA_corner;
		}
		else {
			return false;
		}
	},
	generate: function(element) {
		if (typeof(element) != "object") {
			return false;
		} else if (typeof(element.id) != 'undefined') {
			this.getStyle(element)
			this.generateCorners(element);
		}
		else {
			for(var i = 0; i < element.length; i++) {
				if (this.getStyle(element[i])) {
					this.generateCorners(element[i]);
				}
			}
		}
	},
	get_style: function(obj, property, propertyNS)
	{
		try {
			if(obj.currentStyle) {
				var returnVal = eval("obj.currentStyle." + property);
			}
			else {
				if(this.isSafari && obj.style.display == "none") {
					obj.style.display = "";
					var wasHidden = true;
				}
				var returnVal = document.defaultView.getComputedStyle(obj, '').getPropertyValue(propertyNS);
				if(this.isSafari && wasHidden) {
					obj.style.display = "none";
				}
			}
		}
		catch(e) {
			return e.message;
		}
		return returnVal;
	}
});
