
function JSBAL(){
	this.isLoader=true;	  
	
};
JSBAL.prototype.resources=[];
JSBAL.prototype.widget={};
JSBAL.prototype.toResourceUrl=function JSBAL_toResourceUrl(source, res, type){
	var url=source==undefined?jsbal.basePath:source;
	if(url!=null&& url!='')
		if (url.charAt(url.length-1)!='/'&&res.charAt(0)!='/')url =url.concat("/");
	url=url+res+"."+type;
	return url;
}
JSBAL.prototype.toUrl=function JSBAL_toUrl(source, res, type){
	var url=this.toResourceUrl(source, res, type);
	if(jsbal.resources[url])
		return null;
	else{
		jsbal.resources[url]=true;
		return url;
	}
};
JSBAL.prototype.require=function JSBAL_require(name, source){
	var spath;if((spath=jsbal.toUrl(source, name, "js"))){
		jsbal.xhrRequest('GET', spath, 'js');
	}
	
};
JSBAL.prototype.declare=function JSBAL_declare(name, source){
	jsbal.toUrl(source, name, 'js');
};
JSBAL.XHR_GET='GET';
JSBAL.XHR_POST='POST';
JSBAL.prototype.xhrRequest=function JSBAL_xhrRequest(method, url, type, cache, defer, load, error, done){
	function translateResponse(rq){
		switch(type.toLowerCase()){
		case 'json':
			return eval('('+rq.responseText+')');
		case 'text':return rq.responseText;
		case 'js':
			try{
				if (window.execScript) {
					window.execScript(rq.responseText);
				}else{
					eval.call(null, rq.responseText);
				}
				return null;

			}catch(e){
				if(console){
					if(e.name=="SyntaxError"){
						console.error(url+": "+e.lineNumber+": exception: "+e.name+": "+e.message);
					}else{
						console.error("problem in "+url+" exception: "+e);
					}
				}
				else alert("problem in "+url+" exeception: "+e);
			}
			return null;
		case 'xml':
			return rq.response;
		default:throw new Error('Unknown type');
		}
	};
	var rq=null;
	if(window.XMLHttpRequest){
		rq=new XMLHttpRequest();
	}else if(window.ActiveXObject) {
		//IE standards compliant - yeah right
       	try {
        	rq = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		rq = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		rq = null;
        	}
		}
    }
	var responce;
	if(!cache){
		url+=((url.indexOf('?')<0)?'?':'&')
			+'jsbal.uncached='
			+(new Date().getTime()*4096+Math.floor(Math.random()*4096))
			  .toString(16);
	}
	if(defer&&!load)throw new Error('handler function required for deferred response handling');
	if(load!=undefined && typeof load != 'function')throw new Error('load handler must be a function');
	var handled=false;
	if(load||error||done){
		rq.jsbal={load:load,error:error,done:done};
		function jsbal_xhrHandler(){
			handled=true;
			if(this.readyState==4){
				this.jsbal.response=translateResponse(this);
				if(this.status==200||this.status==1220){
					if(typeof this.jsbal.load =='function')
						this.jsbal.load(this.jsbal.response, status);
				}else{
					if(typeof this.jsbal.error =='function')
						this.jsbal.error(this.jsbal.response, status);
				}
				if(typeof this.jsbal.done =='function')
					this.jsbal.done(this.jsbal.response, status);

			}
		}
		rq.onreadystatechange =jsbal_xhrHandler;
	}
	rq.open(method.toUpperCase(), url, defer==undefined?false:defer);
	rq.send();
	if(!defer){
		if(!(load||error||done)){
			if(rq.status<400||rq.status==1220){
				return translateResponse(rq);
			}else{
				throw Error('Unable To load '+url+", response="+rq.status+", "+rq.statusText);
			}
		}else{
			if(!handled){
				jsbal_xhrHandler.call(rq);
			}
			if("response"in rq.jsbal)return rq.jsbal.response; 
			return translateResponse(rq);
		}
	}else return null;

};
JSBAL.prototype.requireCSS=function JSBAL_requireCSS(name, source){
	var spath;if((spath=jsbal.toUrl(source, name, "css"))){
		var link=document.createElement("LINK");
		link.setAttribute("title", 'style');
		link.setAttribute("href", spath);
		link.setAttribute("rel", "stylesheet");
		document.getElementsByTagName('head')[0].appendChild(link);
	}
};

JSBAL.prototype.loadAbstraction=function JSBAL_loadAbstraction(){
	var script=document.getElementById("jsbal-script");
	if(script!=null){
		var path=script.getAttribute("src");
		if(path.lastIndexOf(".js")==path.length-3 ){
			path=path.substring(0, path.length-8);
			JSBAL.prototype.basePath=path;		
		}
		var browser=navigator.appName;
		if(browser.indexOf("Netscape")>=0){
			browser='mozilla';
		}else if(browser=="Microsoft Internet Explorer"){browser= 'ie';}
		else browser='other';
		JSBAL.prototype.browser=browser;
		jsbal.require('jsbal-'+browser);
		
	}
};
JSBAL.prototype.onLoad= function JSBAL_onLoad(func){
	  
	  if (typeof window.onload=='undefined'){
		    window.onload = func; 
	  }else if(typeof window.onload != 'function') { 
	    window.onload = func; 
	  } else { 
        var oldonload = window.onload; 
	    window.onload = function JSBAL_loadChain() { 
	      if (oldonload) { 
	        oldonload(); 
	      } 
	      func(); 
	    }; 
	  }
};
JSBAL.prototype.toJSON=function JSBAL_toJSON(object){
	var obj=object;
	if(obj===null)return "null";
	if(obj===undefined)return "undefined";
	switch(typeof obj){
	case 'string':{
		var s0='"'+obj+'"';
		var s1=null;
		s0.replace('\\', '\\\\');
		s0.replace('"', '\\"');
		//scan for special character
		for(var i=1;i<s0.length-1;i++){
			var cc=s0.charCodeAt(i);
			//test for special characters
			if((cc>=0&&cc<32)||cc>127){
				var b=0;
				
				for(;i<s0.length-1;i++){
					var c=s0.charAt(i);
					cc=c.charCodeAt(0);
					var mc=jsbal.string.META_CHARACTERS[c];
					if(mc||(cc>=0&&cc<32)||cc>127){
						//copy any preceding segment 							
						if(i>b)
							s1=s1+s0.substring(b,i); 
						//next segment start after character;
						b=i+1;
						//inject escape or unicode notation of character
						s1=s1+mc?mc:
						('\\u' + ('0000' + c.toString(16)).slice(-4).toUpperCase());
					}
				}
				//copy final segament verbatim
				s0=s1+s0.substring(b, s0.length);
			}
		}
		return s0;
	}
	case 'object':{
		if(obj instanceof Array){
			var s='[';
			for(var p in obj){
				if(typeof obj[p]!='function')
				s=s+(s=='['?'':',')+jsbal.toJSON(obj[p]); 
			}
			return s+']';
		}else{
			var s='{';
			for(var p in obj){
				if(typeof obj[p]!='function')
				s=s+(s=='{'?'':',')+p+':'+jsbal.toJSON(obj[p]); 
			}
			for(var p in obj){
				if(typeof obj[p]=='function')
				s=s+(s=='{'?'':',')+p+':'+jsbal.toJSON(obj[p]); 
			}
			return s+'}';
		}
	}
	case 'number':
		return obj.toString();
	
	case 'boolean':
		return obj?'true':'false';
	case 'function':{
		return obj.toString(); 
	}
	default:
		throw new function NotImplemented(){
		    this.message="jsbal.toJSON can't process objects of type "
		    	+typeof obj;};
	}

};

JSBAL.prototype.toHex=function JSBAL_toHex(value, width){
	var sign=value<0?'-':(value>0?'+':'');
	if(sign=='-')value=-value;
	
}

JSBAL.prototype.abstract=function JSBAL_abstract(){
	var args=arguments;
	alert ("call to abstract method with argsuments ("+jsbal.toJSON(args)+")");
};
JSBAL.prototype.appendSelectOption=function JSBAL_appendSelectOption(select,option){
	jsbal.abstract();
};


var jsbal=new JSBAL();
jsbal.loadAbstraction();
