/*
CSSClass - Methods for manipulating the class attributes of DOM elements.
Copyright (c) 2008 Thomas Peri, http://www.tumuski.com/

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The Software shall be used for Good, not Evil.

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

/**
 * Methods for manipulating the class attributes of DOM elements.
 * version 2008-05-15
 */
var CSSClass = {

	/**
	 * An array of the supplied element's classes.
	 * @param  HTMLElement  element  The element whose classes to return.
	 * @return Array
	 */
	get: function(element) {
		var classNames;
		if (element.hasAttribute && element.hasAttribute('class')) {
			classNames = element.getAttribute('class');
		}
		else if (element.className) {
			classNames = element.className;
		}
		if (classNames) {
			return classNames.replace(/^\s*(.+?)\s*$/, '$1').split(/\s+/);
		}
		return [];
	},
	
	/**
	 * The supplied element has the supplied class.
	 * @param HTMLElement element
	 * @param String className
	 * @return boolean
	 */
	has: function(element, className) {
		var classNames = this.get(element);
		for (var i = 0; i < classNames.length; i++) {
			if (classNames[i] == className) {
				return true;
			}
		}
		return false;
	},
	
	/**
	 * Set an element's classes to those in the classNames array.
	 * @param HTMLElement element
	 * @param Array classNames
	 */
	set: function(element, classNames) {
		classNames = classNames.join(' ');
		element.setAttribute('class', classNames);
		element.className = classNames;
	},
	
	/**
	 * Add a class to an element.
	 * @param HTMLElement element
	 * @param String className
	 */
	add: function(element, className) {
		var classNames = this.get(element);
		for (var i = 0; i < classNames.length; i++) {
			if (classNames[i] == className) {
				return;
			}
		}
		classNames[classNames.length] = className;
		this.set(element, classNames);
	},
	
	/**
	 * Remove a class from an element.
	 * @param HTMLElement element
	 * @param String className
	 */
	remove: function(element, className) {
		var classNames = this.get(element);
		var reduced = [];
		for (var i = 0; i < classNames.length; i++) {
			if (classNames[i] != className) {
				reduced[reduced.length] = classNames[i];
			}
		}
		this.set(element, reduced);
	},
	
	/**
	 * Remove all classes from an element.
	 * @param HTMLElement element
	 */
	clear: function(element) {
		this.set(element, []);
	}
};

