
var MooSlide = new Class({
    initialize : function () {
        this.div = $$('.wrapslide');
        if (this.div.length < 1)
            return;
        this.div = this.div[0];
        this.width = 0;
        this.childs = 0;
        this.current = 0;
        this.step = 567;
        this.fx = new Fx.Tween(this.div, {duration : 'long'});
        this.div.getElements('img').each(function (e){
            this.width += parseInt(e.getSize().x);
            this.childs++;
        }.bind(this))
        this.div.setStyle('width', (this.width + 1));
        this.childs = Math.ceil(this.width / this.step);
        $('diaposlider').getElement('a#prev').addEvent('click', function(e) {
            e.preventDefault();
            this.slide(this.current - 1);
        }.bind(this))
        $('diaposlider').getElement('a#next').addEvent('click', function(e) {
            e.preventDefault();
            this.slide(this.current + 1);
        }.bind(this))
        this.checkArrow();
    },
    slide : function (to) {
        if (to < 0 || to > this.childs - 1)
            return;
        var from, next;
        next = (to) % this.childs;
        from = parseInt(this.div.getStyle('left'));
        from = from ? from : 0;
        if (to > this.current)
            to = -(this.step - ((this.step * next) - (this.width - this.step))) + 18;
        else
            to = next * this.step * -1;
        this.fx.start('left', from, to + 'px');
        this.current = next;
        this.checkArrow();
    },
    checkArrow : function () {
        if (this.current == 0)
            $('diaposlider').getElement('a#prev').fade('out');
        else
            $('diaposlider').getElement('a#prev').fade('in');
        if (this.current >= this.childs - 1)
            $('diaposlider').getElement('a#next').fade('out');
        else
            $('diaposlider').getElement('a#next').fade('in');
    }
}),
main = function() {
    try {
        var m = new FlexSlider({
            divs : $('slider'),
            width : 667,
            controls : $('controls'),
            noTouchMove : true
        })
    } catch (e) {
        try {
            console.log(e);
        } catch (e) {

        }
    }
    // Onglets certifications
    if($('certifications')){
        var certifs = $('certifications');
        certifs.addClass('enhanced');
        $$('.onglet').each(function(el, i){
            el.setStyle('position', 'absolute');
            var title = el.getElement('h3');
            title.setStyles({
                'position'	: 'absolute',
                'left'		: (32+(i*86))+'px'
            });
            title.addEvent('click', function(el){
                var onglet = this.getParent();
                certifs.getElements('.onglet').removeClass('current');
                onglet.addClass('current');
            });
        });
        $('certifications').getElements('form').each(function (el) {
            el.addEvent('submit', function (e) {
                this.getElements('input').each(function(input) {
                    if (!input.retrieve('hasChanged'))
                        input.set('value', '');
                })
            });
        });
    }

    $('recherche').getElements('form').each(function (el) {
        el.addEvent('submit', function (e) {
            this.getElements('input').each(function(input) {
                if (!input.retrieve('hasChanged'))
                    input.set('value', '');
            })
        });
    });
    // Navigation
    if($('main_nav')){
        var nav = $('main_nav');
        if (nav.getElement('li.active')) {
            nav.getElement('li.active .sub_nav').setStyle('display', 'block');
            nav.getElements('li').each(function(el, i){
                el.addEvents({
                    'mouseover' : function(e){
                        nav.getElements('li .sub_nav').setStyle('display', 'none');
                        if(this.getElement('.sub_nav')){
                            this.getElement('.sub_nav').setStyle('display', 'block');
                        }
                    },
                    'mouseleave' : function(e){
                        nav.getElements('li .sub_nav').setStyle('display', 'none');
                        nav.getElement('li.active .sub_nav').setStyle('display', 'block');
                    }
                });
            });
        }
    }
	
    // Fiches certifications
    if($('main_content').getElements('.vcard'))
    {
        var vcards = $$('.vcard');
        vcards.each(function(el, i){
            var certif = el.getElement('.certificateur'), sl = new Fx.Slide(certif),
            exDiv = new Element('div', {
                'class': 'expand'
            });
            if (certif.hasClass('nobg') != '')
                exDiv.addClass('nobg');
            else {
                exDiv.addEvent('click', function(e){
                    sl.toggle();
                    if(sl.open === false){
                        exDiv.addClass('opened');
                    }else{
                        exDiv.removeClass('opened');
                    }
                });
            }
            exDiv.inject(el, 'bottom');
            sl.hide();
        });
    }
	
    // Vider automatiquement les champs texte et/ou password
    if($$('input[type=\'text\']')){
        var input = $$('input[type=\'text\']');
        input.addEvent('focus', function(e){
            if (!this.retrieve('hasChanged'))
                this.set('value', '');
            this.store('hasChanged', true);
        });
        input.addEvent('click', function(e){
            if (!this.retrieve('hasChanged'))
                this.set('value', '');
            this.store('hasChanged', true);
        });
    }
    if($$('input[type=\'password\']')){
        $$('input[type=\'password\']').addEvent('click', function(e){
            if (!this.retrieve('hasChanged'))
                this.set('value', '');
            this.store('hasChanged', true);
        });
    }
	
    // Mode Ajax PEFC en France
    if($('pefc_en_france')){
        var enF = $('pefc_en_france');
        var enFSel = enF.getElement('select');
        enFSel.setStyle('width', '262px');
        enF.getElement('input[type=\'submit\']').setStyle('display', 'none');
        enFSel.addEvent('change', function(e){
            if (Browser.Engine.trident)
                window.location = '/regions/' + this.get('value').toString();
            else
                window.location = 'regions/' + this.get('value').toString();
        });
    }
    if ($('back')) {
        $('back').setStyle('cursor', 'pointer');
        $('back').addEvent('click', function () {
            history.go(-1);
        })
    }
    if ($('printo')) {
        $('printo').setStyle('cursor', 'pointer');
        $('printo').addEvent('click', function () {
            window.print();
        })
    }
    if ($('pdt_categorie')) {
        $('pdt_categorie').addEvent('change', function (e) {
            var el = this, to, req;
            to = $('pdt_type');
            req = new Request.JSON({
                method : 'get',
                url : 'index.php?part=certification',
                onRequest : function () {
                    to.empty();
                    new Element('option', {
                        'value' : 0,
                        'text' : 'Chargement...'
                    }).inject(to);
                },
                onSuccess : function (json) {
                    to.empty();
                    new Element('option', {
                        'value' : 0,
                        'text' : 'Type de produit'
                    }).inject(to);
                    json.each(function(o) {
                        if (o.LibProduit) {
                            new Element('option', {
                                'value' : o.CatCode,
                                'text' : o.LibProduit
                            }).inject(to);
                        }
                    });
                }
            });
            req.send('ajax=' + el.options[el.selectedIndex].value);
        })
    }

    new QuickBox();
    /*
    initImageZoom({
        overlay : true
    });*/
}

window.addEvent('domready', function() {
    main();
});

window.addEvent('load', function () {
    try {
        var m = new MooSlide();
    } catch (e) {
        try {
            console.log(e);
        } catch (e) {

        }
    }
})
