function _Motel(){
	this.motions={};
	this.active=new Array();
	this.prefer=new Array();
	this.defines=new Array();
	this.reserved=new Array();
	this.tm=0;
	this.ajReq=null;
	this.load=function(uri,user,pass){
		var d=new Date();
		var s=d.getTime();
		uri+='?'+s;
		this._loadXML(uri,user,pass);
	};
	this.start=function(mid,el,o,cb){
		var id=0, element, opt,callback;
		if(typeof(mid)!='number'){
			if(!el)return -1;
			if(!this.motions[mid]){
				if(this.start.arguments[4]==true){return -1;}
				var res=[mid,el,o,cb,true];
				this.reserved.push(res);
				return this.reserved.length-1;
			}
			element=(typeof(el)=='string')?document.getElementById(el):el;
			if(!element)return -1;
			opt=o||null;
			callback=cb||null;
			id=this.set(mid, element,opt,callback);
		}else{
			id=mid;
			if(!this.prefer[id]){
				this.reserved[id][4]=true;
				return id;
			}
		}
		if(id<0)return id;
		if(this.active[id]!=null){this.stop(id);}
		this.active[id]=this.prefer[id];
		if(!!element){
			this.active[id].element=element;
		}
		this.active[id]._init(0);
		if(!this.tm){
			this.tm=setInterval('Motel.run()',16);
		}
		return id;
	};
	this.stop=function(id){
		this.active[id]=null;
		return true;
	};
	this.stopall=function(id){
		this.tm=clearInterval(this.tm);
		for(var i=0;i<this.active.length;i++)
			this.active[i]=null;
		return true;
	};
	this.set=function(motionid,el,opt,callback){
		if(!this.motions[motionid]){
			if(this.set.arguments[4]==true){return -1;}
			var res=[motionid,el,opt,callback,false];
			this.reserved.push(res);
			return this.reserved.length-1;
		}
		newid=this.active.length;
		var moinfo=this.motions[motionid];
		if(!moinfo)return -1;
		var mo=new _Motion(moinfo[0][0], moinfo[0][1], moinfo[0][2]);//id, limit,callback
		if(!mo)return -1;
		if(typeof(el)=='string'){
			mo.elements[0]=document.getElementById(el);
		}else{
			if(typeof(el) =='Array' || el.constructor == Array || el.length){
				for(var i=0;i<el.length;i++){
					mo.elements[i]=el[i];
				}
			}else{
				mo.elements[0]=el;
			}
		}
		if(!mo.elements)return -1;
		var oldgrp=null;
		var elcnt=-1;
		var len=mo.elements.length;
		for(var i=1;i<moinfo.length;i++){
			var thisgroup=moinfo[i].group;
			if(!oldgrp){
				if(elcnt<0){
					elcnt=0;
				}else{
					var info=moinfo[i-1];
					for(var elcnt=0;elcnt<len;elcnt++){
						var toon=new _Toon(info,mo.elements[elcnt]);
						mo._add(toon,elcnt==0);
					}
					elcnt=0;
				}
			}else if(oldgrp==thisgroup){
				var info=moinfo[i-1];
				var toon=new _Toon(info,mo.elements[elcnt]);
				mo._add(toon,elcnt==0);
				elcnt++;
			}else{// if(oldgrp!=thisgroup){
				var k=i-1;
				for(;elcnt<len;elcnt++){
					var toon=new _Toon(moinfo[k],mo.elements[elcnt]);
					mo._add(toon,elcnt==0);
				}
				elcnt=0;
			}//if
			oldgrp=thisgroup;
		}//for
			if(!oldgrp){
				var info=moinfo[moinfo.length-1];
				for(var elcnt=0;elcnt<len;elcnt++){
					var toon=new _Toon(info,mo.elements[elcnt]);
					mo._add(toon,elcnt==0);
				}
			}else{// if(oldgrp!=thisgroup){
				var k=moinfo.length-1;
				for(;elcnt<len;elcnt++){
					var toon=new _Toon(moinfo[k],mo.elements[elcnt]);
					mo._add(toon,elcnt==0);
				}
			}//if
		
		mo.opt=opt;
		if(!!callback) mo.callback=callback;
		this.prefer[newid]=mo;
		this.active.length=newid+1;
		this.active[newid]=null;
		return newid;
	};//set
	this.run=function(){
		var d=new Date();
		var s=d.getTime();
		var l=this.active.length;
		for(var i=0;i<l;i++){
			if(this.active[i]==null || !this.active[i]._run(s))continue;
			if(this.active[i].callback){
				var fu=this.active[i].callback;
				if(typeof(fu)=='number'){
					this.start(fu);
				}else if(fu.indexOf('(')<0){
//					if(fu.match(/[^\d]/)){
						fu=this.start(fu, this.active[i].elements, null, null);
//					}else{
//						this.start(fu);
//					}
				}else{
					eval(fu);
				}
			}
			this.active[i]=null;
		}//for
	};//run
	this.init=function(){
		var doc;
		if(!!document.body){
			doc=document.body;
		}else if(!!document.documentElement){
			doc=document.documentElement;
		}
		if(!doc){return;}
		for(var i=0;i<this.reserved.length;i++){
			var r=this.reserved[i];
			if(r[4]){
				this.start(r[0], r[1], r[2], r[3]);
			}else{
				this.set(r[0],r[1],r[2],r[3]);
			}
		}
		var l=this.defines.length;
		var elements=new Array();
		defs:for(var i=0;i<l;i++){
			var df=this.defines[i];
			doc.appendChild(df);
			var dfm=df.motion;
			if(!dfm) continue;
			for(var j in elements){
				if(j==dfm){
					elements[j].push(df);
					continue defs;
				}
			}
			elements[dfm]=new Array();
			elements[dfm].push(df);
		}
		for(var j in elements){
			this.start(j, elements[j], null, null);
		}
		if(!this.tm){
			this.tm=setInterval('Motel.run()',16);
		}
	}
/******************/
	this._parseXML=function(){
		var xml=this.ajReq.responseXML;
		if(!xml)return;
		var xmldefs=xml.getElementsByTagName('define');
		var xmlmos=xml.getElementsByTagName('motion');
		for(var i=0;i<xmlmos.length;i++){
			var xmlmo=xmlmos[i];
			var motionid=xmlmo.getAttribute('id');
			var motionlimit=xmlmo.getAttribute('limit')||1;
			var motioncb=xmlmo.getAttribute('callback')||'';
			if(!motionid){continue;}
			var m=new Array();
			m[0]=new Array(motionid, motionlimit, motioncb);
			if(!!xmlmo.childNodes){
			for(var c=0;c<xmlmo.childNodes.length;c++){
				var item=xmlmo.childNodes[c];
				if(item.nodeType!=1)continue;
				var effect=item.getAttribute('effect');
				if(!effect)continue;
				var base=item.getAttribute('base');
				var start=item.getAttribute('start');//start=start.replace(/'/g, '\\'');
				var end=item.getAttribute('end');//end=end.replace(/'/g, '\\'');
				var delay=item.getAttribute('delay')||0;
				var id=item.getAttribute('id');
				var formula=item.getAttribute('formula');
				var vector=item.getAttribute('vector');
				var range=item.getAttribute('range')||0;
				var group=item.getAttribute('group');
				var time=item.getAttribute('time')||0;
				var toon={effect:effect,base:base,start:start,end:end,delay:delay,id:id,formula:formula,vector:vector,range:range,group:group,time:time};
				m.push(toon);
			}}//if for
			this.motions[motionid]=m;
		}
		for(var i=0;i<xmldefs.length;i++){
			var xmldef=xmldefs[i];
			for(var c=0;c<xmldef.childNodes.length;c++){
				var item=xmldef.childNodes[c];
				if(item.nodeType!=1)continue;
				var id=item.getAttribute('id');
				var nodeName=item.nodeName;
				var width=item.getAttribute('width');
				var height=item.getAttribute('height');
				var clone=item.getAttribute('clone');
				var className=item.getAttribute('class');
				var title=item.getAttribute('title');
				var motion=item.getAttribute('motion');
				var onclick=item.getAttribute('onclick');
				var src,alt,text;
				if(nodeName=='img'){
					src=item.getAttribute('src');
					alt=item.getAttribute('alt');
				}else{
					nodeName=item.getAttribute('node');
					text=item.firstChild.nodeValue;
				}
				if(!id)continue;
				if(nodeName=='img' && !src)continue;
				var theNode;
				for(var i=0;i<=clone;i++){
					theNode=document.createElement(nodeName);
					if(i==0){
						theNode.id=id;
					}else{
						theNode.id=id+i;
					}
					if(!!width)theNode.style.width=width+'px';
					if(!!height)theNode.style.height=height+'px';
					if(!!className)theNode.className=className;
					if(!!title)theNode.title=title;
					if(!!src)theNode.src=src;
					if(!!alt)theNode.alt=alt;
					if(!!text)theNode.appendChild(document.createTextNode(text));
					if(!!motion)theNode.motion=motion;
					if(!!onclick)theNode.onclick=function(){Motel._click(this,onclick);};
					theNode.style.position='absolute';
					if(!theNode.motion){theNode.style.visibility='hidden';}
					this.defines.push(theNode);
				}//for(close)
			}//for(c<xmldef.childNodes.length){
		}
		//if(!!document.body || !!document.documentElement){
		//if(document.getElementsByTagName('body').length>0 || document.getElementsByTagName('BODY').length>0){
			Motel.init();
		//}else if(!!window.addEventListener){
		//	window.addEventListener('load', Motel.init, false);
		//}else if(!!window.attachEvent){
		//	window.attachEvent('onload', Motel.init);
		//}
	};
	this._click=function(el,src){
		if(src.indexOf('(')<0){
//			Motel.start(src, el);
		}else{
			eval(src);
		}
	}
	function _Motion(mid, limit, cb){
		this.id=mid;
		this.limit=limit;
		this.cnt=0;
		this.elements=new Array();
		this.callback=cb;
		this.toons=new Array();
		this.act=0;

		this._init=function(id){
			var mid=(!!id)?id:0;
			if(mid==0){
				this.act=0;
				this.cnt=0;
			}
			for(var i=0;i<this.toons[mid].length;i++){
				var to=this.toons[mid][i];
				to.starttime=0;
				for(var j in to._start)to.start[j]=to._start[j];//keyword check
				for(var j in to._end)to.end[j]=to._end[j];//keyword check
				to.start.ini=true;
			}
		};
		
		this._run=function(sec){
			while(1){
			var finished=true;
			var runnext=false;
			var l=this.toons[this.act].length;
			for(var i=0;i<l;i++){
				var re=this.toons[this.act][i].__run(sec);
				if(re==0){
					finished=false;
				}else if(re<0){
					runnext=true;
				}
			}
			if(runnext){
				if(++this.act<this.toons.length){
					this._init(this.act);
					continue;
				}
			}//if(runnext)
			break;
			}//while
			if(!finished){return false;}
			if(++this.act<this.toons.length){
				this._init(this.act);
				return false;
			}
			if(this.limit!=0 && this.limit<=++this.cnt){
				return true;
			}
			this._init();
			return false;
		};
		this._add=function(toon, isNew){
			var i;
			with(this){
			for(i=0;i<toons.length;i++){
				if(!!toon.group && toon.group==toons[i][0].group){
					toon.__setdelay(toons[i].length);
					toons[i].push(toon);
					return;
				}
			}
			if(i==toons.length && isNew){
				toons[i]=new Array();
				toon.__setdelay(0);
				toons[i].push(toon);
			}else{
				toon.__setdelay(toons[toons.length-1].length+1);
				toons[toons.length-1].push(toon);
			}
			}//with
		};
	};
	function _Toon(info,el){
		var tmp;
		this.effect=info.effect;
		this.base=info.base||'top,left';
		this.elid=info.id;//need ?
		this.element=null;
			if(!!this.elid){
				this.element=document.getElementById(this.elid);
			}else if(!!el){
				this.element=el;
			}
		this.start={ini:false,top:0,left:0,x:0,y:0,opacity:0};
		this._start={top:0,left:0,x:0,y:0,opacity:0};
		if(!!info.start){
			var s=info.start.replace(/;$/, '');
			s=s.replace(/;/g, ',');
			if(s.indexOf(':')>=0){
				eval('this._start={'+s+'};');
			}else{
				var val=parseInt(info.start);
				this._start={top:val,left:val,y:val,x:val,opacity:val};
			}
		}
		this.end={};
		this._end={};
		if(this.effect!='wait')
			this._end={top:this._start.top,left:this.start.left,y:this.start.y,x:this.start.x,opacity:this.start.opacity};
		if(!!info.end){
			if(info.end.indexOf(':')<0){
				var val=parseInt(info.end);
				this._end={top:val,left:val,y:val,x:val,opacity:val};
			}else{
				var s=info.end.replace(/;/g, ',');
				s=s.replace(/:([^,]+),/g, ':\'$1\',');
				s=s.replace(/,$/, '');
				eval('this._end={'+s+'};');
			}
		}
		this.distance;
		if(!this._start.top){
			this.distance=this._end-this._start;
		}else{
			this.distance={x:this._end.left-this._start.left, y:this._end.top-this._start.top};
		}
		this.delay=parseInt(info.delay)||0;
		this.formula=info.formula;
		this.vector=info.vector;
		this.range=info.range;
		this.group=info.group;
		this.time=parseInt(info.time);
		this.starttime=0;
		this.__run;
		this.__setdelay=function(magni){
			this.delay*=magni;
		};
		if(this.effect=='move'){
			this.__run=move;
		}else if(this.effect=='opacity'){
			if(!this.element){
					this.__run=function(){return 1;};
			}else if(this.element.filters){
//				if(this.element.filters.item && typeof(this.element.filters.item)=='function'){
					this.__run=opacity_filter;
//				}else{
//					this.__run=function(){return 1;};
//				}
			}else{
				this._start.opacity/=100;
				this._end.opacity/=100;
				this.__run=opacity_opacity;
			}
		}else if(this.effect=='clip'){
			this.__run=clip;
		}else if(this.effect=='resize'){
			this.__run=resize;
		}else if(this.effect=='rect'){
			this.__run=rect;
		}else if(this.effect=='poly'){
			this.__run=poly;
		}else if(this.effect=='circle'){
			this.__run=circle;
		}else if(this.effect=='ellipse'){
			this.__run=ellipse;
		}else if(this.effect=='conc'){
			this.__run=calc;
		}else if(this.effect=='wait'){
			this.__run=wait;
		}else{
			this.__run=function(){return 1;};
		}
	};
	function move(now){
		var sec=now;
		if(!this.starttime)this.starttime=sec;
		sec=sec-this.starttime;
		var finished=0;
		with(this){
		if(delay>sec) return 0;
		sec-=delay;
		if(sec>=time){
			element.style.top=end.top+'px';
			element.style.left=end.left+'px';
			return 1;
		}
		var dift,difl;
		var proportion=sec/time;
		if(start.top==end.top){
			dift=end.top;
		}else{
			dift=start.top+(end.top-start.top)*proportion;
			if((start.top<end.top && end.top<dift) || (start.top>end.top && end.top>dift)){
				dift=end.top;
				finished=1;
			}
		}
		if(start.left==end.left){
			difl=end.left;
		}else{
			difl=start.left+(end.left-start.left)*proportion;
			if((start.left<end.left && end.left<difl) || (start.left>end.left && end.left>difl)){
				difl=end.left;
				finished=1;
			}
		}
		element.style.top=dift+'px';
		element.style.left=difl+'px';
		}//with
		return finished;
	};
	function opacity_filter(now){
		var sec=now;
		if(!this.starttime)this.starttime=sec;
		sec=sec-this.starttime;
		var finished=0;
		with(this){
		if(delay>sec) return 0;
		sec-=delay;
		if(sec>=time){
			try{element.filters.item("DXImageTransform.Microsoft.Alpha").Opacity=end.opacity;}catch(e){}
			return 1;
		}
		dalpha=end.opacity-start.opacity;
		var opacity=parseInt(start.opacity+dalpha*sec/time);
		try{element.filters.item("DXImageTransform.Microsoft.Alpha").Opacity=opacity;}catch(e){}
//		element.filters[0].opacity=opacity;
		if((start.opacity<=end.opacity && end.opacity<=opacity) || (start.opacity>end.opacity && end.opacity>=opacity)){
//			starttime=0;
			return 1;
		}
		}//with
		return 0;
	};
	function opacity_opacity(now){
		var sec=now;
		if(!this.starttime)this.starttime=sec;
		sec=sec-this.starttime;
		var finished=0;
		with(this){
		if(delay>sec) return 0;
		sec-=delay;
		if(sec>=time){
			element.style.opacity=end.opacity;
//			this.starttime=0;
			return 1;
		}
		dalpha=end.opacity-start.opacity;
		var opacity=(start.opacity+dalpha*sec/time);
		element.style.opacity=opacity;
		if((start.opacity<=end.opacity && end.opacity<=opacity) || (start.opacity>end.opacity && end.opacity>=opacity)){
//			starttime=0;
			return 1;
		}
		}//with
		return 0;
	};
	function clip(now){
		var sec=now;
		if(!this.starttime)this.starttime=sec;
		sec=sec-this.starttime;
		var finished=0;
		with(this){
		if(delay>sec) return 0;
		sec-=delay;
		if(sec>=time){
			element.style.clip='rect('+end.top+'px '+end.right+'px '+end.bottom+'px '+end.left+'px)';
//			this.starttime=0;
			return 1;
		}
		var dif;
		var proportion=sec/time;
		var ct,cl,cr,cb;
		if(start.top==end.top){
			ct=end.top;
		}else{
			dif=start.top+(end.top-start.top)*proportion;
			if((start.top<end.top && end.top<dif) || (start.top>end.top && end.top>dif)){
				dif=end.top;
				finished=1;
			}
			ct=parseInt(dif);
		}
		if(start.right==end.right){
			cr=end.right;
		}else{
			dif=start.right+(end.right-start.right)*proportion;
			if((start.right<end.right && end.right<dif) || (start.right>end.right && end.right>dif)){
				dif=end.right;
				finished=1;
			}
			cr=parseInt(dif);
		}
		if(start.bottom==end.bottom){
			cb=end.bottom;
		}else{
			dif=start.bottom+(end.bottom-start.bottom)*proportion;
			if((start.bottom<end.bottom && end.bottom<dif) || (start.bottom>end.bottom && end.bottom>dif)){
				dif=end.bottom;
				finished=1;
			}
			cb=parseInt(dif);
		}
		if(start.left==end.left){
			cl=end.left;
		}else{
			dif=start.left+(end.left-start.left)*proportion;
			if((start.left<end.left && end.left<dif) || (start.left>end.left && end.left>dif)){
				dif=end.left;
				finished=1;
			}
			cl=parseInt(dif);
		}
		element.style.clip='rect('+ct+'px '+cr+'px '+cb+'px '+cl+'px)';
		}//with
		return finished;
	};
	function resize(now){
		var sec=now;
		if(!this.starttime)this.starttime=sec;
		sec=sec-this.starttime;
		var finished=0;
		with(this){
		if(delay>sec) return 0;
		sec-=delay;
		if(sec>=time){
			element.style.width=end.width+'px';
			element.style.height=end.height+'px';
//			this.starttime=0;
			return 1;
		}
		var difw,difh;
		var proportion=sec/time;
		if(start.width==end.width){
			difw=end.width;
		}else{
			difw=start.width+(end.width-start.width)*proportion;
			if((start.width<end.width && end.width<difw) || (start.width>end.width && end.width>difw)){
				difw=end.width;
				finished=1;
			}
		}
		if(start.height==end.height){
			difh=end.height;
		}else{
			difh=start.height+(end.height-start.height)*proportion;
			if((start.height<end.height && end.height<difh) || (start.height>end.height && end.height>difh)){
				difh=end.height;
				finished=1;
			}
		}
		element.style.width=difw+'px';
		element.style.height=difh+'px';
		}//with
		return finished;
	};
	function rect(now){
	};
	function poly(now){
	};
	function circle(now){
	};
	function ellipse(now){
	};
	function wait(now){
		var sec=now;
		if(!this.starttime)this.starttime=sec;
		sec=sec-this.starttime;
		with(this){
		if(sec<time){
			return 0;
		}
		if(end.top!=undefined){element.style.top=end.top+'px';}
		if(end.left!=undefined){element.style.left=end.left+'px';}
		if(end.width!=undefined){element.style.width=end.width+'px';}
		if(end.height!=undefined){element.style.height=end.height+'px';}
		if(end.clip!=undefined){element.style.clip=end.clip;}
		if(end.position!=undefined){element.style.position=end.position;}
		if(end.display!=undefined){element.style.display=end.display;}
		if(end.visibility!=undefined){element.style.visibility=end.visibility;}
		if(end.opacity!=undefined){
			;
		}
		if(formula){
			var lists=formula.split(';');
			for(var i in lists){
				var pair=lists[i].split('=');
				if(pair[0]=='src'){element.src=pair[1];}
				else if(pair[0]=='alt'){element.title=pair[1];}
				else if(pair[0]=='title'){element.title=pair[1];}
			}
		}
		if(time==0)return -1;
		return 1;
		}//with
	};
}//_Motel
_Motel.prototype._loadXML=function(url,user,password){
//	this._loadXML=function(url,user,password){
	this.ajReq=new createAjax();
	if(!this.ajReq) return;
	this.ajReq.onreadystatechange =function (){
		if(Motel.ajReq.readyState == 4){
			if(Motel.ajReq.status == 200) {//load
				Motel._parseXML();
			}else if(Motel.ajReq.status==302){ //not modified
//				alert('302 not modified');
				Motel._parseXML();
			}else{
				alert('load error');
				//Motel._parseXML();
			}
		}
	};
	this.ajReq.open("GET",url,true,user,password);
	this.ajReq.send('');
//	};
	function createAjax(){
		if(!!window.XMLHttpRequest){
			try{
				return new XMLHttpRequest();
			}catch(e){
				return null;
			}
		}else if(!!window.ActiveXObject){
			try{
				return new ActiveXObject("Msxml2.XMLHTTP.3.0");
			}catch(e){
				try{
					return new ActiveXObject("Microsoft.XMLHTTP");
				}catch(e){
					return null;
				}
			}
		}
		return null;
	};
};
_Motel.prototype.Pos=function(el){
var pos={x:0,y:0,w:0,h:0};
var isMac=(navigator.platform.toLowerCase().indexOf("mac")!=-1);
if(!el)return pos;
	if(el.getBoundingClientRect){
		var doc,t=0,l=0;
		if(!!document.body){
			doc=document.body;
		}else if(!!document.documentElement){
			doc=document.documentElement;
		}else{
			doc={scrollTop:0,scrollLeft:0};
		}
		if(!!doc.scrollTop)t=parseInt(doc.scrollTop);
		if(!!doc.scrollLeft)l=parseInt(doc.scrollLeft);
		var rect=el.getBoundingClientRect();
		var top=rect.top+t;
		var left=rect.left+l;
		var width=rect.right-rect.left;
		var height=rect.bottom-rect.top;
		pos={x:left,y:top,w:width,h:height};
	}else if(document.getBoxObjectFor){
		var rect=document.getBoxObjectFor(el);
		var top=rect.y;
		var left=rect.x;
		var width=rect.width;
		var height=rect.height;
		pos={x:left,y:top,w:width,h:height};
	}else{
		pos.w=el.clientWidth;
		pos.h=el.clientHeight;
		while(el){
			pos.x+=parseInt(el.offsetLeft);
			pos.y+=parseInt(el.offsetTop);
			el=el.offsetParent;
		}
		pos.x-=document.body.offsetLeft; // -1* html margin-left
		pos.y-=document.body.offsetTop; // -1* html margin-top
		if (isMac){
			pos.x+=document.body.offsetLeft;
			pos.y+=document.body.offsetTop;
		}
	}
	return pos;
};

var Motel=new _Motel();
