/**
 * A library that works in conjunction with a custom stylesheet
 * to allow the user to toggle the font sizes of the page
 * @author Chris Boden
 * @version 0.4
 * @link svn+ssh://sites.gotenzing.com/javascript/FontSizer/0.4
 * @requires Cookie The cookie library is an optional requirement
 * @param {Element} div The container of the selector (<a> elements to be inside)
 * @param {Object} conf The configuration
 * @param {String} cookie (optional) The name of the cookie to save to
 */
FontSizer = function(div, cookie) {
    /**
     * The name of the cookie (if applicable)
     * @private
     * @type String
     */
    var sCookie = cookie || 'FontSizer';

    /**
     * The active selector
     * @private
     * @type Object|Boolean
     */
    var oActive = false;

    /**
     * The class to add to the active selector
     * @private
     * @type String
     */
    var sActive = 'FontSizerActive';

    /**
     * The body element
     * @private
     * @type Element
     */
    var oBody = document.getElementsByTagName('body')[0];

    /**
     * Boolean determined of the Cookie class is present
     * @private
     * @type Boolean
     */
    var bCookie = (typeof Cookie == 'object' ? true : false);

    /**
     * Functions to be called when a font is selected
     * @private
     * @type Array
     */
    var aCallbacks = Array();

    /**
     * Initilization function, called internally
     * @private
     * @param {Object} oContainer Div container
     * @returns null
     */
    var init = function(oContainer) {
        if (bCookie && Cookie.read(sCookie)) {
            var sPrev = Cookie.read(sCookie);
        }
        var bCookieMatch = false;

        var bClass = ' ' + oBody.className + ' ';
        var oLinks = oContainer.getElementsByTagName('a');
        for (var i = 0, iLen = oLinks.length; i < iLen; i++) {
            oLinks[i].onclick = select;

            var lClass = ' ' + oLinks[i].className + ' ';

            if (bClass.indexOf(lClass) != -1) {
                oActive = oLinks[i];
                Class.add(oLinks[i], sActive);
            }

            if (sPrev && sPrev == oLinks[i].className) {
                bCookieMatch = oLinks[i];
            }
        }

        // Previous user selection
        // If prev doesn't match current, switch
        if (bCookieMatch) {
            var lClass = ' ' + bCookieMatch.className + ' ';
            if (bClass.indexOf(lClass) == -1) {
                toggle(bCookieMatch);
            }
        }
    }

    /**
     * This is the method that changes the page font size
     * @private
     * @param {Object} o The selector object to turn on
     * @returns null
     */
    var toggle = function(o) {
        if (oActive) {
            Class.remove(oActive, sActive);
            Class.remove(oBody, oActive.className);
        }
        oActive = o;
        if (bCookie) {
            Cookie.create(sCookie, oActive.className);
        }

        Class.add(oBody, oActive.className);
        Class.add(oActive, sActive);
    }

    /**
     * Method triggered when a user selects a size
     * @private
     * @see toggle
     * @returns false
     */
    var select = function() {
        toggle(this);

        for (var i = 0, iLen = aCallbacks.length; i < iLen; i++) {
            aCallbacks[i].call(this);
        }

        return false;
    }

    /**
     * A few methods to make class manipulation easier
     * @private
     */
    var Class = {
        add: function(o, sNew) {
            o.className += ' ' + sNew;
        }
      , remove: function(o, sGone) {
            var sClass = ' ' + o.className + ' ';
            o.className = sClass.replace(sGone, '').trim();
        }
      , replace: function(o, sFrom, sTo) {
            var sClass  = ' ' + o.className + ' ';
            o.className = sClass.replace(sFrom, sTo).trim();
        }
    }

    init(div);
    return {
        /**
         * Allows the application to add a function to be called
         *  when the user triggers (clicks) a scoped element
         * @param {Function} fn The function to call
         * @returns null
         */
        addCallback: function(fn) {
            aCallbacks.push(fn);
        }
    }
}

if (typeof String.prototype.trim == 'undefined') {
    String.prototype.trim = function() {
	   return this.replace(/^\s+|\s+$/g,"");
    }
}