/*
    Oishii Shop 1.0
    ===========================
    developed by Core Engine
    http://www.core-engine.com/
*/



var OishiiController = new Class({
    
    
    
    initialize: function() {
        /*
        Initialize
        ------------------------------------
        */
        
        window.formutils = new FormUtils();
        Fx.prototype.options.duration = 250;
        Fx.prototype.options.fps = 25;
        this.animate();
    },
    
    
    
    animate: function() {
        /*
        Animate content
        ------------------------------------
        */
        
        this.__fxFormCheck();
        this.__fxImageButton();
        this.__fxLightbox();
        this.__fxSearch();
        
        this.__fxAccueil();
    this.__fxShop();
    this.__fxSav();
    this.__fxAnimGeneral();
    },
    
    
    
    __fxAccueil: function() {
        /*
        Animate "accueil" page
        ------------------------------------
        */
        
    
    
        if (!$('contenuAccueil') || $('contenuAccueil').extended)
            return;
        
        $('contenuAccueil').extended = true;
        
        $$('.menu-rubriques-accueil a').each(function(item) {
            var hover = item.getElement('.menu-rubriques-image-hover');
            hover.setStyle('opacity', 0);
            hover.fx = new Fx.Tween(hover, {
                'property': 'opacity',
                'link': 'cancel'
            });
            item.addEvents({
                'mouseenter': function(event) {
                    hover.fx.start(0, 1);
                    hover.realTop = hover.getStyle('top');
                },
                'mouseleave': function(event) {
                    hover.setStyle('top', hover.realTop);
                    hover.fx.start.delay(1000, hover.fx, [1, 0]);
                }
            });
        });
    
    },
    


    __fxShop: function() {
        /*
        Animate "shop" page
        ------------------------------------
        */
        
        if (!$('contenuShop') || $('contenuShop').extended)
            return;
        
        $('contenuShop').extended = true;
        
        var menu = $$('#content-menu-shop .item').filter(function(element) { return !element.hasClass('current'); });
        
        Oishii.AnimTween(menu, 'color', '#D4E8DF', '#52BD34');
        
        contenu_bloc = $$('.contenuBloc');
        button_add = $$('.button-add');
        button_details = $$('.button-details');
        
        Oishii.layoutShop(contenu_bloc, ['#626161', '#000']);
        Oishii.AnimTween(button_add, 'color', '#387481', '#52BD34');
        Oishii.AnimTween(button_details, 'color', '#387481', '#52BD34');
        
        Oishii.AnimTween($$('.produit.contenuBloc'), 'background-color','#FFFFFF','#ececec');
        
        $$('.form-quantity').each(Oishii.FormQuantity);
    },
    
    __fxSav: function() {
        
    /*
        Animate "sav" page
        ------------------------------------
        */
        
        if (!$('contenu') || $('contenu').extended)
            return;
        
        Oishii.AnimTween($$('#form-service .button-send'), 'color', '#000000', '#D4E8DF');
        Oishii.AnimTween($$('#form-service .captcha-link'), 'color', '#000000', '#D4E8DF');
    }, 
    
    __fxAnimGeneral: function() {
        
        var menu = $$('#menuTop .item').filter(function(element) { return !element.hasClass('current'); });
        var logo = $$('.logo');
        var buttons = $$('#top .buttons');
        var logo_main = $$('#headerLogo .logo-main');
        var header_olaria = $('header-olaria');
        var header_logo = $('header-logo');
        var sub_menu = $$('.item-sub');
        var footer_logo_ce = $$('#footer-ce .core-engine');
        
        Oishii.AnimMorph(menu, {'background-color' : '#D4E8DF', 'color' : '#252015'}, {'background-color' : '#252015', 'color' : '#52BD34'});
        Oishii.AnimOpacity(logo, 1, 0.7, false);
        Oishii.AnimTween(buttons, 'color', '#D4E8DF', '#52BD34');
        Oishii.AnimTween($$('#footer .links a'), 'color', '#D4E8DF', '#52BD34');
        Oishii.AnimTween($$('.topbar .menu-top .item'), 'color', '#FFB807', '#D4E8DF');
        Oishii.AnimTween2(header_olaria, 'opacity', 1, 0.5, logo_main, true);
        //Oishii.AnimTween2($$('#banner .notify-anim'), 'opacity', 1, 0, $('banner'), true);
        //Oishii.AnimTween2(header_logo, 'background-position', '0px -22px', '0px 0px', logo_main, false);
        Oishii.AnimTween(sub_menu, 'color', '#FEE29C', '#FCB402');
        Oishii.AnimOpacity(footer_logo_ce, 1, 0.7, false);
        Oishii.AnimTopbar();
        Oishii.AnimPaginate();
        Oishii.AnimMenu();
        $$('.layout .image-sub').each(Oishii.ImageSwitch);
        
    },
    
    
    
    __fxSlideShow: function() {
        /*
        Animate slideshow
        ------------------------------------
        */
        
        var banner = $('content-banner');
        if(!banner || banner.extendedSlideShow)
            return;

        banner.extendedSlideShow = true;
        banner.slideshow = new SlideShow(banner);
        banner.slideshow.play();
    },
    
    
    
    __fxFormCheck: function() {
        /*
        Animate form checking
        ------------------------------------
        */
        
        $$('.form-ajaxCheck').each(function(form) {
            if (form.extendedForm)
                return;
            
            form.addEvent('submit', function(event) {
                event.stop();
                formutils.check(form);
            });
        });
    },
    
    
    
    __fxImageButton: function() {
        /*
        Animate image buttons
        ------------------------------------
        */
        
        $$('.imageButton').each(Oishii.ImageButton);
        Oishii.AnimTween($$('.account .link'), 'color', '#387481', '#AB0505');
        Oishii.AnimTween($$('.account .button'), 'color', '#FFFFFF', '#52BD34');
    },
    
    
    
    __fxLightbox: function() {
        /*
        Animate lightbox buttons
        ------------------------------------
        */
        
        $$('.button-lightbox').each(function(button) {
            if (button.extendedLightbox)
                return;
            
            button.extendedLightbox = true;
            button.url = button.getProperty('href');
            button.content = null;
            
            button.addEvent('click', function(event) {
                event.stop();
                if (!button.content)
                    new Request({
                        'url': button.url,
                        'method': 'POST',
                        'data': {'ajax': 1},
                        'onSuccess': function() {
                            button.content = this.xhr.responseText;
                            new LightBox({
                                'content': this.xhr.responseText
                            }).show();
                        }
                    }).send();
                else
                    new LightBox({
                        'content': button.content
                    }).show();
            });
        });
    },
    
    
    
    __fxSearch: function() {
        /*
        Animate search form
        ------------------------------------
        */
        
        var search = $('form-search');
        if (!search || search.extendedForm)
            return;
        
        search.extendedForm = true;
        search.setStyle('width', 20);
        search.fx = new Fx.Tween(search, {
            'duration': 450,
            'link': 'cancel',
            'property': 'width',
            'transition': Fx.Transitions.Quad.easeInOut
        });
        
        search.active = false;
        
        search.addEvents({
            'mouseenter': function() {
                if (!search.active) {
                    search.fx.start(178).chain(function () {
                        search.active = true;
                    });
                }
            }
        });
        search.getElements('input[name=motcle]')[0].addEvents({
            'mouseleave': function() {
                if (search.active == true) {
                    search.fx.start(20).chain(function () {
                      search.active = false;  
                    });
                }
            }
        });
    }
    
});
