/** 
 * corner.js 1.81 (21-Mar-2009)
 * (c) by Christian Effenberger 
 * All Rights Reserved
 * Source: corner.netzgesta.de
 * Distributed under Netzgestade Software License Agreement
 * http://www.netzgesta.de/cvi/LICENSE.txt
 * License permits free of charge
 * use on non-commercial and 
 * private web sites only  
**/

var tmp = navigator.appName == 'Microsoft Internet Explorer' && navigator.userAgent.indexOf('Opera') < 1 ? 1 : 0;
if(tmp) var isIE = document.namespaces ? 1 : 0;

if(isIE) {
	if(document.namespaces['v']==null) {
		var e=["shape","shapetype","group","background","path","formulas","handles","fill","stroke","shadow","textbox","textpath","imagedata","line","polyline","curve","roundrect","oval","rect","arc","image"],s=document.createStyleSheet(); 
		for(var i=0; i<e.length; i++) {s.addRule("v\\:"+e[i],"behavior: url(#default#VML);");} document.namespaces.add("v","urn:schemas-microsoft-com:vml");
	} 
}

function getRadius(radius,width,height){
	var part = (Math.min(width,height)/100);
	radius = Math.max(Math.min(100,radius/part),0);
	return radius + '%';
}

function getImages(className){
	var children = document.getElementsByTagName('img'); 
	var elements = new Array(); var i = 0;
	var child; var classNames; var j = 0;
	for (i=0;i<children.length;i++) {
		child = children[i];
		classNames = child.className.split(' ');
		for (var j = 0; j < classNames.length; j++) {
			if (classNames[j] == className) {
				elements.push(child);
				break;
			}
		}
	}
	return elements;
}

function getClasses(classes,string){
	var temp = '';
	for (var j=0;j<classes.length;j++) {
		if (classes[j] != string) {
			if (temp) {
				temp += ' '
			}
			temp += classes[j];
		}
	}
	return temp;
}

function getClassValue(classes,string){
	var temp = 0; var pos = string.length;
	for (var j=0;j<classes.length;j++) {
		if (classes[j].indexOf(string) == 0) {
			temp = Math.min(classes[j].substring(pos),100);
			break;
		}
	}
	return Math.max(0,temp);
}

function getClassAttribute(classes,string){
	var temp = 0; var pos = string.length;
	for (var j=0;j<classes.length;j++) {
		if (classes[j].indexOf(string) == 0) {
			temp = 1; break;
		}
	}
	return temp;
}

function roundedRect(ctx,x,y,width,height,radius,nopath){
	if (!nopath) ctx.beginPath();
	ctx.moveTo(x,y+radius);
	ctx.lineTo(x,y+height-radius);
	ctx.quadraticCurveTo(x,y+height,x+radius,y+height);
	ctx.lineTo(x+width-radius,y+height);
	ctx.quadraticCurveTo(x+width,y+height,x+width,y+height-radius);
	ctx.lineTo(x+width,y+radius);
	ctx.quadraticCurveTo(x+width,y,x+width-radius,y);
	ctx.lineTo(x+radius,y);
	ctx.quadraticCurveTo(x,y,x,y+radius);
	if (!nopath) ctx.closePath();
}

function addGradient(ctx,x,y,h,radius,color,opacity) {
	var tmp = ctx.createLinearGradient(0,h,0,0);
	var val = (color>0?0.25:0.2);
	tmp.addColorStop(0,'rgba(255,255,255,1)');
	tmp.addColorStop(0.1,'rgba(88,191,255,0.7)');
	tmp.addColorStop(radius,'rgba(43,105,143,0.4)');
	tmp.addColorStop(1,'rgba(0,0,0,0)');
	return tmp;
}

function addShine(ctx,width,height,radius,opacity,extra) {
	var style; var color = (extra!=1?254:0);
	
	ctx.fillStyle = addGradient(ctx,0,0,height,opacity,color,opacity);
	ctx.beginPath();
	ctx.fillRect(0,0,width,height);
	ctx.closePath();
	ctx.save();
}

function addRoundedBorder(ctx, width, height, lineWidth, radius, color){
	ctx.strokeStyle=color;
	ctx.lineWidth=3;
	roundedRect(ctx, lineWidth/2, lineWidth/2, width-lineWidth, height-lineWidth, radius);
	ctx.stroke();
}

function addCorners() {
	var theimages = getImages('corner');
	var image; var object; var canvas; var context; var i;
	var iradius = null; var ishade = null; var ishadow = null;
	var inverse = null; var classes = ''; var newClasses = ''; 
	var maxdim = null; var style = null; var offset = null;
	for (i=0;i<theimages.length;i++) {	
		image = theimages[i];
		object = image.parentNode; 
		
		try{
			canvas = document.createElement('canvas');
			if (canvas.getContext) {
				classes = image.className.split(' ');
				iradius = getClassValue(classes,"iradius");
				ishadow = getClassValue(classes,"ishadow");
				ishade  = getClassValue(classes,"ishade");
				iborder = getClassValue(classes,"iborder");
				inverse = getClassAttribute(classes,"inverse");
				newClasses = getClasses(classes,"corner");
				canvas.className = newClasses;
				canvas.style.cssText = image.style.cssText;
				canvas.style.height = image.height+'px';
				canvas.style.width = image.width+'px';
				canvas.height = image.height;
				canvas.width = image.width;
				//canvas.src = image.src;
				//canvas.alt = image.alt;
				if(image.id!='') canvas.id = image.id;
				if(image.title!='') canvas.title = image.title;
				if(image.getAttribute('onclick')!='') canvas.setAttribute('onclick',image.getAttribute('onclick'));
				maxdim = Math.min(canvas.width,canvas.height)/2;
				iradius = Math.min(maxdim,iradius); offset = 4;
				offset = (ishadow>0?(inverse>0?0:Math.min(Math.max(offset,iradius/2),16)):0);
				context = canvas.getContext("2d");
				
				if(image.getAttribute("usemap")) {
					object.style.position = 'relative';
					object.style.height = image.height+'px';
					object.style.width = image.width+'px';
					canvas.left = 0; canvas.top = 0;
					canvas.style.position = 'absolute';
					canvas.style.left = 0 + 'px';
					canvas.style.top = 0 + 'px';
					image.left = 0; image.top = 0;
					image.style.position = 'absolute';
					image.style.height = image.height+'px';
					image.style.width = image.width+'px';
					image.style.left = 0 + 'px';
					image.style.top = 0 + 'px';
					image.style.opacity = 0;
					object.insertBefore(canvas,image);
				}else {
					object.replaceChild(canvas,image);
				}
				context.clearRect(0,0,canvas.width,canvas.height);
				context.save();
				if (ishadow>0 && inverse<=0) {
					ishadow = ishadow/100;
					if (iradius>0) {
						roundedShadow(context,offset,offset,canvas.width-offset,canvas.height-offset,iradius,ishadow);
					}else {
						offset = 8; 
						roundedShadow(context,offset,offset,canvas.width-offset,canvas.height-offset,offset,ishadow);
					}
				}
				if (iradius<=0) {
					context.beginPath();
					context.rect(0,0,canvas.width-offset,canvas.height-offset);
					context.closePath();
				}else {
					roundedRect(context,0,0,canvas.width-offset,canvas.height-offset,iradius);
				}
				context.clip();
				context.fillStyle = 'rgba(0,0,0,1)';
				context.fillRect(0,0,canvas.width,canvas.height);
				context.drawImage(image,0,0,canvas.width-offset,canvas.height-offset);
				if (ishadow>0 && inverse>0) {
					ishadow = ishadow/100;
					if (iradius>0) {
						addShine(context,canvas.width,canvas.height,iradius,ishadow,1);
						roundedRect(context,0,0,canvas.width,canvas.height,iradius);
					}else {
						iradius = 16; 
						addShine(context,canvas.width,canvas.height,iradius,ishadow,1);
						context.beginPath();
						context.rect(0,0,canvas.width,canvas.height);
						context.closePath();
					}
					context.strokeStyle = 'rgba(0,0,0,'+ishadow+')';
					context.lineWidth = 2;
					context.stroke();
				}
				if (ishade>0) {
					ishade = ishade/100;
					if (iradius<=0) iradius = 16; 
					//addShade(context,canvas.width-offset,canvas.height-offset,iradius,ishade);
					addShine(context,canvas.width,canvas.height,iradius,ishade);
					addRoundedBorder( context, canvas.width, canvas.height,	iborder, iradius, image.style.borderColor);
				}
				canvas.style.visibility = 'visible';
			}
		}catch(e){}
	}
}

var cornerOnload = window.onload;
window.onload = function () { if(cornerOnload) cornerOnload(); if(isIE){addIECorners(); }else {addCorners();}}
