var currentmap='';
var TotPic=0;
var LoadedPic=0;
var playing=1;
var PicN=0;
var DelayTime=750;

function SetMap(id, img, anim)
{
	if( currentmap != '' )
		document.getElementById(currentmap).className = 'mapok';

	el = document.getElementById(id);
	if( el.className == 'mapok' )
	{
		if( typeof(anim) == 'undefined' )
		{
			StopMap();
		    PicN = parseInt(el.title)-1;
		}
	    el.className = 'mapoksel';
	    currentmap = id;
		document.getElementById('imgmap').src = img;
	}
	else
	    currentmap = '';
	try{SetMapAlert(id)}catch(e){}
}

function MapLoaded()
{
	txt=document.getElementById('infotxt');
	if(txt)
	{
		if(++LoadedPic>=TotPic-8)
		{
			txt.innerHTML="";
			//if((typeof(TimerMap)=='undefined')||(TimerMap==null))
			//PlayM()
		}
		else
			txt.innerHTML="Mappe caricate: "+LoadedPic+" su "+TotPic;
	}
}

function LoadMaps(tot)
{
	ctl=document.getElementById('animctl');
	ctl.innerHTML='Animazione:&nbsp;<a id="pplay" href="javascript:PlayMap()">Play <img src="modules/meteomap/pplay.gif" alt="play" title="Play" border="0" /></a>&nbsp;&nbsp;<a id="pstop" href="javascript:StopMap()">Stop <img src="modules/meteomap/pstop.gif" alt="stop" title="Stop" border="0" /></a>&nbsp;&nbsp;<a id="pback" href="javascript:StepMap(-1)">Indietro <img src="modules/meteomap/pback.gif" alt="back" title="Indietro" border="0" /></a>&nbsp;&nbsp;<a id="pback" href="javascript:StepMap(1)">Avanti <img src="modules/meteomap/pforw.gif" alt="forward" title="Avanti" border="0" /></a>';
	TotPic=tot;
	Pic=new Array(TotPic);
	for(i=0;i<TotPic;i++)
	{
		aa = PicSrc[i].split('|');
		Pic[i]=new Image();
		Pic[i].src='i.php?m='+aa[0];
		Pic[i].name=aa[1];
		Pic[i].onload=MapLoaded;
	}
}
function PlayMap(tot)
{
	if((typeof(TimerMap)=='undefined')||(TimerMap==null))
		PlayM(tot);
	else
		alert("Gia' in esecuzione")
}

function PlayM(tot)
{
	if( TotPic == 0 )
	    LoadMaps(tot);
	else if( TotPic > 1 )
	{
		PicN = (PicN+1+TotPic) % TotPic;SetMap('map'+Pic[PicN].name, Pic[PicN].src, true);
		TimerMap = setTimeout('PlayM()', (PicN+1 == TotPic) ? (4 * DelayTime) : DelayTime);
		document.getElementById('infotxt').innerHTML = '';
	}
}

function StopMap()
{
	if(TotPic>1)
	{
		if((typeof(TimerMap)!='undefined')&&(TimerMap!=null))
		{
			clearTimeout(TimerMap);
			TimerMap=null;
			document.getElementById('pplay').href='javascript:PlayMap()';
		}
	}
}

function StepMap(x)
{
	if(TotPic > 1)
	{
		if((typeof(TimerMap)!='undefined')&&(TimerMap!=null))
		{
			clearTimeout(TimerMap);
			TimerMap = null;
		}
		PicN = (PicN + x + TotPic) % TotPic;SetMap('map'+Pic[PicN].name, Pic[PicN].src, true);
	}
}

function ChangeMap(url)
{
	m = currentmap.substr(3)

	if( isFinite(m) )
	{
		window.location = url + '/' + m
		return false
	}
	else
	    return true
}

