//requires Prototype JS Library
// 


function be_ajaxtoc_handle(work_id, url, depth, checkbox) {
	var node_obj, new_ajax, image_obj, image_left;
	node_obj = $("be_child_div_" + work_id);
	image_obj = $('be_image_sign_' + work_id);
	image_left = image_obj.offsetLeft;
//	alert(image_left);
	if(node_obj.style.display == 'block') {
		if(image_obj.src == url + 'themes/_contents/minusbottom.gif') {
			image_obj.src = url + 'themes/_contents/plusbottom.gif';
			new_ajax = new Ajax.Request(
				url + 'handler/a?a=disable_tree_node&nid=' + work_id + '&checkbox=' + checkbox,
				{
					asynchronous: true,
					method: 'post'
					});
		}
		else {
			image_obj.src = url + 'themes/_contents/plus.gif';
		}
		node_obj.style.display = 'none';
	}
	else {
		node_obj.style.display = 'block';
		if(node_obj.innerHTML != '') {
			image_obj.src = url + 'themes/_contents/minus.gif';
		}
		else {
			node_obj.innerHTML = '<i style="padding-left: 30px;">Loading&nbsp;&hellip;</i>';
//			$('be_toc_message_' + work_id).left = image_left + 'px';
			new_ajax = new Ajax.Updater(
				node_obj,
				url + 'handler/a?a=draw_tree&nid=' + work_id + '&depth=' + depth + '&checkbox=' + checkbox,
				{
					asynchronous: true,
					method: 'post',
					evalScripts: true,
					onComplete: function() {
						if(image_obj.src == url + 'themes/_contents/plusbottom.gif') {
							image_obj.src = url + 'themes/_contents/minusbottom.gif';
						}
						else {
							image_obj.src = url + 'themes/_contents/minus.gif';
						}
					}
				}
			);
		}
	}
}

function be_ajaxtoc_set() {

}

function be_ajaxtoc_meta(work_id, url) {
	var node_obj, node_content_obj, new_ajax, positions, curleft, curtop, curheight, curwidth, message, type_node;
	var noPx = document.childNodes ? 'px' : 0;
	node_obj = $('be_ajaxtoc_meta_div_' + work_id);
	node_content_obj = $('be_ajaxtoc_meta_content_' + work_id);
	type_node = $('be_metadata_type_' + work_id);
	if(node_obj.style.display != 'block') {
		hide_all_metadata_divs("be_meta_div_" + work_id);
		if(node_content_obj.innerHTML != '') {
			node_obj.style.display = 'block';
		}
		else if(node_content_obj) {
			new_ajax = new Ajax.Request(
				url + 'handler/a?a=toc_metadata&nid=' + work_id,
					{
						asynchronous: false,
						method: 'post',
						onComplete: function(returned_object) {
							if(returned_object.responseText != '') {
								if (type_node.offsetParent) {
									curleft = type_node.offsetLeft
									curtop = type_node.offsetTop
								}
								node_content_obj.innerHTML = returned_object.responseText;
								node_obj.style.display = 'block';
								node_obj.style.zindex = 1000;
								node_obj.style.bottom = (parseInt(curtop) + 10) + noPx;
								node_obj.style.left = (parseInt(curleft) + 10) + noPx;
							}
						}
					}
				);
		}
		else {
			alert("Can't find content Div!");
			
		}
	}
	else {
		node_obj.style.display = 'none';
	}
}

function hide_all_metadata_divs(current_id) {
	var all_elements, i;
	all_elements = document.getElementsByClassName('be_ajaxtoc_meta');
	for(i = 0; i < all_elements.length; i++) {
		if(all_elements[i].id != current_id) {
			all_elements[i].style.display = 'none';
		}
	}
}

function be_ajaxtoc_close_metadata(work_id) {
	var node_obj;
	node_obj = $("be_ajaxtoc_meta_div_" + work_id);
	node_obj.style.display = 'none';
}

function findPos(obj) {
	// from http://www.quirksmode.org/js/findpos.html
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
		curleft += obj.offsetLeft
		obj = obj.offsetParent;
		}
	}
	else if (obj.x)
	curleft += obj.x;
	return curleft;
}

function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
		curtop += obj.offsetTop
		obj = obj.offsetParent;
		}
	}
	else if (obj.y)
	curtop += obj.y;
	return curtop;
}

function getNodeState(node_id) {
	var node_obj;
	
	node_obj = $(node_id);
	

}

function ajaxTocCheckboxToggle(this_checkbox_object, children, parent_id) {
	var div_object, parent_object;
	div_object = $('becheckdiv_' + this_checkbox_object.value);
	if(this_checkbox_object.checked == false) {
		if(div_object) {
			ajaxTocCheckboxToggleChildren(div_object, false);
		}
		ajaxTocCheckboxUncheckParent(this_checkbox_object);
	}
	else if(div_object) {
		ajaxTocCheckboxToggleChildren(div_object, true);
	}
}

function ajaxTocCheckboxToggle_orig(this_checkbox_object, children, parent_id) {
	var div_object, parent_object;
	div_object = $('be_child_div_' + this_checkbox_object.value);
	if(this_checkbox_object.checked == false) {
		if(div_object) {
			ajaxTocCheckboxToggleChildren(div_object, false);
		}
		ajaxTocCheckboxUncheckParent(this_checkbox_object);
	}
	else if(div_object) {
		ajaxTocCheckboxToggleChildren(div_object, true);
	}
}

function ajaxTocCheckboxToggleChildren(this_parent, state) {
	var children, i;
	children = this_parent.getElementsByTagName('input');
	for(i = 0; i < children.length; i++) {
		children[i].checked = state;
	}
}

function ajaxTocCheckboxUncheckParent(this_checkbox_object) {
	var parent_id, parent_object, next_object;
	if(this_checkbox_object.id != 0) {
		parent_id = this_checkbox_object.title;
		parent_object = $("becheck_" + parent_id);
		if(parent_object) {
			parent_object.checked = false;
			ajaxTocCheckboxUncheckParent(parent_object);
		}
	}
}

function ajaxTocCheckboxUncheckChildren(this_parent_id) {
	var this_parent, children, i;
	this_parent = $(this_parent_id);
	if(this_parent) {
		children = this_parent.getElementsByTagName('input');
		for(i = 0; i < children.length; i++) {
			children[i].checked = false;
		}
	}
}

function ajaxTocCheckboxShowHide(this_id, image_root) {
	var action, image_object, children_div;
	// is it open or closed?
	image_object = $('becheckimage_' + this_id);
	action = 'close';	
	children_div = $('becheckdiv_' + this_id);
	if(image_object.alt == 'minusbottom' ) {
		image_object.alt = 'plusbottom';
		image_object.src = image_root + 'plusbottom';
		children_div.style.display = 'none';		
	}
	else if(image_object.alt == 'minus' ) {
		image_object.alt = 'plus';
		image_object.src = image_root + 'plus';
		children_div.style.display = 'none';		
	}
	else if(image_object.alt == 'plusbottom' ) {
		image_object.alt = 'minusbottom';
		image_object.src = image_root + 'minusbottom';
		children_div.style.display = 'block';		
	}
	else if(image_object.alt == 'plus' ) {
		image_object.alt = 'minus';
		image_object.src = image_root + 'minus';
		children_div.style.display = 'block';		
	}
}
