/**
 * BlackLight - A server side webpage framework
 *
 * @package BlackLight
 * @author Icewind <icewind (at) derideal (dot) com>
 * @copyright 2009
 * @license http://www.gnu.org/licenses/gpl.html GNU Public License
 * @url http://blacklight.metalwarp.com
 * @version 0.1
 */

BLXMLAPI=function(path){
	this.path=path;
	if(this.path.substr(0,1)=='/'){
		this.path=this.path.substr(1);
	}
	this.xmlLoader=new BLXMLLoader(this);
	this.xmlLoader.setCallBack(this.parse);
	this.xmlLoader.method="POST";
}


BLXMLAPI.prototype={
	path:'',
	backend:'xmlLoader',
	callback:false,
	object:false,
	errors:Array(),
	xmlLoader:false,
	url:'',
	notifyErrors:true,
	fullResponse:false,
	response:false,
	iframe:false,
	form:false,
	files:Array(),
	addFile:function(name,file){
		this.files[name]=file;
		this.backend='form';
	},
	run:function(arguments){
		this.url=parseUri('%root%/'+document.config.BL_XMLAPI_FOLDER+'/'+this.path);
		if(this.backend=='xmlLoader'){
			var argstring="";
			var first=true;
			if(typeof arguments=='object' || typeof arguments=='array'){
				for(name in arguments){
					if(typeof arguments[name]!='object' && typeof arguments[name]!='function'){
						if(!first){
							argstring+='&';
						}
						first=false;
						argstring+=name+'='+encodeURIComponent(arguments[name]);
					}
				}
			}else if(typeof arguments=='string'){
				argstring='xmlapi_arguments='+encodeURIComponent(arguments);
			}
			this.xmlLoader.load(this.url,argstring);
		}else if(this.backend=='form'){
			this.form=document.createElement('form');
			var param=false;
			for(name in arguments){
				if(typeof arguments[name]!='object' && typeof arguments[name]!='function'){
					param=document.createElement('input');
					param.setAttribute('type','hidden');
					param.setAttribute('name',name);
					param.setAttribute('value',arguments[name]);
					this.form.appendChild(param);
				}
			}
			for(name in this.files){
				if(typeof arguments[name]!='object' && typeof arguments[name]!='function'){
					param=document.createElement('input');
					param.setAttribute('type','file');
					param.setAttribute('name',name);
					param.setAttribute('value',this.files[name]);
					this.form.appendChild(param);
				}
			}
			this.form.setAttribute('enctype','multipart/form-data');
			this.form.setAttribute('action',this.url);
			this.form.setAttribute('method','post');
			this.form.setAttribute('class','hidden');
			var uuid=randomUUID();
			this.form.setAttribute('target',uuid);
			this.iframe=document.createElement('iframe');
			this.iframe.setAttribute('class','hidden');
			this.iframe.setAttribute('id',uuid);
			this.iframe.setAttribute('name',uuid);
			body=document.getElementsByTagName('body').item(0);
			body.appendChild(this.form);
			body.appendChild(this.iframe);
			mainEventHandler.addListner(this.iframe,this.parse,'onload',this);
			this.form.submit();
		}
	},
	parse:function(req){
		if(this.backend=='xmlLoader'){
			this.fullResponse=req.responseXML;
		}else if(this.backend=='form'){
			this.fullResponse=(this.iframe.contentDocument)?this.iframe.contentDocument:this.iframe.contentWindow.document;
		}
		errors=this.fullResponse.getElementsByTagName('errors').item(0).childNodes;
		for(var i=0;i<errors.length;i++){
			errorNode=errors[i];
			error=new BLAPIError(errorNode.getAttribute('level'),errorNode.getAttribute('code'),errorNode.textContent);
			this.errors[this.errors.count]=error;
			if(this.notifyErrors){
				error.notify();
			}
		}
		this.response=this.fullResponse.getElementsByTagName('response').item(0);
		this.callback.call(this.object);
	},
	setCallBack:function(func,object){
		this.callback=func;
		this.object=object;
	}
}

apitest=function(){
	this.api=new BLXMLAPI('test/lol');
	this.api.setCallBack(this.test,this);
	this.api.addFile('fileTest','home/robin/test.txt');
	this.api.run({"test":"wasd"});
}

apitest.prototype={
	test:function(){
		alert(this.api.response.textContent);
	}
}

// test=new apitest();

BLAPIError=function(level,code,error){
	this.level=parseInt(level);
	this.code=code;
	this.error=error;
}

BLAPIError.prototype={
	level:0,
	code:0,
	error:'',
	notify:function(){
		new BLError(this.level,this.error,this.code);
	}
}