

















/*
* Create map
* 
*/

(function($) { 
$.ti.createMap = $.prototype = {
	
	// Set up the day select for map viewing
	setMapDayOptions: function() {
		var count;
				
		$('#numberPlannedDays').empty()
			.append($('<option>mostra tutti i giorni</option>'));
			
		for(count = 0; count < $.ti.tripData.trip.tripDuration; count += 1) {			
			$('#numberPlannedDays').append($('<option>Giorno' + (count + 1) + '</option>'));
		}
	},	
	
	// Map object stored here.
	map: false,
	
	// Default map options stored here
	mapOptions: {
		zoom: 6,
		center: null,
		mapTypeId: null
	},
	
	// Return a lat long for the center of the map view
	mapCenter: function(){
		return new google.maps.LatLng(53.4329, -8.2439);
	},
	
	// Create map object
	initialiseMap: function() {
		$.ti.createMap.mapOptions.center = $.ti.createMap.mapCenter();
		$.ti.createMap.mapOptions.mapTypeId = google.maps.MapTypeId.ROADMAP;
		$.ti.createMap.map = new google.maps.Map(document.getElementById("mapContainer"), $.ti.createMap.mapOptions);		
	},
	
	// Categories for viewing stored here
	selectedCategories: [],
	
	// Controls whether we show only activities within the calender view or activities from the bag as well
	showBagActivities: false,
	
	// Which day from the trip are we showing, 0 = all days
	dayToShow: 0,

	// Establish map settings, days to show, items from bag, categories.
	getSettings: function() {
		// Clear categories list
		$.ti.createMap.selectedCategories = [];
		// Get required categories
		$('input[name=mapCategories]:checked', '#plannedItemsChecks').each(function(){
			$.ti.createMap.selectedCategories.push($(this).val());
			//console.log($(this).val())
		});
		// Do we show items from the bag?
		$.ti.createMap.showBagActivities = document.getElementById('includeTripPlannerItems').checked;
		// Check which day(s) we show, 0 = all days
		$.ti.createMap.dayToShow = document.getElementById('numberPlannedDays').selectedIndex;
	},
	
	// Returns odd file name prefixes for category icons
	getCategory: function(category) {
		switch (category) {
			case 'Accommodation':
				return 'pts';				
			case 'Whats on':
				return 'wo';				
			case 'Sights & Culture':
				return 'sc';				
			case 'Activities':
				return 'act';				
			case 'Food and Drink':
				return 'pte';				
			case 'Getting around Ireland':
				return 'go';				
			case 'User Generated':
				return 'go';				
			default:
				return 'act';
		}
	},	
	
	// Does the activity fall within the selected day
	// Returns true if it does, false if it doesnt
	checkDays: function(activity) {
		var count, length, start, end, duration, gridReferences;
		
		gridReferences = activity.actRowColumnDays.split(',');
		length = gridReferences.length;
		
		for(count = 0;count < length; count += 1) {
			start = $.ti.utils.getDay(gridReferences[count]);
			duration = $.ti.utils.getDuration(gridReferences[count]);
			end = (start + duration) -1;
			if($.ti.createMap.dayToShow >= start && $.ti.createMap.dayToShow <= end) {
				return true;
			}
		}
		return false;
	},

	// Identify the activities that we will display on the map
	getActivities: function() {
		var activity, fitsDay;
		// Are there any activities
		if($.ti.tripData.trip.tripActivities) {
			// Loop through activities
			for (activity = 0; activity < $.ti.tripData.trip.tripActivities.activity.length; ++activity) {
				if($.ti.tripData.trip.tripActivities.activity.hasOwnProperty(activity)) {
					// Does the activity have lat long data
					if($.ti.tripData.trip.tripActivities.activity[activity].actLatLong){						
						// Are we showing all items regarldess of whether they are in the trip plan calender?						
						if($.ti.createMap.showBagActivities) {
							$.ti.createMap.createMarker($.ti.tripData.trip.tripActivities.activity[activity]);							
						} else {							
							// If not does the activity have grid positions
							if($.ti.tripData.trip.tripActivities.activity[activity].actRowColumnDays) {
								// Are we showing all days?
								if($.ti.createMap.dayToShow) {
									// Check the day
									if($.ti.createMap.checkDays($.ti.tripData.trip.tripActivities.activity[activity])) {
										$.ti.createMap.createMarker($.ti.tripData.trip.tripActivities.activity[activity]);										
									}	
								} else {
									$.ti.createMap.createMarker($.ti.tripData.trip.tripActivities.activity[activity]);
								}
								
							}
						}
					}
				}				
			}
		}
	},	
	
	// Return a google lat long from our stored string
	getLatLong: function(latlongString) {
		var latLongArray, latitude, longitude;
				
		latLongArray = latlongString.split(',');
		
		latitude = parseFloat(latLongArray[0]);
		longitude = parseFloat(latLongArray[1]);
		
		return new google.maps.LatLng(latitude, longitude);
	},
	
	// Create a new map marker and append to map
	createMarker: function(activity) {
		var image, marker, latLong, css, category, dayMarker, day, dayCss;
		
		// Check that the category is selected		
		if($.inArray(activity.actCategory, $.ti.createMap.selectedCategories) >= 0) {
			
			// Creata category prefix
			category = $.ti.createMap.getCategory(activity.actCategory);
			
			// Get lat long value
			latLong = $.ti.createMap.getLatLong(activity.actLatLong);
			
			// If its in the calender
			if(activity.actRowColumnDays) {
				image = new google.maps.MarkerImage("/shared/icons/"+category+"_in_plan.png", new google.maps.Size(32, 32), new google.maps.Point(0,0), new google.maps.Point(34, 16));
				css = 7000;
			
				// Create day indication icon
				if(this.dayToShow === 0) {
					day = $.ti.utils.getDay(activity.actRowColumnDays);
				} else {
					day = this.dayToShow;
				}
																
				marker = new google.maps.Marker ({
					position: latLong,
					map: $.ti.createMap.map,
					title: activity.actName,
					icon: "/shared/icons/map_days/day_" + day + ".png",
					zIndex: 8000
				});			
				
			} else {
				image = "/shared/icons/"+category+".png";
				css = 6000;
			}		
			
			// Create marker
			marker = new google.maps.Marker({
					position: latLong,
					map: $.ti.createMap.map,
					title: activity.actName,
					icon: image,
					zIndex: css
				});

			$.ti.createMap.markerEvent(marker, activity.actName, activity.actDescription);
		}
	},
	
	// Attach click event to marker for opening info bubble
	markerEvent: function(marker, name, desc) {
		google.maps.event.addListener(marker, 'click', function() {
				var infowindow, html;
				
				html = '<div class="bubble"><h4>' + name + '</h4><p>' + desc + '</p></div>';
				
				infowindow = new google.maps.InfoWindow({
					content: html,
					maxWidth: 200					
				});
				
				infowindow.open($.ti.createMap.map, marker);
			});
	},
	
	// Sea port data
	seaPorts: {
		"Cork": {			
			"name": "Cork",
			"longitude": "-8.323259",
			"latitude": "51.832542"
		},		
		"Galway": {			
			"name": "Galway",
			"longitude": "-9.0418",
			"latitude": "53.2729"
		},
		"Waterford": {			
			"name": "Port of Waterford",
			"longitude": "-7.036786",
			"latitude": "52.264092"
		},
		"Dublin": {			
			"name": "Dublin Port",
			"longitude": "-6.20831",
			"latitude": "53.34609"
		},
		"Belfast": {			
			"name": "Port of Belfast",
			"longitude": "-5.901667",
			"latitude": "54.617778"
		},
		"Londonderry": {			
			"name": "Londonderry Port",
			"longitude": "-7.263",
			"latitude": "55.042"
		},
		"Limerick": {			
			"name": "Limerick",
			"longitude": "-8.6238",
			"latitude": "52.6652"
		},
		"Rosslare": {			
			"name": "Rosslare Europort",
			"longitude": "-6.334444",
			"latitude": "52.255556"
		}
	},
	
	// Airport data
	airPorts: {
		"cork": {			
			"name": "Cork",
			"longitude": "-8.48888888",
			"latitude": "51.84083333"
		},
		"Donegal": {			
			"name": "Donegal",
			"longitude": "-8.34111111111",
			"latitude": "55.0441666667"
		},
		"Galway": {			
			"name": "Galway",
			"longitude": "-8.95",
			"latitude": "53.29444444"
		},
		"Waterford": {			
			"name": "Waterford",
			"longitude": "-7.088928",
			"latitude": "52.197454"
		},
		"Dublin": {			
			"name": "Dublin",
			"longitude": "-6.25222222",
			"latitude": "53.43249999"
		},
		"Knock": {			
			"name": "Ireland West Knock",
			"longitude": "-8.81777777",
			"latitude": "53.91"
		},
		"Shannon": {			
			"name": "Shannon",
			"longitude": "-8.91861111",
			"latitude": "52.70055555"
		},
		"Belfast": {			
			"name": "Belfast International",
			"longitude": "-5.87083333",
			"latitude": "54.61722222"
		},
		"Derry": {			
			"name": "City of Derry",
			"longitude": "-7.15333333",
			"latitude": "55.04333333"
		},
		"Kerry": {			
			"name": "Kerry",
			"longitude": "-9.53444444",
			"latitude": "52.18305555"
		}
	},
	
	// Add sea and air ports to the map
	addPorts: function(ports, icon) {
		var port, marker;
		
		for(port in ports) {
			if(ports.hasOwnProperty(port)){												
				marker = new google.maps.Marker({
					position: new google.maps.LatLng(ports[port].latitude, ports[port].longitude),
					map: $.ti.createMap.map,
					title: ports[port].name,
					icon: icon,
					zIndex: 5000
				});
			}
		}
	},
	
	// Main map creation function - initial calls start here
	createMap: function() {
		// Initialise map if needed		
		$.ti.createMap.initialiseMap();					
		// Check settings
		$.ti.createMap.getSettings();
		// Create list of activities to show
		$.ti.createMap.getActivities();
		// Add Airports
		$.ti.createMap.addPorts($.ti.createMap.airPorts, "/shared/icons/airport.png");
		// Add Seaports
		$.ti.createMap.addPorts($.ti.createMap.seaPorts, "/shared/icons/seaport.png");				
	}
};
})(jQuery);
