/**
 * Game
 *
 * @author Steffen Maechtel <s.maechtel@netzbewegung.com>
 * @copyright Netzbewegung GmbH
 */

Nb.Family = Nb.Family || {};
Nb.Family.StainHidden = Nb.Family.StainHidden || {};

(function()
{
    var Y = Nb.Lib;

    Nb.Family.StainHidden.Game = function(config)
    {
        Nb.Family.StainHidden.Game.superclass.constructor.apply(this, arguments);

        this.stain = Y.one('#stain-hidden');

        this.stain.on('click', this._onStainClick, this);
        this.stain.on('mouseout', this._onStainMouseOut, this);
        this.stain.on('mouseover', this._onStainMouseOver, this);

        this.tooltip = new Nb.Core.UI.ToolTip();
        this.tooltip.render(document.body);
    }

    Y.extend(Nb.Family.StainHidden.Game, Nb.Core.Component, {
        _onStainClick : function(event)
        {
            event.halt();

            var url = '/?ajax=1&ext=family_stain_hidden&method=click&hash=' + this.stain.get('rel');
            var layerOpener = Nb.Wr.Controller.getComponentByName('layerOpener');

            layer = layerOpener.initLayer('stain-hidden');

            Y.io(url, {
                method : 'post',
                on : {
                    complete: this._onStainGetLayerContentComplete
                },
                context: this,
                arguments: layer
            });

            layer.show();
        },
        _onStainGetLayerContentComplete : function(transactionId, response, layer)
        {
            var response = Y.JSON.parse(response.responseText);

            if (response.success)
            {
                if (!response.data.termsAccepted)
                {
                    layer.hide();

                    Nb.FeUser.Gui.Api.showTerms('2010_Family');
                }
                else
                {
                    if (response.data.stainCaptured)
                    {
                        this.stain.setStyle('display', 'none');
                    }

                    layer.set('content', response.data.content);
                }



            }
        },
        _onStainMouseOut : function()
        {
            this.tooltip.hide();

        },
        _onStainMouseOver : function()
        {
            if (this.stain.get('rel'))
            {
                this.tooltip.set('text', 'Fang den Fleck');
            }
            else
            {
                this.tooltip.set('text', 'Flecken sammeln und gewinnen!');
            }

            this.tooltip.show(this.stain);
        }
    });

    
})();
