/* (c) Kalamun 2009 - GPL 3 */

k_Traingallery=function() {
	var offsetX=20;
	var spacing=34;
	var width=450;
	var train=null;
	var wagon=null;
	var timer=null;
	var actualStep=0;
	var easeBkup=0;

	this.setContainer=function(c) {
		train=document.getElementById(c);
		wagon=train.getElementsByTagName('UL')[0];
		}
	this.init=function() {
		/* pulizia */
		for(var i=0;wagon.childNodes[i];i++) {
			if(wagon.childNodes[i].tagName!='LI') {
				wagon.removeChild(wagon.childNodes[i]);
				i--;
				}
			}
		/* init dei vagoni */
		for(var i=0;wagon.childNodes[i];i++) {
			var obj=wagon.childNodes[i];
			obj.style.left=(offsetX+(width+spacing)*i)+'px';
			obj.setAttribute('trainstep',0);
			}
		timer=setInterval(move,20);
		}
	easeIn=function(value,totalSteps,actualStep,pwr) { 
		totalSteps=Math.max(totalSteps,actualStep,1);
		var step=Math.pow(((1/totalSteps)*actualStep),pwr)*(value);
		return Math.ceil(step);
		}
	easeOut=function(value,totalSteps,actualStep,pwr) { 
		totalSteps=Math.max(totalSteps,actualStep,1);
		var step=value-(Math.pow(((1/totalSteps)*(totalSteps-actualStep)),pwr)*(value));
		return Math.ceil(step);
		}
	easeInOut=function(value,totalSteps,actualStep,pwr) { 
		totalSteps=Math.max(totalSteps,actualStep,1);
		var p1=Math.ceil(totalSteps/2);
		var p2=totalSteps-p1;
		var p1a=Math.min(actualStep,p1);
		var p2a=actualStep-p1a;
		var step=Math.pow(((1/p1)*p1a),pwr)*(value/2);
		if(p2a>0) {
			step+=value/2-(Math.pow(((1/p2)*(p2-p2a)),pwr)*(value/2));
			}
		return Math.ceil(step);
		}
	move=function() {
		actualStep++;
		if(actualStep<=80) {
			var ease=easeInOut((width+spacing),80,actualStep,5);
			for(var i=0;wagon.childNodes[i];i++) {
				var obj=wagon.childNodes[i];
				obj.style.left=(parseInt(obj.style.left)-(ease-easeBkup))+'px';
				}
			easeBkup=ease;
			}
		else if(actualStep==81) {
			var obj=wagon.firstChild;
			wagon.appendChild(obj);
			obj.style.left=(offsetX+(width+spacing)*(wagon.childNodes.length-1))+'px';
			}
		else if(actualStep>300) {
			actualStep=0;
			easeBkup=0;
			}
		}
	}

function debug(v,m) {
	if(m==true) document.getElementById('debug').innerHTML+=v;
	else document.getElementById('debug').innerHTML=v;
	}
