/**
Peewa-js,<http://sourceforge.net/projects/peewa-js/>, Super Light-Weight Javascript Framework Copyright(c)2010 Viard Julien,<http://blog.peewa.org>, GNU v3 License.
**/

var Peewa={
	version:"Prealpha:0.03",

	M_Parser:{
		getElement:function(s){
			var r=null;
			if(typeof(s)=='array')r=s;
			else if(typeof(s)=='string')r=document.getElementById(s);
			else if(typeof(s=='object'))r=s;
			return _attach(r);},
		
		getElements:function(){
			var a=arguments;
			if(a.length == 0)return null;
			var s=null;
			var i=null;
			var e=null;
			var es=[];
			for(i=0;i<a.length;i++){
				e=a[i];
				if(typeof(e)=='string'){
					if(e.indexOf('.')!=-1){
						s=e.split('.');
						e=_getC(s[1],s[0]);}
					else e=_getC(e);
					if(e){
						for(var j=0;j<e.length;j++)es.push(e[j]);}
					if(_(a[i]))es.push(_(a[i]));
					e=document.getElementsByName(a[i]);	
					if(e){
						for(var j=0;j<e.length;j++){
							es.push(e[j]);}}
					e=document.getElementsByTagName(a[i]);	
					if(e){
						for(var j=0;j<e.length;j++){
							es.push(e[j]);}}}
				else if(typeof e == 'object')es.push(e);}
			for(var j=0;j<es.length;j++)es[j]=_(es[j]);
			return _(es);},
		
		getC:function(s,t){
			var c=[];
			if(t == null)t='*';
			var els=document.getElementsByTagName(t);
			var p=new RegExp('\\b'+s+'\\b');
			var n='';
			var j=0;
			var i=0;
			for(i=0;i<els.length;i++){
				n=els[i].getAttribute('class');
				if(!n&&_isIE())n=els[i].getAttribute('className');
				if(p.test(n)){
					c[j]=els[i];
					c[j].intKey=j;
					j++;}}
			return c;}
		
	},
	
	M_Dom:{
		i:[],
		
		getDomId:function(){
			return _D.i++;},		

		attach:function(e){
			if(!e||_isScalar(e))return;
			if(e._attach==true)return e;
			if(!e.style)this.style=[];
			e.thread=_getThreadId();
			e._attach=true;

			e._multiple=function(f,a){
				if(!this.length)return false;	
				for(var i=0;i<this.length;i++) this[i][f].apply(this[i],a);
				return true;},
			
			e._getChildren=function(s,d){
				if(!d)d='*';
				if(!this.length)return this.getElementsByTagName(d);
				else{			
					var a=[];
					for(var i=0;i<this.length;i++)a.push(this[i]._getChildren(s));}
				return a;};		
		
			e._get=function(s){
				if(!this.length)return this[s];
				else{			
					var a=[];
					for(var i=0;i<this.length;i++)a.push(this[i]._get(s));}
				return a;};
		
			e._set=function(a,v){
				if(this._multiple('_set',arguments))return;
				if(_isScalar(a))this[a]=v;
				else if(!_isScalar(v))this[a]=v[this.intKey];
				else{
					for(var k in a)this[k]=a[k];}
				return this;};
		
			e._style=function(a,v){
				if(this._multiple('_style',arguments))return;
				if(_isScalar(a))this.style[a]=v;
				else{
					for(var k in a)this.style[k]=a[k];}
				return this;};			
			
			e._class=function(v){
					return this._set({'class':v,'className':v});};

			e._innerHTML=function(v){
				if(this._multiple('_innerHTML',arguments))return;
				e.innerHTML=v;
				return this;};
			
			e._opac=function(v){
				if(this._multiple('_opac',arguments))return;
				var a=[];
				v=_int(v);
				if(v<0)v=0;
				if(v==100)a.ie="";
				else a.ie="alpha(opacity='"+v+"');";
				a.o=(v/100);
				this._set('opac',v);
				if(_isIE())this._style('filter',a.ie);
				else this._style({'MozOpacity':a.o,'opacity':a.o,'KhtmlOpacity':a.o});
				return this;};
		
			e._function=function(ev,f){
				if(this._multiple('_function',arguments))return;
				_addFunction(ev,f,this);
				return this;};
				
			e._toggle=function(){
				if(this._multiple('_toggle',arguments))return;
				if(this._isHidden())this._style('display','');
				else this._style('display','none');
				return this;};

			e._isHidden=function(){
				if(this.length)return false;
				else if(this.style.display == 'none')return true;
				else return false;};
				
			e._animOpac=function(z,a,t,s){
				if(this._multiple('_animOpac',arguments))return;
				if(!s)s=false;
				if(!t)t=35;
				if(s=='block')this._style('display','block');
				if(!z)z=_T.getThreadId();
				for(var j=0;j<a.length;j++)_setTimeout(z,function(a,opac){a._opac(opac);},t*j,this,a[j]);
				if(s=='none')_setTimeout(z,function(a){a._style('display','none');},(t*(j+1)),this);
				return this;};

			e._animHeight=function(z,a,t,s){
				if(this._multiple('_animHeight',arguments))return;
				if(!t)t=35;
				if(!z)z=_T.getThreadId();
				for(var j=0;j<a.length;j++)_setTimeout(z,function(a,strH){a._style('height',strH);},t*j,this,a[j]+'px');
				if(s)_setTimeout(z,function(a,s){a._style('height',s);},t*(j+1),this,s);
				return this;};
			
			return e;}
			
	},
	
	M_Utils:{
		
		changeManager:function(a,b){
			if(a.indexOf('M_')==-1) return false;
			Peewa[a]=b;
			for(var k in Peewa[a]){
				if(typeof(Peewa[a][k])=='function' && k.length>2)__make(k,a);}},
		
		multiple:function(f,a,p){
			if(!p)p=0;
			if(!a[p])return false;
			if(_isScalar(a[p]))a[p]=_$(a[p]);		
			if(!a[p].length)return false;	
			var s=[];
			var z=null;
			for(var i=0;z=a[i];i++)s.push(z);
			for(var i=0;s[0]=a[p][i];i++)f.apply(s[0],s);
			return true;},
		
		int:function(i){return parseInt(i);},
		
		isIE:function(){return document.all;},
		
		isScalar:function(d){return !(typeof(d)=='array'||typeof(d)=='object'||typeof(d)=='function');}	,
		
		create:function(s){
			return _D.attach(document.createElement(s))._set('id','peewa-'+_getDomId());},
		
		append:function(child,node){
			if(_multiple(_append,arguments))return;
			if(!node)node=document.body;
			return node.appendChild(child);},
		
		remove:function(child,node){
			if(_multiple(_remove,arguments))return;
			if(!child)return;
			child.innerHTML='';
			if(!node)node=document.body;
			for(var i=node.childNodes.length;i>-1;i--){
				if(child==node.childNodes[i])return node.removeChild(node.childNodes[i]);}},
		
		addFunction:function(e,f,n){
			if(!n)n=window;
			if(_multiple(_addFunction,arguments,2))return;
			if(!e)e='onload';
			if(_isScalar(f))f=new Function('return '+f);
			var o=n[e];
			if(!n[e])n[e]=f;
			else{
				n[e]=function(){
					o.apply(this,arguments);
					f.apply(this,arguments);}}},		
		
		addEvent:function(node, evType, fct, useCapture){
			if(_multiple(_addEvent,arguments))return;
			if(_isScalar(fct))fct=new Function('return '+fct);
			if(node.addEventListener)return node.addEventListener(evType, fct, useCapture);
			else if(node.attachEvent)return node.attachEvent('on' + evType, fct);
			else _addFunction('on'+evType,fct,node);},		
		
		getMouse:function(e){
  		var x=0;
  		var y=0;
  		if(_isIE()){
    		x=event.clientX + document.body.scrollLeft;
    		y=event.clientY + document.body.scrollTop;} 
  		else{
    		x=e.pageX;
    		y=e.pageY;}  
		  if(x<0)x=0;
		  if(y<0)y=0;
  		return [x,y];},		
		
		getWinSize:function(){
			var s=[];
			if(document.documentElement.clientWidth>document.documentElement.scrollWidth)s.push(document.documentElement.scrollWidth);
			else s.push(document.documentElement.clientWidth);
			if(document.documentElement.clientHeight>document.documentElement.scrollHeight)s.push(document.documentElement.clientHeight);
			else s.push(document.documentElement.scrollHeight);
			return s;},
		
		transaction:function(a, p, m){
	    var f=_create("form")._set({'method':m,'action':a});
	    for(var k in p)_append(_create('input')._set({'type':'hidden','name':k,'value':p[k]}),f);
			_append(f);
	    f.submit();
	    _remove(f);},
		
		url2post:function(a, p){
			return _transaction(a,p,'post');},
		
		url2get:function(a, p){
			return _transaction(a,p,'get');},
		
		download:function(s){
			var d=_('download');
			if(!d){
				d=_create('iframe')._style('display','none')._set({'id':'download','src':''});
				_append(d);}
			d._set('src',s);}
	
	},
	
	M_Ajax:{

		getXHR:function(){
			var xhr=null;
			if(window.XMLHttpRequest)xhr=new XMLHttpRequest();
			else if(window.ActiveXObject){
				try {xhr=new ActiveXObject("Msxml2.XMLHTTP");}
				catch(e){xhr=new ActiveXObject("Microsoft.XMLHTTP");}}
			else xhr=false;
			return xhr;}		
		
	},
	
	M_Effects:{
		
		resetEffects:function(n,z,p){
			if(!p)p='';
			if(!z)z=n._get('thread');
			if(z)_resetTimeout(p+z);
			return p+z;},
		
		centerElement:function(n){
			if(_multiple(_centerElement,arguments))return;
			var s=_getWinSize();
			if(n.offetWidth)s[0]=s[0]-n.offetWidth;
			if(n.offetHeight)s[1]=s[1]-n.offetHeight;
			n._style({'position':'absolute','left':_int(s[0]/2)+'px','top':_int(s[1]/2)+'px'});},		
		
		heightUp:function(n,p,z){
			if(_multiple(_heightUp,arguments))return;
			z=_resetEffects(n,z,'height-');
			if(!p)p=[];
			if(!p.timer)p.timer=20;
			var s=n._get('maxH');
			if(!s)s=_int(n.offsetHeight);
			p.anim=[];
			for(var j=0;j<s;j=j+(s/20))p.anim.push(j);
			n._animHeight(z,p.anim,p.timer,s);
			return p.anim.length*p.timer;},
		
		heightDown:function(n,p,z){
			if(_multiple(_heightDown,arguments))return;
			z=_resetEffects(n,z,'height-');
			if(!p)p=[];
			if(!p.timer)p.timer=20;
			var s=n._get('maxH');
			if(!s)s=_int(n.offsetHeight);
			p.anim=[];
			for(var j=s;j>0;j=j-(s/20))p.anim.push(j);
			n._animHeight(z,p.anim,p.timer,0);
			return p.anim.length*p.timer;},
		
		fadeIn:function(n,p,z){
			if(_multiple(_fadeIn,arguments))return;
			z=_resetEffects(n,z,'opac-');
			if(!p)p=[];
			if(!p.maxOpac)p.maxOpac=100;
			if(!p.timer)p.timer=35;
			var s=n._get('opac');
			if(!s)s=0;
			p.anim=[];
			for(var j=(s/10);j<(_int(p.maxOpac/10)+1);j++)p.anim.push(j*10);
			n._animOpac(z,p.anim,p.timer,'block');
			return p.anim.length*p.timer;},
		
		fadeOut:function(n,p,z){
			if(_multiple(_fadeOut,arguments))return;
			z=_resetEffects(n,z,'opac-');
			if(!p)p=[];
			if(!p.timer)p.timer=35;
			var s=n._get('opac');
			if(!s)s=100;
			p.anim=[];
			for(var j=(s/10);j>-1;j--)p.anim.push(j*10);
			n._animOpac(z,p.anim,p.timer,'none');
			return p.anim.length*p.timer;}
	},
	
	M_Modules:{},
	
	M_Thread:{
		dID:0,
		d:[],
		tID:0,
		t:[],
		
		getThreadId:function(){
			return _T.tID++;},
		
		resetTimeout:function(j){
			if(!j)return;
			if(!_isScalar(j))j=j._get('thread');
			if(_T.t[j]){
				for(i=0;i<_T.t[j].length;i++)clearTimeout(_T.t[j][i]);}},
		
		g:function(z){
			delete _T.d[z];},
		
		setTimeout:function(){
			var a=arguments;
			if(a.length<2)return null;
			var z=a[0];
			if(z==null)z=_getThreadId();
			if(!_isScalar(z))z=z._get('thread');
			if(!_T.t[z])_T.t[z]=[];
			var f=a[1];
			if(_isScalar(f))f=new Function('return '+f);
			var timer=a[2];
			if(a.length == 3)_T.t[z].push(setTimeout(f,timer));
			else{
				var s=[];
				if(_isIE()){
					for(var i=3;i<a.length;i++)s.push(a[i]);
					_T.u=_T.dID++;
					_T.d[_T.u]={'f':f,'s':s};
					_T.t[z].push(setTimeout("_T.d["+_T.u+"]['f'].apply(null, _T.d["+_T.u+"]['s']);_T.g("+_T.u+");",timer));}
				else{
					for(var i=1;i<a.length;i++)s.push(a[i]);
					_T.t[z].push(setTimeout.apply(null,s));}}
			return z;},
			
		setInterval:function(){
			var a=arguments;
			_setTimeout.apply(null,a);
			_setTimeout(null,function(a){_setInterval.apply(null,a);},(a[2]*2),a);}
	}
		
};

for(var k in Peewa){
	if(k.indexOf('M_')!=-1){
		eval("var _"+k.charAt(2)+"=Peewa."+k+";");
		for(var k2 in Peewa[k]){
			if(typeof(Peewa[k][k2])=='function' && k2.length>2)__make(k2,k);}}}

function __make(a,b){
	window["_"+a] = new Function("return Peewa."+b+"."+a+".apply(null,arguments);");
}

function _(){
	return _getElement.apply(null,arguments);
}

function _$(){
	return _getElements.apply(null,arguments);
}
