/* $Id: skin.js 1542 2011-09-11 18:22:08Z kstephensop $ */

/**
 * @fileOverview Functions and classes used only in this skin.
 */

/* Namespace */
var bib = bib || {};

/**
 * Add a new bible navigation menu to the window. If a navigation menu is
 * currently displayed, the new menu will replace the current menu.
 *
 * @param {HTMLElement} new_element The new navigation menu.
 */
bib.addBibleNavigation = function (new_element) {
    var element_id = 'sub_nav',
        async_element_info = bib.async_elements[element_id],
        old_element = $(element_id);
    if (old_element) {
        // This will also dispose of the old element.
        bib.bibleNavigationHide(old_element);
    }
    // The new element will slide into place, so reset the top before
    // we add it so that it does not briefly flash into its original
    // position before then sliding into place
    new_element.setStyle('top', -5000);
    // Add element in appropriate place if it is new
    $(async_element_info.parent_id).grab(new_element);
    async_element_info.wire(new_element);
};

/**
 * Slide a bible navigation menu into place.
 * @param {HTMLElement} element The navigation menu to show.
 */
bib.bibleNavigationShow = function (element) {
    var finish = $('main_nav_bar').getSize().y,
        start = 0 - element.getSize().y - finish;
    element.set('tween', {
        duration: 'normal'
    }).tween('top', start, finish);
};

/**
 * Slide a bible navigation menu out of view and remove it from the document.
 * @param {HTMLElement} element The navigation menu to hide.
 */
bib.bibleNavigationHide = function (element) {
    if (element.is_hiding) {
        return;
    }
    element.is_hiding = true;
    var start = element.getStyle('top').toInt(),
        finish = 0 - element.getSize().y - start;
    element.set('tween', {
        duration: 'normal',
        onComplete: function () {
            element.dispose();
        }
    }).tween('top', start, finish);
};

/**
 * Create toggle controller for each note register and note type (rubric).
 *
 * @constructor
 * @param {HTMLElement} heading The element in which a control for hiding or
 *     showing the register or rubric will be placed.
 * @param {HTMLElement} body The element which will be hidden or shown by the
 *     the toggle.
 *
 */
bib.NotesToggle = function (heading, body) {
    var hide = new Element('a', {'href': '#', 'class': 'hide_show'});
    this.img = new Element('img', {'src': bib.skins_dir + '/hide.png'});
    this.img.inject(hide);
    hide.inject(heading, 'top');
    hide.toggle = this;
    this.body = body;
    this.is_open = true;

    hide.addEvents({
        'click': function (event) {
            var toggle = this.toggle,
                img = toggle.img;

            event.stop();

            if (toggle.is_open) {
                toggle.body.setStyle('display', 'none');
                toggle.is_open = false;
            } else {
                toggle.body.setStyle('display', 'block');
                toggle.is_open = true;
            }

            switch (img.get('src')) {
                case bib.skins_dir + '/hide.png':
                    img.set('src', bib.skins_dir + '/show.png');
                    break;
                case bib.skins_dir + '/hide-hover.png':
                    img.set('src', bib.skins_dir + '/show-hover.png');
                    break;
                case bib.skins_dir + '/show.png':
                    img.set('src', bib.skins_dir + '/hide.png');
                    break;
                case bib.skins_dir + '/show-hover.png':
                    img.set('src', bib.skins_dir + '/hide-hover.png');
                    break;
            }
        },

        'mouseenter': function (event) {
            var img = this.toggle.img;

            event.stop();

            if (img.get('src') == bib.skins_dir + '/hide.png') {
                img.set('src', bib.skins_dir + '/hide-hover.png');
            } else {
                img.set('src', bib.skins_dir + '/show-hover.png');
            }
        },

        'mouseleave': function (event) {
            var img = this.toggle.img;

            event.stop();

            if (img.get('src') == bib.skins_dir + '/hide-hover.png') {
                img.set('src', bib.skins_dir + '/hide.png');
            } else {
                img.set('src', bib.skins_dir + '/show.png');
            }
        }
    });
};

//#mark domready
window.addEvent('domready', function () {

    var notes_div = $('notes'),
        toggle;

    if (! notes_div) {
        return;
    }

    notes_div.getElements('h2.register, h3.rubric').each(function (heading) {
        var toggle = new bib.NotesToggle(heading, heading.getNext());
    });
});

