jQuery.noConflict();
jQuery(function($) { 

    $(document).ready(function(){
    
        // gestion du rollover
        //$('.area-2 article.last').addClass('popup');
        
        $('.area-2 article.publish').bind('activate', function(){
            $('.area-2 article.publish').removeClass('popup');
            $(this).addClass('popup');
            
            // garder actif 5 secondes
            setTimeout(function(){
                $($('article.publish').get(ep)).trigger('deactivate');
            }, 5000);
        });
        
        $('.area-2 article.publish').bind('deactivate', function(){
            $(this).removeClass('popup');
            $('.area-2 article.last').addClass('popup');
            
            // activer le prochain dans 2 secondes
            ep++;
            if (ep>2) ep=0;
            setTimeout(function(){
                $($('article.publish').get(ep)).trigger('activate');
            }, 1000);
        });
        
        var ep = 0;
        setTimeout(function(){
            $($('article.publish').get(ep)).trigger('activate');
        }, 1000);
    });
    
    // anim object constructor
    function AmzAnim(episode, name, elts, reset, animateIn){
        var t = this;
        t.debug = false;
        
        t.reset = reset;
        t.animateIn = animateIn;
        
        t.animateOut = function(){
            t.episode.find('.the-anim').stop(true).fadeOut('slow');
        }
        
        t.episode = $($('article.publish').get(episode-1));
        
        // creation des éléments
        t.create = function(){
            for(var i=0; i<elts.length; i++){
                $('<div id="'+elts[i]+'" class="the-anim"></div>').insertAfter(t.episode.find('.play')).css({
                    position:'absolute',
                    background: 'url("/wp-content/themes/1nterval/amazonims/img/'+name+'/'+elts[i]+'.png") no-repeat center center transparent',
                    display: this.debug ? "block" : "none"
                });
            }
            // fond
            $('<div id="fond" class="the-anim"></div>').insertAfter(t.episode.find('.play')).css({
                position:'absolute',
                background: 'url("/wp-content/themes/1nterval/amazonims/img/fond.png") no-repeat center center transparent',
                display: this.debug ? "block" : "none"
            });
            t.reset();
        };
        
        $(document).ready(function(){
            if(t.episode.length) {
                t.create();
                if(!t.debug){
                    //t.episode.hover(t.animateIn, t.animateOut);
                    t.episode.bind('activate', t.animateIn);
                    t.episode.bind('deactivate', t.animateOut);
                }
            }
        });
        
    }
    
    var menace = new AmzAnim(
        1, "menace", 
        ["arbre-centre", "mousse-centre", "arbre-cotes", "perso-droit", "perso-gauche", "yuca-droit", "yuca-gauche", "ailes", "branche-bas", "branche-haut", "liane"],
        function(){ //reset
            menace.episode.find('.the-anim').stop(true).css({display:this.debug ? "block" : "none", opacity:1, width:'554px', height:'790px', left:'-95px'});
            menace.episode.find('#fond').css({width:'370px', height:'214px', left:'0px', top:'0px'});
            
            // mise à l'arrière plan
            menace.episode.find('#arbre-cotes, #perso-droit, #perso-gauche, #liane').css({'z-index': '-1'});
            
            menace.episode.find('#mousse-centre, #arbre-cotes, #perso-droit, #perso-gauche, #liane').css({top: '-345px'});
            menace.episode.find('#arbre-centre').css({top:'-203px'});
            menace.episode.find('#ailes').css({bottom:'228px', height: '374px'});
            menace.episode.find('#branche-haut').css({bottom:'294px', height: '308px'});
            menace.episode.find('#branche-bas').css({top:'291px', height: '154px'});
            menace.episode.find('#yuca-droit').css({top:'-174px', height: '0px', left:'304px', width:'0px', 'border-radius':'100px'});
            menace.episode.find('#yuca-gauche').css({top:'-174px', height: '0px', left:'22px', width:'0px', 'border-radius':'100px'});
            
            menace.episode.find('#perso-droit').css({left:'5px', opacity:'0'});
            menace.episode.find('#perso-gauche').css({left:'-195px', opacity:'0'});
        },
        function(){ //animateIn
            menace.reset();
            menace.episode.find('#arbre-centre, #fond').fadeIn('slow', function(){
                menace.episode.find('#arbre-cotes').fadeIn('slow');
                menace.episode.find('#ailes').slideDown('slow');
                menace.episode.find('#perso-droit, #perso-gauche').show().animate({left:'-95px', opacity:'1'});
                menace.episode.find('#mousse-centre').fadeIn('slow',function(){
                    menace.episode.find('#branche-haut').slideDown('slow');
                    menace.episode.find('#branche-bas').slideDown('slow');
                    menace.episode.find('#yuca-gauche').show().animate({width:'162px', height:'145px', left:'-59px', top:'-246px'}, 'slow');
                    menace.episode.find('#yuca-droit').show().animate({width:'162px', height:'145px', left:'223px', top:'-246px'}, 'slow');
                    menace.episode.find('#liane').slideDown('slow');
                });
            });
        }
    );
    
    var fleuve = new AmzAnim(
        2, "fleuve", 
        ["harpie", "tigre", "martine-pecheuse", "poisson-grand", "poisson-petit", "yuca-droit", "yuca-gauche"],
        function(){ //reset
            fleuve.episode.find('.the-anim').stop(true).css({display:this.debug ? "block" : "none", opacity:1, width:'540px', height:'480px', left:'-78px', top:'-162px'});
            fleuve.episode.find('#fond').css({width:'370px', height:'214px', left:'0px', top:'0px'});
            
            fleuve.episode.find('#yuca-gauche').css({width:'0px', height:'0px', left:'-8px', top:'216px', opacity:0, 'z-index': '-1', 'border-radius':'60px'});
            fleuve.episode.find('#yuca-droit').css({width:'0px', height:'0px', left:'372px', top:'228px', opacity:0, 'z-index': '-1', 'border-radius':'90px'});
            fleuve.episode.find('#tigre').css({width:'180px', height:'90px', left:'-150px', top:'173px', opacity:0});
            fleuve.episode.find('#martine-pecheuse').css({'background-position': 'top left'});
            fleuve.episode.find('#poisson-petit').css({width:'51px', height:'45px', left:'85px', top:'170px', rotate: '0deg'});
            fleuve.episode.find('#poisson-grand').css({width:'93px', height:'83px', left:'214px', top:'204px', rotate: '0deg'});
            if (fleuve.path1 == undefined)
                fleuve.path1 = new $.path.arc({ center: [-36,204], radius: 250, start: 100, end: 190, dir: 1 });
            if (fleuve.path2 == undefined)
                fleuve.path2 = new $.path.arc({ center: [190,204], radius: 100, start: -100, end: 80, dir: -1 });
        },
        function(){ //animateIn
            fleuve.reset();
            fleuve.episode.find('#fond').fadeIn();
            fleuve.episode.find('#martine-pecheuse').fadeIn(function(){
                fleuve.episode.find('#tigre').show().animate({left:'-78px', opacity:1});
                fleuve.episode.find('#harpie').fadeIn('slow', function(){
                    fleuve.episode.find('#martine-pecheuse').css({'background-position': 'bottom left'});
                    $(this).hide();
                    (function repeat() {
                        fleuve.episode.find('#martine-pecheuse').animate({rotate: '1deg'}, 1000, function(){
                            $(this).animate({rotate: '-1deg'}, 1000, repeat);
                        });
                    })();
                    
                    fleuve.episode.find('#poisson-grand').show().animate({path: fleuve.path1, opacity:1}, {easing:'linear', step: function(dumb, fx) {
                        if(fx.prop=='path') {
                            $(this).rotate(fx.pos*-50+'deg');
                        }
                    }, complete: function(){
                        fleuve.episode.find('#poisson-petit').show().animate({path: fleuve.path2, opacity:1}, {easing:'linear', step: function(dumb, fx) {
                            if(fx.prop=='path') {
                                $(this).rotate(fx.pos*80+'deg');
                            }
                        }});
                    }});
                });
            });
            fleuve.episode.find('#yuca-gauche').show().delay(200).animate({width:'140px', height:'120px', left:'-78px', top:'156px', opacity:1});
            fleuve.episode.find('#yuca-droit').show().delay(500).animate({width:'180px', height:'180px', left:'282px', top:'138px', opacity:1});   
        }
    );
    
    var yacumama = new AmzAnim(
        3, "yacumama", 
        ["perroquet", "toucan", "fleur", "sarbacane", "leopard", "archer", "plante", "nenuphar"],
        function(){ //reset
            yacumama.episode.find('.the-anim').stop(true).css({display:this.debug ? "block" : "none", opacity:1, width:'561px', height:'552px', left:'-100px', top:'-220px'});
            yacumama.episode.find('#fond').css({width:'370px', height:'214px', left:'0px', top:'0px'});
            
            yacumama.episode.find('#leopard').css({width:'190px', height:'100px', left:'-136px', top:'180px', opacity: 0});
            yacumama.episode.find('#nenuphar').css({width:'200px', height:'80px', left:'-40px', top:'260px', opacity: 0});
            yacumama.episode.find('#fleur').css({width:'40px', height:'30px', left:'330px', top:'-70px', opacity: 0});
        },
        function(){ //animateIn
            yacumama.reset();
            yacumama.episode.find('#fond').fadeIn();
            yacumama.episode.find('#fleur').delay(500).show().animate({top:'-20px', opacity: 1}, function(){
                yacumama.episode.find('#plante').fadeIn();
            });
            yacumama.episode.find('#nenuphar').show().animate({top:'230px', opacity: 1}, function(){
                yacumama.episode.find('#archer').fadeIn();
                yacumama.episode.find('#sarbacane').delay(200).fadeIn(function(){
                    yacumama.episode.find('#leopard').show().animate({left:'-36px', opacity: 1}, 1000);
                    yacumama.episode.find('#perroquet').delay(200).fadeIn(1000);
                    yacumama.episode.find('#toucan').delay(400).fadeIn(1000);
                });
            });
        }
    );
});

