var CM_Route509Points = [
 new GPoint(-79.3790853023529, 43.64588913888224),
 new GPoint(-79.37682151794434, 43.64109104688982),
 new GPoint(-79.39201354980469, 43.637628108122975),
 new GPoint(-79.39845085144043, 43.635671381830846),
 new GPoint(-79.4068193435669, 43.63629257168392),
 new GPoint(-79.40969467163086, 43.636323631008004),
new GPoint(-79.41857814788818, 43.63534525458737),
new GPoint(-79.42226886749268, 43.632565336242436),
new GPoint(-79.41952228546143, 43.63203729252688),
new GPoint(-79.41540241241455, 43.633155497257654),
new GPoint(-79.41188335418701, 43.63408731864001),
new GPoint(-79.4091796875, 43.63491041550948),
new GPoint(-79.4068193435669, 43.63629257168392)
];

var CM_yongeLineDayNorthEastPoints = [
 new GPoint(-79.46220427751541, 43.74919180376368),
 new GPoint(-79.45009142160416, 43.73435602693744),
 new GPoint(-79.4475594162941, 43.724760034632034),
 new GPoint(-79.44396525621414, 43.7153815729365),
 new GPoint(-79.44098263978958, 43.709098074196895),
 new GPoint(-79.43636387586594, 43.69903319977416),
 new GPoint(-79.41501885652542, 43.683780981337236)
 ];

var CM_yongeLinePoints = [
 new GPoint(-79.41501885652542, 43.683780981337236),
 new GPoint(-79.40665572881699, 43.674787981119245),
 new GPoint(-79.40360605716705, 43.667324474662834),
 new GPoint(-79.39787417650223, 43.66863797940801),
 new GPoint(-79.39368188381195, 43.66730701281089),
 new GPoint(-79.39076900482178, 43.659927955678334),
 new GPoint(-79.38793122768402, 43.654756448242985),
 new GPoint(-79.38624680042267, 43.65094690668266),
 new GPoint(-79.38471794128418, 43.64778148483318),
 new GPoint(-79.38069999217987, 43.64512247919548),
 new GPoint(-79.3778944015503, 43.648848935736545),
 new GPoint(-79.37927037477493, 43.65237915187759),
 new GPoint(-79.38093066215515, 43.65602171373054),
 new GPoint(-79.38269823789597, 43.66133670189694),
 new GPoint(-79.38460797071457, 43.66485842282958),
 new GPoint(-79.38602417707443, 43.670717798144565),
 new GPoint(-79.38869565725327, 43.67685982714917),
 new GPoint(-79.3907555937767, 43.682256378607114),
 new GPoint(-79.3930783867836, 43.68798798559691),
 new GPoint(-79.39727336168289, 43.69771397725964),
 new GPoint(-79.39836770296097, 43.70673068583634)
 ];

var CM_yongeLineDayNorthWestPoints = [
 new GPoint(-79.39836770296097, 43.70673068583634),
 new GPoint(-79.4019216299057, 43.72525624966812),
 new GPoint(-79.4058832526207, 43.74326452254364),
 new GPoint(-79.41296964883804, 43.76875212250278),
 new GPoint(-79.41568404436111, 43.78191400941247)
];

var CM_bloorLineDayWestPoints = [
 new GPoint(-79.53542858362198, 43.6376960488635),
 new GPoint(-79.52391654253006, 43.645227288945236),
 new GPoint(-79.51128333806992, 43.64826281049803),
 new GPoint(-79.49348956346512, 43.64950492342589),
 new GPoint(-79.48387116193771, 43.64995518301882),
 new GPoint(-79.47576552629471, 43.6517542469277),
 new GPoint(-79.46645021438599, 43.654063645875866),
 new GPoint(-79.45968300104141, 43.65554433141758)
 ];

var CM_bloorLinePoints = [ 
 new GPoint(-79.45968300104141, 43.65554433141758),
 new GPoint(-79.45311427116394, 43.65691825074471),
 new GPoint(-79.44306671619415, 43.65898683269404),
 new GPoint(-79.4354036450386, 43.65987556365253),
 new GPoint(-79.42629754543304, 43.662353462375705),
 new GPoint(-79.41835284233093, 43.66408812466335),
 new GPoint(-79.41117525100708, 43.66589258593267),
 new GPoint(-79.40360605716705, 43.667324474662834),
 new GPoint(-79.39787417650223, 43.66863797940801),
 new GPoint(-79.38997238874435, 43.67011248520139),
 new GPoint(-79.37649697065353, 43.67220195308347),
 new GPoint(-79.36862468719482, 43.67378695122794),
 new GPoint(-79.35838401317596, 43.67680357010253),
 new GPoint(-79.35248047113419, 43.678149844953424),
 new GPoint(-79.34494614601135, 43.67982391693447),
 new GPoint(-79.33775782585144, 43.68088691869681),
 new GPoint(-79.33021008968353, 43.68247556688051),
 new GPoint(-79.32302713394165, 43.684108785260214),
 new GPoint(-79.31263357400894, 43.68636457078064)
 ];

var CM_bloorLineDayEastPoints = [
 new GPoint(-79.31263357400894, 43.68636457078064),
 new GPoint(-79.30182695388794, 43.68888404520687),
 new GPoint(-79.28879678249359, 43.694905916707164),
 new GPoint(-79.27906304597855, 43.711383940317916),
 new GPoint(-79.26386028528214, 43.7324354009901)
];

CM_rtLinePoints = [
 new GPoint(-79.25164014101028, 43.774895729824394),
 new GPoint(-79.25830274820328, 43.77431664623901),
 new GPoint(-79.27198201417923, 43.770423664542285),
 new GPoint(-79.27621185779572, 43.76662340516728),
 new GPoint(-79.27034050226212, 43.75067401674027),
 new GPoint(-79.26386028528214, 43.7324354009901)
];

CM_sheppardLinePoints = [new GPoint(-79.4121327996254, 43.761608320294094),
 new GPoint(-79.38771396875381, 43.76699336967006),
 new GPoint(-79.37952786684036, 43.76842090758474),
 new GPoint(-79.36580568552017, 43.77117710216893),
 new GPoint(-79.34604316949844, 43.775569706334004)
];

CM_Route300WestboundPoints = [
 new GPoint(-79.45999145507812, 43.65470793293131),
 new GPoint(-79.48453903198242, 43.6493050226109),
 new GPoint(-79.51136112213135, 43.647410783808894),
 new GPoint(-79.5234203338623, 43.64464695118206)
 ];

CM_Route300EastboundPoints = [
 new GPoint(-79.31281328201294, 43.685703166962846),
 new GPoint(-79.30182695388794, 43.68809272052791),
 new GPoint(-79.28837299346924, 43.69119589475715)
 ];

CM_Route320Points = [
 new GPoint(-79.39840793609619, 43.7066938461304),
 new GPoint(-79.40214157104492, 43.72521166801653),
 new GPoint(-79.40673351287842, 43.744219825256835),
 new GPoint(-79.41596031188965, 43.78162934357166)
 ];

CM_RouteYUSPoints = [
 new GPoint(-79.4150161743164, 43.68381007643242),
 new GPoint(-79.41829919815063, 43.68317386046503),
 new GPoint(-79.42553043365479, 43.70101638296418)
 ];

CM_yongeLineDayNorthEastHtml = [
"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"];

CM_yongeLineHtml = [
"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"];

CM_yongeLineDayNorthWestHtml = [
"Eglinton|2190 Yonge St",
"Lawrence|3101 Yonge St",
"York Mills|4015 Yonge St",
"North York Centre|5152 Yonge St",
"Finch|5714 Yonge St"];

CM_bloorLineDayWestHtml = [
"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"
];

CM_bloorLineHtml = [
"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"
];

CM_bloorLineDayEastHtml = [
"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"];

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_Route509Html = ["Bay St and Front",
"Bay St and Queens Quay",
"Queens Quay at Spadina",
"Fleet at Bathurst Farside",
"Fleet at Fort York Blvd Farside",
"Loop Roadway at Strachan Farside",
"Manitoba Dr and NovaScotia Ave",
"Manitoba Dr and Saskatchewan Rd",
"Princess Blvd and Ontario Dr",
"New Brunswick Way and Pricess Blvd",
"NewFoundland Dr and Princess Blvd",
"Princess Blvd and Strachan Ave",
"Fleet at Fort York Blvd Farside"
];

CM_Route300WestboundHtml = ["Bloor and Keele",
"Bloor and Jane",
"Bloor and Royal York",
"Bloor and Islington"
];

CM_Route300EastboundHtml = ["Danforth and Woodbine",
"Danforth and Main Street",
"Danforth and Victoria Park"
];

CM_Route320Html = ["Yonge and Eglinton",
"Yonge and Lawrence Ave",
"Yonge and York Mills",
"Yonge and Finch"
];

CM_RouteYUSHtml = ["St. Clair West|370 St Clair Ave West",
"St. Clair and Bathurst",
"Bathurst and Eglinton Ave W."
];

var CM_stationMarkers = new Array(CM_yongeLineDayNorthWestHtml.length + CM_yongeLineHtml.length + CM_yongeLineDayNorthEastHtml.length +
                        CM_bloorLineDayWestHtml.length + CM_bloorLineHtml.length + CM_bloorLineDayEastHtml.length +
                        CM_rtLineHtml.length +
                        CM_sheppardLineHtml.length + CM_Route509Html.length + CM_Route300WestboundHtml.length +
                        CM_Route300EastboundHtml.length + CM_Route320Html + CM_RouteYUSHtml.length);
var CM_stations = new Array(CM_stationMarkers.length);

var CM_RouteLines = new Array(13);

// 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, i_PolyLine)
{
    var PolyLine = new GPolyline(points, color, 10);
    map.addOverlay(PolyLine);
    CM_RouteLines[i_PolyLine] = PolyLine;
    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;
}

function drawTtcSubwayMapColor( map, yongeColor, bloorColor, rtColor, sheppardColor, streetCarColor, blueNightColor )
{
    var CM_ttcStationIcon = new GIcon();
    //CM_ttcStationIcon.image = "/images/logos/logoTTC_sm.gif";
    //CM_ttcStationIcon.shadow = "/images/logos/logoTTCshadow_sm.png";
    CM_ttcStationIcon.iconSize = new GSize(44, 22);
    //CM_ttcStationIcon.shadowSize = new GSize(50, 17);
    CM_ttcStationIcon.iconAnchor =  new GPoint(22, 11);
    CM_ttcStationIcon.infoWindowAnchor =  new GPoint(15, 1);

    var lastIndex = 0;
    lastIndex = CM_addLine(map, CM_Route509Points, CM_Route509Html, streetCarColor, CM_stationMarkers, CM_stations, lastIndex, CM_ttcStationIcon, 0);
    lastIndex = CM_addLine(map, CM_yongeLinePoints, CM_yongeLineHtml, yongeColor, CM_stationMarkers, CM_stations, lastIndex, CM_ttcStationIcon, 1);
    lastIndex = CM_addLine(map, CM_bloorLinePoints, CM_bloorLineHtml, bloorColor, CM_stationMarkers, CM_stations, lastIndex, CM_ttcStationIcon, 2);
    lastIndex = CM_addLine(map, CM_yongeLineDayNorthWestPoints, CM_yongeLineDayNorthWestHtml, yongeColor, CM_stationMarkers, CM_stations, lastIndex, CM_ttcStationIcon, 3);
    lastIndex = CM_addLine(map, CM_yongeLineDayNorthEastPoints, CM_yongeLineDayNorthEastHtml, yongeColor, CM_stationMarkers, CM_stations, lastIndex, CM_ttcStationIcon, 4);
    lastIndex = CM_addLine(map, CM_bloorLineDayWestPoints, CM_bloorLineDayWestHtml, bloorColor, CM_stationMarkers, CM_stations, lastIndex, CM_ttcStationIcon, 5);
    lastIndex = CM_addLine(map, CM_bloorLineDayEastPoints, CM_bloorLineDayEastHtml, bloorColor, CM_stationMarkers, CM_stations, lastIndex, CM_ttcStationIcon, 6);
    lastIndex = CM_addLine(map, CM_rtLinePoints, CM_rtLineHtml, rtColor, CM_stationMarkers, CM_stations, lastIndex, CM_ttcStationIcon, 7);
    lastIndex = CM_addLine(map, CM_sheppardLinePoints, CM_sheppardLineHtml, sheppardColor, CM_stationMarkers, CM_stations, lastIndex, CM_ttcStationIcon, 8);
    lastIndex = CM_addLine(map, CM_Route300WestboundPoints, CM_Route300WestboundHtml, blueNightColor, CM_stationMarkers, CM_stations, lastIndex, CM_ttcStationIcon, 9);
    lastIndex = CM_addLine(map, CM_Route300EastboundPoints, CM_Route300EastboundHtml, blueNightColor, CM_stationMarkers, CM_stations, lastIndex, CM_ttcStationIcon, 10);
    lastIndex = CM_addLine(map, CM_Route320Points, CM_Route320Html, blueNightColor, CM_stationMarkers, CM_stations, lastIndex, CM_ttcStationIcon, 11);
    lastIndex = CM_addLine(map, CM_RouteYUSPoints, CM_RouteYUSHtml, blueNightColor, CM_stationMarkers, CM_stations, lastIndex, CM_ttcStationIcon, 12);    
}

function drawTtcSubwayMap( map )
{
    drawTtcSubwayMapColor(map, "#CC6600", "#003300", "#6666FF", "#330033", "#990000", "#000099");
}

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>" );
}

function TTCDay() {
    //Hide Nigthly Service
    /*var i_DailyServiceMarkers = CM_yongeLineHtml.length + CM_bloorLineHtml.length + CM_rtLineHtml.length + CM_sheppardLineHtml.length + CM_Route509Html.length;
    for (i = i_DailyServiceMarkers; i < CM_stations.length; i++) {
        CM_stationMarkers[i].hide();
    }*/
    for (i = 3; i < 9; i++)
        CM_RouteLines[i].show();
    for (i = 9; i < CM_RouteLines.length; i++) 
        CM_RouteLines[i].hide();   
    $("#chk_TTCDay").attr('checked', 'checked');
    $("#chk_TTCNightly").removeAttr('checked');
}

function TTCNightly() {
    //Hide Nigthly Service
    /*var i_DailyServiceMarkers = CM_yongeLineHtml.length + CM_bloorLineHtml.length + CM_rtLineHtml.length + CM_sheppardLineHtml.length + CM_Route509Html.length;
    for (i = i_DailyServiceMarkers; i < CM_stations.length; i++) {
        CM_stationMarkers[i].show();
    }*/
    for (i = 3; i < 9; i++)
        CM_RouteLines[i].hide();
    for (i = 9; i < CM_RouteLines.length; i++) {
        CM_RouteLines[i].show();
    }
    $("#chk_TTCDay").removeAttr('checked');
    $("#chk_TTCNightly").attr('checked', 'checked');
}