/**
 * cssClass, version 0.1 (2006-03-31)
 * Copyright: 2006, David Duret (http://pilgrim.maleo.net/)
 * Licence : http://creativecommons.org/licenses/LGPL/2.1/
 *
 * Example : cssClass(document.getElementById('test')).add('someclass');
 */

// the following functions allow manipulating CSS class names
var cssClass = function ( $$from ) {
    
    // return a regexp object
    this.match = function ( name ) {
        
        return new RegExp('(^|\\s)' + name + '(\\s|$)');
        
    }; // end of 'match()'

    // tell if the class name exists or not
    this.exists = function( name ) {
        
        return this.match(name).test($$from.className);
        
    }; // end of 'exists()'

    // add the class name if not exists
    this.add = function( name ) {
        
        if ( !this.exists(name) ) $$from.className += ' ' + name;
        return this;

    }; // end of 'add()'

    // replace class name by another one if exists
    this.replace = function( name, newname ) {
        
        $$from.className = $$from.className.replace(this.match(name), newname);
        return this;

    }; // end of 'replace()'

    // remove class name if exists
    this.remove = function( name ) {
        
        $$from.className = $$from.className.replace(this.match(name), '');
        return this;

    }; // end of 'remove()'
    
    return this;

}; // end of 'cssClass()'
