function go()
{
	//alert(items.length);
	for(var i=0;i<items.length;i++)
	{
		lines[i]=item_to_line(items[i]);
		//items.shift();	
		//alert(items.length);
	}
	//alert(lines.length);
	timer=setInterval('addaline(true)',scrollDelay);
	timer2=setTimeout('update()',updateDelay);
}


function item_to_line(item)
{
	var outputline='';
	//var image='';
	  outputline+='<span class=spy-story onmouseover="this.style.backgroundColor=\'#E8ECF4\';" onmouseout="this.style.backgroundColor=\'#fff\';">';
	  outputline+='<span class=Dvrow>';
		outputline+='<span class=DvIcon>';
		outputline+='<img src="'+item.forecastSummaryImage+'" border=0 alt="'+item.forecastSummary+'">';
		outputline+='</span>';
		outputline+='<span class=Dvtemp>'+item.forecastTemp+'<sup>o</sup></span>';
		outputline+='<span class=DvweatherDsep>';
		outputline+='<a onclick="showhideDesc('+item.forecastId+')" id="descMain_'+item.forecastId+'" style="cursor:pointer">'+item.forecastSummary+': '+(item.forecastDesc).substring(0,30)+'</a>';
		outputline+='<span style="display:none;" id="desc_'+item.forecastId+'" class="weatherTxt">'+item.forecastDesc+'</span>';
		outputline+='</span>';
		outputline+='<span class="DvWeather">';
		outputline+='<a href="../profile/viewProfile/profileName/'+item.forecastUser+'">'+item.forecastUser+'</a>';
		outputline+='</span>';
		outputline+='<span class="Dvcountry"><a class="Dvcountry" href="../search/allForecastTown?zip='+item.forecastZipcode+'">'+item.forecastCity+','+item.forecastState+'</span></a>';
		outputline+='<span class="Dvtime">'+item.forecastDate+'</span>';
		outputline+='</span>';
		outputline+='</span>';
		return outputline;
}
function dataReceived(xh)
{
	var items=new Array();
	isBusy=false;
	if(xh.responseText.length>5)
	{
		eval('items = ('+xh.responseText+')');		
		if(items.length)
		{			
			for(i=0;i<items.length;i++)
			{
				if(items[i].timestamp>timestamp)
				{
					timestamp=items[i].timestamp;
				}
				for(j=lines.length;j<lines.length+items.length;j++)
				{
					i=j-lines.length;
					var image='';
					lines[j]=item_to_line(items[i]);
					items.shift();
				}
				if(lines.length>0 && Math.round(updateDelay/(lines.length))>2000)
				{
					scrollDelay=Math.round(20000/(lines.length));
					if(!pause)
					{	
						clearInterval(timer);
						timer=setInterval('addaline(true)',scrollDelay);
					}
				}
				else
				{
					scrollDelay=2000;
					if(!pause)
					{
						clearInterval(timer);
						timer=setInterval('addaline(true)',scrollDelay);
					}
				}
			}
		}
	}
	if(!pause)
	{
		clearTimeout(timer2);
		timer2=setTimeout('update()',updateDelay);
	}
}

spyhttp=null;
function update()
{
	if(isBusy)
	{
		return;
	}
	isBusy=true;
	
	spyhttp=new Ajax.Request('default/spyForecast',{method:'get',parameters:"timestamp="+timestamp+"&maxitems=5",onSuccess:dataReceived});
	return false;
}

function addaline(showanim)
{
	var isIE=navigator.appName.indexOf('Microsoft')!=-1;
	var isSafari=navigator.userAgent.indexOf('Safari')!=-1;
	var isOpera=navigator.userAgent.indexOf('Opera')!=-1;
	d=new Date();
	
	if(lines.length)
	{
		if(isIE||isSafari||isOpera)
		{
			var newItem=lines.shift();
			if(newItem.length)
			{
				currentline++;
				
				var spytable=$('spy');
				var rows=spytable.getElementsByTagName('div');
				var numrows=rows.length;
				for(var i=numrows-1;i>0;i--)
				{
					rows[i].innerHTML=rows[i-1].innerHTML;
				}
				var item=$('active-row');
				if(isIE)
				{
					showanim=0;
				}
				if(showanim)
				{
					rows[1].style.display='none';
					rows[1].innerHTML=newItem;
					//$j(item).fadeIn();
				}
				else
				{
					rows[0].innerHTML=newItem;
				}			
			}
		}
		else
		{
			var newItem=document.createElement('div');
			newItem.setAttribute('class','spy-story');
			newItem.innerHTML=lines.shift();
			currentline++;
			var spytable=$('spy');
			var rows=spytable.getElementsByTagName('div');
			var numrows=rows.length;
			if(showanim)
			{
				oldItem=$('active-row');
				spytable.insertBefore(newItem,oldItem);
				//$j(newItem).fadeIn();
				newItem.setAttribute('id','active-row');
				if(oldItem)
				{
					oldItem.removeAttribute('id');
				}
				newItem.style.display='';
			}
			else
			{
				oldItem=$('active-row');
				spytable.insertBefore(newItem,oldItem);
				newItem.setAttribute('id','active-row');
				oldItem.setAttribute('id','');
			}
			spytable.removeChild(rows[rows.length-1]);
			for(i=0;i<5;i++)
			{
				labelnum=4-i;
				rows[rows.length-i-1].setAttribute('class','spy-story class-final-'+labelnum);
			}
		}
	}
}