// Copyright 2005, ian stevens.  Some rights reserved.  Please ask permission before using.

// Creates a marker whose info window displays the given number
function CM_createMarker(point, html, icon) 
{
    var marker = new GMarker(point,icon);
    GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(html); });
    return marker;
}

function CM_getStationHtml(station)
{
    return "<div class=\"station\"><h3>" + station.replace( "|", "</h3><p>" ) + "</p></div>";
}

function CM_addLine(map, points, html, color, stationMarkers, stations, startIndex, icon)
{
    map.addOverlay( new GPolyline( points, color, 10 ) );
    for( var i = 0; i < html.length; i++, startIndex++ )
    {
        var marker = CM_createMarker( points[ i ], CM_getStationHtml( html[ i ] ), icon )
        map.addOverlay( marker );
        stationMarkers[ startIndex ] = marker; 
        var name = html[ i ].substring( 0, html[ i ].indexOf( "|" ) );
        stations[ startIndex ] = "<li id=\"" + name + "\" onclick=\"GEvent.trigger(CM_stationMarkers[" + startIndex + "],'click')\" style=\"background-color:" + color + "\">" + name  + "</li>";
    }

    return startIndex;
}

var CM_yongeLinePoints = [  new GPoint(-79.462871, 43.750241),
 new GPoint(-79.450225, 43.734072),
 new GPoint(-79.447005, 43.724790),
 new GPoint(-79.442936, 43.716210),
 new GPoint(-79.442247, 43.709193),
 new GPoint(-79.435165, 43.699085),
 new GPoint(-79.415245, 43.683991),
 new GPoint(-79.407303, 43.674944),
 new GPoint(-79.402128, 43.666873),
 new GPoint(-79.399655, 43.667400),
 new GPoint(-79.393116, 43.666662),
 new GPoint(-79.389655, 43.658496),
 new GPoint(-79.387971, 43.654750),
 new GPoint(-79.386737, 43.650370),
 new GPoint(-79.384991, 43.648227),
 new GPoint(-79.379466, 43.645671),
 new GPoint(-79.377886, 43.648804),
 new GPoint(-79.379103, 43.652336),
 new GPoint(-79.381110, 43.656371),
 new GPoint(-79.383150, 43.661170),
 new GPoint(-79.384325, 43.664672),
 new GPoint(-79.386525, 43.669998),
 new GPoint(-79.389467, 43.677214),
 new GPoint(-79.391836, 43.683008),
 new GPoint(-79.394044, 43.688217),
 new GPoint(-79.396646, 43.697851),
 new GPoint(-79.398077, 43.704686),
 new GPoint(-79.402126, 43.725369),
 new GPoint(-79.406339, 43.743221),
 new GPoint(-79.412960, 43.768987),
 new GPoint(-79.416208, 43.781990) 
];

var CM_bloorLinePoints = [  new GPoint(-79.537689, 43.638633),
 new GPoint(-79.521333, 43.645283),
 new GPoint(-79.511238, 43.647513),
 new GPoint(-79.495256, 43.649688),
 new GPoint(-79.484372, 43.649481),
 new GPoint(-79.476187, 43.651278),
 new GPoint(-79.464407, 43.653853),
 new GPoint(-79.459862, 43.654589),
 new GPoint(-79.451910, 43.656279),
 new GPoint(-79.442516, 43.658236),
 new GPoint(-79.434994, 43.660082),
 new GPoint(-79.425156, 43.661937),
 new GPoint(-79.418031, 43.663785),
 new GPoint(-79.410912, 43.665040),
 new GPoint(-79.402128, 43.666873),
 new GPoint(-79.399655, 43.667400),
 new GPoint(-79.389716, 43.669747),
 new GPoint(-79.376973, 43.672457),
 new GPoint(-79.369854, 43.672750),
 new GPoint(-79.358765, 43.676346),
 new GPoint(-79.352350, 43.677594),
 new GPoint(-79.344962, 43.679050),
 new GPoint(-79.337492, 43.680499),
 new GPoint(-79.332751, 43.681247),
 new GPoint(-79.323763, 43.683458),
 new GPoint(-79.312997, 43.685757),
 new GPoint(-79.301938, 43.688240),
 new GPoint(-79.289030, 43.693253),
 new GPoint(-79.283861, 43.711529),
 new GPoint(-79.264723, 43.732577) 
];

CM_rtLinePoints = [ 
 new GPoint(-79.252353, 43.775323),
 new GPoint(-79.259592, 43.775804),
 new GPoint(-79.271862, 43.770471),
 new GPoint(-79.277257, 43.766155),
 new GPoint(-79.270838, 43.750430),
 new GPoint(-79.264723, 43.732577) 
];

CM_sheppardLinePoints = [  new GPoint(-79.411016, 43.761353),
 new GPoint(-79.387321, 43.766957),
 new GPoint(-79.379512, 43.768421),
 new GPoint(-79.369908, 43.770518),
 new GPoint(-79.346947, 43.775246) 
];

CM_yongeLineHtml = ["Downsview|1035 Sheppard Ave West",
"Wilson|570 Wilson Ave",
"Yorkdale|Yorkdale Shopping Centre",
"Lawrence West|655 Lawrence Ave West",
"Glencairn|765 Glencairn Ave",
"Eglinton West|1300 Eglinton Ave West",
"St. Clair West|370 St Clair Ave West",
"Dupont|278 Dupont St",
"Spadina|371 Bloor St West",
"St George|323 Bloor St West",
"Museum|75 Queen's Park",
"Queen's Park|671 University Ave",
"St. Patrick|449 University Ave",
"Osgoode|260 University Ave",
"St. Andrew|147 University Ave",
"Union|55 Front St. West",
"King|70 Yonge St",
"Queen|171 Yonge St",
"Dundas|300 Yonge St",
"College|448 Yonge St",
"Wellesley|551 Yonge St",
"Bloor-Yonge|733 Yonge St",
"Rosedale|1009 Yonge St",
"Summerhill|1189 Yonge St",
"St Clair|1441 Yonge St",
"Davisville|1900 Yonge St",
"Eglinton|2190 Yonge St",
"Lawrence|3101 Yonge St",
"York Mills|4015 Yonge St",
"North York Centre|5152 Yonge St",
"Finch|5714 Yonge St" ];

CM_sheppardLineHtml = [ "Sheppard-Yonge|4800 Yonge St",
"Bayview|550 Sheppard Ave East",
"Bessarion|701 Sheppard Ave East",
"Leslie|1209 Sheppard Ave East",
"Don Mills|1700 Sheppard Ave East" ];

CM_bloorLineHtml = [ "Kipling|5247 Dundas St West",
"Islington|3286 Bloor St West",
"Royal York|3012 Bloor St West",
"Old Mill|2672 Bloor St West",
"Jane|2440 Bloor St West",
"Runnymede|2218 Bloor St West",
"High Park|1874 Bloor St West",
"Keele|1733 Bloor St West",
"Dundas West|1525 Bloor St West",
"Lansdowne|1287 Bloor St West",
"Dufferin|1126 Bloor St West",
"Ossington|883 Bloor St West",
"Christie|726 Bloor St West",
"Bathurst|565 Bloor St West",
"Spadina|371 Bloor St West",
"St George|323 Bloor St West",
"Bay|64 Bloor St West",
"Sherbourne|420 Bloor St East",
"Castle Frank|600 Bloor St East",
"Broadview|90 Danforth Ave",
"Chester|370 Danforth Ave",
"Pape|650 Danforth Ave",
"Donlands|990 Danforth Ave",
"Greenwood|1177 Danforth Ave",
"Coxwell|1568 Danforth Ave",
"Woodbine|2072 Danforth Ave",
"Main Street|2550 Danforth Ave",
"Victoria Park|777 Victoria Park Ave",
"Warden|3276 St Clair Ave East",
"Kennedy|2455 Eglinton Ave East" ];

CM_rtLineHtml = [ "McCowan|1275 McCowan Rd",
"Scarborough Centre|Scarborough Town Centre",
"Midland|2085 Midland Ave",
"Ellesmere|1025 Ellesmere Rd",
"Lawrence East|2444 Lawrence Ave East" ];

var CM_stationMarkers = new Array( CM_yongeLineHtml.length + CM_bloorLineHtml.length + CM_rtLineHtml.length + CM_sheppardLineHtml.length );
var CM_stations = new Array( CM_stationMarkers.length );

function drawTtcSubwayMapColor( map, yongeColor, bloorColor, rtColor, sheppardColor )
{
    var CM_ttcStationIcon = new GIcon();
    CM_ttcStationIcon.image = "http://crazedmonkey.com/ttcgooglemap/ttc-sm.png";
    CM_ttcStationIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
    CM_ttcStationIcon.iconSize = new GSize(37, 14);
    CM_ttcStationIcon.shadowSize = new GSize(50, 17);
    CM_ttcStationIcon.iconAnchor =  new GPoint(19, 7);
    CM_ttcStationIcon.infoWindowAnchor =  new GPoint(15, 1);

    var lastIndex = 0;
    lastIndex = CM_addLine( map, CM_yongeLinePoints, CM_yongeLineHtml, yongeColor, CM_stationMarkers, CM_stations, lastIndex, CM_ttcStationIcon );
    lastIndex = CM_addLine( map, CM_bloorLinePoints, CM_bloorLineHtml, bloorColor, CM_stationMarkers, CM_stations, lastIndex, CM_ttcStationIcon );
    lastIndex = CM_addLine( map, CM_rtLinePoints, CM_rtLineHtml, rtColor, CM_stationMarkers, CM_stations, lastIndex, CM_ttcStationIcon );
    lastIndex = CM_addLine( map, CM_sheppardLinePoints, CM_sheppardLineHtml, sheppardColor, CM_stationMarkers, CM_stations, lastIndex, CM_ttcStationIcon );
}

function drawTtcSubwayMap( map )
{
    drawTtcSubwayMapColor( map, "#FF6666", "#66FF66", "#6666FF", "#FF66FF" );
}

function writeTtcSubwayStations()
{
    CM_stations.sort();
    document.write( "<div><div id=\"stations\"><ul>" );
    for( i = 0; i < CM_stations.length; i++ )
    {
        document.write( CM_stations[ i ] );
    }
    document.write( "</ul></div></div>" );
}

