var csvar_allowEdit = false;

function showBC() {
    var root = document.getElementsByTagName("body");
    root = root[0];
	//remove any existing tags
	traverseBCTag(root);
    traverse(root);
}
function editBC() {
	csvar_allowEdit = true;
	showBC();
}

function traverse(startingElement) {
//traverse DOM of passed in element
	if (startingElement.nodeType == 1) {
		//it's an element node
		var childEl = startingElement.childNodes;
		var aElements = new Array(); //new array to hold consecutive collapsed elements.
		for (var i=0; i < childEl.length; i++) {
			curId = childEl[i];
			if (cs_isBC(curId.id)) {
                if (curId.style) {
                    addTag(curId);
                }
			} 
			traverse(curId);
		}
	}
}
function cs_isBC(id){
    if (id) {
        if (id.length != 0) {
            if (id.substring(0, 3).toUpperCase() == "BC-") {
                //it's a bc
                if (!isNaN(id.substring(4))) {
                    return true;
                }
            }
        }
    }
}

function traverseBCTag(startingElement) {
//traverse DOM of passed in element
	if (startingElement.nodeType == 1) {
		//it's an element node
		var childEl = startingElement.childNodes;
		var aElements = new Array(); //new array to hold consecutive collapsed elements.
		for (var i=0; i < childEl.length; i++) {
			curId = childEl[i];
			traverseBCTag(curId);

			if (cs_isBCTag(curId)) {
				removeElement(curId);
			} 
		
		
		}
	}
}

function cs_isBCTag(el){
    if (el.bcId) {
		return true;
    }
}

function addTag(el) {
	
	var bodyEl = document.getElementsByTagName("BODY")[0];
	var newDiv = document.createElement("div");

	newDiv.style.display="block";
	newDiv.style.position = "absolute";
	newDiv.style.top = getAbsoluteTop(el);
	newDiv.style.left = getAbsoluteLeft(el);
	
	cs_setOpacityIndex (newDiv, "75", "1000");
	
	newDiv.style.border = "1px solid black"
	newDiv.style.backgroundColor="yellow";

	var newText = document.createTextNode("ID=" + el.id);
	newDiv.appendChild(newText);	
	newDiv.appendChild(document.createElement("br"));
	var newText = document.createTextNode("Width=" + getElWidth(el));
	newDiv.appendChild(newText);	
	newDiv.appendChild(document.createElement("br"));
	var newText = document.createTextNode("Height=" + getElHeight(el));
	newDiv.appendChild(newText);

	var floater = createFloater(el);

	floater.id = el.id + "_floater"
	newDiv.floaterId = floater.id;

	newDiv.id = el.id + "_tag"
	floater.tagId = newDiv.id;

	floater.bcId = el.id;
	newDiv.bcId = el.id;

	bodyEl.appendChild(floater);
	bodyEl.appendChild(newDiv);

	if (csvar_allowEdit) {
		newDiv.onmouseover = highlight;
		newDiv.onmouseout = dim;
		newDiv.onclick = cs_click;
	}
}


function createFloater(el) {

	var newDiv = document.createElement("div");

	newDiv.style.display="block";
	newDiv.style.position = "absolute";
	newDiv.style.top = getAbsoluteTop(el);
	newDiv.style.left = getAbsoluteLeft(el);
	newDiv.style.width = getElWidth(el);
	newDiv.style.height = getElHeight(el);

	newDiv.style.backgroundColor="gray";

	cs_setOpacityIndex (newDiv, "30", "999");


	if (csvar_allowEdit) {
		cs_setOpacityIndex (newDiv, "20", "999");
		cs_setBorder (newDiv);
		newDiv.onmouseover = highlight;
		newDiv.onmouseout = dim;
		newDiv.onclick = cs_click;
	}

	return newDiv;

}

function getElWidth(el) {
	if (el.style.pixelWidth) {
		return el.style.pixelWidth;
	}
    if (el.style.width) {
        return el.style.width;
    } 
    return el.offsetWidth + 'px';
}
function getElHeight(el) {
	if (el.style.pixelHeight) {
		return el.style.pixelHeight;
	}
    if (el.style.height) {
        return el.style.height;
    }
    return el.offsetHeight + 'px';
}
function getAbsoluteLeft(o) {
	oLeft = o.offsetLeft;
	while(o.offsetParent!=null) {
			oParent = o.offsetParent;
			oLeft += oParent.offsetLeft;
			o = oParent;
	}
	return oLeft + "px";
}
function getAbsoluteTop(o) {
	oTop = o.offsetTop;
	while(o.offsetParent!=null) {
			oParent = o.offsetParent;
			oTop += oParent.offsetTop;
			o = oParent;
	}
	return oTop + "px";
}



function findOwner(e)
{
	var targ;
	if (!e) var e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;
	return targ;
}

function highlight(evt)
{
    var obj = findOwner(evt);
	if (obj.tagId) {
		var el = document.getElementById(obj.tagId);
		if (el) {
			cs_setOpacityIndex (el, "99", "2000");
		}
			//cs_setBorder (obj);
	}
	if (obj.floaterId) {
		var el = document.getElementById(obj.floaterId);
		if (el) {
			//cs_setBorder (el);
		}
		cs_setOpacityIndex (obj, "99", "2000"); 
	}
}

function dim(evt)
{
    var obj = findOwner(evt);
	if (obj.floaterId) {
		var el = document.getElementById(obj.floaterId);
		if (el) {
			//el.style.border="";
		}
		cs_setOpacityIndex (obj, "75", "1000");
	}

	if (obj.tagId) {
		var el = document.getElementById(obj.tagId);
		if (el) {
			cs_setOpacityIndex (el, "75", "1000");
		}
			//obj.style.border=""
	}

}

function cs_setOpacityIndex (el, level, zIndex) {
	el.style.KhtmlOpacity ="." + level;
	el.style.opacity = "." + level;
	el.style.filter="alpha(style=0, opacity=" + level + ")";
	el.style.MozOpacity ="." + level;
	el.style.zIndex = zIndex;
}

function cs_setBorder (el) {
	el.style.borderWidth ="2px";
	el.style.borderStyle = "solid";
	el.style.borderColor = "red";
}
function cs_setBackground (el) {
	el.style.backgroundColor = "transparent";
}

function cs_click(evt)
{
    var obj = findOwner(evt);
	if (obj.bcId) {
		cs_openEditor(obj.bcId);
		if (obj.floaterId) {
			//this is the tag
            //set opacity of float
			var el = document.getElementById(obj.floaterId);
			cs_setOpacityIndex (el, "99", "1000");
			cs_setBackground (el);
			cs_setBorder (el);
			el.onmouseover = null;
			el.onmouseout = null;
			//remove tag
			removeElement(obj);
		}
		if (obj.tagId) {
			//this is the float
			var el = document.getElementById(obj.tagId);
			removeElement(el);
			//set opacity of float
			cs_setOpacityIndex (obj, "99", "1000");
			cs_setBackground (obj);
			cs_setBorder (obj);
			obj.onmouseover = null;
			obj.onmouseout = null;
		}
	}
}

var csvar_EditId;

function cs_openEditor(elId) {
	csvar_EditId = elId;
	var newWin = window.open ("contentEditor.htm", 'newWin', 'toolbar=no, directories=no, location=no, status=yes, menubar=no, resizable=yes, scrollbars=no, width=700, height=350');
	newWin.focus();
}

function replaceContent(elId, content) {
	var obj = document.getElementById(elId);
	obj.innerHTML = content;
}

function removeElement(el) {
	var parentEl = el.parentNode;
	parentEl.removeChild(el);
}




