/* ************************************************
	The Ajax Class to handle tournament specific content.
	IMPORTANT: 
		The Global Class Name 
		must be:TourAjaxObj
* ************************************************* */
function tourAjaxClass() {
	
	this.WAIT_MILISEC_TIMEOUT = 500;
	this.dataReceived = false;
	this.data = ''
	
	function tourMakeRequest(url) {
		new Ajax.Request(url,
			{
				method:'get',
				onSuccess: function(transport){
					var response = transport.responseText || "no response text";
					tourSetDataReceived(response);
				},
				onFailure: function(){
					tourSetDataReceived('');
				}
			}
		);
	}
	this.exec = function(json_vars, i) {
		if(i == undefined) { // first time loop has been run
			i = 0;
			tourMakeRequest(json_vars[i].url);
			setTimeout(function(){
				tourCallExec(json_vars, 0);
			}, this.WAIT_MILISEC_TIMEOUT);
			return false;
		}
		if(!this.dataReceived) { // check to see if some data has been received.
			setTimeout(function(){
				tourCallExec(json_vars, i);
			}, this.WAIT_MILISEC_TIMEOUT);
			return false;
		} else { // data received!
			this.dataReceived = false;
			switch(json_vars[i].elem) {
				case "src":
					$(json_vars[i].id).src = this.data;
					break;
				default:
					$(json_vars[i].id).innerHTML = this.data;
					break;
			}
			this.data = '';
			i++;
			if(i>json_vars.length-1) { // we have looped through all the json urls, not safe to exit function
				return true; // sucessfully
			} else {
				tourMakeRequest(json_vars[i].url);
				setTimeout(function(){
					tourCallExec(json_vars, i);
				}, this.WAIT_MILISEC_TIMEOUT);
			}
		}
	}
}
function tourCallExec(json_vars, i) { // needed for the setTimeout in the exec method
	TourAjaxObj.exec(json_vars, i);
}
function tourSetDataReceived(data) {
	TourAjaxObj.dataReceived = true;
	TourAjaxObj.data = data;
}