// automatically show or hide a div.
function autoShowHideBlock(block_id, css_class_display, css_class_hide, callback) {
	var block_obj = document.getElementById(block_id);
	var display_class = "";
	var hide_class = "";
	
	// get the display class & hide class
	if (css_class_display) display_class = css_class_display;
	if (css_class_hide) hide_class = css_class_hide;
	
	var return_class = display_class;
	if (block_obj == null) return return_class;
	if (block_obj.style.display == "none") {
		block_obj.style.display = "";
		return_class = display_class;
	}
	else {
		block_obj.style.display = "none";
		return_class = hide_class;
	}
	
	// call the callback
	if (callback) {
		eval(callback + "(block_obj)");
	}
	
	return return_class;
}

// Show / Hide the block specified by block_id
function showBlock(block_id, show) {
	var block_obj = document.getElementById(block_id);
	if (block_obj == null) return;
	block_obj.style.display = (show) ? "" : "none";
}

// find the top & left position of this object
function findPos(obj) {
	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];
}

// show loading bar
function showLoadingBar(loading_id) {
	var wait_obj = document.createElement("div");
	wait_obj.id = loading_id;
	document.body.appendChild(wait_obj);
	return wait_obj;
}

// hide loading bar
function hideLoadingBar(wait_obj) {
	if (wait_obj)
		document.body.removeChild(wait_obj);
	wait_obj = null;
}

// show loading bar inside an element (requires prototype.js)
// args[1] = loading image url. If not defined, use "/admin/layout/skins/bannerpod/images/loading.gif"
function showLoadingBarInsideElement(elem) {
	var obj = $(elem);
	
	var loading_img = "/admin/layout/skins/bannerpod/images/loading.gif";
	if (arguments.length > 1)
		loading_img = arguments[1];
	
	// create the loading object
	var loading_obj = document.createElement("div");
	loading_obj.style.background = "url(" + loading_img + ") no-repeat center center";
	loading_obj.style.width = "100%";
	loading_obj.style.height = "100%";
	
	// add the loading bar
	if (obj.firstChild)
		obj.insertBefore(loading_obj, obj.firstChild);
	else
		obj.appendChild(loading_obj);
	
	return loading_obj;
}

// hide loading bar (requires prototype.js)
function hideLoadingBarInsideElement(obj) {
	var loading_obj = $(obj);
	if (loading_obj) {
		try { loading_obj.remove(); } catch (e) {}
	}
}

// create loading bar object
function createLoadingBar() {
	var loading_obj = new Object();
	
	var options = Object.extend({
		image: {
			src: "/images/please_wait.gif",
			classname: "loadingImg"
		},
		background: {
			classname: "loadingGen loadingBkgnd"
		}
	}, arguments[0] || {});

	// create holder for loading image & loading message
	var div_loading_msg = new Element("div", {
		className: "loadingGen"
	});
	
	// loading image
	var obj = new Element("img", {
		src: options.image.src,
		className: options.image.classname
	});
	div_loading_msg.appendChild(obj);
	
	/* loading message
	obj = document.createElement("textarea");
	obj.className = "loadingGen loadingMsg";
	obj.id = "loadingMsgArea";
	obj.readOnly = true;
	obj.style.display = "none";
	div_loading_msg.appendChild(obj);
	*/
	
	// create the background
	var div_loading_bkgnd = new Element("div", {
		className: options.background.classname
	});
	document.body.appendChild(div_loading_bkgnd);

	// add the loading bar
	document.body.appendChild(div_loading_msg);
	
	loading_obj.bkgnd = div_loading_bkgnd;
	loading_obj.msg = div_loading_msg;
	
	return loading_obj;
}

// delete loading bar object
function deleteLoadingBar(loading_obj) {
	if (!loading_obj) return;
	
	for (var obj in loading_obj) {
		if (loading_obj[obj]) {
			try { $(loading_obj[obj]).remove(); }
			catch (e) {}
		}
	}
}

// show the popup block
function popupBlock(obj_clicked, popup_block_id, show_direction, popup_zindex) {
	// check if the popup block exists
	var popup_block_obj = document.getElementById(popup_block_id);
	if (!popup_block_obj) return;
	
	// show the popup block so we can retrieve the size
	popup_block_obj.style.display = "";
	
	// find the position of the clicked object
	var objPos = findPos(obj_clicked);
	
	/*
	//var objPos = Position.cumulativeOffset(obj_clicked);
	console.log("page pos: " +
					"\n\tpage: " + Position.page(obj_clicked) +
					"\n\tcumulativeOffset: " + Position.cumulativeOffset(obj_clicked) +
					"\n\tpositionedOffset: " + Position.positionedOffset(obj_clicked) +
					"\n\trealOffset: " + Position.realOffset(obj_clicked));
	//*/
	
	// set the coord to display the popup block
	var coord = new Array();
	show_direction = show_direction.toLowerCase();
	if (show_direction == "down") {
		// show the popup block below the object clicked
		coord[0] = objPos[0];
		coord[1] = objPos[1] + obj_clicked.offsetHeight;
		
	} else if (show_direction == "right") {
		// show the popup block to the right of the object clicked
		coord[0] = objPos[0] + obj_clicked.offsetWidth;
		coord[1] = objPos[1];
		
	} else if (show_direction == "left") {
		// show the popup block to the left of the object clicked
		coord[0] = objPos[0] - popup_block_obj.offsetWidth;
		coord[1] = objPos[1];
		
	} else if (show_direction == "up") {
		// show the popup block above the object clicked
		coord[0] = objPos[0];
		coord[1] = objPos[1] - popup_block_obj.offsetHeight;
		
	}else {
		// default is the same as the object clicked
		coord[0] = objPos[0];
		coord[1] = objPos[1];
	}

	// show the popup block
	popup_block_obj.style.position = "absolute";
	popup_block_obj.style.left = coord[0] + "px";
	popup_block_obj.style.top = coord[1] + "px";
	popup_block_obj.style.zIndex = "" + popup_zindex;
}

