// JS Game Version 1.0
// By Dustin Dettmer
// no namespace =X

function Image(url)
{
    this.x = 0;
    this.y = 0;
    
    this.img = document.body.appendChild(document.createElement("img"));
    this.img.src = url;
    this.img.style.display = "none";
    this.img.style.position = "absolute";
    this.img.style.left = "0px";
    this.img.style.top = "0px";
}

Image.prototype.moveTo = function(x, y)
{
    this.img.style.left = x + "px";
    this.img.style.top = y + "px";
    this.x = x;
    this.y = y;
}

Image.prototype.display = function()
{
    this.img.style.display = "";
}

Image.prototype.hide = function()
{
    this.img.style.display = "none";
}

// An array of frames.    Each frame must have:
// url, width, height, ttl
//
// ttl is the frame time in milliseconds
function Animation(frames)
{
    this.images = [];
    this.curFrame = 0;
    this.frameTime = 0;
    this.running = true;
    
    for(var i = 0; !!frames[i]; i++) {
        
        var img = new Image(frames[i].url);
        
        if(frames[i].width)
            img.img.width = img.width = frames[i].width * 2;
        
        if(frames[i].height)
            img.img.height = img.height = frames[i].height * 2;
        
        img.ttl = frames[i].ttl || 0;
        
        this.images[this.images.length] = img;
    }
}

Animation.prototype.update = function()
{
    if(!this.running)
        return;
    
    var img = this.getCurFrame();
    
    if(engine.time - this.frameTime > img.ttl) {
    
        this.frameTime = engine.time;
        if(++this.curFrame >= this.images.length)
            this.curFrame = 0;
            
        this.getCurFrame().moveTo(img.x, img.y);
    
        this.getCurFrame().display();
    
        img.hide();
    
    }
}

Animation.prototype.pause = function()
{
    this.running = false;
}

Animation.prototype.stop = function()
{
    this.running = false;
    this.getCurFrame().hide();
    this.curFrame = 0;
}

Animation.prototype.resume = function()
{
    this.running = true;
    this.frameTime = engine.time;
}

Animation.prototype.start = function()
{
    this.running = true;
    this.frameTime = engine.time;
    this.getCurFrame().hide();
    this.curFrame = 0;
}

Animation.prototype.getCurFrame = function()
{
    return this.images[this.curFrame];
}

// Returns a hash table of animations as found in 'data'
Animation.loadAnimations = function(data)
{
    var animations = {};
    
    var indexCounters = {};
    var afterEquals = false;
    var lastIndex = 0;
    var cheatStr = '';
    
    var str = '';
    
    for(var i = 0; i < data.length; i++) {
        
        if(!afterEquals && (data.substr(i,1) == '.' || data.substr(i,1) == '=')) {
            
            var n = data.substr(lastIndex, i - lastIndex);
            
            var arrayReg = new RegExp("^\\[([+])|([0-9]+)|()\\]$");
            
            var results = arrayReg.exec(n);
            
            if(results) {
                
                if(results[1] == '+') {
                    
                    indexCounters[cheatStr] = indexCounters[cheatStr] || 0;
                    
                    n = ++indexCounters[cheatStr] - 1;
                }
                else {
                    
                    n = indexCounters[cheatStr] - 1;
                }
            }
            
            if(parseInt(n) != n)
                n = "'" + n + "'";
            
            cheatStr += '[' + n + ']';
            
            eval('if(!animations' + cheatStr + ')animations' + cheatStr + '={};');
            
            lastIndex = i + 1;
            
            if(data.substr(i,1) == '=')
                afterEquals = true;
        }
        else if(data.substr(i,1) == "\n" || data.substr(i,1) == "\r") {
            
            if(afterEquals) {
                
                var v = data.substr(lastIndex, i - lastIndex);
                
                eval('animations'+cheatStr + '=v;');
            }
            
            if(data.substr(i+1,1) == "\n" || data.substr(i+1,1) == "\r")
                i++;
            
            afterEquals = false;
            lastIndex = i + 1;
            cheatStr = '';
        }
    }
    
    Animation.animations = animations;
}
