
/* Merged Plone Javascript file
 * This file is dynamically assembled from separate parts.
 * Some of these parts have 3rd party licenses or copyright information attached
 * Such information is valid for that section,
 * not for the entire composite file
 * originating files are separated by - filename.js -
 */

/* - Java Script/Menu - */
/**
 * 
 * menu.js
 *
 * Componente que apresenta um menu dropdown
 *
 * @see MenuItem
 */
function Menu(id, rootPath) {
	this.id = id;
	this.rootPath = rootPath ? rootPath + "/" : "";
	this.root = new MenuItem();
	this.root.owner = this.id;
	this.root.rootPath = this.rootPath;
}

Menu.prototype.add = function(menuItem) {
	this.root.add(menuItem);
}

Menu.prototype.show = function() {
	document.getElementById(this.id).innerHTML = this.root.toString();
	document.getElementById(this.id + "." + this.root.id).style.display = "block";
}

Menu.prototype.click = function(id) {
	var element = document.getElementById(this.id + "." + id);
	if (element) {
		var display = element.style.display == "none" ? "block" : "none";
		var elements = getElementsByClassName(element.parentNode, "div", "directory");
		for (var count = 0; count < elements.length; count++) {
			var elementHidden = elements[count];
			elementHidden.style.display = "none";
		}
		element.style.display = display;
	}
}


/* - Java Script/Functions - */
/*
	Written by Jonathan Snook, http://www.snook.ca/jonathan
	Add-ons by Robert Nyman, http://www.robertnyman.com
*/

function getElementsByClassName(oElm, strTagName, strClassName){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/-/g, "\-");
	var oRegExp = new RegExp("(^|\s)" + strClassName + "(\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}

// ====================================================================
//       URLEncode and URLDecode functions
//
// Copyright Albion Research Ltd. 2002
// http://www.albionresearch.com/
//
// You may copy these functions providing that
// (a) you leave this copyright notice intact, and
// (b) if you use these functions on a publicly accessible
//     web site you include a credit somewhere on the web site
//     with a link back to http://www.albionresearch.com/
//
// If you find or fix any bugs, please let us know at albionresearch.com
//
// SpecialThanks to Neelesh Thakur for being the first to
// report a bug in URLDecode() - now fixed 2003-02-19.
// And thanks to everyone else who has provided comments and suggestions.
// ====================================================================
function URLEncode(plaintext)
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '"
                        + ch
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
};

function URLDecode(encoded)
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef";
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2)
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
   return plaintext;
};



/* - Java Script/ImageMenuItem - */
/**
 *
 * imagemenuitem.js
 *
 * @see MenuItem
 * @see Menu
 */

function ImageMenuItem(image, imageOver, action, description) {
	this.image = image;
	this.imageOver = imageOver;
	this.action = action;
	this.description = description;
	this.id = MenuItem.count++;
	this.children = new Array();
}

ImageMenuItem.prototype = new MenuItem();

ImageMenuItem.prototype.getContent = function() {
	var content = "<img border='0' src='" + this.rootPath + this.image + "' alt='" + (this.getDescription() ? this.getDescription() : "") + "' ";
	if (this.imageOver) {
		content += "onmouseover='this.src=\"" + this.rootPath + this.imageOver + "\"' ";
		content += "onmouseout='this.src=\"" + this.rootPath + this.image + "\"' ";
	}
	content += "/>";
	return content;
}


/* - Java Script/MenuItem - */
/**
 *
 * menuitem.js
 *
 * Classe que representa um item do menu
 *
 * @see Menu
 */

function MenuItem(content, action, description) {
	this.content = content;
	this.action = action;
	this.description = description;
	this.id = MenuItem.count++;
	this.children = new Array();
	this.display = "none";
}

MenuItem.count = 0;

MenuItem.prototype.getContent = function() {
	return this.content;
}

MenuItem.prototype.getAction = function() {
	return this.action;
}

MenuItem.prototype.getDescription = function() {
    return this.description;
}

MenuItem.prototype.add = function(menuItem) {
	menuItem.owner = this.owner;
	menuItem.rootPath = this.rootPath;
	this.children.push(menuItem);
}

MenuItem.prototype.toString = function() {

    var href = URLDecode(window.location.href);
    var action = this.getAction();
    var index = href.lastIndexOf(action);
    if (index != -1 && index + action.length == href.length) {
        this.display = "block";
    }

	var childrenHtml = "";
	if (this.children.length > 0) {
		var display = "none";
		var childrenAuxHtml = "";
		for (var count = 0; count < this.children.length; count++) {
			var item = this.children[count];
			childrenAuxHtml += item.toString();
			if (item.display == "block") {
               display = "block";
			}
		}
		childrenHtml += "\n<div id='" + this.owner + "." + this.id + "' class='directory' style='display: " + display + ";'>";
		childrenHtml += childrenAuxHtml;
		childrenHtml += "\n</div>";
	}

	var html = "";
	if (this.getContent()) {
		if (this.children.length > 0) {
			html += "\n<div onclick='" + this.owner + ".click(" + this.id + ");' style='cursor: pointer; display: block;'>\n";
			
		} else {
			html += "\n<div style='display: block;'>";
		}
		if (this.getAction()) {
			html += "\n<a href='" + this.getAction() + "' title='" + (this.getDescription() ? this.getDescription() : "") + "'>";
			html += this.getContent();
			html += "</a>";
		} else {
			html += this.getContent();
		}
		html += "</div>";
	}
	
	return html + childrenHtml;
}

