﻿(function(){
	var demoinfo=[
		{
		'limit':1000,
		'mathfunc':function(x){return (1-Math.cos(x*Math.PI))/2;}
		},
		{
		'limit':1000,
		'mathfunc':function(x){return x;}
		},
		{
		'limit':1000,
		'mathfunc':function(x){return Math.pow(x,2);}
		},
		{
		'limit':3000,
		'mathfunc':function(x){return x+(Math.sin(5*2*x*Math.PI))/10;}
		},
		{
		'limit':3000,
		'mathfunc':function(x){return x*((1+Math.sin(3*3*x*Math.PI-Math.PI/2))/2);}
		},
		{
		'limit':5000,
		'mathfunc':function(x){return Math.random();}
		}
	];
	var w=window,d=w.document;
	var img=d.createElement('img');
	img.setAttribute('src','http://f-u.up.seesaa.net/2009/twicli_animatedemoimg.png');
	w.addEventListener('DOMContentLoaded',function(){
		for(var i=0,n=demoinfo.length;i<n;i++){
			var bNode=d.getElementById('animatebase'+(i+1));
			var p=d.createElement('p');
			var a=p.appendChild(d.createElement('a'));
			a.setAttribute('href','javascript:void(0)');
			(function(i,node){
				a.addEventListener('click',function(e){animatedemo(i,node);},false);
			})(i,bNode);
			a.textContent='アニメーションの動作を見る（デモ'+(i+1)+'）';
			bNode.parentNode.insertBefore(p,bNode.nextSibling);
		}
	},false);
	function animatedemo(i,node){
		var pNode=d.createElement('div');
		var style='text-align:center;overflow:hidden;';
		pNode.setAttribute('style',style);
		pNode.appendChild(img);
		node.parentNode.insertBefore(pNode,node);
		var limit=demoinfo[i]['limit'],mathfunc=demoinfo[i]['mathfunc'];
		(function animate(elem,max,start){
			var t=(new Date).getTime();
			if(start+limit<=t){
				setTimeout(function(){node.parentNode.removeChild(pNode);},1000);
				return elem.setAttribute('style',style+'max-height:none;');
			}
			elem.setAttribute('style',style+'max-height:'+Math.ceil(max*mathfunc((t-start)/limit))+'px;');
			setTimeout(function(){animate(elem,max,start)},20);
		})(pNode,pNode.clientHeight,(new Date).getTime());
	}
})();

