/* Corrects rounded box display (right float height).
 * Accepts an id string or an object.
 * Optionally sets wrapper (full box) to width in pixel,
 * otherwise take full width available.
 * Returns Prototype extended object on success otherwise false.
 * If text should be displayed, set fsize in pixel (default 14).
 * If only a single image should be surrounded by this round border,
 * set pwidth = 'img', so the function searches for the only imgage
 * in the container and uses the width attribute for correcting
 * the border width. The image attribute has to be set!
 * Returns Prototype extended container object or FALSE on error.
 */
function rndBoxFixDisplay(obj, pwidth, fsize) {
	// check for already extended Prototype object
	if(typeof obj == 'string') {
		obj = $(obj); if(!obj) return false;
	} else if(typeof obj == 'object') {
		if(typeof obj.writeAttribute != 'function') Element.extend(obj);
	}
	else return false;

	// check for type of round corner (1 or 2 pixel width type)
	var pw = 0;
	if(obj.hasClassName('rnd1_wrap')) pw = 1;		// left + right = 3 + 3 px
	else if(obj.hasClassName('rnd2_wrap')) pw = 2;	// left + right = 6 + 6 px
	if(!pw) return false;
	// optinal: setup width
	if(pwidth == "img") {		// only border a single image
		var iarr = obj.getElementsByTagName('IMG');
		if(iarr.length != 1) return false;
		if(pw == 1) pwidth =  6 + iarr[0].width;
		else if(pw == 2) pwidth =  12 + iarr[0].width;
	}
	if(typeof pwidth == "number" && pwidth > 0) obj.setStyle({width: pwidth + 'px'});

	// correct bottom right border display
	var co = obj.select('.rnd' + pw + '_cont');

	// difficult to find a Opera 9 specific CSS hack, 'cos it seems all working
	// hacks doesn't work any more with a current version (>= 9.64), so I switch
	// settings via JS. See CSS for details.
	if(Prototype.Browser.Opera) {
		if(pw == 1) co[0].setStyle({paddingBottom: "0"});
		else if(pw == 2) co[0].setStyle({paddingBottom: "2px"});	// + Opera inherit 4 px = 6 px
	}

	if(typeof fsize == "number" && fsize >= 9) co[0].setStyle({fontSize: fsize + 'px'});

	var h = co[0].getHeight();
	var bo = obj.select('.rnd' + pw + '_br');
	bo[0].setStyle({height: h + 'px'});	

	return obj;
}


/* This function takes an DOM object and puts the round corner
 * HTML code around it to display round corners. 
 * Also returns the container object on success, so additional
 * changes can be done, e.g. like adding CSS styles.
 *   obj      the object to extend
 *   cobjid   new container object id, if empty same as obj id, extended with "_rw1" or "_rw2"
 *   lwidth   line width of border (1|2)
 *   pwidth   width of box in pixel, optionally "img", so taken from image inside
 *   fsize    font size in pixel, needed for text, 'cos CSS sets 1px size by default
 * Returns Prototype extended container object or FALSE on error.
 * 
 * If inner element have a margin, height detection possibly fails, so margin
 * doesn't fit exactly. Additionally currently problems with IE7 if real content
 * should be wrapped and not only a single image.
 */
function buildImgRndBox(obj, cobjid, lwidth, pwidth, fsize) {
	// check for already extended Prototype object
	if(typeof obj == 'string') {
		obj = $(obj); if(!obj) return false;
	} else if(typeof obj == 'object') {
		if(typeof obj.writeAttribute != 'function') Element.extend(obj);
	}
	else return false;

	var rnclass, rncont, rcode;

	if(lwidth == 1) {
		rnclass = "rnd1_wrap"; rncont = "rnd1_cont";
		rcode = '<div class="rnd1_fbr"><div class="rnd1_tr"></div><div class="rnd1_br"></div></div><div class="rnd1_main"><div class="rnd1_tl"></div><div class="rnd1_cont"></div></div>';
	} else if(lwidth == 2) {
		rnclass = "rnd2_wrap"; rncont = "rnd2_cont";
		rcode = '<div class="rnd2_fbr"><div class="rnd2_tr"></div><div class="rnd2_br"></div></div><div class="rnd2_main"><div class="rnd2_tl"></div><div class="rnd2_cont"></div></div>';
	} else return false;
	if(!cobjid) cobjid = obj.id + "_rw" + lwidth;

	// wrap object with container, step one, use Element.wrap(), 'cos IE has problems if obj is a textarea element
	// after this obj still has it's reference to the original content object
	var wrap = Element.wrap(obj, 'div', {'id': cobjid, 'class': rnclass});
	// insert remaining round-corner-code
	wrap.update(rcode);
	// insert original content object into container
	var carr = wrap.getElementsBySelector('.' + rncont);
	if(carr[0]) carr[0].insert(obj, {position: 'top'}); else return false;

	return rndBoxFixDisplay(wrap, pwidth, fsize);
}
