var g_datefed = 'http://datafed.net/';
var g_webapps = 'http://webapps.datafed.net/';
var g_cgi;
var g_time_dim;
var g_view_states;

function check_cursor_relative(time, time_cursor, time_peri, time_mult)
{
    if (time.indexOf("cursor") != 0)
        return time;
    var time_dim = new time_dimension(
        time_cursor, "unknown", 
        time_peri, time_mult, 
        parse_date("2001-01-01"), "now");
    var expr = time.substring(6, time.length);
    var offset = 0;
    var fc = expr.substring(0, 1);
    if (fc == "-" || fc == "+" || fc == " ")
        offset = parse_int(expr);
    time_dim.ensure_and_get_current_time();
    var curr_idx = parse_int(time_dim.current_index) + offset;
    time_dim.change_index(to_string(curr_idx));
    return to_string(time_dim.ensure_and_get_current_time());
}

function on_consoles_load(view_states, time_cursor, time_peri, time_mult, time_zoom)
{
    g_view_states = view_states;
    g_cgi = parse_cgi(document.location.href);
    
    time_zoom.time_min = check_cursor_relative(time_zoom.time_min, time_cursor, time_peri, time_mult);
    time_zoom.time_max = check_cursor_relative(time_zoom.time_max, time_cursor, time_peri, time_mult);
    
    g_time_dim = new time_dimension(
        time_cursor, "unknown", 
        time_peri, time_mult, 
        parse_date(time_zoom.time_min), time_zoom.time_max);
    document.console.curr_time.value = g_time_dim.current_time;
    dispatch_urls(g_view_states);
}

function on_goto_first()
{
    g_time_dim.goto_first();
    propagate_time();
    on_go();
}

function read_time_inc()
{
    return parseInt(document.console.time_inc.value);
}

function on_goto_prev()
{
    for (var ii = read_time_inc(); ii--;)
        g_time_dim.goto_prev();
    propagate_time();
    on_go();
}

function on_goto_next()
{
    for (var ii = read_time_inc(); ii--;)
        g_time_dim.goto_next();
    propagate_time();
    on_go();
}

function on_goto_last()
{
    g_time_dim.goto_last();
    propagate_time();
    on_go();
}

function on_curr_time_change()
{
    g_time_dim.change_time(parse_date(document.console.curr_time.value));
    propagate_time();
    on_go();
}

function propagate_time()
{
    document.console.curr_time.value = g_time_dim.current_time;
}

function on_go()
{
    dispatch_urls(g_view_states);
}

function dispatch_urls(view_states)
{
	var views = view_states.split(',');
    var url_extra = "&datetime=" + g_time_dim.current_time;
    if (!isNaN(parseFloat(document.console.lat_min.value)))
        url_extra += "&lat_min=" + document.console.lat_min.value;
    if (!isNaN(parseFloat(document.console.lat_max.value)))
        url_extra += "&lat_max=" + document.console.lat_max.value;
    if (!isNaN(parseFloat(document.console.lon_min.value)))
        url_extra += "&lon_min=" + document.console.lon_min.value;
    if (!isNaN(parseFloat(document.console.lon_max.value)))
        url_extra += "&lon_max=" + document.console.lon_max.value;
    if (!isNaN(parseFloat(document.console.image_width.value)))
        url_extra += "&image_width=" + document.console.image_width.value;
    if (!isNaN(parseFloat(document.console.image_height.value)))
        url_extra += "&image_height=" + document.console.image_height.value;
    var time_range = "&time_min=" + to_string(g_time_dim.time_min) + "&time_max=" + to_string(g_time_dim.get_time_from_text(g_time_dim.time_max));

    for (var ip = 0; ip < g_cgi.names.length; ++ip)
    {
        var name = g_cgi.names[ip];
        if (name == 'view_states' || 
            name == 'datetime' || 
            name == 'increment' ||
            name == 'title' ||
            name == 'lat_min' || name == 'lat_max' || 
            name == 'lon_min' || name == 'lon_max' || 
            name == "time_min" || name == "time_max" ||
            name == 'image_width' || name == 'image_height' || 
            name.indexOf("ctrl_") == 0)
            null;
        else
            url_extra += '&' + name + '=' + g_cgi.params[name];
    }
    
    for (var idx = 0; ; ++idx)
    {
        var img_ctrl = find_any_opt(document, 'img'+idx);
        if (img_ctrl == null)
            break;
        if (idx < views.length)
        {
            var imageurl = g_webapps + 'cgi.wsfl?view_state=' + views[idx] + url_extra
            if (views[idx].indexOf("_time") == (views[idx].length-5))
            {
                imageurl += time_range;
            }
            img_ctrl.src = imageurl;
        }
        else
            img_ctrl.src = g_datefed + 'images/pixel.gif';
    }
}

function write_image_placeholder(index,name)
{
	window.document.writeln(
	    '<a onclick="load_menu(this)" name="'+name+'">',
	    '<IMG id="img'+index+'" onerror=on_image_error(img'+index+') alt="Information on view" name="img'+index+'"></a>');
}

function dispatch_image_placeholders()
{
	var index = 0;
	var views = view_states.split(',');
	while(typeof(views[index]) != "undefined"){
		write_image_placeholder(index,views[index]);
		index++;
	}
}

function on_image_load(tag)
{
}

function on_image_error(tag)
{
	tag.src="include/image_error.bmp";
}
