// ************************************* begin of loadJSON tests *************************************



ludi_io.loadJSON0 = function(fileName) {

	ludi_io.jsonLoaded = 0;
	
	$.ajax({
		async: true,
		url: ludi_io.xmlBasePath + fileName,
		dataType: 'json',
		// success: callback
	})
	
	.done(function(data) {
	  console.log("success");
	  ludi_io.cacheJSON = data;
	  ludi_io.jsonLoaded = 1;
	})
	.fail(function() {
	  console.log("error");
  	  ludi_io.jsonLoaded = -1;

	})
	.always(function() {
	  console.log("complete");
	  // ludi_io.jsonLoaded = -1;
	});

	
}

// http://api.jquery.com/jquery.ajax/
// versión not workinb well on a android tablet:
ludi_io.loadJSON1 = function(fileName) {

	ludi_io.jsonLoaded = 0;
	
	var request = $.ajax({
		async: true,
		url: ludi_io.xmlBasePath + fileName,
		dataType: 'json',
	});

	request.done (handled_done);
	request.fail (handled_fail);
	request.always (handled_always);

}

handled_done = function(data) {
	console.log("success");
	ludi_io.cacheJSON = data;
	ludi_io.jsonLoaded = 1;
}

handled_fail = function() {
	console.log("error");
	ludi_io.jsonLoaded = -1;
}

handled_always = function() {
	console.log("complete");
	// ludi_io.jsonLoaded = -1;
};


ludi_io.loadJSON = function(fileName) {
	var path='./client/data/';
	// var path = ludi_io.xmlBasePath;

	$.ajax({
		url:  path + fileName,
		async: true,
		dataType: 'json',
		success: function (response) {
			ludi_io.cacheJSON = response;
			ludi_io.jsonLoaded = 1;
		},

		timeout:   function (response) {
			console.log("timeout loadJSON " + path + fileName  );
			ludi_io.jsonLoaded = -1;
		},
		error:   function (response) {
			console.log("error loadJSON " + path + fileName );
			ludi_io.jsonLoaded = -2;
		}
	});

}



ludi_io.loadJSON4 = function(fileName) {
  var xhr = typeof XMLHttpRequest != 'undefined'
    ? new XMLHttpRequest()
    : new ActiveXObject('Microsoft.XMLHTTP');
	
  xhr.open('get', ludi_io.xmlBasePath + fileName, true);
  
  xhr.onreadystatechange = function() {
    var status;
    var data;
  
    if (xhr.readyState == 4) { // `DONE`
      status = xhr.status;
      if (status == 200) {
			ludi_io.cacheJSON = JSON.parse(xhr.responseText);;
			ludi_io.jsonLoaded = 1;
      } else {
			alert ("error loadJSON: " + response.responseText);
			ludi_io.jsonLoaded = -1;
      }
    }
  };
  xhr.send();
};

ludi_io.loadJSON5 = function(fileName) {

  var xhr = typeof XMLHttpRequest != 'undefined'
    ? new XMLHttpRequest()
    : new ActiveXObject('Microsoft.XMLHTTP');
  xhr.open('get', ludi_io.xmlBasePath + fileName, true);
  xhr.responseType = 'json';
  xhr.onreadystatechange = function() {

    if (xhr.readyState == 4) { // `DONE`
      if (status == 200) {
			ludi_io.cacheJSON = xhr.response;
			ludi_io.jsonLoaded = 1;
      } else {
			alert ("error loadJSON");
			ludi_io.jsonLoaded = -1;
      }
    }
  };
  xhr.send();
};


// ************************************* end of loadJSON tests *************************************