//(C)Alexander Barnard 2011
var __chatRoom=[]
function _loadChatRooms(){
	var o=document.getElementsByTagName("DIV")
	for(var i=0;i<o.length;i++)if(/\bchatbox\b/i.test(o[i].className)){__chatRoom[__chatRoom.length]=$CR(o[i])}
}
function $CR($id){
	var userName='',message='',tmr=0,lineNum=0,hash=0,status=0,time=0,pollSeconds=6
	var wF=_getObject($id),wC=first(wF),wP=last(wF),wU=last(last(first(wP))),wS=first(last(wP))
	var form=first(first(wP)),field=form.elements,fusr=field[0],fbut=field[1],fmsg=field[2]
	var ticker=setInterval(function(){_tickTock()},1000)

	fusr.value=''
	fusr.maxLength=20
	fusr.onkeypress=function($e){
		if(!$e)$e=window.event;
		var k=$e.which
		if(!k)k=$e.keyCode
		if(k<10)return true//FF 8 is register on KeyDown.
		if(k==13){_btn();return false}
		var c=String.fromCharCode(k)
		return /\w+|\s+/.test(c)
	}
	fmsg.onkeyup=_ku
	fmsg.onkeypress=_kp
	fmsg.className="readonly"
	fmsg.readOnly=true
	fmsg.value=''
	fbut.onclick=_btn
	
	form.disabled=true
	
	var oS=new ani(wC,1000)
	oS.fn=function(){
		wC.scrollTop=this.top+(last(wC).offsetTop+last(wC).offsetHeight-wC.offsetHeight-this.top+2)*Math.sin(_r(90*this.c))
	}
	oS.onend=function(){
		if(!this.a.length)return
		var o=_getObject(this.a.shift());if(!o)return
		var oA=new ani(o,1000)
		oA.fn=function(){_op(this._(),1-this.c)}
		oA.onend=function(){this._().parentNode.removeChild(this._())}
		oA.go()
	}
	
	function _tickTock(){
		if(!time)return
		time.setTime(time.getTime()+1000)
		var st=new Date(field[4].value)-time
		var et=new Date(field[5].value)-time
		var msg="This Chatroom "
		if(st>0)
			msg+=("opens in: "+_getTime(st))
		else if(et>0){
			if(status==0){
				status=1
				form.disabled=false
				var oA=new ani(first(wP),1000)
				oA.fn=function(){_vs(this._(),_eio(1-this.c))}	
				oA.onend=function(){this._().style.xoverflow="visible";this._().style.height="auto";fusr.focus()}
				oA.go()
			}	
			msg+=("closes in: "+_getTime(et))
		}else{
			if(status==1){
				status=2
				_btn()
				first(wP).style.overflow="hidden"
				var oA=new ani(first(wP),1000)
				oA.fn=function(){_vs(this._(),_eio(this.c))}
				oA.go()
			}
			window.focus()
			msg+="is now closed."
			fusr.value=''
			form.disabled=true
			clearInterval(ticker)
			setTimeout(function(){clearTimeout(tmr)},pollSeconds*3.1*1000)
		}
		wS.innerHTML=msg
	}
	function _getTime($t){
		function _v($l,$v,$f){return $v+$l+($l.length>1?($v!=1?'s':''):'')+' '}
		$t/=1000
		var ret=_v('day',Math.floor($t%5184000/86400))+
				_v('h',Math.floor($t%216000/3600%24))+
				_v('m',Math.floor($t%3600/60))+
				_v('s',$t%60)
		return ret.replace(/\b0\w+ /g,'')				
	}
	
	function _btn(){
		if(!fusr.value){fusr.focus();return}
		var isLogin=/login/i.test(fbut.innerHTML)
		fbut.innerHTML="<span>Log"+(isLogin?"out":"in")+"</span>"
		fusr.readOnly=isLogin
		fusr.className=isLogin?'readonly':''
		fmsg.className=!isLogin?'readonly':''
		fmsg.readOnly=!isLogin
		fmsg.value=''
		if(isLogin){
			userName=fusr.value
			new _call(1)
			fmsg.focus()
		}else{
			new _call(2)
			userName=''
		}
	}

	function _updateUsers($h){
		var oA=new ani(first(wU),1000)
		oA.fn=function(){_op(this._(),this.c)}	
		oA.onend=function(){
			last(wU).innerHTML=$h
			var oA= new ani(first(wU),1000)
			oA.fn=function(){_op(this._(),1-this.c)}
			oA.onend=function(){
				var h=wU.offsetHeight
				var oA= new ani(wU,1000)
				oA.fn=function(){
					var o=last(this._())
					var dy=_eio(1-this.c)*(o.offsetTop+o.offsetHeight*(!o.innerHTML?0:1)-h)
					if(!dy)this.c=1
					this._().style.height=h+dy+"px"
				}
				oA.go()
			}
			oA.go()
		}
		oA.go()
	}
	
	function _addChat(h){
		var id='c'+lineNum
		wC.innerHTML+='<div style="zoom:1"><div id="'+id+'" class="cover"></div>'+h+'</div>'
		oS.stop()	
		oS.top=wC.scrollTop
		oS.a.push(id)
		oS.go()
	}
	function first($o){$o=$o.firstChild;while($o.nodeType!=1){$o=$o.nextSibling}return $o}
	function last($o){$o=$o.lastChild;while($o.nodeType!=1){$o=$o.previousSibling}return $o}
	function _r($d){return $d*(Math.PI/180)};
	function _eio($v){return (1+Math.sin(_r(90+180*$v)))/2}
	function _op($o,$v){
		$o.style.opacity=$v
		$o.style.filter='alpha(opacity='+($v*100)+')'
		if($v==1)if($o.style.removeAttribute)$o.style.removeAttribute('filter')
	}
	function _vs($o,$v){
		$o.style.height=($v*(last($o).offsetTop+last($o).offsetHeight+4))+"px"
	}
	function _kp($e){
		if(!$e)$e=window.event
		var o=$e.target||$e.srcElement
		var k=$e.which
		if(!k)k=$e.keyCode
		if(k==13){message=o.value;o.value='';new _call(0);return false}
		return o.value.length<255
	}
	function _ku($e){
		if(!$e)$e=window.event
		var o=$e.target||$e.srcElement
		var max=255,v=o.value
		if(v.length>max){o.value=v.substr(0,max-1);return false}
		return true
	}

	function _call($action){
		var a=[],data=''
		clearTimeout(tmr)
		a[0]=$action
		a[1]=userName
		a[2]=lineNum
		a[3]=message
		a[4]=hash
		a[5]=pollSeconds
		a[6]=field[3].value
		a[7]=field[4].value
		if(a[0]&&!a[1])return
		if(!a[6]){wC.innerHTML="Error: Chatroom has no name.";return}
		for(var i=0;i<a.length;i++)data+=('&x='+escape(a[i]))
		this.oA= new _clsAJAX({file:'_process_chat.asp',target:wS,onchange:this.ret,data:data.substr(1)})
		tmr=setTimeout(function(){new _call(0)},pollSeconds*1000)
		message=''
	}
	_call.prototype.ret=function($r){
		var oD=document.createElement('div'),o,h
		oD.innerHTML=$r.responseText
		for(var i=0;i<oD.childNodes.length;i++){
			o=oD.childNodes[i],h=o.innerHTML,id=o.id
			if(id=='c'){
				_addChat(h)
			}else if(id=='u'){
				_updateUsers(h)
			}else if(id=='l'){
				lineNum=h*1
			}else if(id=='n'){
				fusr.value=userName=h
			}else if(id=='h'){
				hash=h
			}else if(id=='t'){
				time=new Date(h)
			}else if(id=='x')alert(h)
		}
//		_getObject('xxx').innerHTML=new Date().toString()
	}
	function ani($o,$d){
		if(!$o.id)$o.id='r'+Math.random()
		this.id=$o.id
		this.top=$o.scrollTop
		this.duration=$d
		this.c=0
		this.a=[]
	}
	ani.prototype.poll=function(){
		this.c=(new Date()-this.base)/this.duration
		if(this.c>1)this.c=1
		this.fn()
		if(this.c==1)this.stop()
	}	
	ani.prototype._=function(){return document.getElementById(this.id)}
	ani.prototype.fn=function(){}
	ani.prototype.stop=function(){clearInterval(this.tmr);this.onend()}
	ani.prototype.onend=function(){}
	ani.prototype.go=function(){
		var self=this
		this.base=new Date()
		this.tmr=setInterval(function(){self.poll()},10)
	}

	//init chat
	new _call(0)

	return {
	}
}
_onLoad.add('_loadChatRooms()')

