/**
 * @author Ambrosio
 */
function leerEntradas(json){
	
	var columns = [];
	var divd = document.getElementById('lugares');
	for (var j in json.feed.entry[0]) {
		var prefix = j.split('$')[0];
		var column = j.split('$')[1];
		if (prefix == 'gsx') {
			columns.push(column);
		}
	}	
  	var lista = []
	var latlng = new google.maps.LatLng(-33.45770976322056, -70.6620642542839);
	var myOptions = {
		zoom: 17,
		center: latlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		streetViewControl: true
	};
	map = new google.maps.Map(document.getElementById("elmapa"), myOptions);
	
	picada = [];
	var latitudlongitud = [];
	globito = [];
	var contenido = [];
	
	for (i = 0; i < json.feed.entry.length; i++) {
		var entrada = json.feed.entry[i];
 
		 contenido[i] = '<div class = "burbuja" style="width:200px;"><h4>' + entrada.title.$t + '</h4>';
		 if(entrada.gsx$imagen.$t != ""){
		 contenido[i] = contenido[i] + '<div style="width:75px; text-align:left; float:right;"><img src="' + entrada.gsx$imagen.$t + '" title="' + entrada.gsx$tipo.$t + '" style="width:100%;" /></div>'
		 }
		 contenido[i] = contenido[i] + '<p>' + entrada.gsx$descripcion.$t + '</p><table><thead><tr><th>Item</th><th>Precio</th></tr></thead><tbody>';
 
	for (var t=8; t < columns.length; t++){
		if (entrada["gsx$" + columns[t]].$t!= "")
		 	contenido[i] = contenido[i] + '<tr><td>' + columns[t] + '</td><td>' + entrada["gsx$" + columns[t]].$t + '</td></tr>';
		 }
		contenido[i] = contenido[i] + '</tbody></table><br/><p>' +  entrada.gsx$direccion.$t + '<br/><a href="' + entrada.gsx$url.$t + '" title="' + entrada.gsx$descripcion.$t + '">' + entrada.gsx$url.$t + '</a></p></div>';
 
		globito[i] = new google.maps.InfoWindow({
			content: contenido[i]
		});
		
		latitudlongitud[i] = new google.maps.LatLng(parseFloat(entrada.gsx$latitud.$t), parseFloat(entrada.gsx$longitud.$t));
		
		picada[i] = new google.maps.Marker({
			position: latitudlongitud[i],
			map: map,
			title: entrada.title.$t
		});
		asociarGlobitos(picada[i],i);
		
		lista[i] = document.createElement('a');
 		lista[i].appendChild(document.createTextNode(entrada.title.$t));
		lista[i].setAttribute("href","javascript:abrirGlobito(picada[" + i + "]," + i + ")");
		lista[i].setAttribute("title",entrada.gsx$descripcion.$t);
		divd.appendChild(document.createElement('br'));
		divd.appendChild(lista[i]);
	}
	divd.appendChild(document.createElement('br'));
	divd.appendChild(document.createElement('br'));
	var aportar = document.createElement('p');
	aportar.appendChild(document.createTextNode('Creemos en la colaboración, así que compartimos nuestras fuentes por si quieres colaborar con este proyecto o usarlas para tus propios fines:'));
	divd.appendChild(aportar);
	var help = document.createElement('a');
	help.appendChild(document.createTextNode('¿Como colaborar?'));
	help.setAttribute("title",'Archivo de Ayuda');
	help.setAttribute("href","http://docs.google.com/document/pub?id=1tJVhtFUWO35N5r-QtM2ZJAQFWuPiYdeF2LaiQQR7uKU");
	divd.appendChild(help);
	divd.appendChild(document.createElement('br'));
	var base = document.createElement('a');
	base.appendChild(document.createTextNode('Lista de Pecios'));
	base.setAttribute("href","https://spreadsheets0.google.com/ccc?key=tAYAnNZqr42C3Rgrpt4mGAQ&hl=es#gid=0");
	base.setAttribute("title",'Puedes colaborar poniendo más picadas, tambien te puede servir para buscar los mejores precios cerca de tu facultad');
	divd.appendChild(base);
	divd.appendChild(document.createElement('br'));
	var htmlfuente = document.createElement('a');
	htmlfuente.appendChild(document.createTextNode('FUENTE HTML'));
	htmlfuente.setAttribute("title",'EL Codigo HTML usado, puedes ayudar a mejorarlo o usarlo para lo que quieras');
	divd.appendChild(htmlfuente);
	divd.appendChild(document.createElement('br'));
	var jsfuente = document.createElement('a');
	jsfuente.appendChild(document.createTextNode('FUENTE JAVASCRIPT'));
	jsfuente.setAttribute("title",'EL JAVASCRIPT usado, puedes ayudar a mejorarlo o usarlo para lo que quieras');
	divd.appendChild(jsfuente);
	divd.appendChild(document.createElement('br'));
	var cssfuente = document.createElement('a');
	cssfuente.appendChild(document.createTextNode('FUENTE CSS'));
	cssfuente.setAttribute("title",'EL CSS usado, cortesía de Daniel Beth y el proyecto Coordinachile. Puedes ayudar a mejorarlo o usarlo para lo que quieras');
	divd.appendChild(cssfuente);
}
 
function asociarGlobitos (marker,number){
    // so marker is associated with the closure created for the listenMarker function call
    google.maps.event.addListener(marker, 'click', function() {
                        globito[number].open(map, marker);
                    });
}
 
function abrirGlobito (marker,number){
	globito[number].open(map, marker);
 }
 
function desplegarPicadas(){
	if (document.getElementById('elmapa')) {
		quitarOldJSONScriptNodes();
		
		// Show a "Loading..." indicator.
		var div = document.getElementById('elmapa');
		var p = document.createElement('p');
		p.appendChild(document.createTextNode('Cargando...'));
		div.appendChild(p);
		
		// Retrieve the JSON feed.
		var script = document.createElement('script');
		
		script.setAttribute('src', 'http://spreadsheets.google.com/feeds/list/0AuIaB2XKj-ZkdEFZQW5OWnFyNDJDM1JncnB0NG1HQVE/od6/public/values?alt=json-in-script&callback=leerEntradas');
		
		script.setAttribute('id', 'jsonScript');
		script.setAttribute('type', 'text/javascript');
		document.documentElement.firstChild.appendChild(script);
	}
	
	/**
	 * Removes the script element from the previous result.
	 */
	function quitarOldJSONScriptNodes(){
		var jsonScript = document.getElementById('jsonScript');
		if (jsonScript) {
			jsonScript.parentNode.removeChild(jsonScript);
		}
	}
}