// JavaScript Document

function Clock(){
	this.halt=false;
	this.im_act=false;
	this.img;
	this.state;
	this.wrds=new Array();
	this.n=0;
	this.cPos=0;
	this.fx;
	this.get = new Request({
		method: 'get',
		url: '/lib/data/context.php',
		onSuccess: function(text) {if(eval(text))this.ldImg(text)}.bind(this), 
		headers: {'Content-type': 'application/x-www-form-urlencoded; charset=utf-8'}}
	)
}
Clock.prototype.pause=function(){
		this.halt=true;
		if(this.fx){
			if(!this.fx.ld)this.fx.pause();
		}
		this.wrds.each(function(item){				
			switch(item.state){
				case 'ani':
					$clear(item.ani);
				break;
				case 'fall':
					item.div.getElements('.lrr').each(function(item,index){
						$clear(item.f);
					});
				break;
			}
		});
		
}
Clock.prototype.resume=function(){
	this.halt=false;
	switch(this.state){
		case 'sh':
			this.shImg();
		break;
		case 'hd':
			this.hdImg();
		break;
		case 'ld':
			this.getImg();
		break;
	}
	if(this.fx){
		if(!this.fx.ld)this.fx.resume();
	}
	this.wrds.each(function(item){				
		switch(item.state){
			case 'ld':
				item.getTxt();
			break;
			case 'sh':
				item.shWrd();
			break;
			
			case 'ani':
				item.ani=item.type.periodical(40,item);
			break;
			case 'exp':
				item.explode();
			break;
			case 'fall':
				item.fall();
			break;
		}
	});
}

Clock.prototype.mkWrd=function (){
	var w=new Word(this,this.n);
	this.wrds.push(w);
	this.n++;
}

Clock.prototype.shImg=function(ob){
	if(!this.halt){
		this.fx.ld=false;
		var dir=((Math.round(Math.random()))? 'horizontal' : 'vertical');
		this.fx.hide(dir);
		this.fx.toggle(dir);
		this.state='';
	}else{
		this.state='sh';
	}
}
Clock.prototype.hdImg=function(){
	if(!this.halt){
		var dir=((Math.round(Math.random()))? 'horizontal' : 'vertical');
		this.fx.ld=false;
		this.fx.toggle(dir);
		this.state='';
	}else{
		this.state='hd';
	}
}
	
Clock.prototype.setFx=function(){
	this.fx=new Fx.Slide(this.img, {duration: 1200});	
	this.pr='pr';
	this.fx.addEvent('onComplete',function(){this.fxLd()}.bind(this));
	this.fx.ld=false;
}

Clock.prototype.fxLd=function(){
	this.fx.ld=true;
	if(this.im_act){
		$('context').dispose();
		this.im_act=false;
		this.wait=this.getImg.delay(4000,this);
	}else{
		this.im_act=true;
		this.wait=this.hdImg.delay(3500,this);
	}
}
Clock.prototype.getImg=function(){
	if(!this.halt){
		this.state='';
		this.get.send();
	}else{
		this.state='ld';
	}
}

Clock.prototype.ldImg=function(ob){
	var ob=eval(ob);
	var once=false;
	this.img=new Asset.image(ob.src, {
		onload: function(){
			if(once){
				return;
			}else{
				once=true;
				this.mkImg(ob.w,ob.h);
			}
		}.bind(this)
	});
}

Clock.prototype.mkImg=function(w,h){	
	var pos=this.randPos({'typ':'img','w':w,'h':h});
	pos.l=Math.round(pos.l/7)*7;
	pos.t=Math.round(pos.t/7)*7;
	var div=new Element('div', {
		'styles': {
			'position': 'absolute',
			'top':pos.t+'px',
			'left':pos.l+'px',
			'z-index':4000
		},'id':'context'
	});
	$('trueism').adopt(div);
	div.adopt(this.img);
	this.setFx();
	this.shImg();
}

Clock.prototype.randPos=function(ob){
	var wr=0;
	var w=getWinW();
	var h=getWinH();
	var wl=parseInt( $('wrap').getStyle('left'));
	var ww=parseInt($('wrap').getStyle('width'));
	var col1=$('content').getElement('.col1').getSize();
	var col2=$('content').getElement('.mrg0').getSize();
	/*$('a').setStyles({'width':wl-7+'px','height':h+'px'});
	$('b').setStyles({'left':wl+70,'width':ww-70+'px','height':85+'px'});
	$('c').setStyles({'left':wl+ww+'px','width':w-(wl+ww)+'px','height':h+'px'});
	$('d').setStyles({'left':wl-7+'px','width':col1.x+14+'px','top':106+col1.y,'height':h-(106+col1.y)+'px'});
	$('e').setStyles({'left':(wl+ww)-(col2.x+28)+'px','width':col2.x+28+'px','top':120+col2.y,'height':h-(106+col2.y)+'px'});*/
	if(ob.typ=='img'){
		if(ob.h<71){
			var s=3;
		}else{
			var s=Math.round(Math.random())+1;
		}
		this.cPos=s;
	}else{
		if(this.cPos){
			var arr=((this.cPos==1)? new Array(2,3) : ((this.cPos==2)? new Array(1,3): new Array(1,2)));
			var n=Math.round(Math.random());
			var s=arr[n];
		}else{
			var s=Math.round(Math.random()*2)+1;
		}
	}
	switch(s){
		case 1:	//left
			if(ob.w<wl){
				var t=Math.random()*(h-ob.h);
			}else{
				var d=120+col1.y;
				var t=((ob.h>(h-d))? d : 	d+(Math.random()*(h-(ob.h+d))));
			}
			if(d)wl+=col1.x+14;
			if((ob.w+7)<wl){
				var l=Math.random()*(wl-(ob.w+7));
			}else{
				if(ob.typ=='wrd'){
					wr=wl-14;
					l=7;
				}else{
					var l=wl-(ob.w+7);	
				}
			}
		break;
		case 2://right
			if(ob.w<w-(wl+ww)){
				var t=Math.random()*(h-ob.h);
			}else{
				var d=120+col2.y+28;
				var t=((ob.h>(h-d))? d : 	d+(Math.random()*(h-(ob.h+d))));
			}
			if(d)ww-=col2.x+28;
			if(ob.w>(w-(wl+ww))){
				var l=wl+ww;
				if(ob.typ=='wrd')wr=(w-(wl+ww))-14;
			}else{
				var l=(wl+ww)+(Math.random()*(w-(wl+ww+ob.w)));
			}
		break;
		case 3://top
			var t=Math.random()*(71-ob.h);
			var l=wl+Math.random()*(ww-(70+ob.w));
		break;
	}
	//alert(this.cPos);
	return {'l':Math.round(l), 't':Math.round(t),'w':wr};	
}

function Word(parent,nr){
	this.pr=parent;
	this.nr=nr;
	this.txt;
	this.state='';
	this.l=-1;
	this.n=0;
	this.ani;
	this.get = new Request({
		method: 'get',
		url: '/lib/data/ism.php',
		onSuccess: function(text) {
			this.txt=text;
			$('buff').set('text',text);
			var dim=$('buff').getSize();
			var pos=this.pr.randPos({typ:'wrd',w:dim.x, h:dim.y});
			//alert(pos.w);
			this.div=new Element('div', {
				'styles': {
					'position': 'absolute',
					'top':pos.t+'px',
					'left':pos.l+'px',
					'width':((pos.w)? (pos.w+14)+'px' : dim.x+'px')
				}
			});
			$('trueism').adopt(this.div);
			this.shWrd();
		}.bind(this), 
		headers: {'Content-type': 'application/x-www-form-urlencoded; charset=utf-8'}}
	)
	this.getTxt();
}

Word.prototype.shWrd=function(txt){
	/*this.ani=this.type.periodical(40,this);*/
	if(!this.pr.halt){
		this.state='ani';
		this.ani=this.type.periodical(40,this);
	}else{
		this.state='sh';
	}
}

Word.prototype.getTxt=function(){
	if(!this.pr.halt){
		this.state='';
		this.get.send();
	}else{
		this.state='ld';
	}

}

Word.prototype.type=function(){
	if(++this.n<=this.txt.length){
		this.div.appendText(String(this.txt).charAt(this.n));//setText(String(this.txt).substr(0,));
	}else{
		this.state='';
		$clear(this.ani);
		this.explode.delay(4000,this);
	}
}

Word.prototype.explode=function(){
	if(!this.pr.halt){
		this.state='';
		this.div.set('text','');
		this.txt.split(" ").each(function(item,index){
			var w=new Element('span', {'class':'lrr c'+Math.round(Math.random()*2), 'styles':{'margin':0,'padding':0}});
			w.set('html',item+'&nbsp;');
			$(this.div).grab(w);
			//this.l++;
		}.bind(this));
		var off=$(this.div).getStyles('left','top');
		var dim=new Array();
		this.div.getElements('.lrr').each(function(item,index){
			dim.push(item.getCoordinates());
		});
		this.div.getElements('.lrr').each(function(item,index){
			item.pr=this;									 
			item.setStyles({position:'absolute', left:(dim[index].left-parseInt(off.left))+'px', top: (dim[index].top-parseInt(off.top))+'px', width:dim[index].width+'px'});
			item.i=0;
			var l=item.get('text').length;
			dir=Math.round(Math.random());
			item.V=(20-l)* ((dir)? 1 : -1);
			item.X=dim[index].left-parseInt(off.left);
			item.Y=dim[index].top-parseInt(off.top);
			item.off=dim[index].top+30;
			item.angle=-60 * 0.0174532925 * ((dir)? 1 : -1);
			item.g=Math.round(Math.random()*(((l+4)*30)+1))/100.0;
			item.fall=function(){
				var y=this.V*Math.sin(this.angle)*this.i + (this.g*Math.pow(this.i,2)) +this.Y;
				if((y+this.Y)<(getWinH()-item.off)){
					var x=this.V*(Math.cos(this.angle))*(this.i) + this.X;
					var op=1-(Math.pow(y,2)/Math.pow((getWinH()-item.off),2));
					this.setStyles({'left':x+'px','top':y+'px', 'opacity':op});
					this.i+=0.6;
				}else{
					this.setStyle('opacity',0);
					$clear(this.f); 
					if(this.pr.l){
						this.pr.l--;
					}else{
						this.pr.cleanup();
						this.getParent().dispose();
					};                                
				}
			}
		}.bind(this));
		this.state="fall";
		this.fall();
	}else{
		this.state="exp";
	}
}
Word.prototype.fall=function(){
		this.l=-1;
		this.div.getElements('.lrr').each(function(item,index){
			item.f=item.fall.periodical(40,item);	
			this.l++;
		}.bind(this));
}

Word.prototype.cleanup=function(){
	this.state='';
	var tmp=new Array();
	this.pr.wrds.each(function(item){
		if(this.nr!=item.nr){
			tmp.push(item);
		}
	}.bind(this));
	this.pr.wrds=tmp;
	this.pr.mkWrd.delay(1000,this.pr);
}

var clock=new Clock();
function iniContext(){
	clock.getImg();
	//clock.mkWrd();
	clock.mkWrd.delay(5000,clock);
	clock.mkWrd.delay((3*60000),clock);
	clock.mkWrd.delay((10*60000),clock);
}
