vanilla.namespace('alteneve');

alteneve.bonplan =
{
    animation	: null,
    timer	: null,
    width	: 0,
    content	: null,
    background	: null,
    button	: null,

    initSlider : function()
    {
	// bind controller
	this.button = EL('bonplan-button');
	this.button.onclick = function()
	{
	    alteneve.bonplan.toggleSlider();
	    return false;
	};

	this.content	= EL("content");
	this.background = EL("content-background");

	this.width = this.background.clientWidth;

	if ( document.location.hash != '#bonsplans' )
	{
	    this.timer = new SimpleThread(this.closeSlider.bind(this), 3000);
	    this.timer.start();
	}
    },

    openSlider : function()
    {
	if ( this.animation && this.animation.isAlive() )
	{
	    return;
	}

	this.animation = new vanilla.animation.Animation(0, this.width);
	this.animation.onNextValue.subscribe(this._setWidth, this);
	this.animation.onComplete.subscribe
	(
	    function()
	    {
		vanilla.html.DOM.removeClassName(this.button, "closed");
	    }
	    .bind(this)
	);

	this.animation.duration	    = 0.6;
	this.animation.method	    = vanilla.animation.linear;

	this.animation.start();
    },

    closeSlider : function()
    {
	if ( this.animation && this.animation.isAlive() )
	{
	    return;
	}

	this.animation = new vanilla.animation.Animation(this.width, 0);
	this.animation.onNextValue.subscribe(this._setWidth, this);

	this.animation.onComplete.subscribe
	(
	    function()
	    {
		vanilla.html.DOM.addClassName(this.button, "closed");
	    }
	    .bind(this)
	);

	this.animation.duration	    = 0.6;
	this.animation.method	    = vanilla.animation.acceleration;

	this.animation.start();
    },

    toggleSlider : function()
    {
	if ( this.animation && this.animation.isAlive() )
	{
	    return;
	}

	this._stopTimer();
    
	if ( this.isSliderOpened() )
	{
	    this.closeSlider();
	}
	else
	{
	    this.openSlider();
	}
    },

    _stopTimer : function()
    {
	if ( this.timer )
	{
	    this.timer.stop();
	}
    },

    isSliderOpened : function()
    {
	return !vanilla.html.DOM.hasClassName(this.button, "closed");
    },

    _setWidth : function(value)
    {
	this.content.style.width    = value + "px";
	this.background.style.width = value + "px";
    }
};

vanilla.addOnloadListener(alteneve.bonplan.initSlider, alteneve.bonplan);

