vanilla.namespace('alteneve');

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

    initSlider : function()
    {
	// bind controller
	this.button = EL('slideshow-albums');
	this.button.style.display = "block";
	this.button.onclick = function()
	{
	    alteneve.galerie.toggleSlider();
	    return false;
	};

	this.content	= EL("content");
	this.background = EL("content-background");
	this.slideshow	= EL("zone-content-ambiance");

	this.content.style.overflow = "hidden";
	this.width = this.content.clientWidth;
	this.content.style.overflow = "auto";

	// on recherche si un album est sélectionnée
	var albums = Arr(this.content.getElementsByTagName("li"));
	for ( var i = 0 ; i < albums.length ; i++ )
	{
	    if ( vanilla.html.DOM.hasClassName(albums[i], "selected") )
	    {
		this.content.scrollTop = albums[i].offsetTop - 60;

		this.timer = new SimpleThread(this.closeSlider.bind(this), 2000);
		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");
		this.content.style.overflow = "auto";
	    }
	    .bind(this)
	);

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

	this.slideshow.style.zIndex = 0;	
	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");
		this.slideshow.style.zIndex = 2;	
	    }
	    .bind(this)
	);

	this.animation.fps	    = 30;
	this.animation.duration	    = 0.5;
	this.animation.method	    = vanilla.animation.acceleration;

	this.content.style.overflow = "hidden";
	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.galerie.initSlider, alteneve.galerie);

