// JavaScript Document

var map = {

	map_id: 'webMap',
	dir_id: 'mapDir',
	form_id: 'frmDirections',
	form_elements: [
		'directions',
		'selTranspo'
	],
	icon_image: './../../images/map/marker_norm.png',
	icon_route: './../../images/map/icon_greenA.png',
	icon_shadow: './../../images/map/shadow40.png',

	map: false,
	dir: false,
	directions: false,

	transpoMarkers: [],
	transpoZoom: 14,
    

    



	transpo_centers: [
		{
			id: 'Stamford Transportation Center',
			coords: [41.046529, -73.542824],
			props: ['metro-center', 'first-stamford-place', 'metro-tower'],
			icon: './../../images/map/icon-stamford-up.png',
			width: 90
		},
		{
			id: 'Mamaroneck Train Station',
			coords: [40.96942, -73.71298],
			props: ['500-mamaroneck-avenue'],
			icon: './../../images/map/icon-train-station.png',
			width: 52,
			center: [40.973345, -73.722510]
                       
		},
		{
			id: 'White Plains Transportation Center',
			coords: [41.032442, -73.775200],
			props: ['10-bank-street'],
			icon: './../../images/map/transportation-center-down.png',
			width: 90,
			offset: 53
		},
		{
			id: 'South Norwalk Train Station',
			coords: [41.09570, -73.42185],
			props: ['merrittview'],
			icon: './../../images/map/icon-train-station.png',
			width: 52
		},
		{
			id: 'Merritt 7 Station',
			coords: [41.143784, -73.427860],
			props: ['merrittview'],
			icon: './../../images/map/icon-train-station-u.png',
			width: 52,
            zoomer: 1
		}
	],
    
       

	init: function() {
		if (GBrowserIsCompatible())
		{
			// init map
			map.map = new GMap2(document.getElementById(map.map_id));
			
            
             if (map.transpo_centers.zoomer = 1) {
    transpoZoom: 15;
     map.map.setCenter(new GLatLng(lat, lon), 15);
} else {
            
            map.map.setCenter(new GLatLng(lat, lon), 14);  
            }
            
            
			map.map.addControl(new GSmallZoomControl());
			// add marker
			map.add_marker(lat, lon, true);
			// elements and behaviors
			map.dir = document.getElementById(map.dir_id);
			$('#' + map.form_id).submit(map.get_directions);
			// show transpo centers by default
			map.showTranspo(true);
			// add event listener for zoom
			GEvent.addListener(map.map, 'zoomend', function (oldZoom, newZoom)
			{
				if (newZoom == map.transpoZoom)
				{
					map.showTranspo(true);
				}
				else if (newZoom < map.transpoZoom)
				{
					map.showTranspo(false);
				}
			});
		}
	},

	showTranspo: function (action)
	{
		var i,
			l,
			icon,
			marker;
		if (action == true)
		{
			l = map.transpo_centers.length;
			for (i = 0; i < l; i++)
			{
				if (-1 !== $.inArray(propertyId, map.transpo_centers[i].props))
				{
					icon = new GIcon();
					icon.image = map.transpo_centers[i].icon;
					icon.shadow = map.icon_shadow;
					icon.iconSize = new GSize(map.transpo_centers[i].width, 42);
					icon.shadowSize = new GSize(0, 0);
					if (map.transpo_centers[i].offset)
					{
						icon.iconAnchor = new GPoint((Math.ceil((map.transpo_centers[i].width / 2))), map.transpo_centers[i].offset);
					}
					else
					{
						icon.iconAnchor = new GPoint((Math.ceil((map.transpo_centers[i].width / 2))), 0);
					}
					icon.infoWindowAnchor = new GPoint(0, 0);
					marker = new GMarker(new GLatLng(map.transpo_centers[i].coords[0], map.transpo_centers[i].coords[1]), { icon:icon });
					map.transpoMarkers.push(marker);
					map.map.addOverlay(marker);
					if (map.transpo_centers[i].center)
					{
						map.map.setCenter(new GLatLng(map.transpo_centers[i].center[0], map.transpo_centers[i].center[1]), 14);
					}
                                           
					
				}
			}
		}
		else
		{
			l = map.transpoMarkers.length;
			for (i = 0; i < l; i++)
			{
				map.map.removeOverlay(map.transpoMarkers[i]);
				map.transpoMarkers.shift();
			}
		}
	},

	add_marker: function(lat, lon, icon) {
		if (icon) {
			// custom icon
			var icon = new GIcon();
			icon.image = map.icon_image;
			icon.shadow = map.icon_shadow;
			icon.iconSize = new GSize(42, 48);
			icon.shadowSize = new GSize(0, 0);
			icon.iconAnchor = new GPoint(21, 48);
			icon.infoWindowAnchor = new GPoint(0, 0);
		} else {
			// route icon
			var icon = new GIcon();
			icon.image = map.icon_route;
			icon.shadow = map.icon_shadow;
			icon.iconSize = new GSize(24, 38);
			icon.shadowSize = new GSize(0, 0);
			icon.iconAnchor = new GPoint(12, 38);
			icon.infoWindowAnchor = new GPoint(0, 0);
		}
		map.map.addOverlay(new GMarker(new GLatLng(lat, lon), { icon:icon }));
	},

	get_directions: function() {
		// create gdirections
		map.directions = new GDirections(null, map.dir);
		// grab form values and set opts
		var val = document.getElementById(map.form_elements[0]).value;
		var sel = document.getElementById(map.form_elements[1]);
		switch (sel.options[sel.selectedIndex].value) {
			case '1':
				var mode = G_TRAVEL_MODE_DRIVING;
				break;
			case '3':
				var mode = G_TRAVEL_MODE_WALKING;
				break;
		}
		sel = false;
		var directions_options = {
			travelMode: mode,
			getPolyline: true,
			getSteps: true
		}
		if (val) {
			// clear overlays
			map.map.clearOverlays();
			map.dir.innerHTML = '';
			map.directions.load('from: ' + val + ' to: ' + address, directions_options);
			// callback after data has been loaded from google
			GEvent.addListener(map.directions, 'load', function() {
				setTimeout('map._get_directions()', 1);
			} );
			GEvent.addListener(map.directions, 'error', function() {
				map.dir.innerHTML = '<p class="fail">We\'re sorry, but that address cannot be located. Please make sure to enter an address, city and state separated by commas.</p>';
			} );
		}
		return false;
	},

	_get_directions: function() {
		// zoom + center on directions bounds
		var bounds = map.directions.getBounds();
		map.map.setZoom(map.map.getBoundsZoomLevel(bounds));
		map.map.setCenter(bounds.getCenter());
		// add markers
		var start = map.directions.getGeocode(0).Point.coordinates;
		map.add_marker(start[1], start[0], false);
		map.add_marker(lat, lon, true);
		map.map.addOverlay(map.directions.getPolyline());
		// output directions
		var route = map.directions.getRoute(0);
		var l = route.getNumSteps();
		var o = '';
		for (var i = 0; i < l; i++) {
			o = o + '<tr><td>' + ( i + 1 ) + '. ' + route.getStep(i).getDescriptionHtml() + '</td></tr>';
		}
		map.dir.innerHTML = '<table><tr><td><span><img src="' + map.icon_route + '" alt="Start icon" />' + map.directions.getGeocode(0)['address'] + '</span></td></tr>' + o + '<tr><td><span><img src="' + map.icon_image + '" alt="End icon" />' + map.directions.getGeocode(1)['address'] + '</span></td></tr></table>';
	}

}
