// Function for SuckerFish MyTango menu IE fix

function startMenu(ul_id) { // Insert 'id' value for suckerfish menu
	if (document.all && document.getElementById) {
		navRoot = document.getElementById(ul_id);
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName == "LI") {
				node.onmouseover = function() { this.className += " over"; }
				node.onmouseout = function() { this.className = this.className.replace(" over", ""); }
			}
		}
	}
}

//var lastCityName = '';
//var lastCountryId = '';

function getCities() {
// Used by the CityHelper::CityPicker() method to update the map with city points.
	
	var countryId = $("countrySelect").options[$("countrySelect").selectedIndex].value;
	var cityName = $("cityInput").value;
	
	if(cityName != lastCityName || countryId != lastCountryId) { // the value of the input fields has changed

		// Store the input values for next time
		lastCityName = cityName;
		lastCountryId = countryId;
		
		// Hide the non-busy messages
		$("mapContainer").style.display = "none";
		$("beforeClick").style.display = "none";
		$("afterClick").style.display = "none";
		$("noCities").style.display = "none";
		$("gettingCitiesError").style.display = "none";
		
		
		
		var url = "http://"+window.location.hostname + "/cities/get_cities/"+countryId+"/"+cityName;
		
		var failure = function(req) {
			//alert("Failure");
			$("gettingCitiesError").style.display = "block";
		}
		
		var success = function(req) {
		
			//alert("Success");
			
			$("gettingCities").style.display = "none";
			
			var cities = req.responseXML.documentElement;
			
			if(cities.childNodes.length > 1) { // there's more than one city
				
				// Re/create map
				var map = new YMap($("mapContainer"));
				map.setMapType(YAHOO_MAP_REG);
				map.addPanControl();
				map.addZoomLong();
				
				var points = Array();
				for(var i = 0; i < cities.childNodes.length; i++) {
					
					var id = cities.childNodes[i].childNodes[0].firstChild.nodeValue;
					var latitude = cities.childNodes[i].childNodes[4].firstChild.nodeValue;
					var longitude = cities.childNodes[i].childNodes[5].firstChild.nodeValue;
					
					var point = new YGeoPoint(latitude, longitude);
					var marker = new YMarker(point);
					points.push(point);
					
					// Define what happens when a marker is clicked
					function onMarkerClick(marker, id) { 
						// enclose the "marker" and "id" so we have access 
						// to them after we make the marker
						return(function() {
							// Set the cityId field somewhere on the page
							$("cityId").value = id;
							
							// change the marker to indicate markedness
							marker.addLabel("selected");
							
							// change the instructions
							$("beforeClick").style.display = "none";
							$("afterClick").style.display = "block";
						});
					}
					
					// create an instance of the function hand to the Capture()
					var functRef = onMarkerClick(marker, id);
					
					YEvent.Capture(marker, EventsList.MouseClick, functRef);
					map.addOverlay(marker);
				}
				
				// Draw map
				var best = map.getBestZoomAndCenter(points);
				if(best.zoomLevel > 8) {
					var zoom = best.zoomLevel + 1;
				} else {
					var zoom = 8;
				}
				map.drawZoomAndCenter(best.YGeoPoint, zoom);
				
				// display map and instructions
				$("beforeClick").style.display = "block";
				$("mapContainer").style.display = "block";
				
				// Hide others
				$("noCities").style.display = "none";
				
			} else if (cities.childNodes.length == 1) { // There's only one city
			
				var id = cities.childNodes[0].childNodes[0].firstChild.nodeValue;
				$("cityId").value = id;
				$("afterClick").style.display = "block";
				
			} else { // No Cities
			
				$("noCities").style.display = "block";
			}
		}
		
		// Show the busy message
		$("gettingCities").style.display = "block";
		
		var request = new Ajax.Request(
			url, 
			{
				method: "get",
				onFailure: failure,
				onSuccess: success
			}
		);
	
	} else { // the value of the input fields has not changed
		// do nothing
	}
}




var search_criteria_count = 0;

// Used to add another search criteria to the sentence
function searchAppendCriteria(id_of_element_to_show){
	// var search_criteria;
  // console.log('Inserting element: ' + id_of_element_to_show);
	search_criteria_count += 1;
	id_of_new_criteria = "search_criteria_" + search_criteria_count;
	
	// console.log("Building: " + id_of_new_criteria);
	new Insertion.Bottom('criteria_go_here', '<li id="' + id_of_new_criteria + '"></li>');
  
	// console.log("Inserting " + $(id_of_element_to_show).innerHTML + ". into " + id_of_new_criteria);
	$(id_of_new_criteria).update($(id_of_element_to_show).innerHTML);
	
	// console.log("Making sure the ids are friendly and non conflicting");
	$$('#' + id_of_new_criteria + " select", "#" + id_of_new_criteria + " input").each(function(input) { 
		$(input).id =   $(input).id + '_' + search_criteria_count;
		$(input).name = $(input).name + '[]';
	});
	
	$$('#' + id_of_new_criteria + " a.remove").each(function(link) { 
		$(link).id = $(link).id + '_' + search_criteria_count;
	});
	// console.log('Now displaying ' + search_criteria_count + ' search criterias.');
}

// This is called on change of the type of criteria being added (the first select box)
function searchAddCriteriaSelect(id_of_element_to_show, id_of_selector){
	length_of_element_to_show = id_of_element_to_show.length;
	
	// This is needed to help us determine what search criteria we are operating on...
	this_search_criteria_count = id_of_selector.substring(id_of_selector.lastIndexOf('_') + 1);
  id_of_element_to_show = "search" + id_of_element_to_show + '_' + this_search_criteria_count;
	// console.log("thinking the id is: " + id_of_element_to_show);
  // Hide all other select boxes first
  $$('#search_criteria_' + this_search_criteria_count + ' .search_criteria_selects', '#search_criteria_' + this_search_criteria_count + ' .search_criteria_input').each(function(select) { 
		// console.log('Trying to hide ' + select.id);
		Element.hide(select);
		$(select).value='';
	});
	
	
	// Don't try to show anything if the id_of_element_to_show is blank
	if(length_of_element_to_show > 0) {
	  Element.show(id_of_element_to_show);
	}
}

function searchRemoveCriteria(id_of_remove_button){
	// console.log('Trying to remove ' + id_of_remove_button);
	this_criteria_id = searchGetCriteriaId(id_of_remove_button);
	// console.log('Removing value from: ' + $(id_of_remove_button).id);
	
	// console.log('Trying to remove search_criteria_' + this_criteria_id);
	Effect.Fade($('search_criteria_' + this_criteria_id));
	$$('#search_criteria_' + this_criteria_id + " select", '#search_criteria_' + this_criteria_id + " .search_criteria_input").each(function(select) {
		// console.log('Removing value from: ' + $(select).id);
		$(select).value = '';
		// console.log('Removing value from: ' + $(select).value);
	});
}


function searchGetCriteriaId(id_string){
	return id_string.substring(id_string.lastIndexOf('_') + 1);
}

function searchShowCityPicker(city_picker_id, value){
	if(value == 'blank' && $(city_picker_id).style.display != 'none'){
		Effect.BlindUp($(city_picker_id));
		$$('#cityPickerThing select').each(function(select) { 
			$(select).value='';
		});
	  $('cityId').value = '';
		$('cityInput').value = '';
		
	}
	else if($(city_picker_id).style.display == 'none')
		Effect.BlindDown($(city_picker_id));		
	if(value == 2) {
		// show the date range
	}
	
}
