/*
ImagePreloader, ©Copyright 2008 - bezumie.com, All Rights Reserved
ImagePreloader, ©Copyright 2008 - bezumie.com, Всички права запазени
*/
function ProgressBar(container){
/* This will create:
<div id="progress">
	<div id="progressBar"></div>
	<div id="progressText"></div>
</div> */
	var width=240;
	var height=59;
	var w=0,h=0;
	// In the center of container
	if(container){
		w=container.offsetWidth;
		h=container.offsetHeight;
	}
	// In the center of screen
	else{
		//Non-IE
		if(typeof(window.innerWidth)=='number'){
			w=window.innerWidth;
			h=window.innerHeight;
		}
		//IE 6+
		else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)){
			w=document.documentElement.clientWidth;
			h=document.documentElement.clientHeight;
		} 
		// IE4
		else if(document.body && (document.body.clientWidth || document.body.clientHeight)){
			w=document.body.clientWidth;
			h=document.body.clientHeight;
		}
	}
	var oProgress=document.createElement('div');
	with(oProgress){
		//setAttribute('id', 'progress');
		style.border='1px solid black';
		style.position="absolute";
		style.background='#d8d8d8';
		style.zIndex=10000;
		style.width=width+'px';
		style.height=height+'px';
	}
	if(w>width)oProgress.style.left=((w-width)/2)+'px';
	if(h>height)oProgress.style.top=((h-height)/2)+'px';
	
	var oBar=document.createElement('img');
	with(oBar){
		//setAttribute('id', 'progressBar');
		src='/img/loader.gif';
		style.margin='10px';
		style.width='220px';
		style.height='19px';
	}
	oProgress.appendChild(oBar);

	var oText=document.createElement('div');
	with(oText){
		//setAttribute('id', 'progressText');
		style.textAlign='center';
		style.fontWeight='bolder';
		style.fontFamily='Verdana, Tahoma, Arial';
		style.fontSize='11px';
	}
	oProgress.appendChild(oText);
	
	if(container)container.appendChild(oProgress);
	else document.body.appendChild(oProgress);
	this.container=container;
	this.div=oProgress;
	this.bar=oBar;
	this.text=oText;
}
ProgressBar.prototype.hide = function(){
	this.div.style.display='none';
}
ProgressBar.prototype.show = function(){
	this.div.style.display='block';
}
ProgressBar.prototype.setPos = function(imgDone, imgAll){
	var posit=(imgAll)?imgDone/imgAll*100:imgDone;
	this.text.innerHTML=Math.floor(posit)+'%';
	this.pos=posit;
}


function ImagePreloader(container){
	this.oProgressBar=false;
	// In the center of the screen
	if(container==='#'){
		this.oProgressBar = new ProgressBar(false);
	}
	// if container provided, else there will be no ProgressBar!
	else if(container){
		this.oProgressBar = new ProgressBar(container);
	}
	this.nProcessed=0;
	this.aImages=[];
	this.nImages=0;
}
ImagePreloader.prototype.toString = function(){
	return 'ImagePreloader:'+this.nLoaded+'/'+this.aImages.length;
}
ImagePreloader.prototype.preloadAll=function(images, callbackLoaded){
	if(this.oProgressBar){
		this.oProgressBar.show();
		this.oProgressBar.setPos(0);
	}
	this.callbackLoaded=callbackLoaded;
	this.nProcessed=0;
	this.aImages.length=0;
	this.nImages=images.length;
	var i;
	for(i=0;i<images.length;i++){
		this.preload(images[i]);
	}
}
ImagePreloader.prototype.preload = function(image){
	var oImage = new Image;
	this.aImages.push(oImage);
	oImage.onload = ImagePreloader.prototype.onload;
	oImage.onerror = ImagePreloader.prototype.onerror;
	oImage.onabort = ImagePreloader.prototype.onabort;
	oImage.oImagePreloader = this;
	oImage.bLoaded = false;
	oImage.src = image;
}
ImagePreloader.prototype.onComplete = function(){
	this.nProcessed++;
	if(this.oProgressBar)this.oProgressBar.setPos(this.nProcessed, this.nImages);
	if(this.nProcessed==this.nImages){
		this.callbackLoaded(this.aImages);
		this.oProgressBar.hide();
	}
}
ImagePreloader.prototype.onload = function(){
	this.bLoaded=true;
	this.oImagePreloader.onComplete();
}
ImagePreloader.prototype.onerror = function(){
	this.oImagePreloader.onComplete();
}
ImagePreloader.prototype.onabort = function(){
	this.oImagePreloader.onComplete();
}