/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
var markersArray = [];

function gmap_rendermap(target, stationpoints) {
    markersArray = [];
    var latlngs = [];
    var myOptions = {
        zoom: 8,
        //center: ctrlatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        streetViewControl: false
    };
    
    var latlng;
    var marker;
    var infowindow;
    var marker;
    markericons = {
        "start" : "http://www.google.com/intl/en_ALL/mapfiles/marker_green.png",
        "end" : "http://www.google.com/intl/en_ALL/mapfiles/marker.png",
        "mid" : new google.maps.MarkerImage(
                "/images/gmaps/midpt.png",
                new google.maps.Size(8,8),
                new google.maps.Point(0,0),
                new google.maps.Point(4,4)
                 ),
        "transfer" : "http://www.google.com/intl/en_ALL/mapfiles/marker_yellowT.png"
    }
    var bounds = new google.maps.LatLngBounds ();
    for(i in stationpoints){
        if(i == 0)
            $icon = markericons["start"];
        else if(i == stationpoints.length -1)
            $icon =  markericons["end"];
        else if(stationpoints[i].Transfer)
            $icon = markericons["transfer"];
        else
            $icon = markericons["mid"];
        latlng = new google.maps.LatLng(stationpoints[i].Latitude,stationpoints[i].Longitude);
        latlngs.push(latlng);
        bounds.extend(latlng);
        marker = new google.maps.Marker({
            position: latlng,
            title: stationpoints[i].Station,
            //animation: google.maps.Animation.DROP,
            icon : $icon,
        //map : GoogleMap
            html : stationpoints[i].Station + "<br />" + stationpoints[i].FriendlyTime
        });        
        stationpoints[i].gm_latlng = latlng;
        stationpoints[i].gm_marker = marker;
        
        infowindow = new google.maps.InfoWindow(
        {
            content : ""
        }
        );
        markersArray.push(marker);
        google.maps.event.addListener(markersArray[i], 'click', function() {
            infowindow.setContent(this.html);
            infowindow.open(GoogleMap,this);
            GoogleMap.setCenter(this.getPosition());
        });
    }
   
    var tripPath = new google.maps.Polyline({
        path : latlngs,
        strokeColor: "#0066ff",
        strokeOpacity: 0.75,
        strokeWeight: 2
    });
    
    GoogleMap = new google.maps.Map(target,
        myOptions);
   
    GoogleMap.fitBounds(bounds);
    for(i in markersArray)
        markersArray[i].setMap(GoogleMap);
    tripPath.setMap(GoogleMap);
}
