$(document).ready(function() {

	
	var from = getParameterByName('from');
	var llinit = getParameterByName('llinit');
	var ltinit = getParameterByName('ltinit');
	var nombreinit = getParameterByName('nombreinit');
	var imageinit = getParameterByName('imageinit');
	var llend = getParameterByName('llend');
	var ltend = getParameterByName('ltend');
	var nombreend = getParameterByName('nombreend');
	var imageend = getParameterByName('imageend');
	var returnpage = getParameterByName('returnpage');
	var idend = getParameterByName('idend');
	var idinit = getParameterByName('idinit');

	document.getElementById('aback').href=from+'?'+
		"nombreend="+nombreend+
		"&idinit="+idinit+
		"&nombreinit="+nombreinit+
		"&llinit="+llinit+
		"&ltinit="+ltinit+
		"&nombreinit="+nombreinit+
		"&imageinit="+imageinit+
		"&llend="+llend+
		"&ltend="+ltend+
		"&nombreend="+nombreend+
		"&imageend="+imageend+
		'&returnpage='+returnpage+'&idend='+idend+'&nombreend='+nombreend+'&idinit='+idinit+'&=nombreinit'+nombreinit;
		
		var map = null;
		
		findMyCurrentLocation();
		
		function addStationMarkers() {
			/*
			var lineas = new Array(Linea1,Linea2,Linea3,Linea4,Linea5,Linea6,Linea7,Linea8,Linea9,LineaA,LineaB,Linea12);
		
			for (var i = 0; i < lineas.length; i++) {
				for(j=0; j< lineas[i].estaciones.length; j++) {
					
					var currentSt = lineas[i].estaciones[j];
					
					var latlng2 = new google.maps.LatLng (currentSt.lt , currentSt.ll);
					
					//Create the Marker and Drop It
					new google.maps.Marker ({ 
						map : map, 
						animation : google.maps.Animation.DROP,
						position : latlng2,
						icon: './miniImages' + currentSt.image
					}); 
					
					
				}
			}
			*/
		}
		
		
		
		function findMyCurrentLocation(){
		
			if(from=="calculaResultMetro.html") {	
				var lineas = new Array(Linea1,Linea2,Linea3,Linea4,Linea5,Linea6,Linea7,Linea8,Linea9,LineaA,LineaB,Linea12);
			}else {
				var lineas = new Array(Linea1,Linea2,Linea3,Linea4N,Linea4S,Linea5);
			}
			
			var jsonstr = '{';
			
			for (var i = 0; i < lineas.length; i++) {
				for(j=0; j< lineas[i].estaciones.length; j++) {
					
					// agregando estaciones
					var currentSt = lineas[i].estaciones[j];
					if(j==0 && i== 0) {
						jsonstr += '"'+currentSt.id+'":{';
					} else {
						jsonstr += ',"'+currentSt.id+'":{';
					}
					
					// agregando estaciones colindantes a cada estación
					var dibuje = false;
					var numStAdded = 0;
					for(m=0; m < lineas[i].estaciones.length; m++) {
						var currentAuxSt = lineas[i].estaciones[m];
						
						if(currentSt.id != currentAuxSt.id) {
							var stA = parseInt(currentAuxSt.id);
							var stB = parseInt(currentSt.id);
							if((stB + 1) == stA || (stB - 1) == stA) { 
								if(dibuje) {
									jsonstr += ',';
								} 
							
								jsonstr += '"'+currentAuxSt.id+'":1';
								numStAdded++;
								dibuje = true;
							}
							
						}
					}
					
					// agregando transbordes a cada estación
					if(currentSt.transbordes != null) {
						for(n=0; n < currentSt.transbordes.length; n++) {
						
							var currentAuxSt = currentSt.transbordes[n];
							if(numStAdded > 0) {
								jsonstr += ',';
							}
							jsonstr += '"'+currentAuxSt.id+'":1';
						
						}
					}
					
					
					
					jsonstr += '}';
				}
			}
			jsonstr += '}';
			
			
			var map = JSON.parse(jsonstr);
			var graph = new Graph(map);
			var results = graph.findShortestPath(idinit, idend);
			console.log(results);
			
			
			showCurrentLocation(results);
		}
		
		function adjustMapMeasures(results) {
			var mapcanvas = document.getElementById('mapacanvas');
			mapcanvas.style.height = (window.innerHeight - document.getElementById('maptitle').clientHeight) + "px"
			mapcanvas.style.width = screen.width + "px";
		}
		
		function showCurrentLocation(results) {
			
			
			
			//Create the latlng object based on the GPS Position retrieved
			var latlng = new google.maps.LatLng (ltend,llend);	
			
			//Set Google Map options
			var options = { 
				zoom : 12, 
				mapTypeControl: true,
				mapTypeControlOptions: {
				style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
				},
				zoomControl: true,
				zoomControlOptions: {
				style: google.maps.ZoomControlStyle.SMALL
				}
			};
			
			var mapcanvas = document.getElementById('mapacanvas');
			adjustMapMeasures();
			
			map = new google.maps.Map (mapcanvas, options);
			
			
			
			
			
			
			if(from=="calculaResultMetro.html") {	
				var lineas = new Array(Linea1,Linea2,Linea3,Linea4,Linea5,Linea6,Linea7,Linea8,Linea9,LineaA,LineaB,Linea12);
			}else {
				var lineas = new Array(Linea1,Linea2,Linea3,Linea4N,Linea4S,Linea5);
			}	
	
	
			var stations = [];
	
			for (var i = 0; i < lineas.length; i++) {
				for(j=0; j< lineas[i].estaciones.length; j++) {
					var currentSt = lineas[i].estaciones[j];
					for (var m = 0; m < results.length; m++) {
						if(currentSt.id == results[m]) {
							stations.push(currentSt);
						}
					}
				}
			}


			//Ordeno la lista
			var stationsOrd = [];
			var currentSlot = 0;
			for (var m = 0; m < results.length; m++) {
				for (var i = 0; i < stations.length; i++) {
					if(results[m] == stations[i].id) {
						stationsOrd[currentSlot] = stations[i];
						currentSlot++;
					}
				}
			}

			var currentLine = "";
			var ruta = [];
			
			var bounds = new google.maps.LatLngBounds();
			
			for (var i = 0; i < stationsOrd.length; i++) {
				var cur = stationsOrd[i];
				/*
				//var latlng = new google.maps.LatLng (cur.lt,cur.ll);	
				console.log("Dibujando: " + cur.nombre);
				*/
				//Create the Marker and Drop It
				var marker = new google.maps.Marker ({ 
					map : map, 
					position : new google.maps.LatLng (cur.lt,cur.ll),
					icon: './miniImages' + cur.image
				}); 
				
				bounds.extend(marker.position);
				
				ruta.push(new google.maps.LatLng(cur.lt, cur.ll));
				
				currentLine = cur.linea;
			}
			
			map.fitBounds(bounds);
			
			new google.maps.Polyline({
				path: ruta,
				map: map,
				strokeColor: '#8A3C0B',
				strokeWeight: 4,
				strokeOpacity: 0.6,
				clickable: false
			});
			
			
			
			
		}
		function errorHandler(error){
		/*
			alert("Error mientras intentaba obtener posición. Código de error: " + error.code + ", Mensaje: " + error.message);
			location.href='estacionMetro.html?nombre='+nombre+'&imagen='+imagen+'&lt='+lt+'&ll='+ll+'&descripcion='+descripcion+'&ln='+lineNumber
		*/
		}
	
	
	
	
	
	
	
	
	
	
	
	$( window ).resize(function() {
		adjustMapMeasures();
	});
});
