var gDatasetTreeExpanded = true;

//hide or show the text assoicated with the heading that was clicked
function hideShowProviderDatasets(id)  {
	if (is_ie) {
		var sDisplay = "block";
	} else {
		var sDisplay = "table-row";
	}
	$$('tr.Row' + id).each(function (item) {
		if (item.style.display == "none") {
			item.style.display = sDisplay;
		} else {
			item.hide();
		}
	});
}

function hideShowAllDatasets(){
	var i = 1;

	if (gDatasetTreeExpanded) {
		$("tree").update("Expand all");
	} else {
		$("tree").update("Collapse all");
	}
	gDatasetTreeExpanded = !gDatasetTreeExpanded;

	var obj = $("sponsor_" + i);
	while (obj){
		hideShowProviderDatasets(i);
		i++;
		var obj = $("sponsor_" + i);
	}
}


function selectProvider(provider, providerId) {
	var theForm = document.forms['AdvancedDatasetSearch'];
	var providerNum = parseInt(theForm['providerNum'].value);
	for (i = 1; i < providerNum; i++) {
		var objProvider = $("provider" + i);
		if (i == providerId) {
			objProvider.addClassName("currentNavItem");
		} else {
			objProvider.removeClassName("currentNavItem");
		}
	}

	theForm['provider'].value = provider;
	theForm['startAt'].value = 0;		// Initialize the page start.

	submit_advanced_ds_search();
	return true;
}

function submit_advanced_ds_search() {
	var url = g_DATASETS_URL + "ajax_datasets_gm.php";
	var form_name = 'AdvancedDatasetSearch';
	var theForm = document.forms[form_name];

	// The textbox contains scientific names. You need to convert them to ITIS numbers.
	// With ajax, the following codes calls ajax_species_search.php, which returns ITIS numbers.
	// The result is set in speciesTsnList.
	if(theForm['species_advanced'].value != "") {
		var species = theForm['species_advanced'].value;
		dataid_validation(species, "species");
		if (gValidatedDataid == "") {
			return false;
		}
		theForm['speciesTsnList'].value = gValidatedDataid;
	}
	if (radio_value(form_name, 'spatial_interest') == 'roi') {
		set_coords(theForm['coords']);
	}

	var data = $(form_name).serialize();

	// Spatial

	$('result_list').hide();
	$("dataset_list").update("Searching database. Please wait... (may take 30-120 seconds)");
	new Ajax.Request(url,
		{
			parameters: data,
			onSuccess: show_result
		});
}

function show_result(oj) {
	res  = oj.responseText;
	$("dataset_list").update(res);
}

function show_next(shift) {
	var theForm = document.forms['AdvancedDatasetSearch'];
	theForm['startAt'].value = parseInt(theForm['startAt'].value) + shift;
	submit_advanced_ds_search();
	return false;
}

// The following two functions are common for dataset/species advanced search
function add_layer_advanced(dataid, layer_type, layer_name) {
	if ($('search_box_dataset')) {		// ROI popup window includes search_box_dataset textbox
		switch (gSearchType) {
			case "dataset":
				var box_name = "search_box_dataset";
				break;
			case "species":
				dataid = layer_name;	// layer_name=Scientific name should be put into the textbox.
				var box_name = "search_box_species";
				break;
		}
		update_search_box(dataid, box_name);	// Defined in roi.js
	} else {
		gMapserver.request("add_layer", confirm_addition,
			{
				layer_name: dataid,
				layer_type: layer_type
			}, "", {});
	}
}

function confirm_addition(oj) {
	var res = oj.responseXML;
	var dataid = getXmlNodeValue(res, "dataid");
	alert("Layer " + dataid + " is added.\nYou can continue adding datasets/species.\nClose the popup when finished.");

	// Set extent
	if (res.getElementsByTagName("bbox").length > 0) {
		gNumLayerAdded += 1;
		var bbox = getXmlNodeValue(res, "bbox");
		gFullExtent = set_bbox(bbox);
		gCenterPoint = gFullExtent.getCenter();
		map.setCenter(gCenterPoint, gZoomLevel);	// setCenter needed to properly use setZoom
		gZoomLevel = map.getBoundsZoomLevel(gFullExtent);
		map.setZoom(gZoomLevel);
	}

	gMapserver.RefreshMap = true;
}


/***** common for dataset and species *****/
var ThumbnailPopup = null;
function show_thumbnail(event, ds_type, dataid) {
	var posX = Event.pointerX(event) + 20;
	var posY = Event.pointerY(event);
	switch (ds_type) {
		case "dataset":
			var width = 430;
			var height = 350;
			posY -= 320;
			var header = "Dataset";
			var thumbnail_url = BASE_URL + "images/datasets/" + "dataset_" + dataid + "_m.png";
			break;
		case "species":
			var width = 330;
			var height = 230;
			posY -= 200;
			var header = "Species";
			var thumbnail_url = g_MAPSERVICE_URL + "ogc/?REQUEST=GetMap&SERVICE=wms&VERSION=1.1.1&HEIGHT=155&WIDTH=300&LAYERS=bath,spdistr,land,sptsn&sptsn=" + dataid;
			break;
	}
	ThumbnailPopup = new PopupWindow("thumbnail_image", "",
		{
			width: width + 'px',
			height: height + 'px',
			left: posX + "px",
			top: posY + "px"
		});
	ThumbnailPopup.AutoAdjust = false;
	var title = "Thumbnail Map for " + header + ": " + dataid;
	var content = '<BR><img  src="' + thumbnail_url + '" border="0">';
	ThumbnailPopup.show(title, content);
}

function hide_thumbnail(){
	// IE6 sometimes detect ThumbnailPopup as being null
	if (ThumbnailPopup != null) {
		ThumbnailPopup.close();
		ThumbnailPopup = null;
	}
}