/*#####################################################
# factory class
#####################################################*/
function idfr_core(what,opts){
	
	/*#####################################################
	# error handler
	#####################################################*/
	var error = function(error,methods){
	
		if(!methods){methods = 'alert';}
		if(typeof methods != 'array'){
			
			var tmp = methods;
			methods = new Array();
			methods.push(tmp);
		}
		
		for(var id in methods){
			
			switch (methods[id]){
				
				case 'console':
					if(console.log){console.log(error);}
				break;
				
				case 'log':
					void(0); // nothing for the moment
				break;
				
				default: alert(error);
				break;
			}
		}
	}
	
	/*#####################################################
	# makeLocation function
	#####################################################*/
	var makeLocation = function(func,what){
		
		var newHash = func + '-' + what;
	}
	
	/*############################
	# get version
	##############################*/
	var version = function version() {
	
		var vs = '';
		for(var x in this){
		
			if(typeof this[x].version == 'string'){
				
				vs = vs+'Module '+x+' : V'+this[x].version+"\n";
			}
		}
		if(version != ''){alert(vs);}
	}
	
	/*############################
	# debug mode
	##############################*/
	var debug = function(action){
	
		if(action == true){
		
			if(!$('#idfr_debug_win')) {
			
				var debug = $('body').append('<div id="idfr_debug_win"></div>');
				$('body').trigger('debug')
			}
			
			$('#idfr_debug_win').css('display','block');
		}
		else {
			
			$('#idfr_debug_win').css('display','none');
		}
	
	};
	
	/*#####################################################
	# query method
	#####################################################*/
	var query = function (datas,Success,callback,errorCallback){		
		var $this = this;	
		
		$.ajax({
			url: opts.proxy+'proxy.php',
			type: "GET",
			cache: false,
			scriptCharset: 'UTF-8',
			dataType: 'json',
			'data':datas,
			error:function(XMLHttpRequest, textStatus, errorThrowns){
			
				$this.error('query :  status ( '+textStatus+' ) / error ( '+errorThrowns+' )');
				if(errorCallback){errorCallback(XMLHttpRequest, textStatus, errorThrowns);}
			},
			success : function(data,status) {
			
				if(callback){callback();}
				if(data.callback){eval('(function(this){'+data.callback+'})(this)');}
				Success(data);
			}
		});
	
	
		/*
		$.ajax({
			url: opts.proxy+'proxy.php',
			type: "POST",
			cache: false,
			scriptCharset: 'UTF-8',
			dataType : 'json',
			'data':datas,
			error:function(XMLHttpRequest, textStatus, errorThrowns){
			
				$this.error('query :  status ( '+textStatus+' ) / error ( '+errorThrowns+' )');
				if(errorCallback){errorCallback(XMLHttpRequest, textStatus, errorThrowns);}
			},
			success : function(data,status) {
			
				if(callback){callback();}
				if(data.callback){eval('(function(this){'+data.callback+'})(this)');}
				Success(data);
			}
		});
		*/
	
	}
	
	/*#####################################################
	# tplLoader method
	#####################################################*/
	var tplLoader = function (tpls,callback) {
	
		var _this = this;
		
		if(typeof _this.templates!='undefined' && _this.templates[tpls]) {
			if(typeof callback == 'function'){
				callback();
			}
		}
		else {		
			var ext = $.extend({'name':tpls},{'action':'tplLoader'});
			this.query(ext,function(json){
		
				for(var key in json){				
					_this.templates[key]=json[key];
				}
				
				if(typeof callback == 'function'){
					callback();
				}
			});
		}
	
	}
	
	/*#####################################################
	# makeView method
	#####################################################*/
	var makeView = function (values, tpl) {
		
		var tmp = '';
		var key = '';
		var reg = '';
		var html = '';
		var test = '';
			
		if(tpl || values.tpl_id || html){
			
			var html = 'error';
			
			if(tpl){				
				html = this.templates[tpl];
			}
			else if(values.tpl_id){				
				html = this.templates[(values.tpl_id)];
			}
		
			test = html.match(/\{if\(([^\)]*)\)([^}]*)\}/g);
			if(test){
				
				tmp = key = rpl = '';
				for(var i = 0;i < test.length;++i){
					
					tmp = test[i].replace(/if/g,'')
						.replace(/\{/g,'')
						.replace(/\}/g,'')
						.replace(/\(/g,'');
					
					tmp = tmp.split(/\)/g);					
					
					key = tmp[0].replace(/^\s+/g, '').replace(/\s+$/g, '');
					rpl = tmp[1].replace(/^\s+/g, '').replace(/\s+$/g, '');
					
					if(!values[key]) {					
						html = html.replace(test[i],'');
					}
					else{						
						html = html.replace(test[i],rpl);
					}
				}
			}
			
			test = html.match(/%%([^%]*)%%/g);
			if(test){
			
				tmp = key = reg = '';
				for(var i=0;i<test.length;++i){
					
					key = test[i].substring(2);
					key = key.substr(0,key.length-2);
					reg = new RegExp('%%'+key+'%%',"g");
					if(values[key]){html = html.replace(reg,values[key]);}
					else           {html = html.replace(reg,'');}
				}
			}
			
			return html;
		}
		else {
			
			this.error('makeView : template or values undefined');
			return false;
		}
	}

	/*############################
	# loader
	##############################*/
	for(var x in what){
	
		var myFunc = eval(x+'_core');
		if (typeof myFunc == "function"){
			
			this[x] = new myFunc();
			this[x].error = error;
			this[x].makeView = makeView;
			this[x].query = query;
			this[x].makeLocation = makeLocation;
			this[x].tplLoader = tplLoader;
			this[x].templates = {};
			
			if(typeof this[x].init == 'function'){
				
				this[x].init(what[x]);
			}
		}
	}
	
	delete this.makeView;
	delete this.tplLoader;
	delete this.query;
	delete this.makeLocation;
}

function echo(message) {

	if(typeof console == 'object'){console.log(message);}
}
