﻿/*global jQuery, tabView, Event
*/

/*members all_markers, attr, click, display_menu, doClick, doTabSwitch, 
    forEach, fromNodeSet, get, getAttribute, getElementsByTagName, 
    gmap_locations, googleMap, hasOwnProperty, icon, icons, id, 
    images_directory, infobox, init, initial_latitude, initial_longitude, 
    initial_zoom, lastIndexOf, length, locs, map, newValue, on, onDOMReady, 
    pbID, remove, selectLocationByID, setOptions, setVisible, setZoom, src, 
    substring, throwErrors, use_google_directions, visible
*/

"use strict";

if (typeof Array.fromNodeSet === "undefined") {

	Array.fromNodeSet = function (ns) {
		var x, out = [];
		for (x = 0; x < ns.length; x++) {
			out[x] = ns[x];
		}
		return out;
	};

}

var MADirections = {

	init: function () {

		this.locs = jQuery('map-element')
		.gmap_locations(
			"/YMG/directory/public/getAllMapLocations.asp?locMap=allLocations",
			"", {
				display_menu: false,
				images_directory: "/YMG/Images",
				initial_latitude: 41.3042314,
				initial_longitude: -72.9287230,
				initial_zoom: 8,
				use_google_directions: true,
				//directions_link:"ymg_dept_directions.html",
				success: function () {

					if (location.hash === '#NewHaven') {
						if (tabView.getTabIndex('Tab inNewHaven') !== null) {
							tabView.set('activeIndex', tabView.getTabIndex('Tab inNewHaven'));
						}
					} else if (location.hash === '#outNewHaven') {
						if (tabView.getTabIndex('Tab notNewHaven') !== null) {
							tabView.set('activeIndex', tabView.getTabIndex('Tab notNewHaven'));
						}
					}

				}

			});

		//attach events to bottom locations menu
		jQuery("a.map-location-link").click(this.doClick);

		// Attach to tab switch event
		tabView.on('activeTabChange', this.doTabSwitch, this, true);

	},

	doClick: function () {
		var id = parseInt(jQuery(this).attr('rel'), 10);
		if (isNaN(id) === false) {
			MADirections.locs.selectLocationByID(id);
			MADirections.locs.googleMap.setZoom(16);
		}
	},


	doTabSwitch: function (o) {
		if (tabView.get('activeIndex') === 1) {
			MADirections.locs.googleMap.setCenter(new google.maps.LatLng(41.3042314, -72.9287230));
			MADirections.locs.googleMap.setZoom(13);
		} else {
			MADirections.locs.googleMap.setCenter(new google.maps.LatLng(41.3042314, -72.9287230));
			MADirections.locs.googleMap.setZoom(8);
		}
		
		var items =
		Array.fromNodeSet(
			o.newValue.get('contentEl')
			.getElementsByTagName('li'))
		.map(function (li) {
			var img = li.getElementsByTagName('img')[0],
			a = li.getElementsByTagName('a')[0],
			icon = img.src,
			i1 = icon.lastIndexOf('_') + 1,
			i2 = icon.lastIndexOf('.');
			
			return {
				id: parseInt(a.getAttribute('rel'), 10),
				icon: parseInt(icon.substring(i1, i2), 10)
			};
		});

		function getItemByID(id) {
			for (var x = 0; x < items.length; x++) {
				if (items[x].id === id) {
					return items[x];
				}
			}
			return null;
		}

		if (this.locs.infobox &&
			typeof this.locs.infobox.remove === 'function') {
			this.locs.infobox.remove();
		}

		this.locs.all_markers.forEach(function (marker) {

			var item;

			if (marker.hasOwnProperty('pbID')) {
				item = getItemByID(marker.pbID);
				if (item) {
					marker.setOptions({
						visible: true,
						icon: this.locs.icons[item.icon]
					});
				} else {
					marker.setVisible(false);
				}
			}

		}, this);

	}

};

Event.onDOMReady(MADirections.init, MADirections, true);
