/**
 * Class that shows a loader
 *
 * @author Juul Hobert
 * @author Jop Hofste
 * @version 0.3
 * @rebuild by Blazej Sutkowski (Blazej@variomatic.pl)
 */


//sets the number of ajax calls
var iteration = 0;


//stops showing the loaders
var hide = function(){
	if(xajax.loader != undefined) {
		for(var i=0; i<xajax.loader.size(); i++) {
			iteration--;
			if(iteration == 0) {
				xajax.loader[i].hide();
			}
		}
		//console.warn('hide() iteration: '+iteration);
	}
}

//starts showing the loaders
var show = function(){
	if(xajax.loader != undefined) {
		for(var i=0; i<xajax.loader.size(); i++){
			xajax.loader[i].show();
			iteration++;
			window.scroll(0, 0);
		}
		//console.warn('show() iteration: '+iteration);
	}
}



xajax.callback.global.onRequest = show;
xajax.callback.global.onComplete = hide;

var Loader = Class.create();

Loader.prototype = {
	//initializes a loader	
	initialize: function(divId){
		if(divId != undefined){		
			this.id = divId;
		}else{
			//console.warn('print_standard_loader()');
			this.print_standard_loader();
		}
		this.hide();
		this.enable();
	},
	
	//shows the loader
	show : function() {
		if(this.active!=false){
			if($(this.id)){
				$(this.id).style.display = 'block';
				//console.warn('show() id: '+this.id+' | iteration: '+iteration);
			}else{
				this.print_standard_loader();
				document.getElementById('xajaxloader').style.display = 'block';
				//console.warn('loader error: Undefined loader to show');
			}
		}
	},
	
	//hides the loader
	hide : function() {
		if(this.active!=false){		
			if($(this.id)){
				$(this.id).style.display = 'none';
				//console.warn('hide() id: '+this.id+' | iteration: '+iteration);
			}else{
				document.getElementById('xajaxloader').style.display = 'none';
				//console.warn('loader error: Undefined loader to hide');
			}
		}
	},
	
	//disables the loader
	disable : function(){
		this.active = false;
	},
	
	//enables the loader (by default the loader is enabled)
	enable : function(){
		this.active = true;
	},
	
	getDivId : function(){
		return this.id;
	},
	
	//prints a standard loader
	print_standard_loader : function(){
		//console.warn('||| print_standard_loader function |||');
		loader = document.createElement('div');
		loader.setAttribute('id', 'xajaxloader');
		loader.setAttribute('class', 'loader');
		loader.style.position = 'absolute';
		loader.style.overflow = 'hidden';
		loader.style.width = '286px';
		loader.style.background = '#FFFFFF none repeat scroll 0%';
		loader.style.display = 'none';
		loader.style.zIndex = '99';
		loader.style.top = "300px";
		loader.style.left = "50%";
		loader.style.marginLeft = "-143px";
		loader.style.marginTop = "-58px";
		loader.style.textAlign = 'center';
		
		var loader_inner = document.createElement('div');
		loader_inner.setAttribute('class', 'loader-inner');
		loader_inner.style.width = '240px';
		loader_inner.style.background = '#FFFFFF none repeat scroll 0 0';
		loader_inner.style.padding = '20px';
		loader_inner.style.border = '3px solid #28427A';
		
		var zoeken = document.createElement('div');
		zoeken.setAttribute('class', 'teaser');
		zoeken.style.background = '#FFFFFF none repeat scroll 0% 0%';
		zoeken.style.color = '#000000';
		zoeken.style.margin = '0px';
		zoeken.style.padding = '0px';
		zoeken.style.fontSize = '16px';
		zoeken.style.textAlign = 'center';
		zoeken.innerHTML = 'De pagina wordt geladen';
		zoeken.style.fontWeight = 'bold';
		
		var images = document.createElement('img');
		images.setAttribute('alt', 'loader');
		images.setAttribute('src', '/lib/images/loadingbar.gif');
		images.style.textAlign = 'center';
		images.style.height = '19px';
		images.style.width = '220px';
		images.style.margin = '10px';
		
		var tekst = document.createElement('div');
		tekst.setAttribute('class', 'body');
		tekst.style.fontSize = '12px';
		tekst.style.color = '#000000';
		tekst.style.textAlign = 'center';
		tekst.style.textDecoration = 'blink';
		tekst.style.margin = '0px';
		tekst.style.padding = '0px';
		tekst.innerHTML = 'Een moment geduld a.u.b.'
		
		if (navigator.appName == "Microsoft Internet Explorer")
		{
			//alert("Your browser is Microsoft Internet Explorer.");
			var iframe = document.createElement('iframe');
			iframe.style.display = 'none';
			iframe.style.display = 'block';
			iframe.style.position = 'absolute';
			iframe.style.top = '0px';
			iframe.style.left = '0px';
			iframe.style.zIndex = '-1';
			iframe.style.filter = 'mask()';
			iframe.style.width = '3000px';
			iframe.style.height = '3000px';
			
			loader_inner.insertBefore(iframe, loader_inner.firstChild);
		}
		
		loader_inner.insertBefore(tekst, loader_inner.firstChild);
		loader_inner.insertBefore(images, loader_inner.firstChild);
		loader_inner.insertBefore(zoeken, loader_inner.firstChild);
		
		loader.insertBefore(loader_inner, loader.firstChild);	
		document.body.insertBefore(loader, document.body.firstChild);
		
		this.id = 'xajaxloader';
	
	}
}

/**
 * @deprecated
 */
function initialize_loader(divId){
	return loader_initialize(divId);
}

/**
 * Initializes a new loader
 */
function loader_initialize(divId){
	if(xajax.loader == undefined) {
		xajax.loader = new Array();
	}

	xajax.loader.push(new Loader(divId));
	return xajax.loader.last();
}

/**
 * Gets the loader by index
 * @param int index 
 */
function loader_get(index){
	if(xajax.loader != undefined){
		return xajax.loader[index];
	}
	
	return null;
}

/**
 * Gets a loader by divid
 * @param String divId the name of the div to get the loader for
 */
function loader_get_by_div(divId){
	
	if(xajax.loader != undefined) {
		for(var i=0; i<xajax.loader.size(); i++){
			if(xajax.loader[i].getDivId() == divId){
				return xajax.loader[i];
			}
		}
	}
	
	return null;
}