/* menu */
var divMenu=null;
var ulMenu=null;
var menuTimer=null;

function menuInit() {
	divMenu=document.getElementById('menu');
	ulMenu=divMenu.getElementsByTagName('UL')[0];
	var s=ulMenu.getElementsByTagName('A');
	for(i=0;s[i];i++) {
		if(s[i].parentNode.parentNode==ulMenu) s[i].onmouseover=function() { k_menuSub(this); };
		else {
			s[i].onmouseover=function() { k_menuSub(this.parentNode.parentNode); };
			}
		s[i].onmouseout=k_menuNascondi;
		}
	k_menuNascondiTutto();
	}
function k_menuSub(sm) {
	if(menuTimer) clearTimeout(menuTimer);
	k_menuNascondiTutto();
	if(sm.parentNode.getElementsByTagName('UL')[0]) sm.parentNode.getElementsByTagName('UL')[0].style.display='block';
	}
function k_menuNascondiTutto() {
	var s=ulMenu.childNodes;
	for(i=0;s[i];i++) {
		if(s[i].nodeName=='LI') {
			if(s[i].getElementsByTagName('UL')[0]) s[i].getElementsByTagName('UL')[0].style.display='none';
			}
		}
	}
function k_menuNascondi() {
	menuTimer=setTimeout("k_menuNascondiTutto()",200);
	}

/* divisione dei testi */
k_divideEtImpera=function() {
	var offsetX=0;
	var spacing=40;
	var width=450;
	var train=null;
	var wagon=null;
	var timer=null;
	var actualStep=0;
	var easeBkup=0;
	var paging=null;
	var selectedItem=0;

	this.setContainer=function(c) {
		train=document.getElementById(c);
		wagon=train.getElementsByTagName('DIV')[0];
		}
	this.init=function() {
		width=train.offsetWidth;
		/* pulizia */
		for(var i=0;wagon.childNodes[i];i++) {
			if(wagon.childNodes[i].tagName!='DIV') {
				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);
			}
		/* voltapagina */
		if(wagon.childNodes.length>1) {
			paging=document.createElement('DIV');
			paging.className="paging";
			var bullet=null;
			for(var i=0;wagon.childNodes[i];i++) {
				bullet=document.createElement('IMG');
				bullet.setAttribute("src",TEMPLATEDIR+"img/bulletGrey.gif");
				bullet.onclick=this.moveNext;
				paging.appendChild(bullet);
				}
			var arrow=document.createElement('IMG');
			arrow.setAttribute("src",TEMPLATEDIR+"img/smallarrowR.gif");
			arrow.onclick=this.moveNext;
			paging.appendChild(arrow);
			train.parentNode.parentNode.appendChild(paging);
			paging.getElementsByTagName('IMG')[selectedItem].setAttribute("src",TEMPLATEDIR+"img/bulletRed.gif");
			}
		}
	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';
			actualStep=0;
			easeBkup=0;
			clearInterval(timer);
			if(++selectedItem>=wagon.childNodes.length) selectedItem=0;
			for(var i=0;paging.getElementsByTagName('IMG')[i];i++) {
				var obj=paging.getElementsByTagName('IMG')[i];
				if(i<wagon.childNodes.length) obj.setAttribute("src",TEMPLATEDIR+"img/bulletGrey.gif");
				}
			paging.getElementsByTagName('IMG')[selectedItem].setAttribute("src",TEMPLATEDIR+"img/bulletRed.gif");
			}
		}
	this.moveNext=function() {
		if(timer) clearInterval(timer);
		timer=setInterval(move,20);
		}
	}
