/** 
* Copyright 2006 massimocorner.com
* @author      Massimo Foti (massimo@massimocorner.com)
* @version     0.1.3, 2006-06-20
 */

if(typeof(tmt) == "undefined"){
	var tmt = {};
}

// The function below was developed by John Resig
// For additional info see:
// http://ejohn.org/projects/flexible-javascript-events
// http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
tmt.addEvent = function(obj, type, fn){
	if(obj.addEventListener){
		obj.addEventListener(type, fn, false);
	}
	else if(obj.attachEvent){
		obj["e" + type + fn] = fn;
		obj[type + fn] = function(){
				obj["e" + type + fn](window.event);
			}
		obj.attachEvent("on" +type, obj[type+fn]);
	}
}

// Equivalent of the famous $() function. Just with a better name :-)
tmt.get = function(){
	var returnNodes = new Array();
	for(var i=0; i<arguments.length; i++){
		var nodeElem = arguments[i];
		if(typeof nodeElem == "string"){
			nodeElem = document.getElementById(nodeElem);
		}
		if(arguments.length == 1){
			return nodeElem;
		}
		returnNodes.push(nodeElem);
	}
	return returnNodes;
}

// Return an array containing all child nodes
// If no starting node is passed, assume the document is the starting point
tmt.getAllChilds = function(startNode){
	// If no node was passed, use the document
	var rootNode = (startNode) ? tmt.get(startNode) : document;
	return rootNode.getElementsByTagName("*");
}

// Return an array containing all child nodes that contain the given attribute
// If no starting node is passed, assume the document is the starting point
tmt.getNodesByAttribute = function(attName, startNode){
	var nodes = tmt.getAllChilds(startNode);
	var filteredNodes = new Array();
	for(var i=0; i<nodes.length; i++){
		if(nodes[i].getAttribute(attName)){
			filteredNodes.push(nodes[i]);
		}
	}
	return filteredNodes;
}

// Return an array containing all child nodes that contain the given attribute that match a given value
// If no starting node is passed, assume the document is the starting point
tmt.getNodesByAttributeValue = function(attName, attValue, startNode){
	var nodes = tmt.getAllChilds(startNode);
	var filteredNodes = new Array();
	for(var i=0; i<nodes.length; i++){
		if(nodes[i].getAttribute(attName) && (nodes[i].getAttribute(attName) == attValue)){
			filteredNodes.push(nodes[i]);
		}
	}
	return filteredNodes;
}

// Set the value of an attribute on a list of nodes (accept both id or node elements)
tmt.setNodeAttribute = function(nodeList, attName, attValue){
for(var i=0; i<nodeList.length; i++){
		var nodeElem = tmt.get(nodeList[i]);
		if(nodeElem){
			nodeElem[attName] = attValue;
		}
	}
}

// Replace special XML character with the equivalent entities
tmt.encodeEntities = function(str){
	if(str && str.search(/[&<>"]/) != -1){
		str = str.replace(/&/g, "&amp;");
		str = str.replace(/</g, "&lt;");
		str = str.replace(/>/g, "&gt;");
		str = str.replace(/"/g, "&quot;");
	}
	return str
}