var PLOT_URL  = BASE_DOMAIN + 'climate/enso/monitoring/index.html';
var GRAPH_IDS = {
    "nino1": 0,
    "nino2": 1,
    "nino3": 2,
    "nino3.4": 3,
    "nino4": 4,
    "iod": 5
};
var DATA_FILES = [
    '/climate/enso/nino_1.txt',
    '/climate/enso/nino_2.txt',
    '/climate/enso/nino_3.txt',
    '/climate/enso/nino_3.4.txt',
    '/climate/enso/nino_4.txt',
    '/climate/enso/iod_1.txt'
];
var IMAGE_FILES = [
    '/climate/enso/monitoring/nino1.png',
    '/climate/enso/monitoring/nino2.png',
    '/climate/enso/monitoring/nino3.png',
    '/climate/enso/monitoring/nino3_4.png',
    '/climate/enso/monitoring/nino4.png',
    '/climate/enso/monitoring/iod1.png'
];
var POSTSCRIPT_FILES = [
    '/climate/enso/monitoring/nino1.eps',
    '/climate/enso/monitoring/nino2.eps',
    '/climate/enso/monitoring/nino3.eps',
    '/climate/enso/monitoring/nino3_4.eps',
    '/climate/enso/monitoring/nino4.eps',
    '/climate/enso/monitoring/iod1.eps'
];

var bookmark = 2;

function xmlHttpStatusProceed() {
    var response = xmlHttp.responseText;
    document.getElementById('dataDate').innerHTML
        = '<strong>' + response + '</strong>';
    document.getElementById('control-set').style.visibility = 'visible';
    updatePlotView(bookmark);

    return;
}

function xmlHttpStatusError() {
    xmlHttpStatusProceed();

    return;
}

function loadBookmarkedProduct(paramType) {

    if (paramType != EMPTY_STRING) {
        bookmark = GRAPH_IDS[paramType];
    }

    document.getElementById('control-set').style.visibility = 'hidden';
    sendRequest(PLOT_URL, 'text/html');

    return;
}

function updatePlotView(plotNumber) {
    document.getElementsByName('viewPlot')[0].selectedIndex = plotNumber;
    document.getElementById('nino-graph').src = IMAGE_FILES[plotNumber];
    document.getElementById('right-floater').innerHTML
        = getDataLink(plotNumber);

    return;
}

function getDataLink(plotNumber) {
    var dataLink = '<div class="content-item">' +
                   '<p><a href="' + POSTSCRIPT_FILES[plotNumber] +
                   '" target="_blank" title="Download">PostScript version</a>' +
                   '<br /><a href="' + DATA_FILES[plotNumber] +
                   '" target="_blank" title="Opens in a new window">Data sorted by date</a>';

    if (plotNumber == 5) {
        dataLink += '<br /><a href="indices/iod-area-map.gif" target="_blank" title="Opens in a new window">IOD area map</a>';
        dataLink += '<br /><a href="/climate/IOD/" target="_blank" title="Opens in a new window">IOD impacts</a>';
    }
    else {
        dataLink += '<br /><a href="/climate/enso/ensorain.comp.shtml" target="_blank" title="Opens in a new window">ENSO impacts</a>';
    }

    dataLink += '</p></div>';
    
    return dataLink;
}

function showNinoMap() {
    document.getElementById('nino-map').style.visibility = 'visible';

    return;
}

function hideNinoMap() {
    document.getElementById('nino-map').style.visibility = 'hidden';

    return;
}

function plotBack() {
    var selectedIndex = document.getElementsByName('viewPlot')[0].selectedIndex;
    var optionsSize = document.getElementsByName('viewPlot')[0].options.length;

    if (selectedIndex == 0) {
        document.getElementsByName('viewPlot')[0].selectedIndex
            = optionsSize - 1;
    }
    else {
        document.getElementsByName('viewPlot')[0].selectedIndex -= 1;
    }

    updatePlotView(document.getElementsByName('viewPlot')[0].value);

    return;
}

function plotNext() {
    var selectedIndex = document.getElementsByName('viewPlot')[0].selectedIndex;
    var optionsSize = document.getElementsByName('viewPlot')[0].options.length;

    if (selectedIndex == optionsSize - 1) {
        document.getElementsByName('viewPlot')[0].selectedIndex = 0;
    }
    else {
        document.getElementsByName('viewPlot')[0].selectedIndex += 1;
    }

    updatePlotView(document.getElementsByName('viewPlot')[0].value);

    return;
}

