/*
Cómo usar expander
Colocar en la cabecera la siguiente línea:
<script language="javascript" src="scripts/clickExpander.js"></script>
Requiere prototype.js
*/


var mouseOverExecuted;
var lastExpanded;

Event.observe(document,"mouseup",clickHandler);
Event.observe(document,"mouseover",clickHandler);
Event.observe(document,"mouseout",clickHandler);

function clickExpander() {
	$$(".expander").each(function (e) {
		$$("#" + e.className.split(" ")[1].strip()).invoke("hide");
	});
}

function toggleExpander(oid) {
	o = $(oid);
	if (o == window.undefined) {
		return false;
	}

	if (lastExpanded != window.undefined) {
		if (lastExpanded == oid) {
			if (o.style.display == "none") {
				activate(oid);
				o.style.display = "block";

			}
			else {
				deactivate();
				o.style.display = "none";
			}
		}
		else {
			activate(oid);
			o.style.display = "block";
			contract(lastExpanded);
		}
	}
	else {
		activate(oid);
		o.style.display = "block";
	}
	document.location= "#"+oid+"_anchor";
	lastExpanded = oid;
}

function contract(oid) {
	if (o = $(oid)) {
		o.hide();
	}
	else return false;
}

function deactivate() {
	$$("expander").invoke("removeClassName","active")
}

function activate(oid) {
	deactivate();
	$$("expander "+ oid).invoke("addClassName","active");
}


function clickHandler(e) {
	var targ,executed,step,max,action,params,linkContainer;

	executed = false;

	targ = Event.element(e);
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.up();

	//Find out element's asociated action
	do {
		if (targ.id) { //It's in the ID
			action = targ.id; //Action is the id, no params
		}
		else if (targ.className) { //Try the className
			action = targ.className.split(" ")[0].strip(); //First word is the action
			params = targ.className.split(action)[1].strip().split(" "); //Other words are parameters
		}
		if (!targ.up) {
			return true;
		}
	}
	while (!action && (targ = targ.up()));

	if (!action) { //Clicked element has no action associated
		return true;
	}

	//Call functions according to action
	switch (e.type) {
		case "mouseup":
			switch (action) {
				case "expander":
					executed = true;
					toggleExpander(params[0]);
					break;
				default:
					executed = false;
					if (Event.element(e).href == window.undefined) {
						
						contract(lastExpanded);
					}
					break;
			}
			break;
		case "mouseout":
			//First should not be mouseout
			if (!mouseOverExecuted)	{
//				return false;
			}
		case "mouseover":
			//Flag this for mouseouts to work
			mouseOverExecuted = true;

			//Find the link container
			if ((action == "linkContainer") && (linkContainer = targ)) { } //Mouse is over linkContainer
			else if (targ.up(".linkContainer")) { linkContainer = targ.up(".linkContainer") } //Mouse is over some child of linkContainer
			else if (targ.className=="sub" && (linkContainer = targ.previous())) { } //Mouse is over the sublist after linkContainer
			else if ((aux = targ.up(".sub")) && (linkContainer = aux.previous())) { } //Mouse is over some child of the sublist after linkContainer
/*
			else {
				if (targ.className) {
					alert(targ.className);
				}
			}
*/
			if (linkContainer) {
				linkContainer.toggleClassName("active");
			}
		
			break;
		default:
			alert(e.type);
	}
	if (executed) {
		Event.stop(e);
		return false;
	}
	else {
		return true;
	}
}