//Tiny lib, DOM extend (c)Alexander Melanchenko, http://alexnd.com

var $DOM={
	create: function(t)
	{
		if('undefined'==typeof document.createElement) return false;
		var c = ((arguments.length>1)?arguments[1]:'');
		var a = ((arguments.length>2)?arguments[2]:{});
		var p = ((arguments.length>3)?arguments[3]:{});
		if('string'!=typeof t) return false;
		var e=document.createElement(t);
		if(c!='')e.innerHTML=c;
		//try{
			if(e.setAttribute){
			for(var _ in a){
				e.setAttribute(_, a[_]);
			}}
			for(var _ in p){
				e[_]=p[_];
			}
		//}catch(ex){}
		return e;
	},

	insert_before: function(e, r, p)
	{
		try{
		if('undefined'==typeof r) return;
		if('undefined'==typeof p){
			if('undefined'==typeof r.parentNode) return;
			var p = r.parentNode;
		}
		p.insertBefore(e, r);
		}catch(_){}
	},

	insert_after: function(e, r, p)
	{
		try{
		if('undefined'==typeof r) return;
		if('undefined'==typeof p){
			if('undefined'==typeof r.parentNode) return;
			var p=r.parentNode;
		}
		if('undefined'==typeof r.nextSibling){
			p.insertBefore(e, r.nextSibling);
		}else if('undefined'!=typeof p.appendChild){
			p.appendChild(e);
		}
		}catch(_){}
	},

	insert_in: function(e, p)
	{
		try{if('undefined'!=typeof p.appendChild) p.appendChild(e);}catch(_){}
	},
	
	get: function(e){
		try{
		if(arguments.length>1){
			var e=[];
	  	for(var i=0;i<arguments.length;i++)e[e.length]=$(arguments[i]);
			return e;
		}
		if('object'!=typeof e)e=document.getElementById(e);
		}catch(_){}
		return e;
	},
	
	get_by_id: function(e)
	{
		return $DOM.get(e);
	},
	
	get_by_name: function(n)
	{
		var i=((arguments.length>1)?arguments[1]:0);
		if('undefined'!=typeof document.getElementsByName){
			var r=document.getElementsByName(n);
			if('number'==typeof i && i>=0 && 'undefined'!=typeof r[i]) return r[i];
			else return r[0];
		}else var r=null;
		return r;
	},
	
	//TODO: $DOM.get_by_class
	get_by_class: function()
	{
	},
	
	get_by_tag: function(t)
	{
		var i=(arguments.length>1)?arguments[0]:0;
		if(document.getElementsByTagName){
			var ob = document.getElementsByTagName(t);
			if(ob.length){
				if(ob[i])ob=ob[i];
			}
		}else{
			var ob = null;
		}
		return ob;
	},
	
	get_img: function (n){
		if(document.images && 'undefined'==typeof document.images[n]) return document.images[n];
		return null;
	},
	
	get_size: function(e,z){
		if(undefined==z)z=0;
		var _={w:0,h:0};
		if(!('object'==typeof e && e!=null))e=$DOM.get(e);
		if('object'==typeof e && e!=null){
			if('number'==typeof e.clientWidth &&(!z || (z && e.clientWidth!=0 && e.clientHeight!=0))){
				_.w=e.clientWidth;
				_.h=e.clientHeight;
			}else if('number'==typeof e.offsetWidth &&(!z || (z && e.offsetWidth!=0 && e.offsetHeight!=0))){
				_.w=e.offsetWidth;
				_.h=e.offsetHeight;
			}else if('number'==typeof e.scrollWidth &&(!z || (z && e.scrollWidth!=0 && e.scrollHeight!=0))){
				_.w=i.scrollWidth;
				_.h=i.scrollHeight;
			}else if(undefined!=e.style && undefined!=e.style.width &&(!z || (z && e.style.width!=0 && e.style.height!=0))){
				_.w=parseInt(e.style.width);
				_.h=parseInt(e.style.height);
			}else if('number'==typeof e.width &&(!z || (z && e.width!=0 && e.height!=0))){
				_.w=e.width;
				_.h=e.height;
			}else if('number'==typeof e.naturalWidth &&(!z || (z && e.width!=0 && e.height!=0))){
				_.w=e.naturalWidth;
				_.h=e.naturalHeight;
			}
		}
		return _;
	},
	
	get_position: function(e){
		var _={x:0,y:0};
		if(!('object'==typeof e && e!=null))e=$DOM.get(e);
		if('object'==typeof e && e!=null){
			if('undefined'!=typeof  e.offsetParent){
				_.x=e.offsetLeft;
				_.y=e.offsetTop;
				var t=e.offsetParent;
				while(t!=null){
					_.x+=t.offsetLeft;
					_.y+=t.offsetTop;
					t=t.offsetParent;
				}
			}
			else if('undefined'!=typeof e.style && 'undefined'!=typeof e.style.left){
				_.x=parseInt(e.style.left);
				_.y=parseInt(e.style.top);
			}
		}
		return _;
	},
	
	get_vis: function(e){
		if(!('object'==typeof e && e!=null))e=$DOM.get(e);
		if('object'==typeof e && e!=null){
			return (('undefined'!=typeof e.style.visibility && e.style.visibility=='hidden')?false:true);
		}
	},

	get_display: function(e){
		if(!('object'==typeof e && e!=null))e=$DOM.get(e);
		if('object'==typeof e && e!=null){
			return (('undefined'!=typeof e.style.display && e.style.display=='none')?false:true);
		}
	},
	
	get_window: function(){
		var _={x:0,y:0,w:0,h:0,in_w:0,in_h:0,of_x:0,of_y:0};
		if('number'==typeof(window.innerWidth)){
		_.in_w=window.innerWidth;
		_.in_h=window.innerHeight;
		}else if(document.documentElement &&(document.documentElement.clientWidth || document.documentElement.clientHeight)){
		_.in_w=document.documentElement.clientWidth;
		_.in_h=document.documentElement.clientHeight;
		}else if(document.body && (document.body.clientWidth || document.body.clientHeight)){
		_.in_w=document.body.clientWidth;
		_.in_h=document.body.clientHeight;
		}
		if('number'==typeof(window.pageYOffset)){
			_.of_x=window.pageXOffset;
			_.of_y=window.pageYOffset;
		}else if(document.body && (document.body.scrollLeft || document.body.scrollTop)){
			_.of_x=document.body.scrollLeft;
			_.of_y=document.body.scrollTop;
		}else if(document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)){
			_.of_x=document.documentElement.scrollLeft;
			_.of_y=document.documentElement.scrollTop;
		}
		if('number'==typeof(window.screenLeft)){
			_.x=window.screenLeft;
			_.y=window.screenTop;
		}else if('number'==typeof(window.screenX)){
			_.x=window.screenX;
			_.y=window.screenY;
		}else if('number'==typeof(window.pageXOffset)){
			_.x=window.pageXOffset;
			_.y=window.pageYOffset;
		}else if('number'==typeof(window.left)){
			_.x=window.left;
			_.y=window.top;
		}
		if('number'==typeof(window.outerWidth)){
			_.w=window.outerWidth;
			_.h=window.outerHeight;
		}else if('number'==typeof(window.dialogWidth)){
			_.w=window.dialogWidth;
			_.h=window.dialogHeight;
		}else if('number'==typeof(window.width)){
			_.w=window.width;
			_.h=window.height;
		}else{
			_.w=(((_.in_w+20)<screen.width)?(_.in_w+20):screen.width);
			_.h=(((_.in_h+50)<screen.height)?(_.in_h+50):screen.height);
		}
		return _;
	},
	
	get_body: function(){
		var w=window;
		if(arguments.length && 'object'==typeof arguments[0]){
			w=arguments[0];
		}
		var _=null;
		if(w.document.documentElement){
			var _=w.document.documentElement;
		}else if(w.document.body){
			var _=w.document.body;
		}else if(w.document.getElementsByTagName){
			var t=w.document.getElementsByTagName('body');
			if(t.length>0) var _=t[0];
		}
		return _;
	},
		
	set_style: function(_,s){
		if('object'!=typeof s)return;
		if('object'!=typeof _)_=$DOM.get(_);
		if('object'!=typeof _)return;
		if('undefined'==typeof _.style) return;
		for(var p in s)_.style[p]=s[p];
	},
	
	set_display: function(e,s){
		if('object'!=typeof e)e=$DOM.get(e);
		try{
		if(!('object'==typeof e && 'object'==typeof e.style)) return;
		if(!s) s='none';
		else if(s==true || s==1) s='block';
		e.style.display=s;
		}catch(ex){}
	},
	
	//TODO: implemntation
	get_childs: function(_){
		
	},
	
	attach_event: function(e, n, f){
		if (e.addEventListener) {
			//alert('attach_event1');
			e.addEventListener (n, f, false);
		} else if (e.attachEvent) {
			//alert('attach_event2');
			e.attachEvent ("on"+n, f);
		} else {
			//alert('attach_event3');
			e["on"+n] = f;
		}
	}

}
