/*		rcGeorg.js

by Paul Novitski - JuniperWebcraft.com
23 July 2006
*/

//=========================
// values you can change to affect the program
//=========================
// this class identifies the blocks that this script will transform
// (you can use this class to style the boxes if JavaScript is disabled)
var sRcClassBefore = "rc0";

// this class identifies the transformed rounded corner boxes
var sRcClassAfter = "rc";

// class name of the outermost of the two inner wrappers of the content
var sRCOuterWrapClass = "wr";

// top div prefix
var sRCTopPrefix = "t";

// number of top divs before the content
var iRCTopDivs = 7;

// bottom div prefix
var sRCBottomPrefix = "b";

// number of bottom divs after the content
var iRCBottomDivs = 7;

// content (if any) for the top & bottom divs
var sRCDivContent = "";

// generated source formatting -- change these two lines if you need to eliminate whitespace between tags
var sRCNewline = "\n";
var sRCTab = "\t";

// tabs before divs
var sRCTabsBeforeDivs = sRCTab + sRCTab;


//=========================
// run early (DOMContentLoaded)
//      see: http://www.kryogenix.org/days/2007/09/26/shortloaded
//=========================

(function(i) {
  var u = navigator.userAgent.toLowerCase();
  var ie = /*@cc_on!@*/false;
  if (/webkit/.test(u)) {
    // safari
    timeout = setTimeout(function(){
			if ( document.readyState == "loaded" || 
				document.readyState == "complete" ) {
				i();
			} else {
			  setTimeout(arguments.callee,10);
			}
		}, 10); 
  } else if ((/mozilla/.test(u) && !/(compatible)/.test(u)) ||
             (/opera/.test(u))) {
    // opera/moz
    document.addEventListener("DOMContentLoaded",i,false);
  } else if (ie) {
    // IE
    (function (){ 
      var tempNode = document.createElement('document:ready'); 
      try {
        tempNode.doScroll('left'); 
        i(); 
        tempNode = null; 
      } catch(e) { 
        setTimeout(arguments.callee, 0); 
      } 
    })();
  } else {
    window.onload = i;
  }
})(rcTransform);



//=========================
// run initialization function on page load (preserving any existing onload function)
//=========================
var loadRC = window.onload;

window.onload = function()
{
		if (loadRC) loadRC();
	rcTransform();
}


//=========================
function rcTransform()
//=========================
// transform rounded corner boxes
{
		// bail if not DOM-aware
		if (!document.getElementsByTagName) return;

	// look at all tags on the page
	var aEls = document.getElementsByTagName("*");

	// regular expression to locate BEFORE class
	var rRegExp = rcRegExp();

	do
	{
		bFoundOne = false;

		for (var iEl = 0; iEl < aEls.length; iEl++)
		{
			if (aEls[iEl].className && rRegExp.test(aEls[iEl].className))
			{
				bFoundOne = true;
				rcTransformOne(aEls[iEl]);
			}
		}
	}
	while (bFoundOne == true);
}


//=========================
function rcTransformOne(argBox)
//=========================
// pass:	argBox = element with the rounded corners class
{
	//alert("function rcTransformOne(" + argBox.tagName + "." + argBox.className + ")");

	// copy the box but not its content
	var oNewBox = argBox.cloneNode(false);
	
	// regular expression to locate BEFORE class
	var rRegExp = rcRegExp();
	
	// change the class name from BEFORE to AFTER
	oNewBox.className = oNewBox.className.replace(rRegExp, "$1" + sRcClassAfter + "$2");	

	oNewBox.innerHTML = sRCNewline;
		
	//-------------------------
	// add top divs
	//-------------------------
	rcAddDivs(oNewBox, iRCTopDivs, sRCTopPrefix, 1);

	//-------------------------
	// outer wrapper
	//-------------------------
	var oOuterWrap = document.createElement("div");
	oOuterWrap.innerHTML = sRCNewline + sRCTabsBeforeDivs + sRCTab;
	oOuterWrap.className = sRCOuterWrapClass;

	//-------------------------
	// inner wrapper
	//-------------------------
	var oInnerWrap = document.createElement("div");

	//-------------------------
	// copy old box contents into new inner wrapper
	//-------------------------
	var aOldChildren = argBox.childNodes;
	//alert("Copying " + aOldChildren.length + " old children");
	
	for (var iOld = 0; iOld < aOldChildren.length; iOld++)
	{
		// clone child node with all its contents
		var oNewChild = aOldChildren[iOld].cloneNode(true);
		//alert("oNewChild.tagName = " + oNewChild.tagName);

		// add cloned child to new inner div
		oInnerWrap.insertBefore(oNewChild, null);
	}

	//-------------------------
	// add inner wrap to outer
	//-------------------------
	oInnerWrap.innerHTML += sRCTabsBeforeDivs;

	oOuterWrap.insertBefore(oInnerWrap, null);

	oOuterWrap.innerHTML += sRCNewline + sRCTabsBeforeDivs;

	//-------------------------
	// add outer wrap to parent
	//-------------------------
	oNewBox.innerHTML += sRCTabsBeforeDivs;
	oNewBox.insertBefore(oOuterWrap, null);
	oNewBox.innerHTML += sRCNewline + sRCTab;

	//-------------------------
	// add bottom divs
	//-------------------------
	rcAddDivs(oNewBox, iRCBottomDivs, sRCBottomPrefix, -1);

	//-------------------------
	// replace old box with new
	//-------------------------
	var oParent = argBox.parentNode;
	oParent.replaceChild(oNewBox, argBox);

	// we're done
	//alert(oNewBox.parentNode.innerHTML);
}


//=========================
function rcAddDivs(oParent, iDivCount, sDivPrefix, iDirection)
//=========================
// add a number of divs to the box to hold the rounded corner elements
// pass:	oParent = container
//			iDivCount= number of divs to add
//			sDivPrefix = class name prefix
//			iDirection = +1 if ascending (top), -1 if descending (bottom)
{
		if (iDirection == -1)
		{
			var iStart = iDivCount - 1;		// b6 - b0
		}else
		{
			var iStart = 0;					// t0 - t6
		}
	
	// loop to generate divs
	for (var i = 0; i < iDivCount; i++)
	{
		var iDivNo = (i * iDirection) + iStart;
	
		var oChild = document.createElement("div");
		oChild.className = sDivPrefix + iDivNo;
		
		// insert dummy content into child
		oChild.innerHTML = sRCDivContent;
		
		// insert child into parent
		oParent.innerHTML += sRCTabsBeforeDivs;
		oParent.insertBefore(oChild, null);		
		oParent.innerHTML += sRCNewline;
	}
}


//=========================
function rcRegExp()
//=========================
{
	// regular expression to search for rounded corners BEFORE class
	var rRegExp = new RegExp("(^| )" + sRcClassBefore + "( |$)");
	
	return rRegExp;
}


//=========================
function rcAppendClassName(argObj, argClassname)
//=========================
{
		if (!argObj.className)
		{
			argObj.className = argClassname;
		}else{
			argObj.className += " " + argClassname;
		}
}

