function disegnaPendenza()
{
	var coord = [];
	for (var i=0; i< phptracks.length; i++)
	{
		if (phptracks[i].Segment.profile)
		{
			var vlf= phptracks[i].Segment.profile.evalJSON();
			coord = coord.concat(vlf[0]);
		}
	}

	if (!coord)
	{
		return;
	}
// 	var coord = profilo.evalJSON()[0];
// 	var a =	profilo[0].evalJSON();
// 	var coord = a.inject([], function(array, value)
// 		{	
// // 			array.push(value);
// 			if (value)
// 			{
// 				array = array.concat(value);
// 			}
//   			return array;
// 		}
// 	);

	//trasformo in distanze (km) le coordinate e faccio grafico con coppia distanza/altitudine
	var grafico = [];
	var distanza = 0;
// 	var precedente = new OpenLayers.Geometry.Point(coord[0].long, coord[0].lat).transform(map.displayProjection, map.projection);
	var precedente = new OpenLayers.Geometry.Point(coord[0].long, coord[0].lat);
	var altitudine=0;
	for (var i=0;i<coord.length;i++)
	{	
		if (coord[i])
		{
// 			var punto =  new OpenLayers.Geometry.Point(coord[i].long, coord[i].lat).transform(map.displayProjection, map.projection);
			var punto =  new OpenLayers.Geometry.Point(coord[i].long, coord[i].lat);
			var linea = new OpenLayers.Geometry.LineString([precedente, punto]);
			//distanza += punto.distanceTo(precedente)/1000;	//da metri a km
			//distanza[i] = punto.distanceTo(precedente)/1000 + ((i>0)?distanza[i-1]:0) ;	//da metri a km
// 			distanza[i] = linea.getGeodesicLength(new OpenLayers.Projection("EPSG:900913"))/1000+ ((i>0)?distanza[i-1]:0);
// 			distanza = linea.getGeodesicLength(new OpenLayers.Projection("EPSG:900913"))/1000+ ((i>0)?grafico[i-1][1]:0);		
			distanza = linea.getGeodesicLength()/1000 + ((i>0)?grafico[i-1][0]:0);		
			coord[i].alt=parseInt((coord[i].alt) ? coord[i].alt : -9999);	//da stringa a int
			if (coord[i].alt!=-9999)
			{
				altitudine =coord[i].alt;		//usa quella precedente o 0 se e' il primo punto
			} else
			{
				altitudine = (i>0)?coord[i-1].alt:0;
				coord[i].alt = altitudine;	
			}
			grafico.push([distanza, parseInt(altitudine)]);
			precedente = punto;
		} else {
			grafico.push(grafico[i-1]);
		}
	}	

	chart = Flotr.draw($('pendenza'),[grafico]);
	//Element.hide('spinning_ajax');
return;
	//riempie campi con alcuni dati
	document.getElementById('p_puntiTraccia').value=lats.length;
	var amax = altitudini.max();
	document.getElementById('p_altezzaMax').value = amax;
	var myObj={'estremo': amax};
	var y = altitudini.detect(
		function(item, index) 
		{	
			if (item==this.estremo) 
			{	
				myObj.indice=index;	
				return true;
			} else {
				return false;
			}
		}, myObj
	);
	document.getElementById('p_altezzaMaxKm').value = Math.round(distanza[myObj.indice]);
	var amin = altitudini.min();
	document.getElementById('p_altezzaMin').value=amin;
	myObj={'estremo': amin};
	y = altitudini.detect(
		function(item, index) 
		{	
			if (item==this.estremo) 
			{	
				myObj.indice=index;	
				return true;
			} else {
				return false;
			}
		}, myObj
	);
	document.getElementById('p_altezzaMinKm').value = Math.round(distanza[myObj.indice]);
	
// Array.max = function( array ){
//     return Math.max.apply( Math, array );
// };
// 
// Array.min = function( array ){
//     return Math.min.apply( Math, array );
// };
}

//funzione ricorsiva per trasformare da threaded di cake a formato che serve a TafelTree
//radio: 0=check, 1=radio, 2=check grigio, 3=radio grigio 
function toTafel(obj, radio)
{
	var ret=[];
	for(var i=0; i<obj.length; i++)
	{
		var items={};
		var stato=0;
		if (obj[i].Category.one_child_checked!=0)
		{
			stato=1;
		}
		if (!Object.isUndefined(radio))
		{	
			if (radio>0) //(radio==1 || radio==3)
			{
				stato += 2;
			} 
		}
		if (obj[i].children.length>0)
		{
			items=toTafel(obj[i].children, stato);
		}
		var tmp={
			'id': obj[i].Category.id,
			'txt': obj[i].Category.name + (obj[i].Category.one_child_checked!=0 ? ' (*)' : ''),
			'one_child_checked': obj[i].Category.one_child_checked
		};
/*		if (obj[i].Category.one_child_checked!=0)
		{
			tmp.img='lock.gif';
			tmp.imgopen='lock.gif';
		}*/
		tmp.radio=false;
		if (!Object.isUndefined(radio))
		{
			if (radio==0)
			{
				tmp.img=TFBI_UNCHECKED;
				tmp.imgopen=TFBI_UNCHECKED;
			} else if (radio==1) {
				tmp.img=TFBI_UNSELECTED;
				tmp.imgopen=TFBI_UNSELECTED;
				tmp.radio=true;
			} else if (radio==2) {
				tmp.img=TFBI_UNCHECKABLE;
				tmp.imgopen=TFBI_UNCHECKABLE;
			} else {
				tmp.img=TFBI_UNSELECTABLE;
				tmp.imgopen=TFBI_UNSELECTABLE;
				tmp.radio=true;
			}
		}
		if (!Object.isUndefined(items))
		{	
			tmp.items=items;
		}
		ret.push(tmp);
	}
	return ret;
}

function toTafelLevel(obj)
{
	var ret=[];
	for(var i=0; i<obj.length; i++)
	{
		var tmp={
			'id': obj[i][0].id,
			'txt': obj[i][0].txt 
		};
		var items=[];
		if (!Object.isUndefined(obj[i][0].items))
		{		
			for(var j=0; j<obj[i][0].items.length; j++)
			{
				items.push({'id': obj[i][0].items[j].id, 'txt':  obj[i][0].items[j].txt});
			}
		}
		if (!Object.isUndefined(items))
		{	
			tmp.items=items;
		}
		ret.push(tmp);
	}
	rtn = [{	
			'id': 'root',
			'txt': 'livelli',
			'items': ret 
	}];

	return rtn;
}

function crea_swf()
{
	var id=0;
	$$('a.filmato').each( function(ele)
	{
		ele.id = 'cmdswf_' + id++;
		$(ele).update('');
		flowplayer(ele.id, base + "/swf/flowplayer-3.2.5.swf",
		{
			clip: {
				autoPlay: false,
				autoBuffering: false,
				bufferLength: 30,
				scaling: 'scale'
			}/*,
		// play button does not make sense for swf, therefore disable it
		play: null*/
		}
		);
	});
	return;
}
 
 function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

