// RoundCorners.js
// This file will produce rounded corners on elements with appropiate atributes. 

//First check the name space...

var com;
if (!com || !com.jchristy) { throw new Error("com.jchristy: Required object 'com.jchristy.Support' does not exist."); }
if (!com.jchristy.Handler) { throw new Error("com.jchristy: Required object 'com.jchristy.Handler' does not exist."); }

//Name space ready...

com.jchristy.RoundCorners = {};
com.jchristy.RoundCorners.add = function(ID) {
	if (!com.jchristy.Support.features.HTML1) { return; }
	
	var element = (typeof ID == "string") ? document.getElementById(ID) : ID;
	
	if (element) {
		
		var c = document.createDocumentFragment();
		var a = [];
		for (var g = element.firstChild; g != null; g = g.nextSibling) {
			a.push(g);	
		}
		for (var i = 0; i < a.length; i++) {
			c.appendChild(a[i]);	
		}
		var t = document.createElement("div");
		var n = t;
		
		var info = element.getAttribute("src");
		
		if (!info) { return; }
		
		var path = info.substr(0, info.indexOf("*"));
		var ext = info.substr(info.indexOf("*") + 1);
		
		
		
		var borders = element.getAttribute("borders");
		if (borders) { 
			borders = borders.split(" ");
			for (var i = 0; i < borders.length; i++) {
				var b = document.createElement("div");
				
				b.style.backgroundImage = "url(" + path + "border_" + borders[i] + ext + ")";
				
				switch (borders[i]) {
					case "b":
						b.style.backgroundPosition = "bottom";
						b.style.backgroundRepeat = "repeat-x";
						break
					case "t" :
						b.style.backgroundPosition = "top";
						b.style.backgroundRepeat = "repeat-x";
						break
					case "r" :
						b.style.backgroundPosition = "right";
						b.style.backgroundRepeat = "repeat-y";
						break
					case "l" :
						b.style.backgroundPosition = "left";
						b.style.backgroundRepeat = "repeat-y";
						break
				}
					n = n.appendChild(b);
			}
		}
		
		var corners = element.getAttribute("round");
		if (corners) { 
			corners = corners.split(" ");
			
			for (var i = 0; i < corners.length; i++) {
				var b = document.createElement("div");
				var v = (corners[i].charAt(0) == "t") ? "top" : "bottom";
				var h = (corners[i].charAt(1) == "r") ? "right" : "left";
				
					b.style.backgroundImage = "url(" + path + corners[i] + ext + ")";
					b.style.backgroundPosition = v + " " + h;
					b.style.backgroundRepeat = "no-repeat";
					
					n = n.appendChild(b);
			}
		}
		n.appendChild(c);
		element.appendChild(t);
	}
}

com.jchristy.RoundCorners.autoAdd = function(ID) {
	if (!com.jchristy.Support.features.HTML1) { return; }
	
	var allDivs = document.getElementsByTagName("div");
	
	for (var i = 0; i < allDivs.length; i++) {
		if (allDivs[i].getAttribute("round")) {
			com.jchristy.RoundCorners.add(allDivs[i]);	
		}
	}
	
}

com.jchristy.Handler.add(window, "load", com.jchristy.RoundCorners.autoAdd);
