/**
 * JavaScript object to control
 * the toggling of the different
 * sections in the format
 *
 * @author Mark Nielsen
 **/
var formatSteps = {
    /**
     * Performs the actual toggle
     * and sets a cookie "show:id"
     * to remember state.
     *
     * @param string id ID of the element to toggle
     * @param boolean persist Set cookie or not
     * @param string strShow The show text
     * @param string strHide The hide text
     * @return void
     **/
    toggle: function(id, persist, strShow, strHide) {
        el = document.getElementById(id);

        elementToggleHide(el, false, false, strShow, strHide);

        if (persist) {
            new cookie('show:' + el.id, 1, (this.isHidden(el) ? -1 : 356), '/').set();
        }
    },

    /**
     * Reads the "show:id" cookie to
     * determine the render state of the
     * section
     *
     * @param string id ID of the element to check
     * @param string strShow The show text
     * @param string strHide The hide text
     * @return void
     **/
    cookieShow: function(id, strShow, strHide) {
        var cook = new cookie('show:' + id).read();
        if (cook != null) {
            this.toggle(id, false, strShow, strHide);
        }
    },

    /**
     * Reads the anchor hash to determine
     * the render state of the section
     *
     * @param string idfrag The beginning of the ID of the element to check
     * @param string strShow The show text, ###section### will be replaced with the section from the anchor
     * @param string strHide The hide text, ###section### will be replaced with the section from the anchor
     * @return void
     **/
    showAnchor: function(idfrag, strShow, strHide) {
        var hash = document.location.hash;

        if (hash.length > 0) {
            var section = hash.replace('#section-', '');

            if (section == parseInt(section)) {
                var id = idfrag + section;

                if (document.getElementById(id)) {
                    if (this.isHidden(document.getElementById(id))) {

                        // "Hack" - replace this place holder with the section number
                        strShow = strShow.replace('###section###', section);
                        strHide = strHide.replace('###section###', section);

                        this.toggle(id, false, strShow, strHide);
                    }
                }
            }
        } else {
        	var id = 'section_text_0';

            if (document.getElementById(id)) {
                if (this.isHidden(document.getElementById(id))) {

                    // "Hack" - replace this place holder with the section number
                    strShow = strShow.replace('###section###', section);
                    strHide = strHide.replace('###section###', section);

                    this.toggle(id, false, strShow, strHide);
                }
            }
        }
    },

    /**
     * Determine if an element is hidden or not.
     * Uses the class name 'hidden' as the flag.
     *
     * @param element el Element to check
     * @return boolean
     **/
    isHidden: function(el) {
        if (el.className.indexOf('hidden') == -1) {
            // Does NOT have class hidden
            return false;
        } else {
            // Does have class hidden
            return true;
        }
    }
};
