//requires extjs
// Objekt fuer requests
var http = createRequestObject();
// ist eine anfrage aktiv?
var inCall = false;
// queue fuer anfragen
var callToArray = new Array();
// queue fuer Anfragen erfolgreich
var returnToArray = new Array();

/**
 * erstellt ein neues RequestObjekt
 * @return
 */
function createRequestObject(){
	var server_store = new Ext.data.Store({
        id: 'notestore',
        proxy: new Ext.data.ScriptTagProxy({
            url: top.serverURL + '/LoggingController',
            nocache: true
        }),
        reader: new Ext.data.JsonReader({
            root: 'rows',
            id: 'id'
        }, Ext.data.Record.create([{
            name: 'test',
            type: 'bool'
        }])) 
    });//store
	return server_store;
}

/**
 * Anfragen in die Queue stellen.
 * @param whereTo
 * @param returnTo
 * @return
 */
function sendCall(whereTo, returnTo){
	callToArray.push(whereTo);
	returnToArray.push(returnTo);
}


/**
 * watcher Funktion
 * @return
 */
function callQueue(){
	//pruefe die Queue
	if (!inCall && callToArray.length >0){
		if (callToArray.length>0){
			whereTo = callToArray.shift();
			returnTo = returnToArray.shift();
			doCall(whereTo, returnTo);
		}
	}
}

/**
 * stellt die eigentliche Anfrage
 * @param whereTo
 * @param returnTo
 * @return
 */
function doCall(whereTo, returnTo){
	inCall = true;
	http.load({
        params: {
            action: "add",
            category: 	encodeURI(whereTo.category),
            eventtype: 	encodeURI(whereTo.eventtype),
            logdata: 	encodeURI(whereTo.logdata)
        },callback: function(records, options, success){
        	inCall = false;
        	returnTo();
    	}
    });	
}

var queueWatcher = setInterval(callQueue, 100);


