/*needs objectregistry.js*/

/***************************************************************
(C) 2000 - 2001 Alrik Lubbers. Use of this code without explicit 
permission is prohibited.
***************************************************************/
/*
PreloadBar. currently only horizontal-left-to-right. Create has the infrastructure 
to change this to a more full-fledged solution.

API:
call PreloadBar.create(barWidth, barHeight) to create a view of the loadprogres.
*/

/*constructor && image-download functionality. just create a PreLoadBar and 
the images named in srcArr will be pre-loaded*/

function PreloadBar(srcArr, action, lightdot, darkdot)
{
	this.create=__plb_create;
	this.draw=__plb_draw;
	//this.__srcArr=srcArr;
	this.__barCount=0;
	this.__imgArr=new Array();
	this.__lightdot=lightdot;
	this.__darkdot=darkdot;
	this.__action=action;
	this.__ticket=registry.register(this);
	
	
	
	if(document.images)
	{
		light=new Image();
		light.src=lightdot;
		dark=new Image();
		dark.src=darkdot;
		

		for(i=0; i < srcArr.length; i++)
		{
			this.__imgArr[i]=new Image();
			this.__imgArr[i].src=srcArr[i];
		}
	
		updatePreloadBar(this.__ticket);
	}
}

/*member-function. call <PreloadBar>.create in your document to create a view 
of the downloading progres.*/
function __plb_create(width, height)
{
	var lgt=this.__imgArr.length;
	
	var dotWidth=Math.floor(width/lgt);
	var dotHeight=height;

	var totalWidth=0;
	var totalHeight=0;
	
	var str='<div>'
	
	for (i=0; i<lgt; i++)
	{
		if(i==lgt-1)
		{
			dotWidth=width-totalWidth;
			//dotHeight=height-totalHeight;	
		}
		str+=
			'<img src="'+this.__darkdot+
			'" name="bar'+this.__barCount+'img'+i+
			'" width="'+dotWidth+
			'" height="'+dotHeight+
			'"/>';
		totalWidth+=dotWidth;
		totalHeight+=dotHeight;
	}
	str+='</div>';
	document.write(str);
	this.__barCount++;
}

/*For internal use. Updates all views*/
function __plb_draw()
{
	var count=0;
	for(i=0;i<this.__imgArr.length;i++)
	{
		if(this.__imgArr[i].complete)
			count++;
	}
	for(i=0;i<count;i++)
	{
		for(j=0;j<this.__barCount;j++)
		{
			document["bar"+j+"img"+i].src=this.__lightdot;
		}
	}
	if(count==this.__imgArr.length)
	{
		eval(this.__action);
		return false;
	}
	return true;
}

/*External function needed because setTimeout() [and basically eval()]
cannot handle object-references. (@$%@#$%#&#QWW#^&JR$%@%#$^)*/
function updatePreloadBar(id)
{
	obj=registry.retrieve(id);
	if (obj.draw())
		setTimeout('updatePreloadBar(' + id + ');',100);
}

