function com_register_events() {
	//	Navigation buttons have mouseover and mouseouts
	var navbuttonNodes = document.getElementsByName("navdiv");
	for (i=0; i<navbuttonNodes.length; i++) {
		navbuttonNodes[i].addEventListener("mouseover",navbutton_handler,false);
		navbuttonNodes[i].addEventListener("mouseout",navbutton_handler,false);
	}
	
	//Header search box
	$("#search :input").focus(function() {
		if ($(this).val()=="SEARCH") { $(this).val(""); }
	});
	$("#search :input").blur(function() {
		if ($(this).val()=="") { $(this).val("SEARCH"); }
	});
}

function isChildOf(_child, _parent) {
	if (_parent === _child) return false;
	while (_child && _child !== _parent) { _child = _child.parentNode; }
	return _child === _parent;
}
function isChildOrSame(_child, _parent) {
	if (_parent === _child) return true;
	while (_child && _child !== _parent) { _child = _child.parentNode; }
	return _child === _parent;
}
function pop_window(url, width, height) {
	newwindow = window.open(url,'ccc_popup','height='+height+',width='+width+',scrollbars=yes');
	if (window.focus) newwindow.focus();
	return false;	
}

//handle textboxes in top box
function topbox_textbox_handler(e) {
	if (!e) var e = window.event;
	var elem = e.target;
	
	if (e.type=="focus") {
		if (!elem.initValue) elem.initValue = elem.value;
		if (elem.value==elem.initValue) elem.value="";
	}
	if (e.type=="blur") {
		if (elem.value=="") elem.value = elem.initValue;
	}
}

function navbutton_handler(e) {
	if (!e) var e = window.event;
	
	var srcElement_isChild = isChildOf(e.srcElement,this);
	var target_isChild = isChildOf(e.target,this);
	var relatedTarget_isChild = isChildOf(e.relatedTarget,this);
	
	//determine mouseEnter and mouseLeave states
	var mouseEnter = false;
	var mouseLeave = false;
	if (e.type=="mouseover") mouseEnter = !(relatedTarget_isChild);
	if (e.type=="mouseout") mouseLeave = (!mouseEnter && !relatedTarget_isChild);

	if (mouseEnter) animate_navbutton(this,-1);
	if (mouseLeave) animate_navbutton(this,1);
}

function animate_navbutton(elem, dir) {
	//clear any current timeouts
	window.clearInterval(elem.animNavbuttonTimeout);
	
	//constants
	var max_height = 45;
	var min_height = 0;
	var background_off = new Array(239,239,239);
	var background_on = new Array(0,0,0);
	var background_cur = new Array();

	//initialize animation
	var frame = 0;
	var speed = 1;
	var max_frames = 9;
	if (dir==-1) {
		if (elem.style.height=="") elem.style.height = max_height+"px";
		background_cur = background_off.slice(0);
		elem.style.borderColor = "#000";
	} else {
		background_cur = background_on.slice(0);
		elem.style.borderColor = "#919191";
	}
	elem.style.backgroundColor = "rgb("+background_cur[0]+","+background_cur[1]+","+background_cur[2]+")";
	
	
	elem.animNavbuttonTimeout = setInterval(function() {
		//get current top
		eHeight=parseInt(elem.style.height,10);
		//apply direction
		speed=speed*dir;
		//add speed to bottom
		elem.style.height=(eHeight+speed)+"px";
		//check top to make sure it hasn't gone too far
		eHeight=parseInt(elem.style.height,10);
		if (dir==-1 && eHeight<3) elem.style.height=min_height+"px";
		
		//adjust p
		speed=(frame+15)/3;
		//increment frame
		frame++;
		
		//colors
		var stepR = dir * (background_off[0] - background_on[0]) / max_frames;
		var stepG = dir * (background_off[1] - background_on[1]) / max_frames;
		var stepB = dir * (background_off[2] - background_on[2]) / max_frames;
		background_cur[0] = background_cur[0] + stepR;
		background_cur[1] = background_cur[1] + stepG;
		background_cur[2] = background_cur[2] + stepB;
		elem.style.backgroundColor = "rgb("+
			Math.round(background_cur[0])+","+
			Math.round(background_cur[1])+","+
			Math.round(background_cur[2])+")";
		
		//debug output
		/*
		document.getElementById("output").innerHTML=
			"elem.style.backgroundColor: " + elem.style.backgroundColor +
			"<br>stepR = " + stepR +
			" &nbsp; stepG = " + stepG +
			" &nbsp; stepB = " + stepB +
			"<br>background_cur[0] = " + background_cur[0] +
			"<br>background_cur[1] = " + background_cur[1] +
			"<br>background_cur[2] = " + background_cur[2] +
			"<br>max_frames = " + max_frames +
			"<br>frame = " + frame +
			"";
		*/
		
		//finish?
		if (eHeight>max_height || eHeight<min_height || frame>=max_frames) {
			window.clearInterval(elem.animNavbuttonTimeout);
			elem.animFloatboxTimeout=0;
			//final placements
			if (dir==-1) {
				elem.style.height=min_height+"px";
				elem.style.backgroundColor =
					"rgb("+background_on[0]+","+background_on[1]+","+background_on[2]+")";
			} else {
				elem.style.height=max_height+"px";
				elem.style.backgroundColor =
					"rgb("+background_off[0]+","+background_off[1]+","+background_off[2]+")";
			}
		}
	},
	//framerate
	16);			
}
