var stationIcon;
var line_array = new Array();
var station_array = new Array();
var gmap;

function load()
{
   if (GBrowserIsCompatible())
   {
      gmap = new GMap2(document.getElementById("map"));
      gmap.addControl(new GLargeMapControl());
      var mapControl = new GMapTypeControl();
      gmap.enableScrollWheelZoom();
      gmap.setCenter(new GLatLng(45.80750380796182, 15.991802215576172), 13);
      gmap.addControl(mapControl);
      stationIcon = new GIcon();
      stationIcon.iconSize = new GSize(20, 9);
      stationIcon.shadowSize = new GSize(0, 0);
      stationIcon.iconAnchor = new GPoint(6, 6);
      stationIcon.infoWindowAnchor = new GPoint(9, 2);
      stationIcon.infoShadowAnchor = new GPoint(6, 6);
      stationIcon.image = "gfx/icon_station.gif";
   }
}

function showLine(line, toggle)
{
   if (toggle)
   {
      station_array[line] = null;
      station_array[line] = new Array();
      for (var i = 0; i < _lines.length; i ++ )
      {
         var current_line = _lines[i];
         if (current_line.name == line)
         {
            var stations = current_line.stations;
            var poly = [];
            for (var j = 0; j < stations.length; j ++ )
            {
               var station = stations[j];
               var latlng  = new GLatLng(station.lat, station.lng);
               poly.push(latlng);
               var marker = new GMarker(latlng,
               {
                  icon : stationIcon
               }
               );
			   if (station.name != "")
			   {				
//				gmap.addOverlay(marker);
//				GEvent.addListener(marker, "click", function()
//				{
//				marker.openInfoWindowHtml(station.name);
//				}
//				);
				addStation(marker, station.name);
				station_array[line].push(marker);
			   }
            }
            var line_poly = new GPolyline(poly, current_line.color, 4, 0.9);
            gmap.addOverlay(line_poly);
            line_array[line] = line_poly;
         }
      }
   }
   else
   {
      gmap.removeOverlay(line_array[line]);
      for (var i = 0; i < station_array[line].length;
      i ++ )
      {
         gmap.removeOverlay(station_array[line][i]);
      }
   }
}

function addGeocode(lng, lat) {
	tb_remove();
	var point  = new GLatLng(lat, lng);
	var smart = "<div style='width: 110px; color: black; font-size: small; margin: 0 3px 2px 3px;'>"
	smart += "Long: " + lng +  "<br />" + "Lat: " + lat + "<br />";
	var myMarker = new GMarker(point);
	
	GEvent.addListener(myMarker, "mouseover", function() {
    gmap.openInfoWindowHtml(point, smart);
	  });
	  
	gmap.setCenter(point, gmap.getZoom());
	gmap.addOverlay(myMarker);
}

function addStation(marker, name) {
    gmap.addOverlay(marker);
    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(name);
    });
	//ne radi u Chrome
	GEvent.addListener(marker,"mouseover", function() {
        marker.openInfoWindowHtml(name);
    });    
	GEvent.addListener(marker,"mouseout", function() {
        marker.closeInfoWindow();
    });    

}

//funkcija uzima pocetnu i konačnu stanicu, vraća polje sa linijama koje treba ukljuciti
function setRoute(from, to) {
	var fromLines = new Array();
	var toLines = new Array();
	var tempLines = new Array();
	var m = 0, n = 0;
	for (var i = 0; i < _lines.length; i++ ) {
		var current_line = _lines[i];
		var stations = current_line.stations;
		for (var j = 0; j < stations.length; j++ ) {
			var station = stations[j].name;
			if(from == station) {
				fromLines[m] = current_line.name;
				m++; 
			}
			if(to == station) {
				toLines[n] = current_line.name;
				n++;
			}
		}
	}		
	for(var i=0;i<m;i++) {
		for(var j=0;j<n;j++) {
			if(fromLines[i] == toLines[j]) {
				tempLines[0] = fromLines[i];
				return tempLines;
			}
			for(var k=1;k<101;k++) { //
				if(stanice_polje[k][0] == fromLines[i]) { //stanice_polje predstavlja 1D polje sa oznakama sjecista - prvi clan ime linije
					for(var l=1;l<101;l++) { //
						if(stanice_polje[k][l] == "da") {
							if(stanice_polje[0][l] == toLines[j]) {
								tempLines[0]= fromLines[i];
								tempLines[1]= toLines[j];
								return tempLines; //
							}
						}
					}
				}
			}
		}
	}
	return "0";
}