/*
 * Ami [http://x13n.com/alex]
 *
 *          file: style.js
 *   description: Functions for manipulating CSS styles applied to HTML elements.
 *       creator: Alex Macmillan
 *       created: 2006
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/ 
 * 
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 * 
 * The Original Code is Ami Javascript Library, unreleased.
 * 
 * The Initial Developer of the Original Code is Alexander Macmillan.
 * Portions created by Alexander Macmillan are Copyright (C) 2009
 * Alexander Macmillan. All Rights Reserved.
 * 
 * Contributor(s): ______________________________________.
 */
 
 var Style = {
    show : function (e) {
        log("Showing " + e.id);
        Style.removeClass("invisible", e);
    },
    
    hide : function (e) { 
        log("Hiding " + e.id);
        Style.addClass("invisible", e);
    },
    
    select : function (e) {
        log("Selecting " + e.id);
        Style.addClass("selected", e);
    },
    
    unselect : function (e) { 
        log("Unselecting " + e.id);
        Style.removeClass("selected", e);
    },
    
    addClass : function (classname, element) {
        // Get the classes that currently belong to the element.
        var classes = Style.extractClasses(element);
        // Is the class there?
        for (n in classes)
            if (classes[n] == classname)
                return;
        // If we're still here, class isn't there yet.
        classes.push(classname);
        Style.setClasses(element, classes);
    },
    
    removeClass : function (classname, element) {
        // Get the classes that currently belong to the element.
        var newclasses = [];
        var oldclasses = Style.extractClasses(element);
        // Pop all classes that don't match onto the new list.
        for (n in oldclasses)
            if (oldclasses[n] != classname)
                newclasses.push(oldclasses[n]);
        Style.setClasses(element, newclasses);
    },
    
    hasClass : function (classname, element) {
        var classes = Style.extractClasses(element);
        for (n in classes)
            if (classes[n] == classname)
                return true;
        return false;
    },
    
    extractClasses : function (element) {
        var classstr = element.className;
        return classstr.split(" ");
    },
    
    setClasses : function (element, classes) {
        var classstr = classes.join(" ");
        element.className = classstr;
    }
}
