SiteLoader.prototype = new Layer();
SiteLoader.prototype.constructor = SiteLoader;
SiteLoader.superclass = Layer.prototype;

function SiteLoader(id, parent, doc)
{
	//alert("SiteLoader::Constructor");
	this.SetId(id);
	this._doc = doc;
	this._images = new Array();
	this._imageNames = new Array();
	this._numLoadedImages = 0;
	this._parent = parent;
	this._bodyLoaded = false;

	this._doc.writeln("<div id=\"" + this.GetId() + "\" style=\"position:absolute\">");
	this._doc.writeln("<table width=\"100%\" border=\"0\"><tr><td valign=\"bottom\" align=\"center\">");
	this._doc.writeln("<span id=\"pleaseWait\"></span>");
	this._doc.writeln("<span id=\"loadingProcess\"></span>");
	this._doc.writeln("<span id=\"loadedFiles\"></span>");
	this._doc.writeln("</td></tr></table>");
	this._doc.writeln("</div>");
	
	this._message = this._doc.getElementById('pleaseWait');
	this._loadingProcess = this._doc.getElementById('loadingProcess');
	this._loadedFiles = this._doc.getElementById('loadedFiles');
	
	this.SetTop(0);// this._doc.body.offsetHeight * 0.35);
	this.SetLeft(0);
	//this.SetHeight(200);
	//this.SetWidth(200);
	
	this.SetHeight(self.innerHeight);
	this.SetWidth(self.innerWidth);
	//this.Center();
	//this.CenterVert();
	this.SetZIndex(1001);
	this.SetBGColor("#FFFFFF"); //otherwise it will be transparent

	this._message.innerHTML = "Please wait a few seconds boys and girls...";
	this._loadingProcess.innerHTML = "<br><br>Loading 0%";
	this._loadedFiles.innerHTML = "<br>";
	
	this.Show(true);
	
	//
	var _this = this;
	window.onload = function() { _this.OnBodyLoaded() };
}

SiteLoader.prototype.SetImageToLoad = function(imgSrc)
{
	this._imageNames.push(imgSrc);
	//this._message.innerHTML = this._message.innerHTML + "<br>SetImageToLoad: " + imgSrc + "<br>";
	
	var img = new Image();
	var index = this._imageNames.length - 1;
	var _this = this;
	img.onload = function() { _this.OnImageLoaded(index) };
	this._images.push(img);	
}

SiteLoader.prototype.LoadImages = function()
{
	this.loadImage(0);
}

SiteLoader.prototype.loadImage = function(index)
{
	//alert("loadImage: " + index + " " + this._imageNames[index]);
	//this._loadedFiles.innerHTML = this._loadedFiles.innerHTML + " - loadImage: " + index + " " + this._imageNames[index];
	this._images[index].src = this._imageNames[index];
	//this._loadedFiles.innerHTML = this._loadedFiles.innerHTML + " - loadImageEnd: " + index + " " + this._imageNames[index];
}

SiteLoader.prototype.OnImageLoaded = function(index)
{
	//this._images[index].complete will be false until this function exits
	
	//alert(this._images[index].src + " loaded. Index: " + index);	
			
	this._numLoadedImages++;
	
	this._loadingProcess.innerHTML = "<br><br>Loading " + parseInt( (this._numLoadedImages/this._imageNames.length)*100) + "%";
	//this._loadedFiles.innerHTML = this._loadedFiles.innerHTML + "<br>" + this._images[index].src;
	this._loadedFiles.innerHTML = this._loadedFiles.innerHTML + "__";		
		
	if ( this.IsImagesLoaded() )
	{
		//this._message.innerHTML = this._message.innerHTML + "Won't be long now!";
		this._message.innerHTML = "Won't be long now!";
		if (this._bodyLoaded)
		{
			//Since the complete property for the image(s?) wont be true until this function is done,
			//lets wait until calling OnSiteLoaded 
			var _this = this;
			setTimeout( function() { _this._parent.OnSiteLoaded() }, 500 );	
		}
	}
	else
	{
		//this._loadedFiles.innerHTML = this._loadedFiles.innerHTML + " - calling loadImage(" + this._numLoadedImages + ")";
		this.loadImage(this._numLoadedImages);
	}
}

SiteLoader.prototype.OnBodyLoaded = function()
{
	//alert("SiteLoader::OnBodyLoaded");
	this._bodyLoaded = true;
	if (this.IsImagesLoaded())
			this._parent.OnSiteLoaded();
}

SiteLoader.prototype.GetImage = function(imgIndex)
{
	if (imgIndex < 0 || imgIndex >= this._images.length)
	{
		alert("SiteLoader::GetImage: imgIndex is invalid.");
		return null;
	}
	else
		return this._images[imgIndex];	
}

SiteLoader.prototype.IsImagesLoaded = function()
{
	if (this._numLoadedImages == this._imageNames.length)
		return true;
	else
		return false;
}

SiteLoader.prototype.IsBodyLoaded = function()
{
	return this._bodyLoaded;
}


