/**
 * 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++;
			//we don't wont to start from top of the site after every click
			//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_new_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_new_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;
	},
	
	print_new_loader: function()
	{
		//creating loader elements
		//under FF works className and class
		//under ie6 and 7 works className
		//under ie8 works class
		loader = document.createElement('div');
		loader.setAttribute('id', 'xajaxloader');
		loader.setAttribute('className', 'loader');
		loader.setAttribute('class', 'loader');

		var loader_background = document.createElement('div');
		loader_background.setAttribute('className', 'loader-background');
		loader_background.setAttribute('class', 'loader-background');
		
		var loader_inner = document.createElement('div');
		loader_inner.setAttribute('className', 'loader-inner');
		loader_inner.setAttribute('class', 'loader-inner');
		
		var loaderTop = document.createElement('div');
		loaderTop.setAttribute('className', 'loaderTop');
		loaderTop.setAttribute('class', 'loaderTop');
		
		var teaser = document.createElement('div');
		teaser.setAttribute('className', 'teaser');
		teaser.setAttribute('class', 'teaser');

		teaser.innerHTML = 'De pagina wordt geladen';
		
		var image_wrapper = document.createElement('div');
		image_wrapper.setAttribute('className', 'image-wrapper');
		image_wrapper.setAttribute('class', 'image-wrapper');
		
		var image = document.createElement('img');
		image.setAttribute('alt', 'loader bookform');
		image.setAttribute('src', '/images/loader.gif');
		
		var body = document.createElement('div');
		body.setAttribute('className', 'body');
		body.setAttribute('class', 'body');
		body.innerHTML = 'Een moment geduld a.u.b.';
		
		var loaderBottom = document.createElement('div');
		loaderBottom.setAttribute('className', 'loaderBottom');
		loaderBottom.setAttribute('class', 'loaderBottom');
		
		//building loader, adding elements
		image_wrapper.insertBefore(image, image_wrapper.firstChild);
		
		loader_inner.insertBefore(loaderBottom, loader_inner.firstChild);
		loader_inner.insertBefore(body, loader_inner.firstChild);
		loader_inner.insertBefore(image_wrapper, loader_inner.firstChild);
		loader_inner.insertBefore(teaser, loader_inner.firstChild);
		loader_inner.insertBefore(loaderTop, loader_inner.firstChild);
		
		loader.insertBefore(loader_background, loader.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;
}