//Augment Module
var ITW = (function(itw){
    "use strict";
    //aliases
    var components, protot;
    itw.components = itw.components || Object.create(null);
    components = itw.components;

    components.Wave = function( type ){
        this.super.call(this);

        var _init = function(){
            // init stuff here
            //var randomWave = Math.floor( EHDI.NumberUtil.randomRange(1, 3) );

            var _texMain = EHDI.Assets.images[ "wave" + type ];
            this._sprMain = new EHDI.aka.Sprite( _texMain );

            this.addChild( this._sprMain );

            this._sprMain.scale.set( 1 );

            this.x = -( this.width );

            this.tl = new TimelineMax({repeat: -1, yoyo: true});
            this.tl.to(
                this._sprMain,
                EHDI.NumberUtil.randomRange(1, 4),
                { y: Math.floor( EHDI.NumberUtil.randomRange(-12, -7) ), ease: Power1.easeInOut }
            );

            this._bindedfunction = this.loop.bind(this);
            ITW.UpdateMgr.addFrameListener( this._bindedfunction );
        }.bind(this);
        _init();
    }

    protot = components.Wave.prototype = Object.create(EHDI.aka.Container.prototype);
    protot.constructor = components.Wave;
    protot.super = EHDI.aka.Container;

    protot.loop = function( dt ){
        if( ITW.GameMgr.getPaused() === true ) return;
        if( ITW.GameMgr.getGameOver() === true ) return;
  
        if( this.x < -( this._sprMain.width ) ){
            //this.destroy();
            return;
        }

        dt *= 0.001;

        var curSpeed = ITW.GameMgr.getSpeed();
        this.x -= dt * ( curSpeed * 0.35 );
    }

    protot.resetState = function( posX ){
        this._sprMain.scale.set( EHDI.NumberUtil.randomRange(0.75, 1) );

        if( typeof posX !== "number" )  this.x = ITW.SceneMgr.getStageWidth();
        else                            this.x = posX;
    }

    protot.pauseTimeline = function( state ){
        if( state === true )    this.tl.pause();
        else                    this.tl.resume();
    }

    protot.destroy = function(){
        if( typeof this._bindedfunction === 'function' ) ITW.UpdateMgr.removeFrameListener( this._bindedfunction );
        this.tl.stop();
        this.tl.kill();
        this.super.prototype.destroy.call(this, {children: true});
    }

    return itw;
}(ITW || Object.create(null)));