/**
 * @author timharwood
 */
var PageAnimation = new Class({

    Implements: [Events, Options],
    
    options: {
        id: 'PageAnimation' + new Date().getTime() + "-" + Math.random(),
        menuContainer: null,
        footerContainer: null,
        logoDuration: 750,
        startMargin: -280,
        endMargin: 0,
        menuDuration: 750,
        debug: false
    },
    
    menuContainer: null,
    footerContainer: null,

    logoFX: null,
    menuFX: null,
    timer: null,
    
    initialize: function(options){
        this.setOptions(options);
        this.initaliseMenu();
        this.initaliseFooter();
    },

    initaliseFooter: function(){
        this.footerContainer = $(this.options.footerContainer)
        if (this.footerContainer) {
            this.footerFX = new Fx.Tween(this.footerContainer, {
                duration: this.options.logoDuration,
                unit:'px'
            });
            this.footerFX.set('margin-left', this.options.startMargin);
        }
    },
    
    initaliseMenu: function(){
        this.menuContainer = $(this.options.menuContainer);
        if (this.menuContainer) {
            this.menuFX = new Fx.Tween(this.menuContainer, {
                duration: this.options.menuDuration
            });
            this.menuFX.set('opacity', '0');
        }
    },

    beginAnim: function(){
        this.footerFX.addEvent('complete', this.footerComplete.bind(this));
        this.footerFX.start('margin-left', this.options.endMargin);
    },
    
    footerComplete: function(e){
        this.fadeMenuIn();
    },
    
    fadeMenuIn: function(e){
        this.menuFX.addEvent('complete', this.fadeComplete.bind(this));
        this.menuFX.start('opacity', '1');
    },
    
    fadeComplete: function(e){
        this.menuFX.removeEvents('complete');
        this.footerFX.removeEvents('complete');
        
        this.menuContainer = null;
        this.footerContainer = null;
        this.footerFX = null;
        this.menuFX = null;

        this.fireEvent('complete', {
            type: 'complete',
            target: this
        });
    }
    
});


/**
 * @author timharwood
 */
var IntroAnimation = new Class({

    Implements: [Events, Options],
    
    options: {
        id: 'IntroAnimation' + new Date().getTime() + "-" + Math.random(),
        viewContainer: null,
        animContainer: null,
        footerContainer: null,
        animDuration: 3000,
        fadeInDuration: 750,
        postFadePause: 1000,
        fadeOutDuration: 750,
        debug: false
    },
    
    img: null,
    animContainer: null,
    footerContainer: null,
    viewContainer: null,
    footerFX: null,
    containerFX: null,
    timer: null,
    
    initialize: function(options){
        this.setOptions(options);
        this.initaliseAnim();
        this.initaliseFooter();
        this.initaliseContainer();
    },
    
    initaliseAnim: function(){
        this.animContainer = $(this.options.animContainer);
        if (this.animContainer) {
            var theImg = this.animContainer.getFirst('img').dispose();
            this.img = Asset.image(theImg.get('src'), {
                onLoad: this.imageLoad.bind(this)
            });
            
        }
    },
    
    initaliseFooter: function(){
        this.footerContainer = $(this.options.footerContainer)
        if (this.footerContainer) {
            this.footerFX = new Fx.Tween(this.footerContainer, {
                duration: this.options.fadeInDuration
            });
            this.footerFX.set('opacity', '0');
        }
    },
    
    initaliseContainer: function(){
        this.viewContainer = $(this.options.viewContainer);
        if (this.viewContainer) {
            this.containerFX = new Fx.Tween(this.viewContainer, {
                duration: this.options.fadeOutDuration
            });
            this.containerFX.set('opacity', '1');
        }
    },
    
    imageLoad: function(e){
        this.animContainer.adopt(this.img);
        this.beginAnim();
    },
    
    beginAnim: function(e){
        this.timer = setTimeout(this.animComplete.bind(this),this.options.animDuration);
    },
    
    animComplete: function(){
        clearTimeout(this.timer);
        this.footerFX.addEvent('complete', this.footerComplete.bind(this));
        this.footerFX.start('opacity', '1');
    },
    
    footerComplete: function(e){
        this.fadeOut.delay(this.options.postFadePause,this);
    },
    
    fadeOut: function(e){
        this.containerFX.addEvent('complete', this.fadeOutComplete.bind(this));
        this.containerFX.start('opacity', '0');
    },
    
    fadeOutComplete: function(e){
        this.containerFX.removeEvents('complete');
        this.footerFX.removeEvents('complete');
        
        this.animContainer = null;
        this.footerContainer = null;
        this.footerFX = null;
        this.containerFX = null;
        this.img = null;
        
        this.viewContainer.dispose();
        this.fireEvent('complete', {
            type: 'complete',
            target: this
        });
    }
    
});





