//Code ©2010 Bad Math Inc. (http://www.badmath.com/)
// mapControlSuggestedToursClass

/**
This object draws the suggested tours control panel
and populates the map.
**/
function mapControlSuggestedTours(map, controlPanelId, customTourTabId, searchTabId, JSONData, defaultStyles)
{
	var self = this;
	this.prototype = new mapControlBase(map, controlPanelId, customTourTabId, searchTabId, JSONData, defaultStyles); 
	jQuery.extend(true, this, this.prototype);
	
	this.zoomComtrolOne = null;
	this.zoomControlTwo = null;
	this.zoomControlThree =null;
	//this is flag that keeps track of whether we need to put the zoom controls
	//back when we clean up.  It's also used to ensure we don't add them back twice when 
	//searching.
	this.addBackMapControlsOnCleanup = true;
	
	this.init = function()
	{
		if(!this.getCustomTours().length)
		{
			jQuery("#customTourTab").hide();
		}
		jQuery("#customTourTab").removeClass("selected");
		//set the title
		document.title = "Trip-Click: Suggested Tours";
		
		//set the description
		var switchToAll = jQuery("<a/>").attr("href", "javascript:switchToAll()").append("go to the next tab");
		var lineBreak = jQuery("<br/>");
		var skipStep = jQuery("<a/>").attr("href", "javascript:switchToAll()").append("skip this step");
		jQuery("#instructions").empty()
		.append("Build a custom tour! You can start with a suggested tour from the list below, and ")
		.append(switchToAll).append(" to add accommodations, dining or other attractions.")
		.append(lineBreak).append( "Or, ").append(skipStep).append(" and build a tour from scratch!");
				
		//this.flusterObject = new Fluster3(map, defaultStyles);
		
		this.draw();
		
		this.initZoomControls();
	
	
	};
	
	this.initZoomControls = function() {
		//remove the zoom controls.  They are put back in cleanup.
		this.zoomComtrolOne = this.map.controls[google.maps.ControlPosition.RIGHT].pop();
		this.zoomControlTwo = this.map.controls[google.maps.ControlPosition.RIGHT].pop();
		this.zoomControlThree = this.map.controls[google.maps.ControlPosition.RIGHT].pop();
		
		//add back the showAll Points button
		this.map.controls[google.maps.ControlPosition.RIGHT].push(this.zoomComtrolOne);
		//this.addBackMapControlsOnCleanup = false;
		

		//add a click handler to add back the controls when the search window appears
		jQuery("#searchBox").keydown(function()
		{
			if(self.addBackMapControlsOnCleanup)
			{
				self.addBackMapControlsOnCleanup = false;
				//remove the show all points button
				self.map.controls[google.maps.ControlPosition.RIGHT].pop();
				
				//add all buttons back in order
				self.map.controls[google.maps.ControlPosition.RIGHT].push(self.zoomControlThree);
				self.map.controls[google.maps.ControlPosition.RIGHT].push(self.zoomControlTwo);
				self.map.controls[google.maps.ControlPosition.RIGHT].push(self.zoomComtrolOne);
			}
		});
	};
	
	this.showSuggestedTour= function(id) {
		var self = this;
		var filteredJSON = self.getStops(id);
		var listId = "suggested" + id;
		var locationListDivId = "locationList" + id;
		
		self.updateMap(filteredJSON, true);
		
	
	};
	
	this.draw = function()
	{
		//clear the existing control panel div
		jQuery('#' + this.controlPanelId).empty();
		
		var defaultOpenedTourId;
		this.initCustomTourTab();
		this.setSearchEvents(this.searchTabId, this.controlPanelId);
		this.showSuggestedTour(JSONData.suggestedTours[0].id);

		this.drawSuggestedTours(this.controlPanelId);
		
		//fire the click event for the first entry in the accordian list
		//to get the map drawn.
		//jQuery('#accordion').find('h3').eq(0).trigger('click');
		
	}//end draw
	
	/**
	this draws the accordion list on the left side 
	of the screen
	**/
	this.drawSuggestedTours = function(targetId)
	{
		var self = this;
		var accordionDiv = jQuery("<div/>").attr("id", "accordion");
		jQuery("#" + targetId).append(accordionDiv);
		jQuery.each(JSONData.suggestedTours, function(key, value)
		{
			if (value.tourStopIDs.length == 0) 
			{
				return;
			}
			//create the header for the accordion list
			var header = jQuery("<h3/>").append(value.name)
			.addClass("accordHeader").attr("id", "accordHeader" + key);
			//insert this into the dom
			accordionDiv.append(header);
		
			//create the content div
			var contentDivId = "accordionContent" + value.id;
			var contentDiv = jQuery("<div/>")
				.attr("id", contentDivId);
			//get the content div in the dom	
			jQuery('#accordion').append(contentDiv);
			
			//the location list needs its own div because it is cleared 
			//and redrawn in click events	
			var locationListDivId = "locationList" + value.id;
			var locationListDiv = jQuery("<div/>")
				.attr("id", locationListDivId)
				.addClass("locationList");
			//get the locationListDiv div in the dom	
			jQuery('#' + contentDivId).append(locationListDiv);
			
			var filteredJSON = self.getStops(value.id);
			var listId = "suggested" + value.id;
			var locationListDivId = "locationList" + value.id;
			
			self.drawLocationList(filteredJSON, locationListDivId, value.id,
								  listId, "suggested");
			self.drawToolBox(filteredJSON, locationListDivId, listId);

		
		
		}); //end for
		
	
		//set the accordion to an accordion
		jQuery("#accordion").accordion( {
			autoHeight: false,
			header: '.accordHeader',
			icons: {
				'header': 'expand closed',
				'headerSelected': 'expand open'
			},
			
			change: function (event,ui) {
				var id =JSONData.suggestedTours[ui.newHeader.attr('id').substring("accprdHeader".length)].id;
				self.showSuggestedTour(id);
			}
		});
	};
	
	
	
	/**
	This sorts through the JSON for the 
	stops on the suggested tours.
	**/
	this.getStops = function(suggestedTourId)
	{	

		var currentTour;
		//find our suggested tour
		for(var index = 0; index < JSONData.suggestedTours.length; index++)
		{	
			if(this.JSONData.suggestedTours[index].id == suggestedTourId)
			{
				
				currentTour = JSONData.suggestedTours[index];
				break;
			}
		} //end for
		if(currentTour == null)
		{
			if (console && console.log) {
				console.log("We did not match our suggested tour id!");	
			}
			return;
		}
		var newJSON = new Object()
		newJSON.markers = new Object();
	 
		for(var index = 0; index < currentTour.tourStopIDs.length; index++)
		{
			newJSON.markers["id" + currentTour.tourStopIDs[index]] = this.JSONData.markers["id" + currentTour.tourStopIDs[index]];
		}//end for
		return newJSON;
	}
	
	/**
	This sets the sort event for when the item list is sorted
	In suggested tours, it does nothing.
	**/
	this.sortEvent = function(event, ui, listId)
	{ 
		//alert("the wrong search event");
	}
		/**
	cleanUp() needs to be called to remove
	all markers from the map
	before destroying the onject
	**/
	this.cleanUp = function()
	{
		var self = this;
		jQuery("#searchBox").unbind('keydown');
		jQuery.each(self.markersArray, function(key, value)
		{
			self.markersArray[key].setMap(null);
			self.hideTrail(self.JSONData.markers[key]);
		});
		
		this.infoWindow.close();
		//put the zoom controls back if the search box hasn;t already done so
		if(this.addBackMapControlsOnCleanup)
		{
			//remove the add all points button
			this.map.controls[google.maps.ControlPosition.RIGHT].pop();
			
			//add all three back in order
			map.controls[google.maps.ControlPosition.RIGHT].push(this.zoomControlThree);
			map.controls[google.maps.ControlPosition.RIGHT].push(this.zoomControlTwo);
			map.controls[google.maps.ControlPosition.RIGHT].push(this.zoomComtrolOne);
		}
		if(this.flusterObject != null)
		{
			this.flusterObject.clearMarkers();
		}
		
	
		
		jQuery("#" + this.controlPanelId).empty();
		jQuery(".dropDownMenu").remove();
	}
	
	this.getType = function()
	{
		return "mapControlSuggestedTours";
	}
	
}

