/*
 * (c) 2011 David Krause, Interaktive Gestaltung
 * http://www.interaktive-gestaltung.de
 */

/**
 * Class controls the language button of texts.
 * It toggles the content divs and the label of the button.
 *
 * @param {string} buttonSelector Selector for the button.
 * @param {Array} contentSelectors Selectors for the content.
 * @param {Array} labels The labels for the button.
 */
function LanguageButton(buttonSelector, contentSelectors, labels)
{
    var buttonSelector = buttonSelector;
    var contentSelectors = contentSelectors;
    var labels = labels;

    var currentLangId;

    this.init = function()
    {
        $(buttonSelector).click(function(event)
        {
            toggleLanguage();
            event.preventDefault();
            event.stopPropagation();
            this.blur();
        });

        currentLangId = 1;
        setLanguage(0);
    }

    /**
     * Sets the language.
     * @param id The id of the new language.
     */
    var setLanguage = function(id)
    {
        var prevLangId = currentLangId;
        currentLangId = id;
        hideContent(prevLangId);
        showContent(currentLangId);
        setLabel(prevLangId);
    }

    /**
     * Toggles the language.
     */
    var toggleLanguage = function()
    {
        var newLangId = (currentLangId == 1) ? 0 : 1;
        setLanguage(newLangId);
    }

    /**
     * Hides the content of the given language.
     * @param id Language id.
     */
    var hideContent = function(id)
    {
        $(contentSelectors[id]).hide();
    }

    /**
     * Shows the content of the given language.
     * @param id Language id.
     */
    var showContent = function(id)
    {
        $(contentSelectors[id]).show();
    }

    /**
     * Sets the label of the button to the given language.
     * @param id Language id.
     */
    var setLabel = function(id)
    {
        $(buttonSelector).empty();
        $(buttonSelector).append(labels[id]);
    }


}

