/*** Animation object ***/

function Animation() {
    this.spritesInterval = 0;
    this.minX = 0;
    this.minY = 0;
    this.maxX = 960;
    this.maxY = 600;
}

Animation.prototype.createParticle = function(x, y, speedX, speedY, red, green, blue, reactsToGravity) {
    var maxParticles = 50;
    if (sprites.length <= maxParticles * 1) {
        sprites[sprites.length] = new Sprite();
        var sprite = sprites[sprites.length - 1];
        sprite.type = 'particle';
        sprite.energy = 25;
        sprite.x = x;
        sprite.y = y;
        sprite.speedMaxX = 20;
        sprite.speedMaxY = 20;
        sprite.speedX = speedX;
        sprite.speedY = speedY;
        sprite.reactsToGravity = reactsToGravity;
        if (sprite.speedX == 0) { sprite.speedX = 1; }
    
        sprite.elm = document.createElement('div');
        sprite.elm.style.display = 'none';
    
        sprite.elm.className = 'particle';
        sprite.elm.style.color = 'rgb(' + red + ',' + green + ',' + blue + ')';
        sprite.elm.innerHTML = '&bull;';
        var all = document.getElementById('content');
        all.appendChild(sprite.elm);
    
        sprite.show();
        sprite.elm.style.display = 'block';
    }
}

Animation.prototype.startVoteSprite = function(x, y, targetX, targetY, text, isHumanVote, action) {
    sprites[sprites.length] = new Sprite();
    var sprite = sprites[sprites.length - 1];
    sprite.energy = 100;
    sprite.x = x;
    sprite.y = y;
    sprite.targetX = targetX;
    sprite.targetY = targetY;
    sprite.action = action;

    // for tests: sprite.speedMaxX = 0; sprite.speedMaxY = 0;

    sprite.elm = document.createElement('div');

    var thisClassName = isHumanVote ? 'voteSprite' : 'voteSpriteNonHuman';
    sprite.elm.setAttribute('class', thisClassName);
    sprite.elm.className = thisClassName;

    sprite.elmText = document.createElement('div');
    sprite.elmTextShadow = document.createElement('div');

    sprite.elmText.style.display = 'none';
    sprite.elmTextShadow.style.display = 'none';

    if (action != '') {
        sprite.elmText.setAttribute('class', 'voteSpriteText');
        sprite.elmText.className = 'voteSpriteText';
        sprite.elmText.style.marginLeft = '-25px';
        sprite.elmText.innerHTML = '<img src="/image/action/' + action + '.gif" alt="" />';
    }
    else if (text != '') {
        sprite.elmText.setAttribute('class', 'voteSpriteText');
        sprite.elmText.className = 'voteSpriteText';
        sprite.elmText.innerHTML = misc.toXml(text);
        var lettersToUseSmaller = 40;
        if (text.length >= lettersToUseSmaller) {
            sprite.elmText.style.fontSize = '13px';
            sprite.elmText.style.lineHeight = '12px';
        }
        else {
            sprite.elmText.style.fontSize = '14px';
            sprite.elmText.style.lineHeight = '17px';
        }

        sprite.elmTextShadow.setAttribute('class', 'voteSpriteTextShadow');
        sprite.elmTextShadow.className = 'voteSpriteTextShadow';
        sprite.elmTextShadow.style.filter = 'alpha(opacity=20)';
    }

    var all = document.getElementById('content');
    all.appendChild(sprite.elm);
    all.appendChild(sprite.elmText);
    all.appendChild(sprite.elmTextShadow);

    sprite.show();
    sprite.elmText.style.display = 'block';
    sprite.elmTextShadow.style.display = 'block';

    if (this.spritesInterval == 0) {
        this.spritesInterval = setInterval(g_handleSprites, 40);
    }
}

Animation.prototype.handleSprites = function() {
    var i = 0;
    for (i in sprites) {
        sprites[i].move();
        sprites[i].handle();
        sprites[i].show();
    }

    for (i in sprites) {
        if (sprites[i].energy <= 0) {
            var all = document.getElementById('content');
            all.removeChild(sprites[i].elm);

            if (sprites[i].type == 'star') {
                all.removeChild(sprites[i].elmText);
                all.removeChild(sprites[i].elmTextShadow);
            }
            sprites.splice(i, 1);
        }
    }

    if (sprites.length == 0) {
        clearInterval(this.spritesInterval);
        this.spritesInterval = 0;
    }
}


/*** Sprite object ***/

function Sprite() {
    this.x = 0;
    this.y = 0;
    this.targetX = null;
    this.targetY = null;
    this.speedX = 0;
    this.speedY = 0;
    this.speedStepX = .2;
    this.speedStepY = .2;
    this.speedMaxX = 4;
    this.speedMaxY = 4;
    this.type = 'star';
    this.intervalId = 0;
    this.opacity = 100;
    this.energy = 100;
    this.active = false;
    this.id = 'sprite' + misc.getRandomInt(1, 1000000); // could cause rare dupe;
    this.elm = null;
    this.elmText = null;
    this.elmTextShadow = null;
    this.action = '';
    this.reactsToGravity = false;
    this.exploded = false;
}

Sprite.prototype.move = function() {
    if (this.targetX) {
        if (this.targetX < this.x) {
            this.speedX -= this.speedStepX;
        }
        else if (this.targetX > this.x) {
            this.speedX += this.speedStepX;
        }
    }

    if (this.targetY) {
        if (this.targetY < this.y) {
            this.speedY -= this.speedStepY;
        }
        else if (this.targetY > this.y) {
            this.speedY += this.speedStepY;
        }
    }

    if (this.reactsToGravity) {
        this.speedY += .2;
    }

    if (this.speedX < -this.speedMaxX) {
        this.speedX = -this.speedMaxX;
    }
    else if (this.speedX > this.speedMaxX) {
        this.speedX = this.speedMaxX;
    }

    if (this.speedY < -this.speedMaxY) {
        this.speedY = -this.speedMaxY;
    }
    else if (this.speedY > this.speedMaxY) {
        this.speedY = this.speedMaxY;
    }

    this.x += this.speedX;
    this.y += this.speedY;
}

Sprite.prototype.handle = function() {
    if (this.energy <= 30) {
        this.opacity -= 2.2;
    }

    if (this.type == 'star') {
        switch (this.action) {
            case 'explosive':
            case 'dynamite':
                if (this.energy <= 50 && !this.exploded) {
                    this.exploded = true;
                    this.elmText.style.display = 'none';
                    for (var i = 1; i <= 30; i++) {
                        animation.createParticle(this.x + 34, this.y + 6, misc.getRandomInt(-10, 10), misc.getRandomInt(-10, 10), 255, 0, 0, true);
                    }
                }
                break;

            case 'very-explosive':
                if (this.energy <= 50 && !this.exploded) {
                    this.exploded = true;
                    this.elmText.style.display = 'none';
                    for (var i = 1; i <= 50; i++) {
                        animation.createParticle( this.x + 34, this.y + 6, misc.getRandomInt(-12, 12), misc.getRandomInt(-12, 12),
                                255, misc.getRandomInt(0, 255), 50, true );
                    }
                }
                break;

            case 'fireworks':
            case 'more-fireworks':
                if (this.energy >= 20 && this.energy <= 50) {
                    this.exploded = true;
                    this.elmText.style.display = 'none';
                    var red = 0;
                    var blue = 255;
                    if (this.action == 'more-fireworks') {
                        var red = misc.getRandomInt(0, 255);
                        var blue = 0;
                    }
                    animation.createParticle( this.x + 16, this.y + 14, misc.getRandomInt(-8, 8), misc.getRandomInt(-8, 8),
                            red, misc.getRandomInt(0, 255), blue, true );
                }
                break;

            case 'eruption':
                if (this.energy >= 20 && this.energy <= 50 && !this.exploded && misc.getRandomInt(0, 100) <= 60) {
                        animation.createParticle( this.x + 34, this.y + 8, misc.getRandomInt(-4, 4), misc.getRandomInt(-10, -2),
                                255, misc.getRandomInt(0, 200), 0, true );
                }
                break;

            case 'hot':
                if ( this.energy >= 30 && misc.getRandomInt(0, 100) <= 15 ) {
                    animation.createParticle( this.x + 34 + misc.getRandomInt(-10, 10), this.y, 0, -3, 128, 0, 0, false );
                }
                break;

            case 'laughing-tears':
                if ( this.energy >= 20 && misc.getRandomInt(0, 100) <= 15 ) {
                    var speedX = 4;
                    var x = this.x + 40;
                    if ( misc.getRandomInt(0, 100) <= 50 ) {
                        speedX *= -1;
                        x = this.x + 26;
                    }
                    animation.createParticle(x, this.y + 2, speedX, -4, 84, 103, 233, true);
                }
                break;

        }
    }

    this.energy -= .4;

    if ( this.x < parseInt(animation.minX) ||
            this.y < parseInt(animation.minY) ||
            this.x > parseInt(animation.maxX) ||
            this.y > parseInt(animation.maxY) ) {
        this.energy = 0;
    }
}

Sprite.prototype.show = function() {
    this.elm.style.left = this.x + 'px';
    this.elm.style.top = this.y + 'px';

    if (this.type == 'star') {
        this.elm.style.opacity = this.opacity / 100;
        this.elm.style.filter = 'alpha(opacity=' + this.opacity + ')';
    
        this.elmText.style.left = (this.x * 1 + 42) + 'px';
        this.elmText.style.top = this.y + 'px';
        this.elmText.style.opacity = this.opacity / 100;
        this.elmText.style.filter = 'alpha(opacity=' + this.opacity + ')';
        
        this.elmTextShadow.style.left = (this.x * 1 + 40) + 'px';
        this.elmTextShadow.style.top = this.y + 'px';
        if (this.opacity < 20) {
            this.elmTextShadow.style.opacity = this.opacity / 100;
            this.elmTextShadow.style.filter = 'alpha(opacity=' + this.opacity + ')';
        }
    }
}
