String.prototype.replaceAll=function(what,by){
	var str=this;
	var temp='#|';
	while(str.indexOf(temp)!=-1)
		temp+=String.fromCharCode(65+parseInt(Math.random()*25));
	while(str.indexOf(what)!=-1)
		str=str.replace(what,temp);
	while(str.indexOf(temp)!=-1)
		str=str.replace(temp,by);
	return str;
}

String.prototype.urlEncode=function(){
	return this.replaceAll('%','%25').replaceAll('\n','%0A').replaceAll('+','%2B').replaceAll('&','%26');
}

Math.decHex=function(d){
	var hex='0123456789ABCDEF';
	return hex.substr(d>>4&15,1)+hex.substr(d&15,1);
};
Math.hexDec=function(h){
	return parseInt(h,16);
};

var $={
	// utiliser WITH
	vars:{
		Interpolate:[],
		AJAX:{
			xhrList:[]
		},
		frequency:10,
		temp:[]
	},
	no:function(){
		return false;
	},
	yes:function(){
		return true;
	},
	getElementsByClassName:function(className,obj,type){
		var array=[];
		var objs=obj.getElementsByTagName(type || '*');
		for(var i in objs)
			if(objs[i].className==className)
				array.push(objs[i]);
		return array;
	},
	Get:{
		Left:function(obj){
			if(obj.offsetParent)
				return obj.offsetLeft+$.Get.Left(obj.offsetParent);
			else
				return obj.offsetLeft;
		},
		Top:function(obj){
			if(obj.offsetParent)
				return obj.offsetTop+$.Get.Top(obj.offsetParent);
			else
				return obj.offsetTop;
		},
		OuterWidth:function(obj){
			return obj.offsetWidth;
		},
		OuterHeight:function(obj){
			return obj.offsetHeight;
		},
		InnerWidth:function(obj){
			return $.Get.OuterWidth(obj)-(obj.style.borderWidth?parseInt(obj.style.borderLeftWidth)+parseInt(obj.style.borderRightWidth):0);
		},
		InnerHeight:function(obj){
			return $.Get.OuterHeight(obj)-(obj.style.borderWidth?parseInt(obj.style.borderTopWidth)+parseInt(obj.style.borderBottomWidth):0);
		},
		Area:function(obj){
			return {
				left:$.Get.Left(obj),
				top:$.Get.Top(obj),
				outerWidth:$.Get.OuterWidth(obj),
				outerHeight:$.Get.OuterHeight(obj),
				innerWidth:$.Get.InnerWidth(obj),
				innerHeight:$.Get.InnerHeight(obj)
			}
		},
		Cursor:function(e){
			return {
				x:!e?window.event.x:e.pageX,
				y:!e?window.event.y:e.pageY
			}
		},
		Float:function(obj){
			var value=obj.style.cssFloat || obj.style.styleFloat || false;
			return (value=='left' || value=='right')?value:false;
		}
	},
	Array:{
		Find:function(array,value){
			for(var i in array)
				if(array[i]===value)
					return i;
			return false;
		},
		Merge:function(elem1,elem2){
			var array=[];
			for(var i in elem1)
				if(typeof(i)=='string')
					array[i]=elem1[i];
				else
					array.push(elem1[i]);
			for(var i in elem2)
				if(typeof(i)=='string')
					array[i]=elem2[i];
				else
					array.push(elem2[i]);
			return array;
		},
		Keys:function(array){
			var keys=[];
			for(var i in array)
				keys.push(i);
			return keys;
		},
		Filter:function(from,filter,invert){
			var array=[];
			if(invert===true){
				for(var i in from)
					if($.arrayFind(filter,i)===false)
						array[i]=from[i];
			}
			else
				for(var i in filter)
					array[filter[i]]=from[filter[i]];
			return array;
		},
		Del:function(array,key){
			if(key!==false)
				array.splice(key,1);
		}
	},
	DOM:{
		Obj:function(id){
			return document.getElementById(id);
		},
		Del:function(obj){
			if(obj && obj.parentNode)
				obj.parentNode.removeChild(obj);
		},
		PutFirst:function(obj,to){
			if(to==obj)
				return;
			if(to.childNodes[0])
				to.insertBefore(obj,to.childNodes[0]);
			else
				to.appendChild(obj);
		},
		PutLast:function(obj,to){
			if(to==obj)
				return;
			to.appendChild(obj);
			return obj;
		},
		PutBefore:function(obj,to){
			if(to==obj)
				return;
			$.DOM.Del(obj);
			to.parentNode.insertBefore(obj,to);
		},
		PutAfter:function(obj,to){
			if(to==obj)
				return;
			$.DOM.Del(obj);
			if(to.nextSibling)
				to.parentNode.insertBefore(obj,to.nextSibling);
			else
				to.parentNode.appendChild(obj);
		}
	},
	CSS:{
		SetOpacity:function(obj,value){
			value=parseInt(value);
			if(obj.style)
				obj=obj.style;
			obj.opac=value;
			obj.filter='alpha(opacity='+value+')';
			obj.opacity=value/100;
		},
		SetFloat:function(obj,value){
			obj.cssFloat=value;
			obj.styleFloat=value;
		}
	},
	Object:{
		_Base:{
			getElementsByClassName:function(className){
				return $.getElementsByClassName(className,this);
			},
			isObject:true,
			isMouseOver:false,
			onmousemove:function(e){
				this.isMouseOver=true;
				if(this._onmousemove)
					this._onmousemove(e);
			},
			onmouseout:function(e){
				this.isMouseOver=false;
				if(this._onmouseout)
					this._onmouseout(e);
			},
			Del:function(){
				$.DOM.Del(this);
			},
			PutFirst:function(obj){
				$.DOM.PutFirst(obj,this);
			},
			PutLast:function(obj){
				$.DOM.PutLast(obj,this);
			},
			PutBefore:function(obj){
				$.DOM.PutBefore(obj,this);
			},
			PutAfter:function(obj){
				$.DOM.PutAfter(obj,this);
			}
		},
		_Document:{
			
		},
		New:function(model,params,add,type){
			var obj=document.createElement(type || 'div');
			if(add)
				add.appendChild(obj);
			$.Object.Apply($.Object._Base,[],obj);
			var att=$.Object.Apply(model,params,obj);
			if(att.__childNodes)
				for(var i in att.__childNodes)
					$.Object.New(
						att.__childNodes[i][0] || att.__childNodes[i],
						att.__childNodes[i][1] || [],
						obj,
						att.__childNodes[i][2] || att.__childNodes[i].__type || false
					);
			if(att.__initialize)
				att.__initialize.call(obj,params);
			return obj;
		},
		Fusion:function(elem,add){
			if(add && add.inherit){
				var inherits=[];
				if(typeof(add.inherit)=='string')
					inherits=[add.inherit];
				else for(var i in add.inherit)
					inherits.push(add.inherit[i]);
				for(var i in inherits)
					eval('$.Object.Fusion(elem,'+inherits[i]+');');
			}
			for(var i in add){
				if(i.substr(0,2)=='__')
					continue;
				else if(i=='opac')
					$.CSS.SetOpacity(elem,add[i]);
				else if(i=='float')
					$.CSS.SetFloat(elem,add[i]);
				else if(i.charAt(0)=='$' || typeof(add[i])!='object')
					elem[i]=add[i];
				else{
					if(!elem[i])
						elem[i]=new Object;
					$.Object.Fusion(elem[i],add[i]);
				}
			}
		},
		Apply:function(model,params,obj){
			var att=(typeof(model)=='function'?model.apply(obj,params || []):model);
			$.Object.Fusion(obj,att);
			if(att.__apply)
				att.__apply.call(obj,params);
			return att;
		},
		GlobalInit:function(){
			$.Object.Apply($.Object._Document,[],document);
			$.Object.RecurrentInit(document.body);
		},
		RecurrentInit:function(obj){
			$.Object.Apply($.Object._Base,[],obj);
			for(var i=0;i<obj.childNodes.length;i++){
				if(obj.childNodes[i].style)
					$.Object.RecurrentInit(obj.childNodes[i]);
			}
		},
		OnlyInit:function(objs){
			for(var i in objs)
				$.Object.Apply($.Object._Base,[],objs[i]);
		}
	},
	Interpolate:{
		Start:function(objs,final,time,func,timeout){
			if(timeout){ // ameliorer
				var id=$.vars.temp.length;
				window.setTimeout('$.Interpolate.Start($.vars.temp['+id+'].objs,$.vars.temp['+id+'].final,$.vars.temp['+id+'].time,$.vars.temp['+id+'].func);',timeout);
				$.vars.temp.push({objs:objs,final:final,time:time,func:func});
				return;
			}
			if(!objs[0])
				objs=[objs];
			for(var j in objs){
				var obj=objs[j];
				$.Interpolate.Stop(obj,true);
				$.Object.Fusion(obj,final);
				var att=$.Array.Keys(final.__Interpolate.goal);
				var periods=time/$.vars.frequency;
				var goalData=[];
				for(var i in final.__Interpolate.goal)
					goalData[i]=$.Interpolate.GetData(final.__Interpolate.goal[i],i);
				var startData=$.Array.Filter(obj.style,att);
				var regularData=[];
				for(var i in startData){
					startData[i]=$.Interpolate.GetData(startData[i],i);
					if(typeof(startData[i])=='number')
						regularData[i]=(goalData[i]-startData[i])/periods;
					else{
						regularData[i]=[];
						for(var j in startData[i])
							regularData[i][j]=(goalData[i][j]===false?false:(goalData[i][j]-startData[i][j])/periods);
					}
				}
				$.vars.Interpolate.push(
					{
						obj:obj,
						final:final,
						curr:startData,
						regularData:regularData,
						remain:periods,
						func:func || false
					}
				);
			}
		},
		Regular:function(){
			for(var id=0;id<$.vars.Interpolate.length;id++){
				if(!$.vars.Interpolate[id])
					continue;
				var vars=$.vars.Interpolate[id];
				if(--$.vars.Interpolate[id].remain<1)
					$.Interpolate.Finish(id);
				else for(var i in vars.curr){
					var t=vars.curr[i];
					if(typeof(t)=='number')
						t+=vars.regularData[i];
					else for(var j in t)
							t[j]=(t[j]===false?false:t[j]+vars.regularData[i][j]);
					vars.curr[i]=t;
					if(i=='opac')
						$.CSS.SetOpacity(vars.obj,t);
					else if(i=='margin' || i=='padding'){
						if(t[0]!==false)
							vars.obj.style[i+'Top']=t[0]+'px';
						if(t[1]!==false)
							vars.obj.style[i+'Right']=t[1]+'px';
						if(t[2]!==false)
							vars.obj.style[i+'Bottom']=t[2]+'px';
						if(t[3]!==false)
							vars.obj.style[i+'Left']=t[3]+'px';
					}
					else if(i.toLowerCase().indexOf('color')!=-1){
						var str='rgb(';
						for(var j in t){
							str+=parseInt(t[j]);
							if(j<2)
								str+=',';
						}
						vars.obj.style[i]=str+')';
					}
					else
						vars.obj.style[i]=t+'px';
				}
			}
		},
		Finish:function(id,abort){
			var vars=$.vars.Interpolate[id];
			$.vars.Interpolate[id]=false;
			if(abort)
				return;
			$.Object.Fusion(vars.obj.style,vars.final.__Interpolate.goal);
			if(vars.final.__Interpolate.final)
				$.Object.Fusion(vars.obj,vars.final.__Interpolate.final);
			if(vars.func)
				vars.func.call(vars.obj);
			vars.obj.activity=false;
		},
		Stop:function(obj){
			for(var i in $.vars.Interpolate)
				if(!obj || $.vars.Interpolate.obj==obj)
					$.Interpolate.Finish(i,true);
		},
		GetData:function(data,type){
			if(type=='margin' || type=='padding'){
				var str=data.split(' ');
				if(!str[1])
					for(var i=1;i<4;i++)
						str[i]=str[0];
				else if(!str[2]){
					str[2]=str[0];
					str[3]=str[1];
				}
				else if(!str[3])
					str[3]=0;
				return [
					str[0]=='auto'?false:parseFloat(str[0]),
					str[1]=='auto'?false:parseFloat(str[1]),
					str[2]=='auto'?false:parseFloat(str[2]),
					str[3]=='auto'?false:parseFloat(str[3])
				];
			}
			else if(data.indexOf && data.indexOf('#')==0){
				var str=data.substr(1);
				if(str.length==3)
					str=str.charAt(0)+str.charAt(0)+str.charAt(1)+str.charAt(1)+str.charAt(2)+str.charAt(2);
				return [
					Math.hexDec(str.substr(0,2)),
					Math.hexDec(str.substr(2,2)),
					Math.hexDec(str.substr(4,2))
				];
			}
			else if(data.indexOf && data.indexOf('rgb(')!=-1){
				var curr=data.substr(data.indexOf('rgb(')+4).split(')')[0].split(',');
				for(var j in curr)
					curr[j]=parseFloat(curr[j]);
				return curr;
			}
			else
				return parseFloat(data);
		}
	},
	AJAX:{
		_XHRObj:function(){
			return {
				__apply:function(args){
					this.open('POST','ajax.php',true);
					this.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
					this.send('input='+$.JSON.FromObject(args[0]));
				},
				onreadystatechange:function(){
					(function(){
						if(this.readyState==4){
							if(this.status==200){
								$.AJAX.interprate(
									$.JSON.ToObject(this.responseText)
								);
								$.vars.AJAX.xhrList.splice(0,1);
								$.AJAX.xhrDo();
							}
						}
					}).call($.vars.AJAX.xhrObj);
				}
			}
		},
		Send:function(x1){
			$.vars.AJAX.xhrList.push(x1);
			if($.vars.AJAX.xhrList.length<2)
				$.AJAX.xhrDo();
		},
		interprate:function(output){
			var interpreter=$.AJAX.Interpreters[output.type] || false;
			if(interpreter)
				interpreter(output.data);
		},
		xhrDo:function(){
			if($.vars.AJAX.xhrList.length>0){
				try{
					var xhrObj=new ActiveXObject('Msxml2.XMLHTTP');
				}
				catch(e1){
					try{
						var xhrObj=new ActiveXObject('Microsoft.XMLHTTP');
					}
					catch(e2){
						try{
							var xhrObj=new XMLHttpRequest();
						}
						catch(e3){
							var xhrObj=false;
						}
					}
				}
				$.vars.AJAX.xhrObj=xhrObj;
				$.Object.Apply($.AJAX._XHRObj,[$.vars.AJAX.xhrList[0]],xhrObj);
			}
		},
		Interpreters:[]
	},
	JSON:{
		FromObject:function(obj){
			var str='{';
			for(var i in obj){
				if(str!='{')
					str+=',\n';
				str+=i+':';
				if(typeof(obj[i])=='object')
					str+=$.JSON.FromObject(obj[i]);
				else
					str+=$.JSON.FromString(obj[i]);
			}
			return str+'}';
		},
		FromString:function(str){
			if(!str || typeof(str)=='number')
				return '"'+str+'"';
			return '\''+str.replaceAll('\\','\\\\').replaceAll('\'','\\\'').replaceAll('\n','\\n').replaceAll('\t','\\t').replaceAll('\r','\\r').urlEncode()+'\'';
		},
		ToObject:function(str){
			eval('var obj='+str+';');
			return obj;
		}
	},
	Cookie:{
		Set:function(name,value,days){
			var expire=new Date();
			expire.setTime(expire.getTime()+86400000*days);
			document.cookie=name+'='+escape(value)+'; expires='+expire.toGMTString();
		},
		Get:function(name){
			var nameEQ=name+'=';
			var ca=document.cookie.split(';');
			for(i in ca){
				var c=ca[i];
				if(c.indexOf(nameEQ)!=-1)
					return c.substring(c.indexOf(nameEQ)+nameEQ.length,c.length);
			}
		}
	}
}

window.setInterval('$.Interpolate.Regular();',$.vars.frequency);
