//Code ©2010 Bad Math Inc. (http://www.badmath.com/)
// mapControlCustomTourClass

/**
This object draws the Custom tour control panel
and populates the map.
**/
function mapControlCustomTour(map, controlPanelId, customTourTabId, searchTabId, JSONData, defaultStyles)
{
	this.prototype = new mapControlBase(map, controlPanelId, customTourTabId, searchTabId, JSONData, defaultStyles); 
	jQuery.extend(true, this, this.prototype);

	this.init = function()
	{
		//set the title
		document.title = "Trip-Click: My Custom Tour";
		//set the instructions
		jQuery("#instructions").empty()
		.append("This is your own customized tour! You can drag and drop locations in the list below to reorder your itinerary, and then print it or download it to a supported GPS device. Have fun exploring Peterborough and the Kawarthas!");
		
		this.draw();
		//add a clich event to the search keydown to replace the custom tour tab
		//when we switch to a search
		jQuery("#searchBox").one('keydown', function()
		{
			jQuery("#customTourTabCheckBoxDiv").removeClass("hidden");
		});
	}
	
	this.draw = function()
	{
		//empty the control Panel Div
		jQuery('#' + this.controlPanelId).html("");
		
		this.initCustomTourTab();
		this.setSearchEvents(this.searchTabId, this.controlPanelId);
		
				
		//create the location list div
		var locationListDiv = jQuery("<div/>");
		var locationListId = "locationListDiv";
		locationListDiv.attr("id", locationListId);
		jQuery('#' + this.controlPanelId).append(locationListDiv);
		
		var customTours = this.getCustomTours();
		var filteredJSON = this.getJSONForLocationIds(customTours);
		var sortableListId = "sortableCustom";
		this.drawLocationList(filteredJSON, locationListId, null, sortableListId, "custom");
		
		//draw the toolbox 
		this.drawToolBox(filteredJSON, locationListId, sortableListId);
		//adjust the left value of the download box
		//jQuery(".dropDownMenu").css("left", "34px");
		
		this.updateMap(filteredJSON);
		
	
	}//end draw
	

	
	/*
	this is passed in to drawLocationList
	so that the function can have different
	click events for the add/remove from custom tour buttons
	in the suggested and custom tours objects.
	*/
	this.locationListCustomTourClick = function(id, locationListDivId, tourId, ULId, JSONData)
	{
		//we don't want the customized behaviour if the search tab is selected
		if(jQuery("#searchTab").hasClass("selected"))
		{
			this.prototype.locationListCustomTourClick.call(this, id, locationListDivId, tourId, ULId, this.JSONData);
		}
		else
		{
			this.removeFromCustomTour(id, true);
			var customTour = this.getCustomTours();
			var filteredJSON = this.getJSONForLocationIds(customTour);
			jQuery("#locationListDiv").empty();
			if (customTour.length) {
				this.drawLocationList(filteredJSON, "locationListDiv", null, "custom", "custom");
						//draw the toolbox 
				this.drawToolBox(filteredJSON, "locationListDiv", "custom");
			}
			this.updateMap(filteredJSON);
		}
		
	};
	/**
	infoWindowCustomTourClick is passed into draw map 
	so that the add to or remove from custom tourts button in the info window
	behaves as we need it to in the custom tours page.
	Here, we can only remove from a custom tour.  
	we need the location entry to disappear and we need
	the location marker and the info window to disappear.
	**/
	this.infoWindowCustomTourClick = function(id, targetId, marker)
	{
		
		this.locationListCustomTourClick(id, null, null, null);
		this.infoWindow.close();
	};
	
	/**
	This sets the sort event for when the item list is sorted
	It updates the order of the locations in the cookie so 
	that the order is semi-perminant
	**/
	this.sortEvent = function(event, ui, listId)
	{ 
		var self = this;
		//remove all of the tours from the custom tour.
		jQuery('#' + listId).find ("li").each(function()
		{
			var locationID = jQuery(this).attr("id");
			self.removeFromCustomTour(locationID, false);									  
		});
		
		var listString = self.getLocationsFromSortableList(listId);
		var listArray = listString.split(',');
		
		//put them back in in the new order.
		self.addToCustomTour(listArray, false);	
		var idBeforeMe;
		var movedId = jQuery(ui.item).attr("id");
		var idAfterMe;
		var lastId = -1;
		var currentId;
		
		
		jQuery('#' + listId + " li:even").removeClass("odd").addClass("even");
		jQuery('#' + listId + " li:odd").removeClass("even").addClass("odd");
		
		self.initCustomTourTab();
		
	
/*		var newTop;
		var currentTop = parseInt(jQuery("#customTourTabLocationsList").css("top"));
		var changePerTimeout = 1;//Math.abs(currentTop - newTop) / 10;
		var remove = true;
		var listItemToMove;
		var timerFunction = function()
		{
			if(currentTop > newTop)
			{
				currentTop -= changePerTimeout; 
			}
			else if(currentTop < newTop)
			{
				currentTop += changePerTimeout;
			}
			if(Math.abs(currentTop - newTop) < changePerTimeout)
			{
				currentTop = newTop;
			}
			if(currentTop == newTop)
			{
				jQuery("#customTourTabLocationsList").css("top", currentTop + "px");
				if(remove == true)
				{
					jQuery("#customTourTabLocationsList").find("li").removeClass("new");
					jQuery("#customTourListItem" + movedId).hide(800, function()
					{
						remove = false;
						listItemToMove = jQuery("#customTourListItem" + movedId).remove();
						//we have to find the insertion point
						if(idAfterMe == null && idBeforeMe != null)
						{//the insertion point is the bottom of the list
							newTop = -1 * (jQuery("#customTourTabLocationsList").find("li").eq(-1).position().top
							//we add the width of a list item so that we can see the add.
							+ jQuery("#customTourTabLocationsList").find("li").eq(-1).height()
							-  (jQuery("#customTourTabLocationsDiv").height() / 2) 
							+ parseInt(jQuery("#customTourTabLocationsDiv").css('line-height')));	
							
						}
						else if(idAfterMe != null)
						{//we're somewhere in the middle or at the beginning- we insert before, so we need to find the one after me.
							newTop = -1 * (jQuery("#customTourListItem" + idAfterMe).position().top
							-  (jQuery("#customTourTabLocationsDiv").height() / 2) 
							+ parseInt(jQuery("#customTourTabLocationsDiv").css('line-height')));
						}
						else{}
						window.setTimeout(timerFunction, 10);													   
					});
				}//end remove is true
				else //we insert
				{
					if(idAfterMe == null && idBeforeMe != null)
					{
						jQuery("#customTourListItem" + idBeforeMe).after(listItemToMove);
					}

					else if(idAfterMe != null)
					{
						jQuery("#customTourListItem" + idAfterMe).before(listItemToMove);
					}
					jQuery(listItemToMove).addClass("new").show(800);
				}
				
			} //end if currentTop == newTop
			else
			{
				jQuery("#customTourTabLocationsList").css("top", currentTop + "px");
				window.setTimeout(timerFunction, 10);
			}
					
		} //end timer function
		
		//update the customTour tab list

		//var insertAfterId = ...
		//scroll to the moved item
		newTop = -1 * (jQuery("#customTourListItem" + movedId).position().top
					-  (jQuery("#customTourTabLocationsDiv").height() / 2) 
					+ parseInt(jQuery("#customTourTabLocationsDiv").css('line-height')));
		var currentTop = parseInt(jQuery("#customTourTabLocationsList").css("top"));
		remove = true;
		window.setTimeout(timerFunction, 20); */
		
	}
		
	this.getType = function()
	{
		return "mapControlCustomTour";
	}
	
	//this draws an add all/remove all button on the location list
	this.drawAddOrRemoveAllButton = function(newJSON, targetId, locationListId, sortableId)
	{
		var self = this;
		var customTourArray = self.getCustomTours();
		
		var buttonId = "addRemoveAllButtonFor" + sortableId;
		var button = jQuery("<img/>").attr("id", buttonId);
		var message = '';
		var spanId = "addRemoveSpanFor" + sortableId;
		var addRemoveSpan = jQuery("<span/>").attr("id", spanId);
		
		button.attr("src", REMOVE_FROM_CUSTOM_TOUR_ICON);
		message = "Clear My Custom Tour";

		addRemoveSpan.empty().html(message);
		jQuery("#" + targetId).append(button).append(addRemoveSpan);
		
		//set the event
		//clear any previous event
		jQuery("#" + buttonId + ", #addRemoveSpanFor" + sortableId).die("click").live("click", function()
		{
			
			var idArray = new Array();
			idArray = self.getLocationsFromSortableList(sortableId).split(',');
			if(idArray.length == 0)
			{
				//remove this button - there's nothing to add or remove
				//jQuery("#" + buttonId).parent().empty();
				return;
			}
	
			for (var index = 0; index < idArray.length; index++)
			{
					self.removeFromCustomTour(idArray[index], true);
			}
			jQuery("#locationListDiv").empty();
			var emptyJSON = new Object();
			emptyJSON.markers = new Object();
			//self.updateMap(emptyJSON);
			switchToSuggested();
				
				
		});

	}

};


