window.addEvent('domready', function() {
    //var navigationManager = new NavigationManagerClass();
	var blocClient = $('blocclient');
	var blocClientState = 0;
	 // Long version
	 $('btClient').addEvent('click', function(e) {
		e.stop();
                var options = {
                    onComplete: function(){
                        blocClientState = (blocClientState==0)?1:0;
                        if(!blocClientState)
                        {
                            blocClient.setStyle('display','none');
                        }
                    },
                    onStart: function(){
                        if(!blocClientState){
                            blocClient.setStyle('display','block');
                            blocClient.setStyle('visibility','visible');
                        }
                    }
                }
                blocClient.get('tween',options).start('opacity',(blocClientState==0)?1:0);
		//blocClient.fade((blocClientState==0)?1:0).chain(function(){console.log(blocClientState)});
		//blocClientState = (blocClientState==0)?1:0;
	 });
         blocClient.get('tween').start('opacity',0);
});

var NavigationManagerClass = new Class({
    Implements: Options,
    options: {
        prefix_main: 'mainnav_',
        prefix_sous: 'sousnav_',
        sous_height: 24,
        delay: 500
    },
    currentMain: null,
    currentSous: null,
    wantedDisplayStatus: 0,
    displayStatus:0,
    wrapper: null,
    permanentSousNav: null,
    initialize: function(options) {
        this.setOptions(options);
        this.wrapper = $('wrapper_sousnav');

        if(this.wrapper.hasClass('actif'))
        {
            this.permanentSousNav = this.wrapper.getElement('.navigation_l1.active');
        }
        if( this.permanentSousNav == null)
        {
            this.addMainnavEvent();
            this.addWrapperEvent();
            this.fxClose = new Fx.Tween(this.wrapper,{
                property:'height',
                onComplete: function(){this.hideSousNavigation()}.bind(this),
                onCancel: function(){
                    this.fxOpen.start(this.options.sous_height);
                }.bind(this)
            });

            this.fxOpen = new Fx.Tween(this.wrapper,{
                property:'height'
            });
        }
        else
        {
            this.addMainnavWithSousEvent();
            this.addWrapperWithSousEvent();
        }
    },
    timerFunctionWithSous: function()
    {
        if(this.displayStatus == 1 && this.wantedDisplayStatus == 0 && this.currentSous != null)
        {
            this.currentSous.setStyle('display','none');
            this.permanentSousNav.setStyle('display','block');
        }
        $clear(this.periodical);
    },
    timerFunction: function()
    {
        if(this.displayStatus == 1 && this.wantedDisplayStatus == 0  && this.currentSous != null)
        {
            if(Browser.Engine.trident)
                this.fxClose.start(1);
            else
                this.fxClose.start(0);
        }
    },
    hideSousNavigation: function()
    {
        this.displayStatus = this.wantedDisplayStatus = 0;
        this.currentSous.setStyle('display','none');
        if(Browser.Engine.trident)
            this.wrapper.setStyle('height',0);
        this.currentSous = null;
    },
    showSousNavigation: function(apply_fx)
    {
        this.displayStatus = this.wantedDisplayStatus = 1;
        if(this.currentSous)
        {
            if(apply_fx && Browser.Engine.trident)
                this.wrapper.setStyle('height',1);
            this.currentSous.setStyle('display','block');
        }
    },
    addWrapperWithSousEvent: function()
    {
        this.wrapper.addEvent('click',function(e) {
            e.stopPropagation(); $clear(this.periodical);
            this.wantedDisplayStatus = 1;
        }.bind(this));

        /*this.wrapper.addEvent('mouseleave',function(e) {
            e.stopPropagation();
            this.wantedDisplayStatus = 0;
            this.periodical = this.timerFunctionWithSous.periodical(this.options.delay,this);
        }.bind(this));*/
    },
    addWrapperEvent: function()
    {
        this.wrapper.addEvent('click',function(e) {
            e.stopPropagation(); $clear(this.periodical);
            this.wantedDisplayStatus = 1;
        }.bind(this));

        /*this.wrapper.addEvent('mouseleave',function(e) {
            e.stopPropagation();
            this.wantedDisplayStatus = 0;
            this.periodical = this.timerFunction.periodical(this.options.delay,this);
        }.bind(this));*/
    },
    addMainnavWithSousEvent: function() {
        var navigation_links = $('navigation').getElements('li.wrapper_mainnav');
        navigation_links.each(function(item, index){
            item.addEvent('click', function(e){
                e.stopPropagation(); //$clear(this.periodical);
                var oldSous = this.currentSous;
                var target = $(e.target);
                if(target.get('tag') != 'li')
                 {   target = target.getParent('li');e.stop();}

                this.currentSous = $(target.id.replace(this.options.prefix_main,this.options.prefix_sous));

                if(oldSous != null && oldSous.get('id') != this.currentSous.get('id'))
                {
                    oldSous.setStyle('display','none');
                    this.currentSous.setStyle('display','block');
                }
                else if(this.currentSous.get('id') == this.permanentSousNav.get('id'))
                {
                    this.currentSous.setStyle('display','block');
                }
                else
                {
                    this.currentSous.setStyle('display','block');
                    this.permanentSousNav.setStyle('display','none');
                }
                this.displayStatus = this.wantedDisplayStatus = 1;
            }.bind(this));

            /*item.addEvent('mouseleave', function(e){
                e.stopPropagation();
                this.wantedDisplayStatus = 0;
                this.periodical = this.timerFunctionWithSous.periodical(this.options.delay,this);
            }.bind(this));*/
        }.bind(this));
    },
    addMainnavEvent: function()
    {
        var navigation_links = $('navigation').getElements('li.wrapper_mainnav');
        navigation_links.each(function(item, index){
            item.addEvent('click', function(e){
                
                e.stopPropagation(); //$clear(this.periodical);
                this.fxClose.cancel();
                var apply_fx = false;
                var oldSous = this.currentSous;
                var target = $(e.target);
                if(target.get('tag') == 'a')
                {
                    target = target.getParent('li');
                    e.stop();
                }
                this.currentSous = $(target.id.replace(this.options.prefix_main,this.options.prefix_sous));
                if(oldSous != null && oldSous.get('id') != this.currentSous.get('id'))
                {
                    
                    this.showSousNavigation(apply_fx);
                    oldSous.setStyle('display','none');
                }
                else
                {
                    apply_fx = (oldSous == null);
                    //apply_fx = true;
                    this.showSousNavigation(apply_fx);
                }
                if(apply_fx) this.fxOpen.start(this.options.sous_height);
            }.bind(this));

            /*item.addEvent('mouseleave', function(e){
                e.stopPropagation();
                this.wantedDisplayStatus = 0;
                this.periodical = this.timerFunction.periodical(this.options.delay,this);
            }.bind(this));*/
        }.bind(this));
    }


});

