var cruise_station_list = new Array();

function PrismCruiseStation(sta_id, sta_sname, sta_lname, longitude, latitude, z_min, dtime_start, dtime_end, sensor_cnt, platform_handle, platform_lname, state, provider, cruises) {
	this.vis = false;
	this.id = this.getPrismCruiseStationID(sta_sname); //P15, e.g.  //PrismCruiseStation.getPrismCruiseStationID();
	
	this.station_id = sta_id;
	this.sta_sname = sta_sname;
	this.sta_lname = sta_lname;
	this.lon = longitude;
	this.lat = latitude;
	this.max_depth = z_min;
	this.time_first = dtime_start;
	this.time_end = dtime_end;
	this.sensor_count = sensor_cnt;
	this.platform_handle = platform_handle;
	this.platform_lname = platform_lname;
	this.state = state;
	this.provider = provider;
	
	this.cruises = cruises;
	
	this.opts = new PrismCruiseStationMarkerOpts();
	//this.opts.bubble_size = (chart_dim.height < 620 || chart_dim.width < 640)? "small": "large";
	
	this.station_number = this.sta_sname.replace("Station P", "");
	this.icon = new GIcon(station_default, "/nvs/images/icons/stations/station-balloon-"+this.station_number+".png", null, null);
	//this.icon = new GIcon(station_default, "/nvs/images/asset_icons/marker-point_v1.gif", null, null);
	
	this.html = null; //this.createHTML();
	
	this.point = new GLatLng(this.lat, this.lon);
	this.marker = this.createMarker(this.point, this.icon);
	
	//register prism cruise station in list
	cruise_station_list[this.id] = this;
	
	var station_id = this.id;
	this.plot_img_obj = new Image();
	this.plot_img_obj.onload = function() {cruise_station_list[station_id].processChangeVarImage()};
	
	this.table_width = {"large": 530, "small": 350};
	this.plot_width = {"large": 240, "small": 165};
	this.plot_height = {"large": 320, "small": 220};
	this.var_div_height = {"large": 356, "small": 256};
}

PrismCruiseStation.getSensorAbbrev = function(sensor_name) {
	var sensor_abbrev = sensor_name;

	switch (sensor_name) {
		case "Ammonium Concentration": sensor_abbrev = "Ammonium Conc."; break;
		case "Chl Concentration": sensor_abbrev = "Chl Conc."; break;
		case "Light Transmissivity": sensor_abbrev = "Light Trans."; break;
		case "Oxygen Concentration": sensor_abbrev = "Oxygen Conc."; break;
		case "Nitrate Concentration": sensor_abbrev = "Nitrate Conc."; break;
		case "Nitrite Concentration": sensor_abbrev = "Nitrite Conc."; break;
		case "Phaeophytin Concentration": sensor_abbrev = "Phaeophytin Conc."; break;
		case "Phosphate Concentration": sensor_abbrev = "Phosphate Conc."; break;
		case "Photosynthetically Active Radiation": sensor_abbrev = "Photo. Act. Rad."; break;
		case "Potential Density Sigmat": sensor_abbrev = "Pot. Dens. Sigma-T"; break;
		case "Silicate Concentration": sensor_abbrev = "Silicate Conc."; break;
		case "Turbidity": break;
		case "Water Pressure": break;
		case "Water Salinity": break;
		case "Water Temperature": sensor_abbrev = "Water Temp."; break;
	}
	
	return sensor_abbrev;
}

PrismCruiseStation.getSampleDescAbbrev = function(sample_description) {
	var sample_desc_abbrev = sample_description;

	switch (sample_description) {
		case "Uncalibrated": sample_desc_abbrev = "Uncal."; break;
		case "Calibrated": sample_desc_abbrev = "Cal."; break;
		case "Analyzed": sample_desc_abbrev = "Analy."; break;
	}
	
	return sample_desc_abbrev;
}

PrismCruiseStation.prototype.getPrismCruiseStationID = function(sta_sname) {
	var name_pieces = sta_sname.split(" ");
	return name_pieces[1]; //P15, e.g.
}

PrismCruiseStation.prototype.createHTML = function() {
	var html = "";
		
	html += "<div class='markerContent'>";
		html += "<p class='markerHead'>"+this.sta_lname+"</p>";
		
		html += "<div class='area center markerVSpace'>";
			html += "<b>Cruise&nbsp;&nbsp;</span></b>";
			html += "<select id='cruise_menu_"+this.id+"' name='cruise_menu_"+this.id+"' onChange='cruise_station_list[\""+this.id+"\"].changeCruise();'>";
				html += "<option value=''>Updating...</option>";
			html += "</select>";
			html += "<b>&nbsp;&nbsp;<span id='title-num_cruises'></span></b>";
		html += "</div>";
		
		html += "<div class='area markerVSpace'>";
			html += "<div id='casts_content_"+this.id+"'>";
				html += "<table class='layoutAssistant' align='center'>";
					html += "<tr>";
						html += "<td class='default'>";
							html += "<table class='chart'>";
									html += "<tr>";
										html += "<td class='chartButtonLabel'><b>&nbsp;</b></td>";
									html += "</tr>";
									html += "<tr>";
										html += "<td class='chartButtonInvis'>&nbsp;</td>";
									html += "</tr>";
								html += "</table>";
						html += "</td>";
					html += "</tr>";
				html += "</table>";
			html += "</div>";
		html += "</div>";
		
	
		html += "<div class='area markerVSpace'>";
			html += "<table class='layoutAssistant' style='margin-top: 0px;' width='"+this.table_width[MapOpts.map_size]+"'>";
				html += "<tr> \n";
					html += "<td class='default' width='1%'>";
						html += "<a id='a_var_img_"+this.id+"' class='dataImage' style='width: "+this.plot_width[MapOpts.map_size]+"px; height: "+this.plot_height[MapOpts.map_size]+"px;' href='javascript:;'><img id='var_img_"+this.id+"' class='dataImage' src='/images/spacer.gif' border='0'><img id='magnify_plot-"+this.id+"' class='magnifyPlot' src='/nvs/images/misc_buttons/btn_magnify.gif' width='22' height='22' border='0'></a>";
							html += "<div id='download_holder' class='downloadHolder' style='margin: 3px 0px;'>";
								html += "<a id='download_csv_"+this.id+"' href='javascript:;' title='Download CSV File of Profile Data' onMousedown='changeImageN(\"download_csv_"+this.id+"\", \"csv_img_"+this.id+"\", \"/nvs/images/misc_buttons/btn_download_v1-hl.gif\"); return true;' onMouseup='changeImageN(\"download_csv_"+this.id+"\", \"csv_img_"+this.id+"\", \"/nvs/images/misc_buttons/btn_download_v1-norm.gif\"); return true;'><img id='csv_img_"+this.id+"' src='/nvs/images/misc_buttons/btn_download_v1-disabled.gif' width='25' height='31' border='0'></a>";
								html += "<div id='data_opt' class='dataFormatOpt'>";
									html += "<a href='javascript: cruise_station_list."+this.id+".initiateDownload(\"csv\");'>CSV</a>";
									html += "<a href='javascript: cruise_station_list."+this.id+".initiateDownload(\"csv_excel\");'>CSV - for Excel</a>";
									html += "<a href='javascript: cruise_station_list."+this.id+".initiateDownload(\"json\");'>JSON</a>";
									html += "<a href='javascript: cruise_station_list."+this.id+".initiateDownload(\"xml\");'>XML</a>";
								html += "</div>";
							html += "</div>";
					html += "</td> \n";
					html += "<td class='mediumSpacer' width='1%'></td> \n";
					html += "<td class='default' width='98%'> \n";
						html += "<div id='variables_"+this.id+"' style='height: "+this.var_div_height[MapOpts.map_size]+"px; padding: 1px; overflow: auto;'></div>";
					html += "</td> \n";
				html += "</tr> \n";
			html += "</table> \n";
		html += "</div>";
		
		var link_box_width = (MapOpts.map_size == "small")? 45: 70;
		html += "<p class='default' style='margin-top: 4px; white-space: nowrap;'><a class='iwa' href='javascript: cruise_station_list[\""+this.id+"\"].toggleInfoWinLinkVis();' title='View Link for this Info Window'><b>Link</b></a>&nbsp;&nbsp;<input id='info_window_link' name='info_window_link' class='linkTextBox'  type='text' size='"+link_box_width+"' value='' readonly style='visibility: hidden;'/></p>";
	
	html += "</div>";
	
	return html;
}

PrismCruiseStation.prototype.createMarker = function(point, icon) {
	var marker = new GMarker(point, icon);
	var prism_cruise_station = this;
	
	GEvent.addListener(marker, "mouseover", function() {
		mapTip.display(prism_cruise_station.sta_sname);
	});
	
	GEvent.addListener(marker, "mouseout", function() {
		mapTip.hide();
	});
	
	GEvent.addListener(marker, "click", function() {
		mapTip.hide();
		prism_cruise_station.manuallyOpenInfoWindow();
	});
	
	GEvent.addListener(marker, "infowindowopen", function() { //fires when window actually exists
		prism_cruise_station.loadCruises();
	});
	
	GEvent.addListener(marker, "infowindowclose", function() { //fires when window actually closes
		prism_cruise_station.opts.resetValues();
	});
	
	return marker;
}

PrismCruiseStation.prototype.openInfoWindow = function() {
	if (!this.html) this.html = this.createHTML();
	this.marker.openInfoWindowHtml(this.html);
}

PrismCruiseStation.prototype.manuallyOpenInfoWindow = function() {
	trackUserActionNVS("Opened Cruise Station", "User", [this.id]);
	this.openInfoWindow();
}

PrismCruiseStation.prototype.autoOpenInfoWindow = function(cruise, cast, sensor) {
	this.opts.sel_cruise = cruise;
	this.opts.sel_cast = cast;
	this.opts.sel_sensor = sensor;
	trackUserActionNVS("Opened Cruise Station", "NVS", ["PRISM Cruise", this.id]);
	this.openInfoWindow();
}

PrismCruiseStation.prototype.showOverlay = function() {
	map.addOverlay(this.marker);
}

PrismCruiseStation.prototype.hideOverlay = function() {
	map.removeOverlay(this.marker);
}

PrismCruiseStation.prototype.toggleInfoWinLinkVis = function() {
	if (document.getElementById("info_window_link")) {
		var info_window_link = document.getElementById("info_window_link");
		if (info_window_link.style.visibility == "visible") {
			info_window_link.style.visibility = "hidden";
		} else {
			info_window_link.style.visibility = "visible";
			info_window_link.select();
		}
		this.hideDownloadFormatOpts();
	}
}

PrismCruiseStation.prototype.updateInfoWinLink = function() {
	var path = "section="+nvs_path;
	var params = new Array();
		params.push("action::auto_open");
		params.push("station::"+this.id);
		if (this.opts.sel_cruise != "" && this.opts.sel_cast != "" && this.opts.sel_sensor != "") {
			params.push("cruise::"+this.opts.sel_cruise);
			params.push("cast::"+this.opts.sel_cast);
			params.push("sensor::"+this.opts.sel_sensor);
		}
	var url = "http://"+getURLDomain()+"/nvs/nvs.php?"+path+"&"+"infoWindow="+params.join("||");
	setValue("info_window_link", url);
}

PrismCruiseStation.prototype.loadCruises = function() {
	var menu = document.getElementById("cruise_menu_"+this.id);
	menu.options.length = 0; //clear option list
	var valid_sel_cruise = false;
	
	var num_cruises = 0;
	for (var i in this.cruises) {
		if (inArray(this.cruises[i], visible_cruises)) {
			var value = this.cruises[i];
			var date_pieces = this.cruises[i].split("_");
			var label = date_pieces[1]+" "+abbrevMonthToLongMonth(date_pieces[0]);
			menu.options[menu.length] = new Option(label, value);
			if (this.opts.sel_cruise == value) {
				menu.options[menu.length-1].selected = true;
				valid_sel_cruise = true;
			}
			num_cruises++;
		}
	}
	
	if (!valid_sel_cruise) { //if specified cruise is invalid, so are cast and sensor
		this.opts.sel_cast = "";
		this.opts.sel_sensor = "";
		//opts.sel_cruise will be automatically set in changeCruise()
	}
	
	document.getElementById("title-num_cruises").innerHTML = num_cruises;
	
	this.changeCruise();
}

PrismCruiseStation.prototype.changeCruise = function() {
	this.opts.sel_cruise = document.getElementById("cruise_menu_"+this.id).value;
	this.hideDownloadFormatOpts();
	this.loadCruiseCasts();
}

PrismCruiseStation.prototype.loadCruiseCasts = function() {
	document.getElementById("casts_content_"+this.id).style.visibility = "hidden";
	
	var station_id = this.id;
	var station_sname = "Station%20"+this.id; //spaces " " must replaced with "%20"
	var cruise_name = "PRISM%20Cruise%20"+this.opts.sel_cruise; //spaces " " must replaced with "%20"
	
	var query_url = "http://"+getURLDomain()+"/nvs/assets/iaps/prism_cruises/get_prism_cruise_casts.php?cruise_name="+cruise_name+"&station_sname="+station_sname;
//window.alert(query_url);
	new AjaxRequest(query_url, function(response){cruise_station_list[station_id].processCruiseCasts(response)});
}

PrismCruiseStation.prototype.processCruiseCasts = function(response) {
	var casts = parseJSON(response);
	if (casts && casts.success) {
		var html = "";
		var valid_sel_cast = false;
		this.opts.cast_info = new Array(); //permanent coll_id -> date & time collection
		if (casts.result.length > 0) {
			
			//loop through casts and assemble casts and subcasts
			var cast_arr = new Array();
			var casts_processed = new Array(); //keeps track of unique casts in order to prevent duplicates
			for (var i in casts.result) {
				if (!inArray(casts.result[i].fixed_date+" - "+casts.result[i].qualifier, casts_processed)) { //don't allow duplicate casts
					casts_processed.push(casts.result[i].fixed_date+" - "+casts.result[i].qualifier);
					
					var date_code = casts.result[i].fixed_date;
					var date_pieces = date_code.split(" ");
					var date = date_pieces[0].split("-");
					var year = date[0];
					var month = intToMonth(date[1], "short");
					var day = (date[2].charAt(0) == "0")? date[2].charAt(1): date[2];
					var time_code = date_pieces[1];
					var time_pieces = time_code.split(".");
					var time = time_pieces[0];
					if (casts.result[i].qualifier == "primary") { //primary downcast
						var cast_type = "Downcast";
					} else if (casts.result[i].qualifier == "secondary") { //secondary downcast
						var cast_type = "Downcast";
					} else if (casts.result[i].qualifier == "upcastbottle") { //only upcast
						var cast_type = "Upcast";
					} else {
						var cast_type = "Cast";
					}
					
					var date_label = day+" "+month+" "+year+" - "+time;
					var index = "c_"+date_pieces[0]+"_"+time;
					
					if (!cast_arr[index]) {
						cast_arr[index] = {"date_label": date_label, "casts": {"Downcast": "", "Upcast": ""}}
					}
					cast_arr[index]["casts"][cast_type] = casts.result[i].coll_id;
					
					this.opts.cast_info[casts.result[i].coll_id] = {"date": date_pieces[0], "time": time.replace(/:/g, "-"), "cast_type": cast_type.charAt(0)};
				}
				
				if (casts.result[i].coll_id == this.opts.sel_cast) {
					valid_sel_cast = true;
				}
			}
			
			if (!valid_sel_cast) {
				this.opts.sel_cast = casts.result[0].coll_id;
				this.opts.sel_sensor = "";
			}

			//loop through cast groups and build html - number of cast sets is limited to 3
			var cast_type_arr = ["Downcast", "Upcast"];
			html += "<table class='layoutAssistant' align='center'>";
				html += "<tr>";
					var html_arr = new Array();
					var num_cast_sets = 0;
					for (var i in cast_arr) {
						var html_item = "";
						html_item += "<td class='default'>";
							var num_sub_casts = 0;
							for (var j in cast_type_arr) {
								if (cast_arr[i]["casts"][cast_type_arr[j]] != "") num_sub_casts++;
							}
							html_item += "<table class='chart'>";
									html_item += "<tr>";
										html_item += "<td class='chartButtonLabel' colspan="+num_sub_casts+" nowrap>"+"<b>"+cast_arr[i].date_label+"</b>"+"</td>";
									html_item += "</tr>";
									html_item += "<tr>";
										for (var j in cast_type_arr) {
											var cast_type = cast_type_arr[j];
											if (cast_arr[i]["casts"][cast_type] != "") {
												var cast = cast_arr[i]["casts"][cast_type];
												var button_class = (cast == this.opts.sel_cast)? "chartButton selected": "chartButton"; //"buttonSelected": "button";
												html_item += "<td id='cast_button_"+cast+"' class='"+button_class+"' width='50%'>"+"<a class='button' href='javascript: cruise_station_list[\""+this.id+"\"].changeCast("+cast+");'>"+"<b>"+cast_type+"</b>"+"</a>"+"</td>";
											}
										}
									html_item += "</tr>";
								html_item += "</table>";
						html_item += "</td>";
						html_arr.push(html_item);
						num_cast_sets++;
						if (num_cast_sets == 3) break; //limits number of cast sets to 3
					}
					html += html_arr.join("<td class='largeSpacer'></td>");
				html += "</tr>";
			html += "</table>";

		} else { //no casts for the selected station and cruise
			this.opts.sel_cast = "";
			html += "<table class='layoutAssistant' align='center'>";
				html += "<tr>";
					html += "<td class='default'>";
						html += "<table class='chart'>";
								html += "<tr>";
									html += "<td class='chartButtonLabel'><b>No casts found for the selected station and cruise.</b></td>";
								html += "</tr>";
								html += "<tr>";
									html += "<td class='chartButtonInvis'>&nbsp;</td>";
								html += "</tr>";
							html += "</table>";
					html += "</td>";
				html += "</tr>";
			html += "</table>";
		}
		
		document.getElementById("casts_content_"+this.id).innerHTML = html;
		document.getElementById("casts_content_"+this.id).style.visibility = "visible";
		this.loadCastVariables();
	}
}

PrismCruiseStation.prototype.changeCast = function(cast) {
	if (cast != this.opts.sel_cast) {
		var selected = "cast_button_"+cast;
		document.getElementById(selected).className = "chartButton selected";
		var old_selected = "cast_button_"+this.opts.sel_cast;
		document.getElementById(old_selected).className = "chartButton";
		this.opts.sel_cast = cast;
		
		this.hideDownloadFormatOpts();
		this.loadCastVariables();
	}
}

PrismCruiseStation.prototype.loadCastVariables = function() {
	if (this.opts.sel_cast != "") {
		document.getElementById("variables_"+this.id).innerHTML = "<img class='loadingContent' src='/images/spacer.gif' width='36' height='36'>";
		//this.clearVarImage();
		
		var station_id = this.id;
		
		//spaces " " must replaced with "%20"
		var query_url = "http://"+getURLDomain()+"/nvs/assets/iaps/prism_cruises/get_prism_cruise_variables.php?coll_id="+this.opts.sel_cast;
//window.alert(query_url);
		new AjaxRequest(query_url, function(response){cruise_station_list[station_id].processCastVariables(response)});
	}
}

PrismCruiseStation.prototype.processCastVariables = function(response) {
	var variables = parseJSON(response);
	if (variables && variables.success) {
		var valid_sel_sensor = false;
		for (var i in variables.result) {
			if (variables.result[i].sensor_id == this.opts.sel_sensor) valid_sel_sensor = true;
		}
		if (!valid_sel_sensor) this.opts.sel_sensor = variables.result[0].sensor_id;
		
		this.opts.sensor_info = new Array();
		var html = "";
		html += "<table class='chart' width='100%'>";
		for (var i in variables.result) {
			switch (variables.result[i].sensor_descr) {
				case "insitu CTD, uncalibrated values":
						var sample_description = "Uncalibrated";
						var var_ext = "U";
						break;
				case "insitu CTD, calibrated values":
						var sample_description = "Calibrated";
						var var_ext = "C";
						break;
				case "sample extracted and analyzed elsewhere":
						var sample_description = "Analyzed";
						var var_ext = "A";
						break;
				default:
						var sample_description = "N/A";
						var var_ext = "Z";
						break
			}

			var sensor = variables.result[i].sensor_id; //should be unique integer
			var variable_label = capitalizeString(variables.result[i].obs_type_name.replace(/_/g, " "));
			
			if (MapOpts.map_size == "small") { //optimize button labels for small windows
				variable_label = PrismCruiseStation.getSensorAbbrev(variable_label);
				sample_description = PrismCruiseStation.getSampleDescAbbrev(sample_description);
			}
			
			html += "<tr>";
				var button_class = (sensor == this.opts.sel_sensor)? "chartButtonL selected": "chartButtonL";
				html += "<td id='cvbl_"+sensor+"' class='"+button_class+"'>"+"<a class='button' href='javascript: cruise_station_list[\""+this.id+"\"].changeCastVar(\""+sensor+"\");'><b>"+variable_label+"&nbsp;</b></a>"+"</td>";
				var button_class = (sensor == this.opts.sel_sensor)? "chartButtonR selected": "chartButtonR";
				html += "<td id='cvbr_"+sensor+"' class='"+button_class+"'>"+"<a class='button' href='javascript: cruise_station_list[\""+this.id+"\"].changeCastVar(\""+sensor+"\");'>"+sample_description+"</a>"+"</td>";
			html += "</tr>";
			
			this.opts.sensor_info[sensor] = {"var_label": variable_label, "units": variables.result[i].units};
		}
		html += "</table>";
	} else {
		html += "";
	}
	document.getElementById("variables_"+this.id).innerHTML = html;
	this.changeVarImage();
}

PrismCruiseStation.prototype.changeCastVar = function(sensor) {
	if (sensor != this.opts.sel_sensor) {
		var selected_l = "cvbl_"+sensor;
		var selected_r = "cvbr_"+sensor;
		document.getElementById(selected_l).className = "chartButtonL selected";
		document.getElementById(selected_r).className = "chartButtonR selected";
		
		var old_selected_l = "cvbl_"+this.opts.sel_sensor;
		var old_selected_r = "cvbr_"+this.opts.sel_sensor;
		document.getElementById(old_selected_l).className = "chartButtonL";
		document.getElementById(old_selected_r).className = "chartButtonR";
		
		this.opts.sel_sensor = sensor;
		
		this.hideDownloadFormatOpts();
		this.changeVarImage();
	}
}

PrismCruiseStation.prototype.changeVarImage = function() {
	var this_id = this.id;
	
	this.updateInfoWinLink();
	this.updateDataDownloads();
	//this.clearVarImage();
	//this.getVarImage();
	setSrc("var_img_"+this.id, "/images/spacer.gif");
	//setClassName("var_img_"+this.id, "loadingImage");
	setClassName("a_var_img_"+this.id, "loadingImage");
	setHREF("a_var_img_"+this.id, "");
	setSrc("download_csv_"+this.id, "/nvs/images/btn_download_v1-disabled.gif");
	setHREF("download_csv_"+this.id, "");
	
	var url = "http://"+getURLDomain()+"/nvs/assets/iaps/prism_cruises/get_prism_cruise_profile_plot.php";
	var params = new Array();
		params.push("station="+this.id);
		params.push("cruise="+this.opts.sel_cruise);
		params.push("cast="+this.opts.sel_cast);
		params.push("sensor="+this.opts.sel_sensor);
		var x_label = this.opts.sensor_info[this.opts.sel_sensor].var_label+" ("+this.opts.sensor_info[this.opts.sel_sensor].units+")";
		params.push("value_label="+x_label);
		params.push("plot_size="+"small");
		var random_number = Math.floor(Math.random()*100001); //random number cache avoiding
		params.push("rand="+random_number);
		
	var query_url = url+"?"+params.join("&");
	
	this.plot_img_obj.src = query_url;
	
	//setSrc("var_img_"+this.id, query_url);
	setHREF("a_var_img_"+this.id, query_url.replace("plot_size=small", "plot_size=large"));
	setHREFTarget("a_var_img_"+this.id, "_blank");
	//setTimeout("setClassName('var_img_"+this_id+"', 'dataImage');", 20000);
	setTimeout("setClassName('a_var_img_"+this_id+"', 'dataImage');", 20000);
	
	trackUserActionNVS("Viewed Plot", "User", ["PRISM Cruise", this.id, this.opts.sel_cruise, this.opts.sel_cast, this.opts.sel_sensor]);
}

PrismCruiseStation.prototype.processChangeVarImage = function() {
	if (this.plot_img_obj.width > 0) { //make sure image has been loaded
		var a_width = getStyleWidth("a_var_img_"+this.id);
		var scale = a_width / this.plot_img_obj.width;
		setSrc("var_img_"+this.id, this.plot_img_obj.src);
		setStyleWidth("var_img_"+this.id, a_width);
		setStyleHeight("var_img_"+this.id, Math.round(this.plot_img_obj.height * scale));
		setClassName("a_var_img_"+this.id, 'dataImage');
	}
}

PrismCruiseStation.prototype.updateDataDownloads = function() {
	var url = "http://"+getURLDomain()+"/nvs/assets/iaps/prism_cruises/get_prism_cruise_data_availability.php";
	var params = new Array();
		params.push("cast="+this.opts.sel_cast);
		params.push("sensor="+this.opts.sel_sensor);
		
	var query_url = url+"?"+params.join("&").replace(/ /g, "%20"); //spaces " " must replaced with "%20"
	var station_id = this.id;
	new AjaxRequest(query_url, function(response){cruise_station_list[station_id].processUpdateDataDownloads(response)});
}

PrismCruiseStation.prototype.processUpdateDataDownloads = function(response) {	
	var result = parseJSON(response);
	
	if (result.data_exists) {
		setSrc("csv_img_"+this.id, "/nvs/images/misc_buttons/btn_download_v1-norm.gif");
		//setHREF("download_csv_"+this.id, "javascript: cruise_station_list."+this.id+".downloadData('csv');");
		setHREF("download_csv_"+this.id, "javascript: cruise_station_list."+this.id+".toggleDownloadFormatOpts();");
	}
}

PrismCruiseStation.prototype.hideDownloadFormatOpts = function() {
	setDisplay("data_opt", "none");
}

PrismCruiseStation.prototype.showDownloadFormatOpts = function() {
	this.hideDownloadFormatOpts();
	setDisplay("data_opt", "block");
	var container_w = getObjWidth("download_holder");
	var opts_w = getObjWidth("data_opt");
	setStyleLeft("data_opt", Math.round((container_w - opts_w) / 2));
}

PrismCruiseStation.prototype.toggleDownloadFormatOpts = function() {
	if (getDisplay("data_opt") == "block") {
		this.hideDownloadFormatOpts();
	} else {
		this.showDownloadFormatOpts();
	}
}

PrismCruiseStation.prototype.initiateDownload = function(format) {
	this.downloadData(format);
	this.hideDownloadFormatOpts();
}

PrismCruiseStation.prototype.downloadData = function(format) {
	var url = "http://"+getURLDomain()+"/nvs/assets/iaps/prism_cruises/get_prism_cruise_download_data.php";
	var params = new Array();
		params.push("station="+this.id);
		params.push("cruise="+this.opts.sel_cruise);
		params.push("cast="+this.opts.sel_cast);
		params.push("sensor="+this.opts.sel_sensor);
		var sensor_label = this.opts.sensor_info[this.opts.sel_sensor].var_label+" ("+this.opts.sensor_info[this.opts.sel_sensor].units+")";
		params.push("sensor_label="+sensor_label);
		params.push("data_type="+format);
		
	var href = url+"?"+params.join("&");
	window.location = href;
	
	trackUserActionNVS("Downloaded Data", "User", ["PRISM Cruise", this.id, this.opts.sel_cruise, this.opts.sel_cast, this.opts.sel_sensor, format]);
}


function PrismCruiseStationMarkerOpts() {
	this.sel_cruise = "";
	this.sel_cast = "";
	this.sel_sensor = "";
	this.sensor_info = "";
	this.cast_info = "";
}

PrismCruiseStationMarkerOpts.prototype.resetValues = function() {
	this.sel_cruise = "";
	this.sel_cast = "";
	this.sel_sensor = "";
	this.sensor_info = "";
	this.cast_info = "";
}

