/*
 * ITINERARY PLANNER FUNCTIONALITY
 */
(function(app, $, undefined) {
	
	var self = app.itinerary = {};
	
	self.add = function(listingType, id) 
	{
		var val = app.cookie.get('itinerary');
		listings = (val) ? val.split(',') : [] ;
		if ($.inArray(String(id), listings) < 0) {
			listings.push(id);
			val = listings.join(',');
			app.cookie.set('itinerary', val, 8);
			this.updateCounter(listings.length);
		}
		var $dialog = $('<div></div>').html('<p>Listing added to your Itinerary</p><p><a class="more_info view"><span>View My Itinerary</span></a></p>');
		$dialog.find(".more_info.view").attr('href', $("#itinerary_link").attr('href'));
		$($dialog).dialog({
			draggable: false,
			title: 'Attraction Added!',
			resizable: false,
			modal: true,
			hide: 'fade',
			buttons: [
				{ text: "Ok", click: function() { $(this).dialog("close"); }
			}]
		});
	};
	
	self.remove = function(listingType, id)
	{
		var val = app.cookie.get('itinerary');
		if (!val) return false;
		listings = val.split(',');
		var i = $.inArray(String(id), listings);
		if (i > -1) {
			listings.splice(i, 1);
			val = listings.join(',');
			app.cookie.set('itinerary', val, 8);
			var idx = $("#snippet_"+id).index();
			$("#snippet_"+id).remove();
			dataApp.itinerary.markers[idx].setMap(null);
			this.updateCounter(listings.length);
		}
	}
	
	self.updateCounter = function(count)
	{
		if (!count) {
			var val = app.cookie.get('itinerary');
			listings = (val) ? val.split(',') : [] ;
			count = listings.length;
		}
		$("#itinerary_count").html(count);
	};
	
	self.mapData = {};
	
	self.setMapData = function(data)
	{
		self.mapData = data;
	}
	
	self.loadMap = function()
	{
		var lat = 50.75;
		var lng = -3.86;
		var latlng = new google.maps.LatLng(lat,lng);
		var myOptions = {
			zoom: 8,
			center: latlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		
		self.map = new google.maps.Map(document.getElementById("itinerary_map"), myOptions);
		self.markers = [];
		self.infoWindows = [];
		
		for (var i = 0; i < self.mapData.length; i++) {
			
			latlng = self.mapData[i].latlng.split(',');
			lat = Number(latlng[0]);
			lng = Number(latlng[1]);
			
			var listing = self.mapData[i];
			var marker_latlng = new google.maps.LatLng(lat,lng);
			var marker = new google.maps.Marker({
				position: marker_latlng,
				title: listing.name
			});		
			marker.our_id = listing.id;
			marker.index = i;
			
			var infoWindow = new google.maps.InfoWindow({
				content: '<div class="gmap-popup-small">' + listing.name + '</div>'
			});
			
			google.maps.event.addListener(marker, 'click', function() {
				self.infoWindows[this.index].open(self.map, this);
			});	
			google.maps.event.addListener(marker, 'mouseover', function() {
				self.highlightItem(this.our_id);
			});
			google.maps.event.addListener(marker, 'mouseout', function() {
				self.highlightItem(this.our_id);
			});
			
			// To add the marker to the map, call setMap();
			marker.setMap(self.map);
			self.markers.push(marker);
			self.infoWindows.push(infoWindow);
		}
	}
	
	self.highlightItem = function(num)
	{
		$("#snippet_" + num).toggleClass('highlight');
	}
	
	self.view = function()
	{
		$(function() {
			$("#email_itinerary").hide();
			$("#result").hide();
			$("#email_itinerary form").submit(function(e) {
				e.preventDefault();
				var $form = $(this);
				var data = $form.serialize();
				var url = $form.attr('action') + '?ajax=1';
				$.post(url, data, function(result) { self.displayResponse(result); });
			});
			$("#email_link").click(function(e) { 
				e.preventDefault; 
				if ($("#email_itinerary").is(":hidden")) {
					$("#email_itinerary").slideDown('slow'); 
				} else {
					$("#email_itinerary").hide('slow');
				}
			});
		});
	}
	
	self.displayResponse = function(result) 
	{
		result = eval('('+result+')');
		if (!result.errors.length >= 1) {
			$("#email_itinerary form").children().hide();
			$("#result").show().html(result.message);
		} else {
			alert(result.errors[0]);
		}
	}
	
}(window.dataApp = window.dataApp || {}, jQuery));
