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

/**
 * Class controls the info button and the texts of a photoseries.
 *
 * @param linkSelector
 * @param targetSelector
 * @param textSelector
 */
function InfoButton(linkSelector, targetSelector, textSelector)
{
    var linkSelector = linkSelector; // the link, which controls the appearance of the target
    var targetSelector = targetSelector; // target = the whole infoText container whichs contains multiple texts
    var textSelector = textSelector; // the text = individual text for each photo

    var currentTextId;
    var texts;

    /**
     * Inits the texts and the link.
     */
    this.init = function(currTxtId)
    {
        texts = $(textSelector); // all the texts

        //$(linkSelector).click(function(event)  {
        $(linkSelector).mouseenter(function(event)  {
            showTarget();
            event.preventDefault();
            event.stopPropagation();
        });

        if (currTxtId != undefined) this.setCurrentText(currTxtId);
    }

    /**
     * Sets the id of the current text.
     * Each photo has its own text.
     * Is called from photoSeries
     *
     * @param id The id of the text.
     */
    this.setCurrentText = function (id)
    {
        currentTextId = id;
    }

    /**
     * Shows the target (i.e. the whole info text container)
     */
    var showTarget = function()
    {
        var target = $(targetSelector);
        target.show();

        showText(currentTextId);

        // for ie8 fade bug
        if($.browser.msie)
        {
            var text = $(targetSelector + ' .content');
            text.show();
            text.css('opacity', 0.99);
        }

        target.mouseleave(function()  { hideTarget(); }); // leaving the target will hide the target
    }

    /**
     * Hides the target (i.e. the whole info text container)
     */
    var hideTarget = function()
    {
        var fadeSpeed = 250;
        var target = $(targetSelector);
        target.fadeOut(fadeSpeed);

        // for ie8 fade bug
        if($.browser.msie)
        {
            var text = $(targetSelector + ' .content');
            text.fadeOut(fadeSpeed);
        }

        target.unbind('mouseleave');
    }

    /**
     * Shows a text.
     *
     * @param id
     */
    var showText = function (id)
    {
        hideAllTexts();
        var text = texts.eq(id);
        text.show();
    }

    /**
     * Hides all Texts.
     */
    var hideAllTexts = function ()
    {
        texts.hide();
    }

}

