$(document).ready(function() {
	var needed = getParameterByName('needed');
	var returnpage = getParameterByName('returnpage');
	var idend = getParameterByName('idend');
	var nombreend = getParameterByName('nombreend');
	var idinit = getParameterByName('idinit');
	var nombreinit = getParameterByName('nombreinit');
	
	
	
	document.getElementById('aback').href = returnpage+"?idinit="+idinit+"&nombreinit="+nombreinit+"&idend="+idend+"&nombreend="+nombreend;
	
	
	
	function drawAutocomplete(results) {
		var stationsList = "";
		
		for (var i = 0; i < results.length; i++) {
			var cur = results[i];
			
			stationsList += "<li>"+
							((needed=="init")?"<a class=\"acontainer\" href=\""+returnpage+"?idinit="+cur.id+"&nombreinit="+cur.nombre+"&idend="+idend+"&nombreend="+nombreend+"\">&nbsp;</a>" : "<a  class=\"acontainer\" href=\""+returnpage+"?idinit="+idinit+"&nombreinit="+nombreinit+"&idend="+cur.id+"&nombreend="+cur.nombre+"\">&nbsp;</a>") + 
							"<aside class=\"pack-end\">" +
							"  <img alt=\"placeholder\" src=\"."+cur.image+"\">" +
							"</aside>" +
							"<a href=\"#\">" +
							"  <p class=\"bl\">"+cur.nombre+"</p>" +
							"  <p class=\"bl\">Línea "+cur.linea+"</p>" +
							"</a>" +
						  "</li>";
			
		}
		

		document.getElementById('loadLinesHere').innerHTML = stationsList;
	}
	
	
	function find(stationNeeded, keepSpace) {
		
		if(stationNeeded.trim() != "" || keepSpace){
			var lineas = new Array(Linea1,Linea2,Linea3,Linea4,Linea5,Linea6,Linea7,Linea8,Linea9,LineaA,LineaB,Linea12);
			var results = [];
			for (var i = 0; i < lineas.length; i++) {
				for(j=0; j< lineas[i].estaciones.length; j++) {
				
					var currentSt = lineas[i].estaciones[j];
				
					if(currentSt.nombre.toLowerCase().contains(stationNeeded.toLowerCase())) {
						results.push(currentSt);
					}
				}
			
			
				drawAutocomplete(results);
			}
		}
	}
	
	
	
	
	find("",true);
	
	
	$('#searchtxt').keyup(function() {
		find(this.value,false);
	});
	
	
});
