////////////////////////////////////////////////////////////////////////////////////////////////////////
// OC Rounding 1.0.js - Based on Nifty Corners http://www.html.it/articoli/niftycube/index.html
// 
// In affiliation with OC Food Review
// By: Tawin Kiethanom 08/03/2006
////////////////////////////////////////////////////////////////////////////////////////////////////////

function Rounded(selector, type, outerColor, innerColor, border){
	//Call the function to get array of elements
	var element_array = getElementsBySelector(selector);
	
	//Declare variables for top and bottom types
	var top_type = null;
	var bottom_type = null;
	//Declare variables for actual completed tags;
	var top_tags;
	var bottom_tags;
	
	//Find out which types will be used on top and bottom.
	if(type.match("all")){
		top_type = "ar";
		bottom_type = "ar";
	}
	if(type.match("top")){
		top_type = "ar";
	}
	if(type.match("bottom")){
		bottom_type = "ar";  
	}
	if(type.match("tl")){
		top_type = "lr";
	}
	if(type.match("tr")){
		top_type = "rr";
	}
	if(type.match("bl")){
		bottom_type = "lr";
	}
	if(type.match("br")){
		bottom_type = "rr";
	}
	
	function createTop(){
		//Create top tags here
		if(top_type != null){
			var top = document.createElement("b");
			top.className = top_type+"top";
			top.style.backgroundColor = outerColor;
			var t1 = document.createElement("b");
			t1.className = top_type+"b1";			
			t1.style.backgroundColor= border;
			var t2 = document.createElement("b");
			t2.className = top_type+"b2";
			t2.style.backgroundColor= innerColor;
			t2.style.borderColor = border;
			var t3 = document.createElement("b");
			t3.className = top_type+"b3";
			t3.style.backgroundColor= innerColor;
			t3.style.borderColor = border;
			var t4 = document.createElement("b");
			t4.className = top_type+"b4";
			t4.style.backgroundColor= innerColor;
			t4.style.borderColor = border;
			
			if(border == "transparent"){				
				t1.style.backgroundColor= innerColor;
				t2.style.border = 0;
				t3.style.border = 0;
				t4.style.border = 0;
			}
			top.appendChild(t1);
			top.appendChild(t2);
			top.appendChild(t3);
			top.appendChild(t4);
			//assign all the work we've done to this global var
			top_tags = top;
		}
	}
	
	//Create bottom tags here
	function createBottom(){
		if(bottom_type != null){
			var bottom = document.createElement("b");
			bottom.className = bottom_type+"bottom";
			bottom.style.backgroundColor = outerColor;
			var b1 = document.createElement("b");
			b1.className = bottom_type+"b1";
			b1.style.backgroundColor= border;
			var b2 = document.createElement("b");
			b2.className = bottom_type+"b2";
			b2.style.backgroundColor= innerColor;
			b2.style.borderColor = border;
			var b3 = document.createElement("b");
			b3.className = bottom_type+"b3";
			b3.style.backgroundColor= innerColor;
			b3.style.borderColor = border;
			var b4 = document.createElement("b");
			b4.className = bottom_type+"b4";
			b4.style.backgroundColor= innerColor;
			b4.style.borderColor = border;
	
			if(border == "transparent"){				
				b1.style.backgroundColor= innerColor;
				b2.style.border = 0;
				b3.style.border = 0;
				b4.style.border = 0;
			}
			bottom.appendChild(b4);
			bottom.appendChild(b3);
			bottom.appendChild(b2);
			bottom.appendChild(b1);
			//assign all the work we've done to this global var
			bottom_tags = bottom;
		}
	}
	
	
	//for loop to go through all the tag elements with the correct ids
	for(i=0; i <  element_array.length ; i++){
		if(top_type != null){			
			createTop();			
			element_array[i].insertBefore(top_tags,element_array[i].firstChild);
			//element_array[i].lastChild.style.borderTopColor = "white";
			
		}
		if(bottom_type != null){
			createBottom();
			//element_array[i].lastChild.style.borderBottomColor = "black";
			element_array[i].appendChild(bottom_tags,element_array[i].firstChild);
			
			
		}
    }
}
	

//Gets elements for you ... Modified nifty corner code to handle multiple tags with same id
function getElementsBySelector(selector){
	var i;
	var s=[];
	var selid="";
	var selclass="";
	var tag=selector;
	var objlist=[];
	if(selector.indexOf(" ")>0){  //descendant selector like "tag#id tag"
		s=selector.split(" ");
		var fs=s[0].split("#");
		if(fs.length==1) {
			return(objlist);
		}
		return(document.getElementById(fs[1]).getElementsByTagName(s[1]));
	}
	if(selector.indexOf("#")>0){ //id selector like "tag#id"
		s=selector.split("#");
		tag=s[0];
		selid=s[1];
		}
	if(selid!=""){
			var v=document.getElementsByTagName(tag);  // tag selector like "tag"
			for(i=0;i<v.length;i++){
			if(v[i].id==selid){
				objlist.push(v[i]);
				}
			}
			return(objlist);
		}
	if(selector.indexOf(".")>0){  //class selector like "tag.class"
		s=selector.split(".");
		tag=s[0];
		selclass=s[1];
		}
	var v=document.getElementsByTagName(tag);  // tag selector like "tag"
	if(selclass=="")
		return(v);
	for(i=0;i<v.length;i++){
		if(v[i].className==selclass){
			objlist.push(v[i]);
			}
		}
	return(objlist);
}

