// cnr.js
// Rounded corners v1.01 Copyright 20032004 Peter Mooring. Email: peterpm@xs4all.nl. You can use the code but must add this line. 
// There is no need to edit this code
// History: 
// - 20040106/PPM cnrCreate swap correction to include all points
// Todo: 
// - Optimize swap/points 
// Bugs/features: 
// - Line width 
// Note(s):
// - Don't forget to specify style and cnr_pixelurl before the line including this file

var cnr_boxwh;
var cnr_linew;
var cnr_outsidecolor;
var cnr_linecolor;
var cnr_insidecolor;

var cnr_divcnt = new Array();

var cnr_debug = 0;

var cnr_elemcnt;
var cnr_elem_x = new Array();
var cnr_elem_y = new Array();
var cnr_elem_w = new Array();
var cnr_elem_c = new Array();

var divstart_1 = '<div style="position: absolute; height: 1px; overflow: hidden; background-color: ';
var divstart = '<div id="cnr" style="background-color: ';

var cnr_y_xmax = new Array();
var cnr_y_xmin = new Array();


// Algorithm to plot circle by John Kennedy, Email: rkennedy@ix.netcom.com
// http://homepage.smc.edu/kennedy_john/BCIRCLE.PDF
// This algorithm avoids use of the Math object
function cnrPlotCircle(m, r){
	var x, y;
	var xchange, ychange;
	var radiuserror;
	var ymin, ymax; 

	if(cnr_debug){ alert('cnrPlotCircle: ' + m + ', ' + r); }

	x = r;
	y = 0;
	xchange = 1 - (2 * r);
	ychange = 1;
	radiuserror = 0;
	while(x >= y){
		cnrPutCirclePoints(m, x, y);
		y++;
		radiuserror += ychange;
		ychange += 2;
		if( ((2 * radiuserror) + xchange) > 0){
			x--;
			radiuserror += xchange;
			xchange += 2;
		}
	}
}

function cnrPutCirclePoints(m, x, y){

	var t;

	if(m == 0){
		if(cnr_y_xmax[y] == -1){
			cnr_y_xmax[y] = x;
			cnr_y_xmin[y] = x;
		}else{
			if(x < cnr_y_xmin[y]){
				cnr_y_xmin[y] = x;
			}else{
				if(x > cnr_y_xmax[y]){
					cnr_y_xmax[y] = x;
				}
			}
		}

		// Swap x,y
		t = x;
		x = y;
		y = t;

		if(cnr_y_xmax[y] == -1){
			cnr_y_xmax[y] = x;
			cnr_y_xmin[y] = x;
		}else{
			if(x < cnr_y_xmin[y]){
				cnr_y_xmin[y] = x;
			}else{
				if(x > cnr_y_xmax[y]){
					cnr_y_xmax[y] = x;
				}
			}
		}

	}else{
		// Note: xmin and xmax have been set already

		if(x < cnr_y_xmin[y]){
			cnr_y_xmin[y] = x;
		}else{
			if(x > cnr_y_xmax[y]){
				cnr_y_xmax[y] = x;
			}
		}

		// Swap (include all points!)
		ymin = cnr_y_xmin[y];
		ymax = cnr_y_xmax[y];
		x = y;
		for(y = ymin; y <= ymax; y++){
			
			if(x < cnr_y_xmin[y]){
				cnr_y_xmin[y] = x;
			}else{
				if(x > cnr_y_xmax[y]){
					cnr_y_xmax[y] = x;
				}
			}
		}

	}
}

function cnrCreate(boxwh, linew){

	var radius;
	var x, y, xmin, xmax, xpos, ypos;
	var outsidecolor, linecolor, insidecolor;

	if(cnr_debug){ alert('cnrCreate: ' + boxwh + ', ' + linew); }

	// Store
	cnr_boxwh = boxwh;
	cnr_linew = linew;

	// Get colors
	outsidecolor = cnr_outsidecolor;
	linecolor  = cnr_linecolor;
	insidecolor = cnr_insidecolor;

	// Circle radius is boxwh minus one!
	radius = boxwh - 1;

	// Preset coords arrays
	for(y = 0; y < boxwh; y++){
		cnr_y_xmax[y] = -1; cnr_y_xmin[y] = -1;
	}

	// 'Outside' line
	cnrPlotCircle(0, radius);

	// 'Inside' line only if linew > 1
	if(linew > 1){
		cnrPlotCircle(1, (radius - (linew - 1)));
	}

	// Got all relevant pixels
	// Build elements for fast reference
	cnr_elemcnt = 0;
	for(y = 0; y < boxwh; y++){

		xmax = cnr_y_xmax[y];
		xmin = cnr_y_xmin[y];

		// Outside elem
		if(outsidecolor){
			if(xmax < (boxwh - 1)){
				cnr_elem_x[cnr_elemcnt] = xmax + 1;
				cnr_elem_y[cnr_elemcnt] = y;
				cnr_elem_w[cnr_elemcnt] = (boxwh - 1) - xmax;
				cnr_elem_c[cnr_elemcnt] = outsidecolor;
				cnr_elemcnt++;
			}
		}
		// Line elem (always)
		if(linecolor){
			cnr_elem_x[cnr_elemcnt] = xmin;
			cnr_elem_y[cnr_elemcnt] = y;
			cnr_elem_w[cnr_elemcnt] = (xmax - xmin) + 1;
			cnr_elem_c[cnr_elemcnt] = linecolor;
			cnr_elemcnt++;
		}
		// Inside elem
		if(insidecolor){
			if(xmin > 0){
				cnr_elem_x[cnr_elemcnt] = 0;
				cnr_elem_y[cnr_elemcnt] = y;
				cnr_elem_w[cnr_elemcnt] = xmin;
				cnr_elem_c[cnr_elemcnt] = insidecolor;
				cnr_elemcnt++;
			}
		}
	}
	if(cnr_debug){ alert('elems added = ' + cnr_elemcnt); }
}

function cnrSet(outsidecolor, linecolor, insidecolor){

	cnr_outsidecolor = outsidecolor;
	cnr_linecolor = linecolor;
	cnr_insidecolor = insidecolor;
}

function cnrGet(corner){

	var p, cx, cy, i, x, y, w, h, c;
	var xnew, ynew;
	var boxwh = cnr_boxwh;
	var insidecolor = cnr_insidecolor;

	if(cnr_debug){ alert('cnrGet: ' + corner); }

	p = 'relative';
	cx = 0;
	cy = 0;
	if(cnrGet.arguments.length > 1){
		p = 'absolute';
		cx = cnrGet.arguments[1];
		cy = cnrGet.arguments[2];
	}

	// Container start
	var s = '<div style="position: ' + p + '; left: ' + cx + 'px; top: ' + cy + 'px; width: '+  boxwh +'px; height: '+ boxwh  +'px;">' + "\n";

	for(i = 0; i < cnr_elemcnt; i++){

		x = cnr_elem_x[i];
		y = cnr_elem_y[i];
		w = cnr_elem_w[i];
		c = cnr_elem_c[i];

		if(corner == 0){
			xnew = boxwh - (w + x);
			ynew = (boxwh - 1) - y;
		}else if(corner == 1){
			xnew = x;
			ynew = (boxwh - 1) - y;
		}else if(corner == 2){
			xnew = boxwh - (w + x);
			ynew = y;
		}else{
			xnew = x;
			ynew = y;
		}
		s += divstart + c + '; left: ' + xnew + 'px; top: ' + ynew + 'px; width: ' + w + 'px;"></div>' + "\n";
	}

	// Container end
	s += '</div>' + "\n";
	document.write(s);
}

// getRect
// Description: Generate rectangle with rounded corners at specified position!
// r = absolute(=0)/relative(=1) positioning
// b = corner box width/height
// l = line width (>= 1)
// g = regenerate corners (=1)
function getRect(r, x, y, w, h, b, l, outsidecolor, linecolor, insidecolor, g){

	var p, s, color;

	p = (r == 0)? 'absolute': 'relative';

	// Rectangle container start
//	s = '<div style="position: ' + p + '; left: ' + x + 'px; top: ' + y + 'px; width: ' + w + 'px; height: ' + h  +'px;">' + "\n";
	s = '<div style="position: ' + p + '; left: ' + x + 'px; top: ' + y + 'px; width: ' + w + 'px; height: ' + h  +'px; background-color: ' + insidecolor + '">' + "\n";


	// Add lines
	color = (linecolor == '')? '': 'background-color: ' + linecolor;
	// Top 
	s += '<div style="position: absolute; left: ' + b + 'px; top: ' + 0 + 'px; width: ' + (w - b - b) + 'px; height: ' + l  +'px; overflow: hidden; ' + color + '"></div>' + "\n";
	// Bottom 
	s += '<div style="position: absolute; left: ' + b + 'px; top: ' + (h - l) + 'px; width: ' + (w - b - b) + 'px; height: ' + l  +'px; overflow: hidden; ' + color + '"></div>' + "\n";
	// Left 
	s += '<div style="position: absolute; left: ' + 0 + 'px; top: ' + b + 'px; width: ' + l + 'px; height: ' + (h - b - b)  +'px; overflow: hidden; ' + color + '"></div>' + "\n";
	// Right
	s += '<div style="position: absolute; left: ' + (w - l) + 'px; top: ' + b + 'px; width: ' + l + 'px; height: ' + (h - b - b)  +'px; overflow: hidden; ' + color + '"></div>' + "\n";
	document.write(s);


	// Add corners
	cnrSet(outsidecolor, linecolor, '');
	if(g == 1){
		cnrCreate(b, l);
	}

	cnrGet(0, 0, 0);
	cnrGet(1, (w - b), 0);
	cnrGet(2, 0, (h - b));
	cnrGet(3, (w - b), (h - b));

	s = '</div>';
	document.write(s);


}

// Scales with content
function getRectHTMLStart(r, x, y, w, h, b, l, outsidecolor, linecolor, insidecolor, g){

	var p, s, lcolor;

	p = (r == 0)? 'absolute': 'relative';

	// Line color
	lcolor = (linecolor == '')? '': 'background-color: ' + linecolor;

	// Generate corners
	cnrSet(outsidecolor, linecolor, '');
	if(g == 1){
		cnrCreate(b, l);
	}

var t = '';
	// Container is table
	s = '<table width="' + w + '" border="0" cellpadding="0" cellspacing="0" bgcolor="' + insidecolor + '">';
	s += '<tr><td colspan="2" width="' + b + '" height="' + b + '">';
t += s + "\n";
	document.write(s);

	cnrGet(0);

	s = '</td><td width="' + (w - b - b) + '" height="' + b + '" valign="top">';
t += s + "\n";
	document.write(s);
	
	// Add top line
	// Top 
	s = '<div style="position: relative; left: ' + 0 + 'px; top: ' + 0 + 'px; width: ' + (w - b - b) + 'px; height: ' + l  +'px; overflow: hidden; ' + lcolor + '"></div>' + "\n";
t += s + "\n";
	document.write(s);

	s = '</td><td colspan="2" width="' + b + '" height="' + b + '">';
t += s + "\n";
	document.write(s);
	
	cnrGet(1);

	s = '</td></tr>' + "\n";
	s += '<tr>' + "\n";
	s += '<td width="' + l + '" height="100%" bgcolor="' + linecolor + '"><img src="' + cnr_pixelurl + '" width="' + l + '" height="1" border="0"></td>' + "\n";
	s += '<td width="' + (b - l) + '"><img src="' + cnr_pixelurl + '" width="' + (b - l) + '" height="1" border="0"></td>' + "\n";
	s += '</td><td width="' + (w - b - b) + '" height="100%">';
t += s + "\n";
	document.write(s);

// alert(t);
}


function getRectHTMLEnd(r, x, y, w, h, b, l, outsidecolor, linecolor, insidecolor, g){

	var p, s, lcolor;

	// Line color
	lcolor = (linecolor == '')? '': 'background-color: ' + linecolor;

	// Generate corners
	cnrSet(outsidecolor, linecolor, '');
	if(g == 1){
		cnrCreate(b, l);
	}

	s = '</td>';
	s += '<td width="' + (b - l) + '"><img src="' + cnr_pixelurl + '" width="' + (b - l) + '" height="1" border="0"></td>' + "\n";
	s += '<td width="' + l + '" height="100%" bgcolor="' + linecolor + '"><img src="' + cnr_pixelurl + '" width="' + l + '" height="1" border="0"></td>' + "\n";
	s += '</tr>';
	document.write(s);

	s = '<tr><td colspan="2" width="' + b + '" height="' + b + '">';
	document.write(s);

	cnrGet(2);

	s = '</td><td width="' + (w - b - b) + '" height="' + b + '" valign="bottom">';
	document.write(s);

	s = '<div style="position: relative; left: ' + 0 + 'px; width: ' + (w - b - b) + 'px; height: ' + l  +'px; overflow: hidden; ' + lcolor + '"></div>' + "\n";
	document.write(s);

	s = '</td><td colspan="2" width="' + b + '" height="' + b + '">';
	document.write(s);

	cnrGet(3);

	s = '</tr></table>';
	document.write(s);
}

// Half of a rect with text starting in the top part
// Scales with content
function getRectTopHTMLStart(r, x, y, w, h, b, l, outsidecolor, linecolor, insidecolor, g){

	var p, s, lcolor;

	p = (r == 0)? 'absolute': 'relative';

	// Line color
	lcolor = (linecolor == '')? '': 'background-color: ' + linecolor;

	// Generate corners
	cnrSet(outsidecolor, linecolor, '');
	if(g == 1){
		cnrCreate(b, l);
	}

var t = '';
	// Container is table
	s = '<table width="' + w + '" border="0" cellpadding="0" cellspacing="0" bgcolor="' + insidecolor + '">';
	s += '<tr><td colspan="2" width="' + b + '" height="' + b + '">';
t += s + "\n";
	document.write(s);

	cnrGet(0);

	s = '</td><td rowspan="2" width="' + (w - b - b) + '" height="' + h + '" valign="top">';
	s += '<table width="' + (w - b - b) + '" border="0" cellpadding="0" cellspacing="0">';
	s += '<tr>';
	s += '<td width="' + (w - b - b - 1) + '" height="' + l + '" bgcolor="' + linecolor + '"><img src="' + cnr_pixelurl + '" width="' + (w - b - b - 1) + '" height="' + l + '" border="0"></td>';
	s += '<td width="1" height="' + l + '" bgcolor="' + linecolor + '"><img src="' + cnr_pixelurl + '" width="1" height="' + l + '" border="0"></td></tr>';
	s += '<tr><td width="' + (w - b - b - 1) + '" height="' + (h - l) + '">';
t += s + "\n";
	document.write(s);

// alert(t);
}


function getRectTopHTMLEnd(r, x, y, w, h, b, l, outsidecolor, linecolor, insidecolor, g){

	var p, s, lcolor;

	// Line color
	lcolor = (linecolor == '')? '': 'background-color: ' + linecolor;

	// Generate corners
	cnrSet(outsidecolor, linecolor, '');
	if(g == 1){
		cnrCreate(b, l);
	}

var t = '';
	s = '</td>';
	s += '<td width="1"><img src="' + cnr_pixelurl + '" width="1" height="1" border="0"></td></tr></table></td>' + "\n";
	s += '<td colspan="2" width="' + b + '" height="' + b + '">';
t += s + "\n";
	document.write(s);

	cnrGet(1);

	s = '</td></tr><tr>';
	s += '<td width="' + l + '" height="100%" bgcolor="' + linecolor + '"><img src="' + cnr_pixelurl + '" width="' + l + '" height="' + (h - b) + '" border="0"></td>' + "\n";
	s += '<td width="' + (b - l) + '" height="100%"><img src="' + cnr_pixelurl + '" width="' + (b - l) + '" height="' + (h - b) + '" border="0"></td>' + "\n";
	s += '<td width="' + (b - l) + '" height="100%"><img src="' + cnr_pixelurl + '" width="' + (b - l) + '" height="' + (h - b) + '" border="0"></td>' + "\n";
	s += '<td width="' + l + '" height="100%" bgcolor="' + linecolor + '"><img src="' + cnr_pixelurl + '" width="' + l + '" height="' + (h - b) + '" border="0"></td>' + "\n";
	s += '</tr></table>';
t += s + "\n";
	document.write(s);

// alert(t);
}
