function Sprite(x, y, svgNode, type, width, height, shape, areaLeft, areaTop, areaWidth, areaHeight)
{
    this.x = x;
    this.y = y;
    this.speedX = 0;
    this.speedY = 0;
    this.speedMax = 2;
    this.svgNode = svgNode;
    this.type = type;
    this.width = width;
    this.height = height;
    this.oldX = 0;
    this.oldY = 0;
    this.pushedX = 0;
    this.pushedY = 0;
    this.pushedBy = 0;
    this.energy = 2;
    this.energyDirection = 0;
    this.shape = shape;
    this.timer = 0;
    this.timerMax = 6;
    this.timerReady = false;
    this.targetX = 0;
    this.targetY = 0;
}

Sprite.prototype.areaTop = 0;
Sprite.prototype.areaLeft = 0;
Sprite.prototype.areaWidth = 0;
Sprite.prototype.areaHeight = 0;

Sprite.prototype.ready = function()
{
    var isReady = this.timerReady;
    if (isReady)
    {
        this.timerReady = false;
    }
    return isReady;
}

Sprite.prototype.handleTimer = function()
{
    if (!this.timerReady)
    {
        this.timer++;
        if (this.timer == this.timerMax)
        {
            this.timer = 0;
            this.timerReady = true;
        }
    }
}

Sprite.prototype.dieOutside = function()
{
    var died = false;
    died = spriteDieAreaBottom(this);
    if (!died)
    {
        died = spriteDieAreaTop(this);
    }
    if (!died)
    {
        died = spriteDieAreaLeft(this);
    }
    if (!died)
    {
        died = spriteDieAreaRight(this);
    }
    return died;
}

Sprite.prototype.dieAreaLeft = function()
{
    var died = false;
    if (this.x + this.width < this.areaLeft)
    {
        this.energy = 0;
        died = true;
    }
    return died;
}

Sprite.prototype.dieAreaRight = function()
{
    var died = false;
    if (this.x > this.areaWidth)
    {
        this.energy = 0;
        died = true;
    }
    return died;
}

Sprite.prototype.dieAreaTop = function()
{
    var died = false;
    if (this.y + this.height < this.areaTop)
    {
        this.energy = 0;
        died = true;
    }
    return died;
}

Sprite.prototype.dieAreaBottom = function()
{
    var died = false;
    if (this.y > this.areaHeight)
    {
        this.energy = 0;
        died = true;
    }
    return died;
}

Sprite.prototype.checkCollision = function(that)
{
    if (this.pushedBy == 0 && that.pushedBy == 0)
    {
        var rightBorderIn = (this.x + this.width > that.x && this.x + this.width < that.x + that.width);
        var leftBorderIn = (this.x < that.x + that.width && this.x > that.x);
        var bottomBorderIn = (this.y + this.height > that.y && this.y + this.height < that.y + that.height);
        var topBorderIn = (this.y < that.y + that.height && this.y > that.y);
        this.setPushed(that, rightBorderIn, leftBorderIn, topBorderIn, bottomBorderIn);
    }
}

Sprite.prototype.setPushed = function(that, rightBorderIn, leftBorderIn, topBorderIn, bottomBorderIn)
{
    if ( (rightBorderIn || leftBorderIn) && (bottomBorderIn || topBorderIn) )
    {
        if (rightBorderIn)
        {
            this.pushedX = -1;
            that.pushedX = 1;
        }
        else if (leftBorderIn)
        {
            this.pushedX = 1;
            that.pushedX = -1;
        }

        if (bottomBorderIn)
        {
            this.pushedY = -1;
            that.pushedY = 1;
        }
        else if (topBorderIn)
        {
            this.pushedY = 1;
            that.pushedY = -1;
        }

        if (leftBorderIn && rightBorderIn)
        {
            this.pushedX = sgn(this.speedX);
            that.pushedX = sgn(that.speedX);
        }
        if (topBorderIn && bottomBorderIn)
        {
            this.pushedY = sgn(this.speedY);
            that.pushedY = sgn(that.speedY);
        }

        if (this.pushedX != 0 || this.pushedY != 0 || that.pushedX != 0 || that.pushedY != 0)
        {
            this.pushedBy = that.type;
            that.pushedBy = this.type;
        }
    }
}

Sprite.prototype.position = function()
{
    var x = 0;
    var y = 0;
    if (this.shape == shapeCircle)
    {
        x = this.x + this.width / 2;
        y = this.y + this.height / 2;
    }
    else
    {
        x = this.x;
        y = this.y;
    }

    if (this.oldX != x || this.oldY != y)
    {
        switch(this.shape)
        {
            case shapeCircle:
                this.svgNode.setAttribute("cx", x);
                this.svgNode.setAttribute("cy", y);
                break;

            case shapeLine:
                this.svgNode.setAttribute("y1", y);
                this.svgNode.setAttribute("y2", y);
                break;

            default: // shapeRect
                this.svgNode.setAttribute("x", x);
                this.svgNode.setAttribute("y", y);
        }
        this.oldX = x;
        this.oldY = y;
    }
}
  
Sprite.prototype.move = function()
{
    this.limitSpeed();
    this.x += this.speedX * game.speed;
    this.y += this.speedY * game.speed;
}

Sprite.prototype.limitSpeed = function()
{
    if (this.speedX < -this.speedMax)
    {
        this.speedX = -this.speedMax;
    }
    else if (this.speedX > this.speedMax)
    {
        this.speedX = this.speedMax;
    }

    if (this.speedY < -this.speedMax)
    {
        this.speedY = -this.speedMax;
    }
    else if (this.speedY > this.speedMax)
    {
        this.speedY = this.speedMax;
    }
}

Sprite.prototype.bounceArea = function()
{
    this.bounceAreaLeft();
    this.bounceAreaRight();
    this.bounceAreaTop();
    this.bounceAreaBottom();
}

Sprite.prototype.bounceAreaLeft = function()
{
    if (this.x < this.areaLeft)
    {
        this.x = this.areaLeft;
        this.speedX *= -1;
    }
}

Sprite.prototype.bounceAreaRight = function()
{
    if (this.x + this.width > this.areaWidth)
    {
        this.x = this.areaWidth - this.width;
        this.speedX *= -1;
    }
}

Sprite.prototype.bounceAreaTop = function()
{
    if (this.y < this.areaTop)
    {
        this.y = this.areaTop;
        this.speedY *= -1;
    }
}

Sprite.prototype.bounceAreaBottom = function()
{
    if (this.y + this.height > this.areaHeight)
    {
        this.y = this.areaHeight - this.height;
        this.speedY *= -1;
    }
}
