/**
 * Class that loads functions when a ajax call gets performed and is perfomed
 * @author Juul Hobert
 * @author Jop Hofste
 * @version 0.3
 */
var loader_instance = null;

var XajaxLoader = Class.create({
    //initializes a loader
    initialize: function(){
	    this.showFunctions = [];
        this.hideFunctions = [];
	
		this.enable();
        this.hide();
    },

    /**
     * Adds a function to the ajax loader that gets called when a ajax call
     * gets performed
     * @param function f a function to show when a ajax call gets performed
     */
    addShow : function(object) {
		this.showFunctions.push(object);
    },

    /**
     * Adds a function to the ajax loader thats gets called when a ajax call
     * has _been_ performed
     */
    addHide : function(object) {
		this.hideFunctions.push(object);
    },

    /**
     * evals all the functions in the show
     */
    show : function() {
        if(this.active!=false){
            this.showFunctions.each(function(object){
				object.functioncall(object.arguments);
				//run(object); this doesnt work properly
			});
        }
    },

    /**
     * evals all the functions in the hide array
     */
    hide : function() {
        if(this.active!=false){
            this.hideFunctions.each(function(object){
				object.functioncall(object.arguments);
				//run(object); this doesnt work properly
			});
        }
    },
	
	//runs a object commando
	run : function(object) {
		object.functioncall(object.arguments);
	},

    //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;
    }
});

//standard function for showing a div
var loader_shower = function(div) {
	if($(div) != undefined) {
        $(div).show();
    }
}
//standard function for hiding a div
var loader_hider = function(div) {
    if($(div) != undefined) {
        $(div).hide();
    }
}
//standard function for showing a div with timeout


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

//Initializes a function in the loader that shows and hides a div
function loader_initialize(divId){
	var loader_instance = loader_get();
	loader_instance.addShow({'functioncall': loader_shower, 'arguments': divId});
    loader_instance.addHide({'functioncall': loader_hider, 'arguments': divId});
	loader_instance.run({'functioncall': loader_hider, 'arguments': divId});
    return loader_instance;
}

//Gets the instance loader
function loader_get(){
    if(loader_instance == undefined) {
		loader_instance = new XajaxLoader();
	}
	return loader_instance;
}