// -------------------------------------------------------------
// Variable used throughout script.
// -------------------------------------------------------------

var fn;				// Object that determines image file name.
var base_small;			// Array of data file base, indexed by
				// the field type. For small images
var base_large;                 // Same thing for large images.
var field_type_index;		// Selected field type.
var field_type_index_arr;	// Selected field types for each data type
var selected_time;		// Selected time.
var animation;			// Object used for animation.
var animate_type;		// Type of animation.
var animate_advanced_window;	// Animation window, if active.
var window_array = new Array;	// List of windows that we have created, 
                                // so they can be cleanup when we are done.
var clean_up_window = 1;	// If true, then cleanup closes the windows 
				// in window_array. This is used so that 
				// loading other data types through go_to 
				// doesn't delete windows.
var dn;				// Object used to do download.
var min_day = new Time("MAR", 2000, 1);
var max_day = new Time("DEC", 2010, 31);
var old_time;			// Time before animation started.
var old_camera;			// Camera before animation started.
var old_height;			// Height before animation started.

// -------------------------------------------------------------
// Constants.
// -------------------------------------------------------------

var USE_BIOME_TYPE;
var USE_HEIGHT;
var CGAS, CGLS, CGAL, CGCL, CGGRP, RCCM;
var TYPE_LIST;
var NUMBER_TYPE;		// These are filled in by customization.js
var DA_SELECT = 8, CAMERA_ANIMATE_SELECT = 9;
var EXPANSIVE_ALBEDO = 0, LOCAL_ALBEDO = 1, RESTRICTIVE_ALBEDO = 2, 
    FORCING = 3;
var HEIGHT_ALL = 0, HEIGHT_CLEAR = 1, HEIGHT_CLOUDY = 2, HEIGHT_OVERCAST = 3;
var OPTICAL_DEPTH = 0, ANGSTROM_EXPONENT = 1, SSA = 2, 
    ODEPTH_FRAC = 3, NUM_FRAC = 4, VOL_FRAC = 5;
var STEREO_HEIGHT = 0, WIND_SPEED = 1, CLOUD_FRACTION = 2;
var WITH_RANGE = 0, WITH_RANGE_AND_HEIGHT = 1, NO_RANGE = 2;
var ANIMATE_OFF = 0, ANIMATE_TIME = 1, ANIMATE_CAMERA = 2, 
    ANIMATE_HEIGHT = 3, ANIMATE_HEIGHT2 = 4, ANIMATE_ADVANCED = 5;

// -------------------------------------------------------------
// Initialize variables.
// -------------------------------------------------------------

function init() {
// -------------------------------------------------------------
// Initialize customization information read from customization.js
// This fills in constants describing the types, as well as
// supplying the routine variable_append.
// -------------------------------------------------------------

    customization_init();

// -------------------------------------------------------------
// Create object used to determine if data is available, and what
// version it is.
// -------------------------------------------------------------

    var available = new Available();

// -------------------------------------------------------------
// Get image size from cookie, if available. Otherwise, default
// to small.
// -------------------------------------------------------------

    var image_size = get_cookie("image_size");
    if(image_size ===null) {
	image_size = "small";
    }
    if(myimgsize =="hidden") {
	image_size = "hidden";
    }

// -------------------------------------------------------------
// Get view type from cookie, if available. Otherwise, default
// to small.
// -------------------------------------------------------------

    var view_type = get_cookie("view_type");
    if(view_type ===null) {
	view_type = "map";
    }

// -------------------------------------------------------------
// Default values
// -------------------------------------------------------------

    field_type_index_arr = new Array(NUMBER_TYPE);
    for(var i = 0; i < field_type_index_arr.length; ++i) {
	field_type_index_arr[i] = 0;
    }
    if(CGGRP >= 0) {
	field_type_index_arr[CGGRP] = 4;
    }
    if(RCCM >= 0) {
	field_type_index_arr[RCCM] = 4;
    }	
    selected_time = available.latest_month(data_type);
    dn = new Download(available);
    var rccm_type = "all";
    var rccm_type2 = 0;
    var biome_type = 6;
    var image_type = "natural";
    var resolution_type = "1";
    var band_type = 0;
    var band_type2 = 0;
    var particle_type = 0;
    var odepth_type = 5;
    var range_type = 0;
    var height_type = 0;
    var wind_type = 0;
    var speed = 0.3;
    var height_type2 = 0;
    var wind_index = 0;
    var cloud_type = 0;
    var help = 0;
    var advanced = 0;
    var print = 0;
    var download_type = dn.download_type;

// -------------------------------------------------------------
// Check to see if we have variables passed in using the query
// syntax. If so, override the default values already set.
// -------------------------------------------------------------

    if (location.search.length > 0) {
	var s = new String(location.search.substring(1));
	var t = "";
	for(var j = 0; j < s.length; ++j) {
	    if(s.charAt(j) =="&") {
		t = t + ";";
	    } else {
		t = t + s.charAt(j);
	    }
	}
	eval(unescape(t));
    }

// -------------------------------------------------------------
// Set up other variables.
// -------------------------------------------------------------

    field_type_index = field_type_index_arr[data_type];
    if(data_type ==CGAS) {
	fn = new AerosolFileName(available, image_size, band_type, 
				 particle_type, odepth_type);
    } else if(data_type ==CGLS) {
	fn = new LandFileName(available, image_size, biome_type);
    } else if(data_type ==CGAL) {
	fn = new AlbedoFileName(available, image_size, band_type2, 
				range_type, height_type, wind_type, 
				resolution_type);
    } else if(data_type ==CGCL) {
	fn = new CloudFileName(available, image_size, height_type2, 
			       wind_index, wind_type, cloud_type);
    } else if(data_type ==CGGRP) {
	fn = new RadianceFileName(available, image_size, 
				  image_type, rccm_type);
    } else if(data_type ==RCCM) {
	fn = new RccmFileName(available, image_size, rccm_type2);
    } else {
	alert("Unknown type");
    }

// -------------------------------------------------------------
// Stash all of the other variable types in fn, even though fn 
// doesn't use them. This is just a useful place to keep them.
// -------------------------------------------------------------

    fn.view_type = view_type;
    fn.rccm_type = rccm_type;
    fn.rccm_type2 = rccm_type2;
    fn.biome_type = biome_type;
    fn.image_type = image_type;
    fn.band_type = band_type;
    fn.band_type2 = band_type2;
    fn.particle_type = particle_type;
    fn.odepth_type = odepth_type;
    fn.range_type = range_type;
    fn.height_type = height_type;
    fn.wind_type = wind_type;
    fn.resolution_type = resolution_type;
    fn.height_type2 = height_type2;
    fn.wind_index = wind_index;
    fn.cloud_type = cloud_type;
    dn.download_type = download_type;
    animation = new Animation("dataimg", "animation", 
		  "img" + fn.image_size + "/loading_image_animation.jpg");
    animation.speed = speed;

// -------------------------------------------------------------
// If this isn't the correct size page, then reload page. This has the 
// side effect of figuring out the correct file to load.
// -------------------------------------------------------------

    if(fn.image_size != myimgsize) {
	reload_page();
    }

// -------------------------------------------------------------
// If we should be looking a globe then reload page. This has the 
// side effect of figuring out the correct file to load.
// -------------------------------------------------------------

    if(fn.view_type != myviewtype) {
	reload_page();
    }

// -------------------------------------------------------------
// Look for any windows open by previous view_data web pages.
// -------------------------------------------------------------

    var win;
    if(help) {
	win = window.open("", "HelpWindow");
	if(win &&
	   !win.am_here) {	// Unfortunately, there is no way to tell 
	                        // if the window actually exists before
				// trying to reattach to it. We use the
				// absence of the am_here variable to detect
				// when we open an empty window.
	    win.close();
	}
	if(win &&
	   !win.closed) {
	    window_array[window_array.length] = win;
	}
    }
    if(advanced) {
	win = window.open("", "AdvancedWindow");
	if(win &&
	   !win.am_here) {
	    win.close();
	}
	if(win &&
	   !win.closed) {
	    window_array[window_array.length] = win;
	    animate_advanced_window = win;
	}
    }
    if(print) {
	win = window.open("", "PrintWindow");
	if(win &&
	   !win.am_here) {
	    win.close();
	}
	if(win &&
	   !win.closed) {
	    window_array[window_array.length] = win;
	}
    }

// -------------------------------------------------------------
// Start up animation if that is what is selected.
// -------------------------------------------------------------

    if(advanced ==2 &&
       animate_advanced_window &&
       !animate_advanced_window.closed &&
       animate_advanced_window.start_animation) {
	animate_advanced_window.start_animation();
    } else {
	animate_start_or_stop_if_needed();
    }

// -------------------------------------------------------------
// There is a bug in netscape, so that when we resize a window the page gets
// redrawn by not reinitialized. An easy workaround is explicitly have
// resize call the initialization routine.
// -------------------------------------------------------------

    window.onresize = init_data;
}

// -------------------------------------------------------------
// Function to clean up before leaving a page.
// -------------------------------------------------------------

function cleanup() {
    for(var i = 0; i < window_array.length; i++) {
	if(window_array[i] && !window_array[i].closed) {
	    if(clean_up_window ||
	       window_array[i].name == "TextWindow") {
				// Skip clean up if we are told to, except
		                // for TextWindow, which is always closed.
		window_array[i].close();
	    }
	}
    }
}

// -------------------------------------------------------------
// Function to print message if we are using an applet and live
// connect doesn't work.
// -------------------------------------------------------------

function no_live_connect_alert() {
    alert("The function you've requested required \"liveconnect\" that allows " +
          "java and javascript to pass messages to each other. Your system " +
          "doesn't support that. You can select \"View Type\" of \"Flat map\" " +
          "which will support this function.");
}

// -------------------------------------------------------------
// Function to print image.
// -------------------------------------------------------------

function print_image() {
    var fname;
    if(fn.view_type =="globe") {
	if(document.Level3Globe && 
	   document.Level3Globe.fileName) {
	    fname = String(document.Level3Globe.fileName());
	} else {
	    no_live_connect_alert();
	    return;
	}
    } else {
	fname = image_file_name(animation, fn, selected_time, 
				field_type_index);
    }
    var s = String("small_");
    var t = fname.indexOf(s);
    if(t != -1) {
	fname = fname.substr(0, t) + 
	    fname.substr(t + s.length, fname.length);
    }
    t = fname.indexOf("image_not_available");
    if(t == -1) {
	t = fname.indexOf("loading_image_animation");
    }
    if(t != -1) {
	alert("Data is not available for printing");
	return;
    }
    var win = window.open(fname, "PrintWindow",
			  "height=510,width=820,menuBar=1,scrollbars=1,resizable=1,toolBar=1");
    win.am_here = 1;		// Mark the window as containing data, so we
				// can detect when the window isn't actually
				// there on loading (see init() above)
    win.focus();
    window_array[window_array.length] = win;
}

// -------------------------------------------------------------
// Function to download data.
// -------------------------------------------------------------

function download_data() {
    var t;
    var dt;
    if(fn.view_type =="globe") {
	if(document.Level3Globe && 
	   document.Level3Globe.getYear) {
	    t = new Time(String(document.Level3Globe.getSeason()),
			 document.Level3Globe.getYear(),
			 document.Level3Globe.getDay());
	    dt = document.Level3Globe.dataType();
	} else {
	    no_live_connect_alert();
	    return;
	}
    } else {
	t = selected_time;
	if(animation.am_on) {
	    t = new Time(animation.current_image().time.season,
			 animation.current_image().time.year,
			 animation.current_image().time.day);
	}
	dt = data_type;
    }
    dn.download(t, data_type);
}

// -------------------------------------------------------------
// Display zonal information as text.
// -------------------------------------------------------------

function display_zonal_information() {
    if(animation.am_on &&
       animate_type ==ANIMATE_TIME) {
	alert("Can't display zonal summary while animating.\n" +
	      "Select a season/month, and then retry\n");
	return;
    }
    var version = fn.available.version(selected_time, data_type);
    if(version) {
	var fname = "data/zonal_" + 
	    selected_time.season + "_" + selected_time.year + "_" +
	    version + ".html";
	var win = window.open(fname, "TextWindow");
	if(!win) {
	    alert("You appear to have popups disabled. \nPlease enable for this site.\n");
	} else {
	    win.focus();
	    window_array[window_array.length] = win;
	}
    } else {
	alert("Data is not available for current season and year");
	return;
    }
}

// -------------------------------------------------------------
// Display albedo summary information as text.
// -------------------------------------------------------------

function display_albedo_summary_information() {
    if(animation.am_on &&
       animate_type ==ANIMATE_TIME) {
	alert("Can't display albedo summary while animating.\n" +
	      "Select a season/month, and then retry\n");
	return;
    }
    var version = fn.available.version(selected_time, data_type);
    if(version) {
	var fname = "data/local_summary_" + 
	    selected_time.season + "_" + selected_time.year + "_" +
	    version + ".html";
	var win = window.open(fname, "TextWindow");
	if(!win) {
	    alert("You appear to have popups disabled. \nPlease enable for this site.\n");
	} else {
	    win.focus();
	    window_array[window_array.length] = win;
	}
    } else {
	alert("Data is not available for current season and year");
	return;
    }
}

// -------------------------------------------------------------
// Reload this page. We figure out if the name of the page needs
// to change to "small" or "large", in case the image size has 
// changed.
// -------------------------------------------------------------

function reload_page() {
    if(fn.view_type =="globe") {
	if(fn.image_size =="small") {
	    go_to("globe.html");
	} else {
	    go_to("globe_large.html");
	}
	return;
    }
    if(myviewtype =="globe") {
	go_to("level3_CGAS_small.html");
	return;
    }

// Handle both IE, where pathname uses "\" and netscape, where it is "/".

    var i = location.pathname.lastIndexOf("/");
    var j = location.pathname.lastIndexOf("\\");
    if(j > i) {
	i = j;
    }
    var f = location.pathname.substr(i+1);
    i = f.lastIndexOf("small");
    j = f.lastIndexOf("large");
    if(i < j) {
	i = j;
    }
    f = f.substr(0, i);
    f = f + fn.image_size + ".html";
    go_to(f); 
}

// -------------------------------------------------------------
// Go to the given location. We add variables that we want to
// pass to this location using the query syntax.
// -------------------------------------------------------------

function go_to(hr) {
    clean_up_window = 0;	// Suppress removing created windows 
				// (such as help)
    field_type_index_arr[data_type] = field_type_index;
    var hr2 = hr + "?" + variable_append();
    hr2 = hr2 + "field_type_index_arr=" + escape("new Array(");
    var i;
    for(i = 0; i < field_type_index_arr.length; ++i) {
	hr2 = hr2 + escape(field_type_index_arr[i] + ",");
    }
    hr2 = hr2 + escape("0)");
    for(i = 0; i < window_array.length; i++) {
	if(window_array[i] && !window_array[i].closed) {
	    if(window_array[i].name =="HelpWindow") {
		hr2 = hr2 + ",help=1";
	    }
	    if(window_array[i].name =="AdvancedWindow") {
		if(animation.am_on &&
		   animate_type ==ANIMATE_ADVANCED) {
		    hr2 = hr2 + ",advanced=2";
		} else {
		    hr2 = hr2 + ",advanced=1";
		}
	    }
	    if(window_array[i].name =="PrintWindow") {
		hr2 = hr2 + ",print=1";
	    }
	}
    }
    
    hr2 = location.href.substr(0, location.href.lastIndexOf("/")) +
	"/" +  hr2;
    location = hr2;
}

// -------------------------------------------------------------
// Set the button (field type) to the given value.
// -------------------------------------------------------------

function set_button(id) {
    if(id ==CAMERA_ANIMATE_SELECT &&
       field_type_index != CAMERA_ANIMATE_SELECT) {
	old_camera = field_type_index;
    }
    field_type_index = id;

// -------------------------------------------------------------
// Special handling for CGAL, which has certain pull downs used 
// or not used depending on the field type.
// -------------------------------------------------------------

    if(data_type ==CGAL) {
	if (field_type_index == FORCING) {
	    set_select(document.viewform.RangeType, "-1");
	} else {
	    set_select(document.viewform.RangeType, fn.range_type);
	}
	set_select(document.viewform.BandType2, fn.band_type2);
	if (field_type_index == EXPANSIVE_ALBEDO ||
	    field_type_index == FORCING) {
	    set_select(document.viewform.HeightType, "-1");
	    set_select(document.viewform.WindType, "-1");
	} else {
	    set_select(document.viewform.HeightType, fn.height_type);
	    set_select(document.viewform.WindType, fn.wind_type);
	}
    }

// -------------------------------------------------------------
// Similiar special handling for CGCL.
// -------------------------------------------------------------

    if(data_type ==CGCL) {
	if(field_type_index == STEREO_HEIGHT) {
	    set_select(document.viewform.HeightType2, fn.height_type2);
	    set_select(document.viewform.WindType, fn.wind_type);
	    set_select(document.viewform.WindIndex, "-1");
	    set_select(document.viewform.CloudType, "-1");
	} else if(field_type_index ==WIND_SPEED) {
	    set_select(document.viewform.HeightType2, fn.height_type2);
	    set_select(document.viewform.WindType, "-1");
	    set_select(document.viewform.WindIndex, fn.wind_index);
	    set_select(document.viewform.CloudType, "-1");
	} else if(field_type_index ==CLOUD_FRACTION) {
	    set_select(document.viewform.HeightType2, "-1");
	    set_select(document.viewform.WindType, "-1");
	    set_select(document.viewform.WindIndex, "-1");
	    set_select(document.viewform.CloudType, fn.cloud_type);
	}
    }

// -------------------------------------------------------------
// Similiar special handling for CGAS.
// -------------------------------------------------------------

    if(data_type ==CGAS) {
	if (field_type_index == OPTICAL_DEPTH ||
	    field_type_index == ANGSTROM_EXPONENT ||
	    field_type_index == SSA) {
	    set_select(document.viewform.ParticleType, "-1");
	} else {
	    set_select(document.viewform.ParticleType, fn.particle_type);
	}
	if(field_type_index == OPTICAL_DEPTH) {
	    set_select(document.viewform.OdepthType, "-1");
	} else {
	    set_select(document.viewform.OdepthType, fn.odepth_type);
	}
	var version = fn.available.version(selected_time, data_type);
	if (field_type_index == ANGSTROM_EXPONENT ||
	    field_type_index ==NUM_FRAC ||
	    field_type_index ==VOL_FRAC) {
	    set_select(document.viewform.BandType, "-1");
	} else {
	    set_select(document.viewform.BandType, fn.band_type);
	}
    }
    animate_start_or_stop_if_needed();
    update_image();
}

// -------------------------------------------------------------
// Set the season to the given value.
// -------------------------------------------------------------

function set_season(seas) {
    if(seas =="animate" &&
       selected_time.season !="animate") {
	old_time = new Time(selected_time.season, selected_time.year,
			    selected_time.day);
    }
    selected_time.season = seas;
    check_day_allowed();
    animate_start_or_stop_if_needed();
    set_button(field_type_index);
}

// -------------------------------------------------------------
// Set the day to the given value.
// -------------------------------------------------------------

function set_day(d) {
    var dold = selected_time.day;
    selected_time.day = d;
    if(check_day_allowed() ===0) {
	return;
    }
    if(d != "animate" &&
       d > DAY_MONTH[selected_time.season]) {
	selected_time.day = dold;
	set_select(document.viewform.Day, dold);
	return;
    }
    if(d =="animate" &&
       dold !="animate") {
	old_time = new Time(selected_time.season, selected_time.year,
			    dold);
    }
    animate_start_or_stop_if_needed();
    set_button(field_type_index);
}

// -------------------------------------------------------------
// Set the day to the given value.
// -------------------------------------------------------------

function check_day_allowed() {
    if(selected_time.season =="ANN" ||
       selected_time.season =="SUM" ||
       selected_time.season =="FALL" ||
       selected_time.season =="SPR" ||
       selected_time.season =="WIN" ||
       selected_time.season =="animate") {
	set_select(document.viewform.Day, -1);
	selected_time.day = 0;
	set_select(document.viewform.Day, -1);
	return 0;
    } else {
	set_select(document.viewform.Day, selected_time.day);
	return 1;
    }
}

// -------------------------------------------------------------
// Set the year to the given value.
// -------------------------------------------------------------

function set_year(yr) {
    selected_time.year = yr;
    check_day_allowed();
    set_button(field_type_index);
}

// -------------------------------------------------------------
// Set the image type to the given value.
// -------------------------------------------------------------

function set_image_type(itype) {
    fn.image_type = itype;
    set_button(field_type_index);
}

// -------------------------------------------------------------
// Set the resolution type to the given value.
// -------------------------------------------------------------

function set_resolution_type(rtype) {
    fn.resolution_type = rtype;
    set_button(field_type_index);
}

// -------------------------------------------------------------
// Set the band type to the given value.
// -------------------------------------------------------------

function set_band_type(btype) {
    var version = fn.available.version(selected_time, data_type);
    if(data_type ==CGAS &&
       (field_type_index ==ANGSTROM_EXPONENT ||
	field_type_index ==NUM_FRAC ||
	field_type_index ==VOL_FRAC)) {
	set_select(document.viewform.BandType, -1);
	return;
    } else if(btype ==-1) {
	set_select(document.viewform.BandType, fn.band_type);
	return;
    }
    fn.band_type = btype;
    update_image();
}

// -------------------------------------------------------------
// Set the band type to the given value.
// -------------------------------------------------------------

function set_band_type2(btype2) {
    fn.band_type2 = btype2;
    update_image();
}

// -------------------------------------------------------------
// Set the particle type to the given value.
// -------------------------------------------------------------

function set_particle_type(ptype) {
    if(data_type ==CGAS &&
       (field_type_index ==OPTICAL_DEPTH ||
	field_type_index ==ANGSTROM_EXPONENT ||
	field_type_index ==SSA)) {
	set_select(document.viewform.ParticleType, -1);
	return;
    } else if(ptype ==-1) {
	set_select(document.viewform.ParticleType, fn.particle_type);
	return;
    }
    fn.particle_type = ptype;
    update_image();
}

// -------------------------------------------------------------
// Set the odepth type to the given value.
// -------------------------------------------------------------

function set_odepth_type(odtype) {
    if(data_type ==CGAS &&
       field_type_index ==OPTICAL_DEPTH) {
	set_select(document.viewform.OdepthType, -1);
	return;
    } else if(odtype ==-1) {
	set_select(document.viewform.OdepthType, fn.odepth_type);
	return;
    }
    fn.odepth_type = odtype;
    update_image();
}

// -------------------------------------------------------------
// Set the range type to the given value.
// -------------------------------------------------------------

function set_range_type(rtype) {
    if(data_type ==CGAL &&
       field_type_index == FORCING) {
	set_select(document.viewform.RangeType, "-1");
	return;
    } else if(rtype ==-1) {
	set_select(document.viewform.RangeType, fn.range_type);
	return;
    }
    fn.range_type = rtype;
    update_image();
}

// -------------------------------------------------------------
// Set the height type to the given value.
// -------------------------------------------------------------

function set_height_type(htype) {
    if(htype =="animate" &&
       fn.height_type !="animate") {
	old_height = fn.height_type;
    }
    if(data_type ==CGAL &&
       (field_type_index == EXPANSIVE_ALBEDO ||
	field_type_index == FORCING)) {
	set_select(document.viewform.HeightType, "-1");
	return;
    } else if(htype ==-1) {
	set_select(document.viewform.HeightType, fn.height_type);
	return;
    }
    fn.height_type = htype;
    if(fn.height_type ==HEIGHT_ALL ||
       fn.height_type ==HEIGHT_CLOUDY ||
       fn.height_type ==HEIGHT_CLEAR ||
       fn.height_type ==HEIGHT_OVERCAST) {
	set_select(document.viewform.WindType, -1);
    } else {	
	set_select(document.viewform.WindType, fn.wind_type);
    }

    animate_start_or_stop_if_needed();
    update_image();
}

// -------------------------------------------------------------
// Set the height type 2 to the given value.
// -------------------------------------------------------------

function set_height_type2(htype) {
    if(htype =="animate" &&
       fn.height_type2 !="animate") {
	old_height = fn.height_type;
    }
    if(data_type ==CGCL &&
       field_type_index == CLOUD_FRACTION) {
	set_select(document.viewform.HeightType2, "-1");
	return;
    } else if(htype ==-1) {
	set_select(document.viewform.HeightType2, fn.height_type2);
	return;
    }
    fn.height_type2 = htype;
    animate_start_or_stop_if_needed();
    update_image();
}

// -------------------------------------------------------------
// Set the wind index to the given value.
// -------------------------------------------------------------

function set_wind_index(wi) {
    if(data_type ==CGCL &&
       field_type_index != WIND_SPEED) {
	set_select(document.viewform.WindIndex, "-1");
	return;
    } else if(wi ==-1) {
	set_select(document.viewform.WindIndex, fn.wind_index);
	return;
    }
    fn.wind_index = wi;
    update_image();
}

// -------------------------------------------------------------
// Set the cloud type to the given value.
// -------------------------------------------------------------

function set_cloud_type(ct) {
    if(data_type ==CGCL &&
       field_type_index != CLOUD_FRACTION) {
	set_select(document.viewform.CloudType, "-1");
	return;
    } else if(ct ==-1) {
	set_select(document.viewform.CloudType, fn.cloud_type);
	return;
    }
    fn.cloud_type = ct;
    update_image();
}

// -------------------------------------------------------------
// Set the wind type to the given value.
// -------------------------------------------------------------

function set_wind_type(wtype) {
    if((data_type ==CGAL &&
       ((field_type_index == EXPANSIVE_ALBEDO ||
	 field_type_index == FORCING) ||
	fn.height_type ==HEIGHT_ALL ||
	fn.height_type ==HEIGHT_CLOUDY ||
	fn.height_type ==HEIGHT_CLEAR ||
	fn.height_type ==HEIGHT_OVERCAST)) ||
       (data_type ==CGCL &&
	field_type_index != STEREO_HEIGHT)) {
	set_select(document.viewform.WindType, "-1");
	return;
    } else if(wtype ==-1) {
	set_select(document.viewform.WindType, fn.wind_type);
	return;
    }
    fn.wind_type = wtype;
    update_image();
}

// -------------------------------------------------------------
// Set the rccm type to the given value.
// -------------------------------------------------------------

function set_rccm_type(rtype) {
    fn.rccm_type = rtype;
    update_image();
}

// -------------------------------------------------------------
// Set the rccm type to the given value.
// -------------------------------------------------------------

function set_rccm_type2(rtype) {
    fn.rccm_type2 = rtype;
    update_image();
}

// -------------------------------------------------------------
// Set the biome type to the given value.
// -------------------------------------------------------------

function set_biome_type(btype) {
    fn.biome_type = btype;
    update_image();
}

// -------------------------------------------------------------
// Start animation button
// -------------------------------------------------------------

function animate_button() {
    if(animation.am_on) {
	animation.restart();
    } else {
	if(selected_time.day ===0) {
	    if(document.viewform) {
		set_select(document.viewform.Month, "animate");
	    }
	    set_season("animate");
	} else {
	    if(document.viewform) {
		set_select(document.viewform.Day, "animate");
	    }
	    set_day("animate");
	}
    }
}

// -------------------------------------------------------------
// Stop animation button
// -------------------------------------------------------------

function animate_stop() {
    if(!animation.am_on) {
	return;
    }
    animation.stop();
    if(animate_type ==ANIMATE_TIME) {
	if(document.viewform) {
	    set_select(document.viewform.Year, old_time.year);
	    set_select(document.viewform.Month, old_time.season);
	    set_select(document.viewform.Day, old_time.day);
	}
	selected_time.year = old_time.year;
	selected_time.season = old_time.season;
	selected_time.day = old_time.day;
	update_image();
    } else if(animate_type ==ANIMATE_CAMERA) {
	field_type_index = old_camera;
	update_image();
    } else if(animate_type ==ANIMATE_HEIGHT) {
	fn.height_type = old_height;
	if(document.viewform) {
	    set_select(document.viewform.HeightType, fn.height_type);
	}
	update_image();
    } else if(animate_type ==ANIMATE_HEIGHT2) {
	fn.height_type2 = old_height;
	if(document.viewform) {
	    set_select(document.viewform.HeightType2, fn.height_type2);
	}
	update_image();
    } else if(animate_type ==ANIMATE_ADVANCED) {
	update_image();
    } else {
	alert("Unrecognized animate_type = " + animate_type);
    }
}

// -------------------------------------------------------------
// Previous button.
// -------------------------------------------------------------

function previous_button() {
    if(animation.am_on) {
	animation.previous();
    } else {
	selected_time.previous();
	if(selected_time.season_type() ==TIME_DAY &&
	   compare_time(selected_time, min_day) == -1) {
	    selected_time = new Time(min_day.season, min_day.year, min_day.day);
	}
	if(document.viewform) {
	    set_select(document.viewform.Year, selected_time.year);
	    set_select(document.viewform.Month, selected_time.season);
	    set_select(document.viewform.Day, selected_time.day);
	}
	set_button(field_type_index);
    }
}

// -------------------------------------------------------------
// Next button.
// -------------------------------------------------------------

function next_button() {
    if(animation.am_on) {
	animation.next();
    } else {
	selected_time.next();
	if(selected_time.season_type() ==TIME_DAY &&
	   compare_time(selected_time, max_day) ==1) {
	    selected_time = new Time(max_day.season, max_day.year, max_day.day);
	}
	if(document.viewform) {
	    set_select(document.viewform.Year, selected_time.year);
	    set_select(document.viewform.Month, selected_time.season);
	    set_select(document.viewform.Day, selected_time.day);
	}
	set_button(field_type_index);
    }
}

// -------------------------------------------------------------
// Help documentation
// -------------------------------------------------------------

function help_button() {
    var win = window.open("help.html", "HelpWindow",
		  "height=600,width=500,menubar=1,scrollbars=1,resizable=1,toolBar=1");
    if(!win) {			// Popup blocked, just go to page.
	var hr2 = location.href.substr(0, location.href.lastIndexOf("/")) +
	    "/help.html";
	location = hr2;
    } else {
	win.am_here = 1;        // Mark the window as containing data, so we
				// can detect when the window isn't actually
				// there on loading (see init() above)
	window_array[window_array.length] = win;
	win.focus();
    }
}

// -------------------------------------------------------------
// Advanced animation options
// -------------------------------------------------------------

function advanced_button() {
    var win = window.open("advanced_animation.html", "AdvancedWindow",
		          "height=600,width=600,menubar=1,scrollbars=1,resizable=1,toolBar=1");
    if(!win) {			// Popup blocked, just go to page.
	alert("You appear to have popups disabled. \nPlease enable for this site.\n");
    } else {
	win.am_here = 1;        // Mark the window as containing data, so we
				// can detect when the window isn't actually
				// there on loading (see init() above)
	win.focus();
	animate_advanced_window = win;
	window_array[window_array.length] = win;
    }
}

// -------------------------------------------------------------
// Fill in imagearr for the images between two times. If end 
// time is before start time, then go backwards.
// -------------------------------------------------------------

function imagearr_between_time(tstart, tend) {
    var backward = false;
    if(compare_time(tstart, tend) > 0) {
	backward = true;
    }
    var t = new Time(tstart.season, tstart.year, tstart.day);
    var i;
    var done = false;
    for(i = 0; !done; ++i) {
	done = (compare_time(t, tend) ===0);
	if(backward) {
	    t.previous();
	} else {
	    t.next();
	}
    }
    var iarr = new Array(i);
    t = new Time(tstart.season, tstart.year, tstart.day);
    for(i = 0; i < iarr.length; ++i) {
	iarr[i] = new Image;
	iarr[i].src = fn.image_file_name(t, field_type_index);
	iarr[i].time = new Time(t.season, t.year, t.day);
	if(backward) {
	    t.previous();
	} else {
	    t.next();
	}
    }
    return iarr;
}

// -------------------------------------------------------------
// Set up for doing animation.
// -------------------------------------------------------------

function animate_start(t) {
    animate_type = t;
    var imagearr;
    var i;
    if(animate_type ==ANIMATE_TIME) {	
	if(selected_time.season =="animate") {
				// Monthly animation
	    imagearr = 
		imagearr_between_time(new Time("DEC", selected_time.year - 1, 0), 
				      new Time("NOV", selected_time.year, 0));
	} else {
	    imagearr = 
		imagearr_between_time(new Time(selected_time.season, 
					       selected_time.year, 1), 
				      new Time(selected_time.season, 
					       selected_time.year, 
					       DAY_MONTH[selected_time.season]));
	}
    } else if(animate_type ==ANIMATE_CAMERA) {
				// Animate through cameras.
	imagearr = new Array(9);
	for(i = 0; i < imagearr.length; ++i) {
	    imagearr[i] = new Image;
	    imagearr[i].src = fn.image_file_name(selected_time, i);
	    imagearr[i].time = new Time(selected_time.season,
					selected_time.year,
					selected_time.day);
	}
    } else if(animate_type ==ANIMATE_HEIGHT) {
				// Animate through heights
        imagearr = new Array(16);
	for(i = 0; i < imagearr.length - 1; ++i) {
	    imagearr[i] = new Image;
	    imagearr[i].src = 
		fn.image_file_name_height(selected_time, field_type_index, 
					  i + 4);
	    imagearr[i].time = new Time(selected_time.season,
					selected_time.year, 
					selected_time.day);
	}
	imagearr[imagearr.length - 1] = new Image;
	imagearr[imagearr.length - 1].src = 
		fn.image_file_name_height(selected_time, field_type_index, 
					  3);
	imagearr[imagearr.length - 1].time = new Time(selected_time.season,
						      selected_time.year,
						      selected_time.day);
    } else if(animate_type ==ANIMATE_HEIGHT2) {
				// Animate through heights
        imagearr = new Array(14);
	for(i = 0; i < imagearr.length; ++i) {
	    imagearr[i] = new Image;
	    imagearr[i].src = 
		fn.image_file_name_height(selected_time, field_type_index, 
					  i + 1);
	    imagearr[i].time = new Time(selected_time.season,
					selected_time.year, 
					selected_time.day);
	}
    } else if(animate_type ==ANIMATE_ADVANCED) {
	if(animate_advanced_window) {
	    animate_advanced_window.start_animation();
	}
	return;
    } else {
	alert("Unknown animation type.");
    }
    animation.start(imagearr);
}

// -------------------------------------------------------------
// Check to see if we should start animation, and if needed do
// so.
// -------------------------------------------------------------

function animate_start_or_stop_if_needed() {
    if(animation.am_on && 
       animate_type ==ANIMATE_ADVANCED &&
       animate_advanced_window &&
       !animate_advanced_window.closed) {
	animation.stop();
	animate_advanced_window.start_animation();
	return;
    }
    animation.stop();
    if((data_type ==CGGRP ||
	data_type ==RCCM) &&
       field_type_index ==CAMERA_ANIMATE_SELECT) {
				// Special handling for camera animation
	if(selected_time.season =="animate") {
	    alert("Can't animate through both camera\n"+
		  "and year at the same time.\n" +
		  "Selecting DA camera instead.\n");
	    field_type_index = DA_SELECT;
	} else {
	    animate_start(ANIMATE_CAMERA);
	    return;
	}
    }
    if(data_type ==CGAL &&
       (field_type_index ==LOCAL_ALBEDO ||
	field_type_index ==RESTRICTIVE_ALBEDO) &&
       fn.height_type =="animate") {
	if(selected_time.season =="animate") {
	    alert("Can't animate through both height\n"+
		  "and year at the same time.\n" +
		  "Selecting all heights instead.\n");
	    fn.height_type = 0;
	    if(document.viewform.HeightType) {
		set_select(document.viewform.HeightType, fn.height_type);
	    }
	} else {
	    animate_start(ANIMATE_HEIGHT);
	    return;
	}
    }
    if(data_type ==CGCL &&
       (field_type_index ==STEREO_HEIGHT ||
	field_type_index ==WIND_SPEED) &&
       fn.height_type2 =="animate") {
	if(selected_time.season =="animate") {
	    alert("Can't animate through both height\n"+
		  "and year at the same time.\n" +
		  "Selecting all heights instead.\n");
	    fn.height_type2 = 0;
	    if(document.viewform.HeightType2) {
		set_select(document.viewform.HeightType2,
			   fn.height_type2);
	    }
	} else {
	    animate_start(ANIMATE_HEIGHT2);
	    return;
	}
    }
    if(selected_time.season =="animate") {
	animate_start(ANIMATE_TIME);
	return;
    }
    if(selected_time.day =="animate") {
	animate_start(ANIMATE_TIME);
	return;
    }
}

// -------------------------------------------------------------
// Return name of button file to use for the given field type
// being selected.
// -------------------------------------------------------------

function button_file_name() {
    return "img" + fn.image_size + "/button_" + 
	data_type + "_" + field_type_index + ".gif";
}

// -------------------------------------------------------------
// Update input version, if it is in the web page.
// -------------------------------------------------------------

function update_image_version(v) {
    if(document &&
       document.getElementById &&
       document.getElementById("InputVersion")) {
	var t;
	t = document.getElementById("InputVersion");
	t.innerHTML = v;
    }
}

// -------------------------------------------------------------
// Update the image being displayed.
// -------------------------------------------------------------

function update_image() {
    if(animation.am_on) {
	animate_start(animate_type);
    }
    if(animation.am_on && 
       !animation.am_paused &&
       animate_type ==ANIMATE_TIME) {
	update_image_version("N/A");
    } else {
	if(fn.input_version(selected_time)) {
	    update_image_version(fn.input_version(selected_time));
	} else {
	    update_image_version("N/A");
	}
    }
    if(document.images["buttonimg"]) {
	document.images["buttonimg"].src = button_file_name();
    }
    if(document.images["dataimg"]) {
	document.images["dataimg"].src =
	    image_file_name(animation, fn, selected_time, field_type_index);
    }
}

// -------------------------------------------------------------
// Set the image size. This sets a cookie, so that the value is
// sticky the next time the user visits the page, and then 
// reload the page (which selects which size to use). Note that
// the cookie isn't required, this will change the image size
// even if the user has cookies disabled (but the next time they
// visit the web site, the size will go back to the default
// "small" size.
// -------------------------------------------------------------

function set_image_size(sz) {
    if(fn.image_size ==sz) {
	return;
    }
    fn.image_size = sz;
    var today = new Date();
    //var expires = new Date(today.getTime() + (56 * 86400000));
    var expires = new Date(today.getTime() + 3600000);
    set_cookie("image_size", sz, expires);
    reload_page();
}

// -------------------------------------------------------------
// Same sort of thing for view type.
// -------------------------------------------------------------

function set_view_type(vt) {
    if(fn.view_type ==vt) {
	return;
    }
    fn.view_type = vt;
    var today = new Date();
    //var expires = new Date(today.getTime() + (56 * 86400000));
    var expires = new Date(today.getTime() + 3600000);
    set_cookie("view_type", vt, expires);
    reload_page();
}

// -------------------------------------------------------------
// Similarly, set download type.
// -------------------------------------------------------------

function set_download_type(dt) {
    dn.set_download_type(dt);
}

// -------------------------------------------------------------
// Initialize the documents forms to the current values of 
// year, season, image size, and image type.
// -------------------------------------------------------------

function init_data() {
    if(document.viewform) {
	set_select(document.viewform.Year, selected_time.year);
	set_select(document.viewform.Month, selected_time.season);
	set_select(document.viewform.Day, selected_time.day);
	set_select(document.viewform.ImageSize, fn.image_size);
	set_select(document.viewform.ViewType, fn.view_type);
	set_select(document.viewform.ImageType, fn.image_type);
	set_select(document.viewform.BiomeType, fn.biome_type);
	set_select(document.viewform.RccmType, fn.rccm_type);
	set_select(document.viewform.RccmType2, fn.rccm_type2);
	set_select(document.viewform.ResolutionType, fn.resolution_type);
	set_select(document.viewform.DownloadType, dn.download_type);
	set_select(document.viewform.BandType2, fn.band_type2);
	set_select(document.viewform.HeightType2, fn.height_type2);
	set_select(document.viewform.WindIndex, fn.wind_index);
	set_select(document.viewform.CloudType, fn.cloud_type);
	var version = fn.available.version(selected_time, data_type);
	if(data_type ==CGAS &&
	   (field_type_index ==ANGSTROM_EXPONENT ||
	    field_type_index ==NUM_FRAC ||
	    field_type_index ==VOL_FRAC)) {
	    set_select(document.viewform.BandType, -1);
	} else {
	    set_select(document.viewform.BandType, fn.band_type);
	}
	if(data_type ==CGAS &&
	   (field_type_index ==OPTICAL_DEPTH ||
	    field_type_index ==ANGSTROM_EXPONENT ||
	    field_type_index ==SSA)) {
	    set_select(document.viewform.ParticleType, -1);
	} else {
	    set_select(document.viewform.ParticleType, fn.particle_type);
	}
	if(data_type ==CGAS &&
	   field_type_index ==OPTICAL_DEPTH) {
	    set_select(document.viewform.OdepthType, -1);
	} else {
	    set_select(document.viewform.OdepthType, fn.odepth_type);
	}
	if(data_type ==CGAL &&
	   (field_type_index == EXPANSIVE_ALBEDO ||
	    field_type_index == FORCING)) {
	    set_select(document.viewform.HeightType, "-1");
	    set_select(document.viewform.WindType, "-1");
	} else {
	    set_select(document.viewform.HeightType, fn.height_type);
	    if(fn.height_type ==HEIGHT_ALL ||
	       fn.height_type ==HEIGHT_CLOUDY ||
	       fn.height_type ==HEIGHT_CLEAR ||
	       fn.height_type ==HEIGHT_OVERCAST) {
		set_select(document.viewform.WindType, -1);
	    } else {
		set_select(document.viewform.WindType, fn.wind_type);
	    }
	}
	if(data_type ==CGCL) {
	    if(field_type_index == STEREO_HEIGHT) {
		set_select(document.viewform.HeightType2, fn.height_type2);
		set_select(document.viewform.WindType, fn.wind_type);
		set_select(document.viewform.WindIndex, "-1");
		set_select(document.viewform.CloudType, "-1");
	    } else if(field_type_index ==WIND_SPEED) {
		set_select(document.viewform.HeightType2, fn.height_type2);
		set_select(document.viewform.WindType, "-1");
		set_select(document.viewform.WindIndex, fn.wind_index);
		set_select(document.viewform.CloudType, "-1");
	    } else if(field_type_index ==CLOUD_FRACTION) {
		set_select(document.viewform.HeightType2, "-1");
		set_select(document.viewform.WindType, "-1");
		set_select(document.viewform.WindIndex, "-1");
		set_select(document.viewform.CloudType, fn.cloud_type);
	    }
	}
    }
    update_image();
}



