/** 
* Copyright 2006 massimocorner.com
* License: http://www.massimocorner.com/license.htm
* @author      Massimo Foti (massimo@massimocorner.com)
* @version     0.3.0, 2006-11-16
* @require     tmt_core.js
*/

if(typeof(tmt) == "undefined"){
	alert("Error: tmt.core JavaScript library missing");
}

tmt.css = {};

/**
* Add a CSS class to a given node (accept both an id or a node element)
*/
tmt.css.addClass = function(element, className){
	var nodeElem = tmt.get(element);
	if(!nodeElem || (tmt.css.hasClass(nodeElem, className) == true)){
		return;
	}
	nodeElem.className += (nodeElem.className ? " " : "") + className;
}

/**
* Check if a given node use a CSS class (accept both an id or a node element)
*/
tmt.css.hasClass = function(element, className){
	var nodeElem = tmt.get(element);
	if(nodeElem){
		return nodeElem.className.search(new RegExp("\\b" + className + "\\b")) != -1;
	}
	return null;
}

/**
* Remove a CSS class from a given node (accept both an id or a node element)
*/
tmt.css.removeClass = function(element, className){
	var nodeElem = tmt.get(element);
	if(!nodeElem || (tmt.css.hasClass(nodeElem, className) == false)){
		return;
	}
	nodeElem.className = nodeElem.className.replace(new RegExp("\\s*\\b" + className + "\\b", "g"), "");
}

/**
* Toggle a CSS class from a given node (accept both an id or a node element)
*/
tmt.css.toggleClass = function(element, className){
	var nodeElem = tmt.get(element);
	if(tmt.css.hasClass(nodeElem, className)){
		tmt.css.removeClass(nodeElem, className);
	}
	else{
		tmt.css.addClass(nodeElem, className);
	}
}

/**
* Set display to "block" on a list of nodes (accept both id or node elements)
*/
tmt.css.displayBlock = function(){
	var nodes = tmt.get(arguments);
	for(var i=0; i<nodes.length; i++){
		tmt.css.setStyleProperty(nodes[i], "display", "block");
	}
}

/**
* Set display to "none" on a list of nodes (accept both id or node elements)
*/
tmt.css.displayNone = function(){
	var nodes = tmt.get(arguments);
	for(var i=0; i<nodes.length; i++){
		tmt.css.setStyleProperty(nodes[i], "display", "none");
	}
}

/**
* Toggle display of a given set of nodes
* If it's "block" it set it to "none" and viceversa (accept both id or node elements)
*/
tmt.css.toggleDisplay = function(){
	var nodes = tmt.get(arguments);
	for(var i=0; i<nodes.length; i++){
		var currentStatus = tmt.css.getStyleProperty(nodes[i], "display");
		var newStatus = (currentStatus != "block") ? "block" : "none";
		tmt.css.setStyleProperty(nodes[i], "display", newStatus);
	}
}

/**
* Set visibility to "visible" on a list of nodes (accept both id or node elements)
*/
tmt.css.show = function(){
	var nodes = tmt.get(arguments);
	for(var i=0; i<nodes.length; i++){
		tmt.css.setStyleProperty(nodes[i], "visibility", "visible");
	}
}

/**
* Set visibility to "hidden" on a list of nodes (accept both id or node elements)
*/
tmt.css.hide = function(){
	var nodes = tmt.get(arguments);
	for(var i=0; i<nodes.length; i++){
		tmt.css.setStyleProperty(nodes[i], "visibility", "hidden");
	}
}

/**
* Toggle visibility of a given set of nodes
* If it's "visible" it set it to "hidden" and viceversa (accept both id or node elements)
*/
tmt.css.toggle = function(){
	var nodes = tmt.get(arguments);
	for(var i=0; i<nodes.length; i++){
		var currentStatus = tmt.css.getStyleProperty(nodes[i], "visibility");
		var newStatus = (currentStatus != "visible") ? "visible" : "hidden";
		tmt.css.setStyleProperty(nodes[i], "visibility", newStatus);
	}
}

/**
* Return an array of nodes that contain the given class
* If no starting node is passed, assume the document is the starting point
*/
tmt.css.getNodesByClass = function(className, startNode){
	var nodes = tmt.getAllChilds(startNode);
	var filteredNodes = new Array();
	for(var i=0; i<nodes.length; i++){
		if(tmt.css.hasClass(nodes[i], className)){
			filteredNodes.push(nodes[i]);
		}
	}
	return filteredNodes;
}

/**
* Get the value of a specified CSS property out of a given node (accept both an id or a node element)
*/
tmt.css.getStyleProperty = function(element, property){
	var nodeElem = tmt.get(element);
	try{
		if(nodeElem.style[property]){
			return nodeElem.style[property];
		}
		else if(nodeElem.currentStyle){
			return nodeElem.currentStyle[property];
		}
		else if(document.defaultView && document.defaultView.getComputedStyle){
			var style = document.defaultView.getComputedStyle(nodeElem, null);
			return style.getPropertyValue(property);
		}
	}
	catch(e){}
	return null;
}

/**
* Set the value of a CSS property on a given node (accept both an id or a node element)
*/
tmt.css.setStyleProperty = function(element, propName, propValue){
	var nodeElem = tmt.get(element);
	if(nodeElem){
		nodeElem.style[propName] = propValue;
	}
}