﻿function GPreload()
{
	//global/////////////////////////////////////
	this.images=            new Array();	    //set on go
	this.isRunning=         false;
	this.total=             0;
	this.errors=            0;
	this.completed=         0;
	/////////////////////////////////////////////
}




//public methods/////////////////////////////
GPreload.prototype.start = function()
{
    this.isRunning = true;
    for(var i = 0; i < this.images.length; i++)
    {
        this.images[i].src = this.images[i].thesrc;
    }
}
GPreload.prototype.addImage = function(url)
{
    this.total++;
    
    var oImage = new Image;
    oImage.isLoaded = false;
    oImage.thesrc = url;
    
    oImage.onload = GPreload.prototype.onload;
    oImage.onerror = GPreload.prototype.onerror;
    oImage.onabort = GPreload.prototype.onabort;
    
    oImage.parent = this;
    this.images.push(oImage);
}
/////////////////////////////////////////////





//events/////////////////////////////////////
GPreload.prototype.onTick = function(percent)
{
    //event fired on each tick (internal movement tick)
    //passes back the value for use in whatever means you wish
    //the user then doesnt have to think about setting a timeout. 
}

GPreload.prototype.onFinish = function()
{
    //event only fired when it gets to the postion, and has ceased any activity.
    //i guess we should clean up?
}
/////////////////////////////////////////////




//internal events////////////////////////////
GPreload.prototype.onload = function()
{
    this.isComplete = true;
    this.error = false;
    this.parent.completed++;
    this.parent.onTick(Math.round(this.parent.completed / this.parent.total * 100));
    if(this.parent.completed == this.parent.total)
        this.parent.onFinish();
}
GPreload.prototype.onerror = function()
{
    this.isComplete = true;
    this.error = true;
    this.parent.completed++;
    this.parent.errored++;
    this.parent.onTick(Math.round(this.parent.completed / this.parent.total * 100));
    if(this.parent.completed == this.parent.total)
        this.parent.onFinish();
}
GPreload.prototype.onabort = function()
{
    this.isComplete = true;
    this.error = true;
    this.parent.completed++;
    this.parent.errored++;
    this.parent.onTick(Math.round(this.parent.completed / this.parent.total * 100));
    if(this.parent.completed == this.parent.total)
        this.parent.onFinish();
}
/////////////////////////////////////////////

