/* dynlite javascript api
 * peter assenov- aip solutions ltd' 2001-2010
 * ver 2.3.10 - 2010-06-30
 */
/* main environment */
dl={ver:2.3, dev:1, cash:0, w:window, d:document, n:navigator};
/* browser detection */
dl.is={
	ua:dl.n.userAgent.toLowerCase(),
	os:dl.n.platform.toLowerCase(),
	sniff:function(){	
		this.rv=parseFloat((this.ua.match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/)||[])[1]);
		this.wk=/webkit/.test(this.ua); 					/* Webkit - Safari, Chrome */
		this.pr=/opera/.test(this.ua);  					/* Presto - Opera */
		this.tr=/msie/.test(this.ua)&&!this.pr; 			/* Trident - Internet Explorer 4-8 */
		this.gk=/mozilla/.test(this.ua)&&!this.tr&&!this.wk;/* Gecko - Firefox, Mozilla */
	/* browser aliases */
		this.sf=this.ch=this.wk;
		this.op=this.pr;
		this.ie=this.tr;
		this.ns=this.ff=this.mz=this.gk;
	/* specific browsers */
		this.ie6=this.ie&&this.rv==6;
	return this;
	}
}
dl.is.sniff();
/* debug functionality */
dl.trace=function(el,str){
	if(typeof(el)=='object'){
		var code=[];
		for(var i in el){try{code.push(i+' -> '+el[i])}catch(e){}}
		if(str) return code.join('<br />');		
		(code.length>30)? dl.d.write(code.join('<br />')) : alert(code.join('\n'));
	}
	else  
		if(str)  return el; else alert(el);
}
dl.log=['--- dynlite log ---'];
/* javascript error handling */
dl.error=function(msg,url,lno,type){
	var err=[msg,' ['+lno+']',url.replace(/^.*(\/|\\)/,'')];
	dl.log.push('!'+(type||'')+'Error: '+err.join(' '));
	if(dl.dev) dl.trace(dl.log);
return true;
}
onerror=dl.error;
/* events management */
dl.evt={
	add:function(el,evt,func){
		if(el.addEventListener)	el.addEventListener(evt,func,false);
        else el['on'+evt]=function(event){func(event)}
    return this;
	},
	remove:function(el,evt,func){
		if(el.removeEventListener) el.removeEventListener(evt,func,false);
		else el['on'+evt]=null;
	return this;
	}
}
/* onload code execution */
dl.init=[]; //DOMContentLoaded
dl.is.init=0;
dl.oninit=function(event){	
	if(dl.is.init) return;
	dl.is.init=-dl.gmt();
	dl.log.push('- processing oninit code');
	if(dl.tags.length) dl.process(); 			//auto creation
	if(dl.init.length) eval(dl.init.join(';'));	//modules autoinit
	if(dl.w.init) init();						//page init
	dl.log.push('#oninit code executed');
	dl.is.init=1;
};
dl.doinit=function(){
	if(dl.is.gk||dl.is.pr)
		dl.evt.add(dl.d,'DOMContentLoaded',dl.oninit);
    if(dl.is.wk){	
    	if(/loaded|complete/.test(dl.d.readyState)) dl.oninit();
       	else {setTimeout(arguments.callee,10);return;}
	}
	if(dl.is.tr){	
		try{dl.d.documentElement.doScroll("left");}
		catch(e){setTimeout(arguments.callee,10);return;}
		dl.oninit();
	}
}
dl.doinit();
dl.load=[]; //onLoad
dl.is.load=0;
dl.onload=function(event){
	if(!dl.is.init) dl.oninit();
	if(dl.is.load) return;
	dl.is.load=-1;
	dl.log.push('- processing onload code');
	if(dl.load.length) eval(dl.load.join(';')); //modules autoload
	if(dl.w.load) load();						//page load
	dl.log.push('#onload code executed');
	dl.is.load=1;
};
dl.evt.add(dl.w,'load',dl.onload);
/* html elements dynamic manipulations */
dl.els=[];
dl.el=function(id,ob){	
	if(dl.cash && dl.els[id]) return dl.els[id];
	var el=dl.d.getElementById(id);
	if(!el)	{dl.error('no element with id='+id+' found','dynlite.js','dl.el()','Runtime');return 0;}
	if(ob) el.ob=ob;
	el.attr=function(attr,val){
		if(!val) return (attr=='class')? el.className : (!dl.is.ie6&&el.hasAttribute(attr))? el.getAttribute(attr):el[attr];
		else (attr=='class')? el.className=val:el.setAttribute(attr,val,false);
	return el;
	}
	el.css=function(prop,val){
		if(!val) return el.style[prop];
		else el.style[prop]=val;
	return el;
	}
	el.on=function(disp){el.css('display',(disp? disp:'block')); return el;}
	el.off=function(){el.css('display','none'); return el;}
	dl.els[id]=el;
return el;
}
dl.is.el=function(id){return dl.d.getElementById(id);}
dl.dom={
	el:function(tag,par,attr,text){	
		var id=(attr&&attr.id)? attr.id:dl.els.length;
		dl.els.push(id);
		//if(dl.els[id]) return dl.els[id];
		var el=dl.d.createElement(tag);
			el.id=id;
		(par)?  par.appendChild(el) : dl.d.body.appendChild(el);
		dl.log.push('# '+tag+' element with id="'+id+'" created.')
		var del=dl.el(id);
		if(attr&&typeof(attr)=='object')
			for(i in attr)
				del.attr(i,attr[i]);
		if(text) el.appendChild(dl.d.createTextNode(text));
	return del;
	},
	evt:function(_this,evt){if(_this[evt]) return (function(){return _this[evt](this);});}, /* closure */
	css:function(el, rule){ //should be used with caution- on ie returns cascaded style, not computed one.
		if(dl.d.defaultView && dl.d.defaultView.getComputedStyle)
			return dl.d.defaultView.getComputedStyle(el, "").getPropertyValue(rule);
		if(el.currentStyle){	
			tempRule=rule.replace(/\-(\w)/g, function (strMatch, p1){return p1.toUpperCase();});
			return el.currentStyle[tempRule];
		}
	return this;
	}
}
/* modules initialisation */
dl.mods=['dl'];
dl.mod={
	add:function(mods) {
		if(typeof(mods)=='string') 	mods=[mods];
		for(var i=mods.length;i;) {
			var mod=mods[--i];
			if(dl.mods[mod]) continue;
			dl.mods[mod]=-1;
			var el=dl.dom.el('script', dl.d.getElementsByTagName('head')[0], {type:"text/javascript",src:"dl."+mod+".js"})
			if(el.readyState) //IE
				el.onreadystatechange=function(){
					if(el.readyState=="loaded" || el.readyState=="complete"){
						el.onreadystatechange=null;
						dl.mod.start(mod);
					}
				} 
			else  //Others
				el.onload = function(){ dl.mod.start(mod); };
		}
	return this;
	},
	init:function(mods){ dl.init.push(dl.mod.add(mods)); return this;},
	load:function(mods){ dl.load.push(dl.mod.add(mods)); return this;},
	start:function(mod){ dl.mods[mod]=1; if(dl[mod]['init']) dl[mod]['init']();	return this;}
}
/* automatic objects creation */
dl.tags=[];
dl.make=function(tag){if(tag) dl.tags.push(tag); return dl;}
dl.process=function(){
	for(i in dl.tags){
		var tag = dl.tags[i];
		dl.log.push('- processing ['+tag+'] tags');
		var els=dl.d.getElementsByTagName(tag);
		for(var i=els.length;i;){	
			var el=els[--i];
			if(!el||!el.id||!el.className) continue;
			var obsa = el.className.match(/dl\..*\)/g);
			for(j in obsa) {
				var obr = obsa[j].toString().match(/dl\.(\w+)\((.*)\)/);
				if(!obr) continue; //ie6 sucks.
				var ob = obr[1];
				var pars=obr[2].toString().replace(/'/g,'').split(",");
				if(dl[ob]) dl.w[ob] = new dl[ob](el.id, pars);
				else dl.error('no ptototype '+ob+' found for element '+el.id,'dynlite.js','dl.process()','Runtime');;
			}
		}
	}
return this;
}
/* benchmark */
dl.mt=new Date();
dl.smt=function(){dl.mt=new Date();return this;}
dl.gmt=function(){return new Date()-dl.mt;}
/* shortcuts */
trace=dl.trace;
/* keep the comments and use freely! enjoy... */
