/**
 * Class mootools qui permet de gérer un disclaimer
 */

var MooDisclaimer = new Class({

    getOptions: function() {
        return {}
    },
    
    initialize: function(domain, flags, texts, options) {
        this.setOptions(this.getOptions(), options);
        this.domain = domain;
        this.flags = flags;
        this.texts = texts;
        if(Cookie.read('disclaimer_dsp')!='hide') {
            this.build();
        }
    },
    
    centerize: function() {
        var body_size = $(document.body).getSize();
        this.discl_el.setStyles({
            'top':((body_size.y/2)-(this.discl_el.getSize().y/2)+window.getScrollTop())+'px',
            'left':((body_size.x/2)-(this.discl_el.getSize().x/2)+window.getScrollLeft())+'px'
        });
    },

    build: function() {
        var mentions_array = new Array(
            this.texts.disc_mention_1,
            this.texts.disc_mention_2,
            this.texts.disc_mention_3,
            this.texts.disc_mention_4,
            this.texts.disc_mention_5,
            this.texts.disc_mention_6,
            this.texts.disc_mention_7,
            this.texts.disc_mention_8
        );

        //on cree la liste ul avec les mentions
        var ul_el = new Element('ul');
        ul_el.adopt(new Element('h1',{'html':this.texts.disc_title+' : '}));
        for(var i=0;i<mentions_array.length;i++) {
            ul_el.adopt(new Element('li',{'html':mentions_array[i]}));
        }
        //on crée le div pour les drapeaux
        var div_flags = new Element('div',{'id':'flag_bar'});
        for(var i=0; i<this.flags.length; i++) {
            div_flags.adopt(new Element('a',{'href':this.flags[i].url}).adopt(new Element('img',{'src':'/img/_global/flags_lng/'+this.flags[i].abbre+'.gif'})));
        }
        //on crée les bouton entrer, quitter
        var div_btns = new Element('div');
        //btn entrer
        div_btns.adopt(new Element('input',{
            'type':'button',
            'name':'enter',
            'value':this.texts.disclaimer_ok,
            'styles': {
                'background-color':'#01CF00',
                'color':'white',
                'font-weight':'bold',
                'font-size':'16px',
                'border':'1px solid white',
                'margin-right':'5px'
            }
        }).addEvent('click',this.enter.bind(this)));
        //btn quitter
        div_btns.adopt(new Element('input',{
            'type':'button',
            'name':'leave',
            'value':this.texts.disclaimer_quit,
            'styles': {
                'background-color':'red',
                'color':'white',
                'border':'1px solid white',
                'margin-left':'5px'
            }
        }).addEvent('click',this.leave.bind(this)));
        //on crée le conteneur de ces éléments
        this.discl_el = new Element('div',{
            'id':'disclaimer_box',
            'class':'disclaimer',
            'styles':{
                'top':'10px',
                'z-index':999
            }
        });
        //on inject les éléments
        this.discl_el.adopt(div_flags);
        this.discl_el.adopt(ul_el);
        this.discl_el.adopt(div_btns);
        
        //on crée l'overlay
        var scroll_size = $(document.body).getScrollSize();
        this.overlay_el = new Element('div', {
            'styles' : {
                'position':'absolute',
                'top':'0px',
                'left':'0px',
                'width':scroll_size.x+'px',
                'height':scroll_size.y+'px',
                'background-color':'#000',
                'z-index':998
            }
        });
        this.overlay_el.set('opacity','0.8');
        $(document.body).adopt(this.overlay_el);
        $(document.body).adopt(this.discl_el);
        
        //on centre
        window.addEvents({
            "scroll": function() {this.centerize();}.bind(this),
            "resize": function() {this.centerize();}.bind(this)
        });
        this.centerize();

    },
    
    
    hide: function() {
        this.overlay_el.setStyle('display','none');
        this.discl_el.setStyle('display','none');
    },
    
    enter: function() {
        this.hide();
        Cookie.write('disclaimer_dsp','hide',{
            'domain':this.domain,
            'path':'/'
        });
    },
    
    leave: function() {
        window.location.href='http://www.google.com/';
    }

});

MooDisclaimer.implement(new Options);
