// -------------------------------------------------------------
// Handle download. This requires  the inclusion of Time.js, 
// available.js, and customization.js.
//
// Usage:
//   var d = new Download(available);
//                         // Argument is object of type Available.
//   var dt = d.download_type;
//   d.set_download_type(dt);
//   d.generate_download_table("d", miny, maxy);
//                         // Generate table with all downloads.
//                         // First argument is the global variable
//                         // that contains download.
//                         // Second and third are minimum and maximum
//                         // year.
//   d.download(tm, dt);
//                         // Download data. tm is the Time,
//                         // and dt is data type (e.g., CGAS).
// -------------------------------------------------------------

// -------------------------------------------------------------
// Constructor.
// -------------------------------------------------------------

function Download(a) {
    this.available = a;
    this.download_type = get_cookie("download_type");
    if(this.download_type ===null) {
	this.download_type = "hdf";
    }
    this.set_download_type = download_set_download_type;
    this.generate_download_table = download_generate_download_table;
    this.ftp_base = "ftp://l4ftl01.larc.nasa.gov/MISR/";
    this.download = download_download;
    this.type_to_name = new Array();
    this.type_to_name[CGAS] = "CGAS";
    this.type_to_name[CGLS] = "CGLS";
    this.type_to_name[CGAL] = "CGAL";
    this.type_to_name[CGGRP] = "CGGRP";
    this.type_to_name[RCCM] = "CGGRP";
    this.type_to_name[CGCL] = "CGCL";
    this.season_longname_list = 
	new Array("Annual", "Winter", "Spring", 
		  "Summer", "Fall", "January", "February", "March",
		  "April", "May", "June", "July", "August", "September",
		  "October", "November", "December");
    this.season_shortname_list = 
	new Array("ANN", "WIN", "SPR", "SUM", "FALL", "JAN", "FEB",
		  "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP",
		  "OCT", "NOV", "DEC");
}

// -------------------------------------------------------------
// Set download type.
// -------------------------------------------------------------

function download_set_download_type(dt) {
    if(this.download_type ==dt) {
	return;
    }
    this.download_type = dt;
    var today = new Date();
    //var expires = new Date(today.getTime() + (56 * 86400000));
    var expires = new Date(today.getTime() + 3600000);
    set_cookie("download_type", dt, expires);
}

function download_generate_download_table(v, miny, maxy) {
    var y, s, t, i;
    document.writeln("<table border=1>");
    document.writeln("<tr><th>Year</th><th>Season</th>");
    for(i = 0; i < TYPE_LIST.length; ++i) {
	document.writeln("<th>" + TYPE_LIST[i] + "</th>");
    }
    document.writeln("</tr>");
    for(y = miny; y <= maxy; y++) {
	for(s = 0; s < this.season_longname_list.length; s++) {
	    document.writeln("<tr>");
	    if(s ===0) {
		document.writeln("<td rowspan=" + 
				 this.season_longname_list.length + 
				 ">" + y + "</td>");
	    }
	    document.writeln("<td>" + this.season_longname_list[s] + "</td>");
	    for(t = 0; t < TYPE_LIST.length; ++t) {
		document.writeln("<td>");
		if(s ==0 && t ==0) {
		    document.writeln("<A NAME=\"" + y + "\"></A>");
		}
		if(this.available.version(new Time(this.season_shortname_list[s], y, 0), t)) {
		    document.writeln(
                      "<a href='#' onClick='" + v + ".download(new Time(\"" +
                      this.season_shortname_list[s] + "\" ," + y + ", 0), " + t + 
                      "); return false;'>Download data</a></td>");
		} else {
		    document.writeln("Not available</td>");
		}
	    }
	    document.writeln("</tr>");
	}
    }
    document.writeln("</table>");
}

// -------------------------------------------------------------
// Download data
// -------------------------------------------------------------

function download_download(tm, dt) {
    var v = this.available.version(tm, dt);
    var esdt = this.available.esdt(tm, dt);
    if(!v || !esdt) {
	alert("Data is not available.");
	return;
    }
    var fname = this.ftp_base + esdt + "/" + tm.data_pool_time() + "/MISR_AM1_" +
	this.type_to_name[dt] + "_" +
	tm.misr_file_name_time() + "_" + v + "." + this.download_type;
    location = fname;
}

