/**
 * ...
 * @author DefaultUser
 */

(function() {
	MYNAMESPACE.namespace('modules.FileLoader');
	MYNAMESPACE.modules.FileLoader = function() {
		this.filePath = "";
		this.retryNum = 3;
		this.retryCount = 0;
		this.successInfo;
		this.errorInfo;
		this.initialize.apply(this, arguments);
	};
	MYNAMESPACE.modules.FileLoader.prototype = {
		// コンストラクタ
		initialize: function() {
			
		},
		
		load: function(filePath, retryNum, successInfo, errorInfo) {
			var thisObj = this;
			thisObj.filePath = filePath || thisObj.filePath;
			thisObj.retryNum = retryNum || thisObj.retryNum;
			thisObj.successInfo = successInfo || thisObj.successInfo;
			thisObj.errorInfo = errorInfo || thisObj.errorInfo;
			
			$.ajax({
				url: thisObj.filePath,
				cache: false,
				success: function(data, dataType) { thisObj.onFinishLoadHanlder(true, data, dataType); },
				error: function(XMLHttpRequest, textStatus, errorThrown) { thisObj.onFinishLoadHanlder(false, XMLHttpRequest, textStatus, errorThrown); }
			});
		},
		onFinishLoadHanlder: function() {
			var thisObj = this;
			/*
			console.log('onFinishLoadHanlder');
			console.log('	arguments[0] = ' + arguments[0]);
			console.log('	thisObj.retryCount = ' + thisObj.retryCount);
			
			if (arguments[0] === true) {
				thisObj.retryCount ++;
				if (thisObj.retryCount <= thisObj.retryNum) {
					thisObj.load();
				} else {
					thisObj.errorInfo.method.call(thisObj.errorInfo.scope, arguments[1], arguments[2], arguments[3]);
				}
			} else {
				thisObj.load();
			}
			*/
			
			if (arguments[0] === true) {
				if (thisObj.successInfo.method && thisObj.successInfo.scope) {
					thisObj.successInfo.method.call(thisObj.successInfo.scope, arguments[1], arguments[2], thisObj.filePath);
				}
			} else {
				if (thisObj.retryNum === -1) {
					thisObj.load();
				} else {
					thisObj.retryCount ++;
					if (thisObj.retryCount <= thisObj.retryNum) {
						thisObj.load();
					} else {
						thisObj.errorInfo.method.call(thisObj.errorInfo.scope, arguments[1], arguments[2], arguments[3]);
					}
				}
			}
		}
	};
})();

