////////////////////////////////////////////////////////////////////////////////
var Oishii = {};

////////////////////////////////////////////////////////////////////////////////
Oishii.AnimMorph = function(content, object1, object2){  
    if ($type(content) != 'array')
        content = [content];
    
    content.each(function(item){ 
        item.fx = new Fx.Morph(item, {'link' : 'cancel'});
        
        item.setStyles(object1);
        
        item.addEvents({
            'mouseenter': function(){
                item.fx.start(object2);
            },
            'mouseleave': function(){
                item.fx.start(object1);
            }
        });
    });
};

////////////////////////////////////////////////////////////////////////////////
Oishii.AnimProd = function(){  
       
    var produit = $$('.content-column .produit');
 
    produit.each(function(item){
        var infos = item.getElement('.part-infos');
        
        if (infos)
            infos.setStyles({
                'display': 'block',
                'height' : 0,
                'padding-top': 0,
                'padding-bottom': 0
            });
        
        item.fxButton = new Fx.Morph(infos, {
            'link': 'cancel',
            'duration' : 400
        });
    
        item.addEvents({
            'mouseenter': function() {
                item.fxButton.start({
                    'height': 72,
                    'padding-top': 10,
                    'padding-bottom': 10
                });
            },
            'mouseleave': function() {
                item.fxButton.start({
                    'height': 0,
                    'padding-top': 0,
                    'padding-bottom': 0
                });
            }
        });
    });    
};

////////////////////////////////////////////////////////////////////////////////
Oishii.AnimMenu = function(){  
       
    var produit = $$('.menu-side li');
 
    produit.each(function(item){
        var menu_sub = item.getElement('.menu-sub');
        if (!menu_sub) return;
        
        var length = ((menu_sub.getElements('li')).length * 34) + 'px';
        
        menu_sub.setStyles({
            'display' : 'block',
            'overflow' : 'hidden',
            'height' : '0px'
        });
        
        item.fxButton = new Fx.Tween(menu_sub, {
            'link': 'cancel',
            'property': 'height',
            'duration' : 800,
            'transition' : Fx.Transitions.Quad.easeInOut
        });
    
        item.addEvents({
            'mouseenter': function() {
                item.fxButton.start(length);
            },
            'mouseleave': function() {
                item.fxButton.start('0px');
            }
        });
    });    
};

////////////////////////////////////////////////////////////////////////////////
Oishii.AnimPaginate = function(){
    var link = $$('.paginate .item');
    
    link.each(function(item){
        if (!(item.hasClass('item-current'))) {
            item.setStyle("color", "#fff");
        
            item.addEvents({
                'mouseover':function(){
                    item.tween("color", "#52BD34");
                },
                'mouseout':function(){
                    item.tween("color", "#D4E8DF");
                }
            });
        };
    });
};


////////////////////////////////////////////////////////////////////////////////
Oishii.AnimTween = function(content, property, value1, value2){
    
    if ($type(content) != 'array')
        content = [content];
    
    content.each(function(item){
        if (item) {
            item.setStyle(property, value1);
            
            item.addEvents({
                'mouseover':function(){
                    item.tween(property, value2);
                },
                'mouseout':function(){
                    item.tween(property, value1);
                }
            })
        }
    });
};

////////////////////////////////////////////////////////////////////////////////
Oishii.AnimTween2 = function(content, property, value1, value2, bloc, block){
    if ($type(content) != 'array')
        content = [content];
                
    content.each(function(item){
        if (block == true)
            item.setStyle('display', 'block');
        item.setStyle(property, value1);
        
        bloc.addEvents({
            'mouseover':function(){
                item.tween(property, value2);
            },
            'mouseout':function(){
                item.tween(property, value1);
            }
        })
    });
};

////////////////////////////////////////////////////////////////////////////////
Oishii.AnimOpacity = function(content, opacity1, opacity2, display){
    
    content.each(function(item){
        if (display == true)
            item.setStyle('display', 'block');
        item.setStyle('opacity', opacity1);
        
        item.addEvents({
            'mouseover':function(){
                item.fade(opacity2);
            },
            'mouseout':function(){
                item.fade(opacity1);
            }
        })
    });

};

////////////////////////////////////////////////////////////////////////////////
Oishii.layoutAccueil = function(bloc, colors){
    bloc.each( function (item){
        Oishii.AnimTween2(item.getElements('.ref'), 'color', colors[0], colors[1], item);
        Oishii.AnimTween2(item.getElements('h4 a'), 'color',colors[0], colors[1], item);
        Oishii.AnimTween2(item.getElements('.chapo'), 'color', colors[0], colors[1], item);
        Oishii.AnimTween2(item.getElements('.details'), 'color', colors[2], colors[3], item);
        Oishii.AnimTween2(item.getElements('.imageProduit'), 'border-color', colors[4], colors[0], item);
    });
};

////////////////////////////////////////////////////////////////////////////////
Oishii.layoutShop = function(bloc, colors){
    bloc.each( function (item){
        Oishii.AnimTween2(item.getElements('.ref'), 'color', colors[0], colors[1], item);
        Oishii.AnimTween2(item.getElements('h4 a'), 'color',colors[0], colors[1], item);
        Oishii.AnimTween2(item.getElements('.chapo'), 'color', colors[0], colors[1], item);
        Oishii.AnimTween2(item.getElements('.details'), 'color', colors[0], colors[1], item);
        Oishii.AnimTween2(item.getElements('.imageProduit'), 'border-color', colors[0], colors[1], item);
    });
};

////////////////////////////////////////////////////////////////////////////////
Oishii.layoutRecettes = function(bloc, colors){
    bloc.each( function (item){
        Oishii.AnimTween2(item.getElements('.titleRct a'), 'color', colors[0], colors[1], item);
        Oishii.AnimTween2(item.getElements('.descriptRct'), 'color', colors[0], colors[1], item);
        Oishii.AnimTween2(item.getElements('.imgProduct img'), 'border-color', colors[0], colors[1], item);
    });
};

////////////////////////////////////////////////////////////////////////////////
Oishii.AnimLink = function(bloc){
    bloc.each( function (item){
        var text_banner_link = item.getElement('.text-banner-link');
        var link = item.getElement('.link');
        
        Oishii.AnimTween(text_banner_link, 'color', '#626161', '#000');
        Oishii.AnimTween(link , 'background-position', '0px 0px', '0px -22px');	
        Oishii.AnimTween2(link , 'background-position', '0px 0px', '0px -22px', text_banner_link, false);	
    });
};

////////////////////////////////////////////////////////////////////////////////
Oishii.AnimTopbar = function(){
    
    if (!$('contenu'))
        return;
    var topbar = $('contenu').getElement('.topbar');
    if (!topbar || topbar.extendAnim)
        return;
    
    topbar.extendedAnim = true;
    var title_shop = topbar.getElement('.title-top');
    var menu_top = topbar.getElement('.menu-top');
    var size_box = 0;
    var before_size = 0;
    
    if (topbar) {
        before_size = topbar.getStyle('height');
        menu_top.setStyle('display', 'block');
        size_box = topbar.getStyle('height');
        topbar.setStyle('height', before_size);
        menu_top.setStyle('opacity', '0');
        
        menu_top.fxTopMenu = new Fx.Tween(menu_top, {
            'duration': 500,
            'link': 'cancel',
            'property': 'opacity'
        });
        topbar.fxTopMenu = new Fx.Tween(topbar, {
            'duration': 500,
            'link': 'cancel',
            'property': 'height'
        });
        
        topbar.addEvents({
            'mouseover':function(){
                topbar.fxTopMenu.start(size_box);
                menu_top.fxTopMenu.start(1);
            },
            'mouseout':function(){
                topbar.fxTopMenu.start(before_size);
                menu_top.fxTopMenu.start(0);
            }
        });
    }
};

////////////////////////////////////////////////////////////////////////////////
Oishii.ImageButton = function(element){
    if (element.extendedButton)
        return;
    
    element.extendedButton = true;
    try {
        element.getElement('.hover').setStyles({
            'display': 'block',
            'opacity': 1
        });
    }
    catch (e) {
        if ($defined(console)) console.log(element, e.message);
    };
    
    element.fxButton = new Fx.Tween(element.getElement('.hover'), {
        'duration': 250,
        'link': 'cancel',
        'property': 'opacity'
    });
    
    element.addEvents({
        'mouseenter': function() {
            element.fxButton.start(0);
        },
        'mouseleave': function() {
            element.fxButton.start(1);
        }
    });
};



////////////////////////////////////////////////////////////////////////////////
Oishii.FormQuantity = function(element){
    if (element.getElement('.no-stock') || element.extendedForm)
        return;
    
    element.extendedForm = true;
    element.quantity = 1;
    element.stock = element.getElement('.input-stock').value.toInt();
    
    var url = '/mon_compte/panier/ajouter_[{reference}-{quantity}]';
    
    element.getElement('.button-more').addEvent('click', function(event) {
        event.stop();
        element.quantity++;
        if (element.quantity > element.stock)
            element.quantity = element.stock;
        else if (element.quantity >= 100)
            element.quantity = 99;
        element.quantite.value = element.quantity;
    });
    
    element.getElement('.button-less').addEvent('click', function(event) {
        event.stop();
        element.quantity--;
        if (element.quantity <= 0)
            element.quantity = 1;
        element.quantite.value = element.quantity;
    });
    
    element.getElement('.input-quantity').addEvent('change', function(event) {
        element.quantity = this.value.toInt();
        if ($type(element.quantity) != 'number' || element.quantity == 0)
            this.value = element.quantity = 1;
        if (element.quantity > element.stock)
            element.quantity = element.stock;
    });
    
    element.getElement('.button-add').addEvent('click', function(event) {
        event.stop();
        new Request({
            'url': '/mon_compte/panier',
            'method': 'POST',
            'data': {
                'action': 'ajouter',
                'ref': element.getElement('.input-ref').value,
                'quantite': element.quantity
            },
            'onSuccess': function() {
                element.quantite.value = element.quantity = 1;
                new ToolTip({
                    'link':  element.getElement('.button-add'),
                    'content': 'Cet article a bien été ajouté au panier',
                    'anchor': 'right',
                    'autohide': 1000
                });
            }
        }).send();
    });
    
    element.getElement('.button-buy').addEvent('click', function(event) {
        event.stop();
        element.submit();
    });
    
};



Oishii.ImageSwitch = function(element) {
    if (element.extendedSwitch)
        return;
    
    element.extendedSwitch = true;
    element.main = element.getParent('.layout').getElement('.image-main');
    element.properties = {
        'src': element.main.getElement('.' + element.getElement('.target').value + '-src').value,
        'alt': element.main.getElement('.' + element.getElement('.target').value + '-alt').value
    };
    
    element.setStyle('cursor', 'pointer');
    element.fx = new Fx.Tween(element.getElement('img'), {
        'duration': 250,
        'link': 'cancel',
        'property': 'border-color'
    }).set('#000');
    
    element.addEvents({
        'click': function(event) {
            event.stop();
            element.main.getElement('img').setProperties(element.properties);
        },
        'mouseenter': function() {
            element.fx.start('#706F6D');
        },
        'mouseleave': function() {
            element.fx.start('#000');
        }
    });
};
