/**
 * Snowflake generator. Generates and updates the position of Snowflake elements on a target Canvas.
 *
 * Depends on Snowflake.js, StringUtils.js
 */


/**
 * Constructor
 */
function SnowFlakeGenerator( wpfeControl, targetCanvas, maxFlakes, flakeXaml ) 
{
    if( Snowflake == undefined ) throw "Error in SnowFlakeGenerator - Snowflake.js must be included before use.";
    if( StringUtils == undefined ) throw "Error in SnowFlakeGenerator - StringUtils.js must be included before use.";
    
    this.wpfeControl = wpfeControl;
    this.targetCanvas = targetCanvas;
    this.maxFlakes = maxFlakes;
    this.flakeXaml = flakeXaml;
    
    this.snowflakes = [];
}


/**
 * Public properties
 */
SnowFlakeGenerator.prototype.windXVel = 0;
SnowFlakeGenerator.prototype.windYVel = 1;
SnowFlakeGenerator.prototype.flakeXaml;

/**
 * Private properties
 */
SnowFlakeGenerator.prototype.wpfeControl;
SnowFlakeGenerator.prototype.targetCanvas;
SnowFlakeGenerator.prototype.maxFlakes;
SnowFlakeGenerator.prototype.flakeXaml;
SnowFlakeGenerator.prototype.flakeId = 0;
SnowFlakeGenerator.prototype.snowflakes;


/**
 * Adds a snowflake to the canvas
 */
SnowFlakeGenerator.prototype.addSnowFlake = function()
{   
    // Add unique x:Names to flakeXaml
    var tempSnowFlakeXaml = this.flakeXaml;
    tempSnowFlakeXaml = StringUtils.replaceAll( tempSnowFlakeXaml, "__ID__", this.flakeId );
    
    var snowflake = new Snowflake();
    snowflake.setXaml( tempSnowFlakeXaml );
    
   // Debug.log( snowflake.toXaml() );
    
    var snowflakeObj = this.wpfeControl.createFromXaml( snowflake.toXaml() );
    snowflake.xamlObject = snowflakeObj;
    this.targetCanvas.Children.Add( snowflakeObj );
    
    // Store references to the XAML elements Transforms for later use
    snowflake.scaleTransform = snowflakeObj.FindName( "flake"+this.flakeId+"ScaleTrans" );
    
    // Scale and position
    var scale = Math.random()/2 + 0.1;
    snowflake.scaleTransform.ScaleX = snowflake.scaleTransform.ScaleY = scale;
    snowflake.xamlObject["Canvas.Left"] = (Math.random() * this.targetCanvas.Width+150) - 300;   
    snowflake.xamlObject["Canvas.Top"] = (Math.random() * this.targetCanvas.Height)-200;   
    snowflake.xamlObject.Opacity = scale;
    snowflake.xVel = Math.random()*2;
    snowflake.yVel = scale*10;
    
    this.snowflakes.push( snowflake );
    ++this.flakeId;
}


/**
 * Update the position of the snowflakes as they fall (adding new ones when count gets too low)
 */
SnowFlakeGenerator.prototype.update = function()
{
    try
    {
        var canvasHeight = this.targetCanvas.Height + 100;
 
        for( var i=this.snowflakes.length; i--; )
        {
            var snowflake = this.snowflakes[i];
            
            snowflake.xamlObject["Canvas.Top"] += snowflake.yVel + this.windYVel/10;
            snowflake.xamlObject["Canvas.Left"] += snowflake.xVel + this.windXVel;
             
            if( snowflake.xamlObject["Canvas.Top"] > canvasHeight )
            {
                this.targetCanvas.Children.Remove( snowflake.xamlObject );
                this.snowflakes.splice( i, 1 );
                delete snowflake;
            }
        }
        
        if( this.snowflakes.length < this.maxFlakes )
        {
            this.addSnowFlake();
        }
    }
    catch( e ) 
    {
        try{ Debug.log( "Error in SnowFlakeGenerator - " + e ); } catch(e) {}
    }
}

/**
 * Clean up and remove all flakes
 */
SnowFlakeGenerator.prototype.destroy = function()
{
     for( var i=this.snowflakes.length; i--; )
     {
       this.targetCanvas.Children.Remove( this.snowflakes[i].xamlObject );
       this.snowflakes.splice( i, 1 );
     }
     this.snowflakes = [];
}