window.addEvent('domready', function(){
	var DateHelper = {
		  time_ago_in_words_with_parsing: function(from) {
		    var date = new Date;
		    date.setTime(Date.parse(from));
		    return this.time_ago_in_words(date);
		  },

		  time_ago_in_words: function(from) {
		    return this.distance_of_time_in_words(new Date, from);
		  },
		  distance_of_time_in_words: function(to, from) {
			var minuto=(from.getMinutes().toString().length<2) ? '0'+from.getMinutes() : from.getMinutes();
		    var distance_in_seconds = ((to - from) / 1000);
		    var distance_in_minutes = (distance_in_seconds/60).floor();
		    if (distance_in_minutes == 0) { return 'hace menos de un minuto'; };
		    if (distance_in_minutes == 1) { return 'hace un minuto'; };
		    if (distance_in_minutes < 45) { return 'hace '+distance_in_minutes + ' minutos'; };
		    if (distance_in_minutes < 90) { return 'hace una hora'; };
		    if (distance_in_minutes < 1440) { return 'hace casi ' + (distance_in_minutes / 60).floor() + ' horas'; };
		    if (distance_in_minutes < 2880) { return 'ayer a las '+from.getHours()+':'+minuto+' '+(from.getHours()>=12 ? 'PM' : 'AM'); };
		    return from.getHours()+':'+minuto+' '+(from.getHours()>=12 ? 'PM  ' : 'AM  ')+from.getDate()+'/'+(from.getMonth()+1)+'/'+from.getFullYear();
		  }
	};
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("mapa"));
		map.setCenter(new GLatLng(24.0625, -102.677068), 5);
		map.setUIToDefault();
		map.enableContinuousZoom();
		map.disableDoubleClickZoom();
		map.setMapType(G_HYBRID_MAP);
	};

	var twitter_call=new Request.JSONP({
	  url: 'http://twitter.com/statuses/user_timeline/autoplusmexico.json?since_id=2214890773&count=100&max_id=2458851674',
	    onComplete: function(dss){
			totall=dss.length-3;
			hacer_dots(dss);
		}
	}).send();
	var pos_todos=[];
	hacer_punto=function(ap,msg,id,fecha,avatar){
		var ap=ap.split('?');
			ap=ap[1].split('&');
		var lat=ap[0].split('=')[1];
		var lon=ap[1].split('=')[1];
		var fecha=DateHelper.time_ago_in_words_with_parsing(fecha);
		window['pos'+id] = new GLatLng(lat,lon);
		window['marker'+id] = new GMarker(window['pos'+id],{title:fecha});
		map.addOverlay(window['marker'+id]);
		GEvent.addListener(window['marker'+id], "click", function() {
			map.openInfoWindowHtml(window['pos'+id], '<img src="'+avatar+'" style="float:left;margin-right:6px;border:2px solid #ddd" /><p style="font:14px arial,helvetica,sans-serif;color:#444">'+msg+'</p><span style="font:10px georgia;font-style:italic;color:#996249">'+fecha+'</span>',
			{maxWidth:400});
		});
		pos_todos.push({'id':id,'pos':window['pos'+id]});
		if(pos_todos.length==totall){
			hacer_ruta();
		};
	};
	sab_pos=function(id){
		var es='';
		pos_todos.filter(function(a){
			if(a.id==id){
				es=a.pos;
			};
		});
		return es;
	};
	hacer_ruta=function(){
		ids.each(function(o,i){
			if(i>0){
				var uno=sab_pos(ids[i-1]);
				var dos=sab_pos(ids[i]);
			};
			window['gp'+i] = new GPolyline([
			    uno,
			    dos
			], "#ff0000", 3);
			map.addOverlay(window['gp'+i]);
		});
	};
	ids=[];
	hacer_dots=function(datos,nuevo){
		datos.each(function(o,i){
			ids.push(o.id);
			var contenido=o.text;
			var ligas=contenido.split(" ");
			var msg='';
			ligas.each(function(oo){
				var liga=oo.substring(0,4);
				if (oo.contains("http://bit") || oo.contains('&lon') || oo.contains('24.2')){
					pos=oo;
				}else{
					if (liga=='http'){
						msg+='<a href="'+oo+'" class="ligas_twiter" >'+oo+'</a> ';
					}else{
						msg+=oo+' ';
					};
				};
			});
			if (o.id=='2274344549'){
				hacer_punto('http://www.jambitz.com/ruta/ruta.html?lat=29.0965&lon=-110.9320','<a href="http://twitpic.com/82qu8">http://twitpic.com/82qu8</a> '+msg,o.id,o.created_at,o.user.profile_image_url);
			}else if(o.id=='2281820429'){
				hacer_punto('http://www.jambitz.com/ruta/ruta.html?lat=28.281027&lon=-111.040106',msg,o.id,o.created_at,o.user.profile_image_url);
			}else if(o.id=='2283161755'){
				hacer_punto('http://www.jambitz.com/ruta/ruta.html?lat=27.963320&lon=-110.97805',msg,o.id,o.created_at,o.user.profile_image_url);
			}else if(o.id=='2298108125'){
				hacer_punto('http://www.jambitz.com/ruta/ruta.html?lat=27.4048&lon=-112.5295','<a href="http://twitpic.com/87er9">http://twitpic.com/87er9</a> - Ya casi llegando a El Viscaíno a ver el berrendo.',o.id,o.created_at,o.user.profile_image_url);
			}else if(o.id=='2298991329'){
				hacer_punto('http://www.jambitz.com/ruta/ruta.html?lat=27.4048&lon=-112.5295','<a href="http://twitpic.com/87m7d">http://twitpic.com/87m7d</a> - En la salina de Guerrero negro.',o.id,o.created_at,o.user.profile_image_url);
			}else if(o.id=='2290226002'){
				hacer_punto('http://www.jambitz.com/ruta/ruta.html?lat=27.80707&lon=-110.92367','En medio del océano',o.id,o.created_at,o.user.profile_image_url);
			}else if(o.id=="2295403638"){
				ids.erase(o.id);
			}else if(o.id=="2315638056"){
				hacer_punto('http://www.jambitz.com/ruta/ruta.html?lat=24.2742&lon=-110.3273','<a href="http://twitpic.com/8b3p4">http://twitpic.com/8b3p4</a> - Vaaamonos. El ferry tiene capacidad para 120 autos y hasta 960 personas, todas fan de la Ruta Eco.',o.id,o.created_at,o.user.profile_image_url);
			}else if(o.id=="2314105600"){
				hacer_punto('http://www.jambitz.com/ruta/ruta.html?lat=24.2546&lon=-110.3150','<a href="http://twitpic.com/8aqw9">http://twitpic.com/8aqw9</a> - Antes de salir la visita a la playa era obligada. ¡El agua esta tibia!',o.id,o.created_at,o.user.profile_image_url);
			}else if(o.id=="2313832194"){
				hacer_punto('http://www.jambitz.com/ruta/ruta.html?lat=24.2783&lon=-110.3308','<a href="http://twitpic.com/8aon8">http://twitpic.com/8aon8</a> - Pichilingue es de donde sale el ferry, nos sorprendió lo cristalino del mar a pesar de ser una zona industrial.',o.id,o.created_at,o.user.profile_image_url);
			}else if(o.id=="2313177217"){
				hacer_punto('http://www.jambitz.com/ruta/ruta.html?lat=24.1150&lon=-110.3452','<a href="http://twitpic.com/8ajmb">http://twitpic.com/8ajmb</a> - Llegando a La Paz, un buen desayuno y al Ferry. Hace de esos calores "sabrosos".',o.id,o.created_at,o.user.profile_image_url);
			}else if(o.id=="2311895501"){
				hacer_punto('http://www.jambitz.com/ruta/ruta.html?lat=25.8781&lon=-111.3423','<a href="http://twitpic.com/8aae7">http://twitpic.com/8aae7</a> - Loreto nos despidió con un amanecer cálido y sereno. Estamos a 360 km del ferry que nos llevará a Topolobampo.',o.id,o.created_at,o.user.profile_image_url);
			}else if(o.id=="2311819650"){
				hacer_punto('http://www.jambitz.com/ruta/ruta.html?lat=27.4146&lon=-113.2478','<a href="http://twitpic.com/8a9xa">http://twitpic.com/8a9xa</a> - Del Viscaíno vamos rumbo a Loreto, para luego salir rumbo al ferry de La Paz.',o.id,o.created_at,o.user.profile_image_url);
			}else if(o.id=="2311764137"){
				ids.erase(o.id);
			}else if(o.id=="2311708807"){
				hacer_punto('http://www.jambitz.com/ruta/ruta.html?lat=27.5572&lon=-113.9723','<a href="http://twitpic.com/8a979">http://twitpic.com/8a979</a> - El acceso al campamento La Choya es complicado, pero vale la pena ver al berrendo libre de caza.',o.id,o.created_at,o.user.profile_image_url);
			}else if(o.id=="2311666072"){
				hacer_punto('http://www.jambitz.com/ruta/ruta.html?lat=27.7497&lon=-114.012','<a href="http://twitpic.com/8a8wh">http://twitpic.com/8a8wh</a> - Aquí es la Laguna Ojo de Liebre, en la reserva del Viscaíno, en feb vienen las ballenas.',o.id,o.created_at,o.user.profile_image_url);
			}else if(o.id=="2387634124"){
				hacer_punto('http://www.jambitz.com/ruta/ruta.html?lat=18.8776&lon=-97.3866','<a href="http://twitpic.com/8r7ty">http://twitpic.com/8r7ty</a> - Vamos a comenzar la pruueba de la altura máxima.',o.id,o.created_at,o.user.profile_image_url);
			}else if(o.id=="2361290416"){
				hacer_punto('http://www.jambitz.com/ruta/ruta.html?lat=20.6590&lon=-103.3833','<a href="http://twitpic.com/8l0n4">http://twitpic.com/8l0n4</a> - Saliendo de GDL, después de 257 tortas ahogadas.',o.id,o.created_at,o.user.profile_image_url);
			}else if(o.id=="2386684911"){
				hacer_punto('http://www.jambitz.com/ruta/ruta.html?lat=19.2391&lon=-98.3841','<a href="http://twitpic.com/8r1vf">http://twitpic.com/8r1vf</a> - El Popocatépetl nos escolta al pasar por Puebla.',o.id,o.created_at,o.user.profile_image_url);
			}else if(o.id=="2377589474"){
				ids.erase(o.id);
			}else{
				Endpoint.resolve(pos, function(url) { 
						hacer_punto(url,msg,o.id,o.created_at,o.user.profile_image_url);
			 	});
			};
		});
	};
});