var sajax_debug_mode = false;
var canRefresh = new Array();
var fetched = new Array();
var timeCoefficient = 1;
var wgAjaxR = {} ;

// Initialise the automatic refresh for each box.
wgAjaxR.onLoad = function() {
    var block = null;
    for( block in ajaxrBlocks) {
        if(ajaxrBlocks.hasOwnProperty(block))
        {
            var curVal = String(ajaxrBlocks[block]);

            setTimeout( 'wgAjaxR.refresh("' + curVal + '")', 1000 );

            wgAjaxR.allowRefresh(ajaxrBlocks[block]);
            fetched[ajaxrBlocks[block]] = false;

        }
    }
};

// Fetch the R output and update with result
wgAjaxR.refresh = function(blockID) {
    if( fetched[blockID] ) {
        return;
    }

    var Rbox = document.getElementById( 'ajax-r-results-' + blockID );
    if(Rbox.innerHTML.indexOf('retrieved') > 0 || Rbox.innerHTML.indexOf('Execution halted') > 0)
    {
        wgAjaxR.setStatusTime(blockID);
        fetched[blockID] = true;
        return;
    }
    else
    {
        setTimeout( 'wgAjaxR.refresh("'+ blockID + '")', 1001 * timeCoefficient);
        timeCoefficient = timeCoefficient * 1.2;
    }

	// Do the ajax call to the server
	sajax_do_call( "fetchROutput", [ blockID ], Rbox );

};

// Fetch the R output and update with result
wgAjaxR.userRefresh = function(blockID) {
    if( !canRefresh[blockID] ) {
        return;
    }

    // Disable new requests for 1 second
    canRefresh[blockID] = false;

    setTimeout( 'wgAjaxR.allowRefresh("' + blockID + '")', 1000 );

    var Rbox = document.getElementById( 'ajax-r-results-' + blockID );

	// Do the ajax call to the server
	sajax_do_call( "fetchROutput", [ blockID ], Rbox );

    wgAjaxR.setStatusTime(blockID);
    document.getElementById('ajaxrresult-' + blockID).className = 'level1 active';
    document.getElementById('ajaxrscript-' + blockID).className = 'level1';
    document.getElementById('ajaxrlink-' + blockID).className = 'level1';
    document.getElementById('ajaxrinfluence-' + blockID).className = "level1";
    document.getElementById('ajaxrinfluencexml-' + blockID).className = "level1";
};

// Fetch the R output and update with result
wgAjaxR.userRecalculate = function(blockID) {
    if( !canRefresh[blockID] ) {
        return;
    }

    // Disable new requests for 2 seconds
    canRefresh[blockID] = false;

    setTimeout( 'wgAjaxR.allowRefresh("' + blockID + '")', 2000 );

    var Rbox = document.getElementById( 'ajax-r-results-' + blockID );
    //sajax response is asynchronous. We need to change the text now.
    Rbox.innerHTML = 'Processing begun.';
    // Do the ajax call to the server
    sajax_do_call( "recalculateR", [ blockID ], Rbox );

    var statusBox = document.getElementById( 'Recalculate-' + blockID);
    if(!(statusBox.innerHTML.indexOf("ecalculating") > 0))
    {
        fetched[blockID] = false;
        timeCoefficient = 1;
        setTimeout( 'wgAjaxR.refresh("'+ blockID + '")', 1000);
    }
    wgAjaxR.setStatus(blockID, "Recalculating");
    document.getElementById('ajaxrresult-' + blockID).className = 'level1 active';
    document.getElementById('ajaxrscript-' + blockID).className = 'level1';
    document.getElementById('ajaxrlink-' + blockID).className = 'level1';
    document.getElementById('ajaxrinfluence-' + blockID).className = "level1";
    document.getElementById('ajaxrinfluencexml-' + blockID).className = "level1";
};

// Fetch the R output and update with result
wgAjaxR.userRecalculateTinyBox = function(blockID) {
	if( !canRefresh[blockID] ) { return; }

	// Disable new requests for 2 seconds
	canRefresh[blockID] = false;

	setTimeout( 'wgAjaxR.allowRefresh("' + blockID + '")', 2000 );

	var Rbox = document.getElementById( 'ajax-r-results-' + blockID );
	//sajax response is asynchronous. We need to change the text now.
	Rbox.innerHTML = 'Processing begun.';
	// Do the ajax call to the server
	sajax_do_call( "recalculateR", [ blockID ], Rbox );

	var statusBox = document.getElementById( 'Recalculate-' + blockID);
	if(!(statusBox.innerHTML.indexOf("ecalculating") > 0))
	{
		fetched[blockID] = false;
		timeCoefficient = 1;
		setTimeout( 'wgAjaxR.refresh("'+ blockID + '")', 1000);
	}
	wgAjaxR.setStatus(blockID, "Recalculating");
};

// Fetch the R input and update with result
wgAjaxR.getSource = function(blockID) {
    if( !canRefresh[blockID] ) {
        return;
    }

    // Don't let a stray autorefresh overwrite the source.
    fetched[blockID] = true;

    // Disable new requests for 1 second
    canRefresh[blockID] = false;

    setTimeout( 'wgAjaxR.allowRefresh("' + blockID + '")', 1000 );

	// Do the ajax call to the server
	var Rbox = document.getElementById( 'ajax-r-results-' + blockID );
	sajax_do_call( "fetchRInput", [ blockID ], Rbox );

    wgAjaxR.setStatusTime(blockID);

    document.getElementById('ajaxrresult-' + blockID).className = 'level1';
    document.getElementById('ajaxrscript-' + blockID).className = 'level1 active';
    document.getElementById('ajaxrlink-' + blockID).className = 'level1';
    document.getElementById('ajaxrinfluence-' + blockID).className = "level1";
    document.getElementById('ajaxrinfluencexml-' + blockID).className = "level1";
};

// Fetch the R input and update with result
wgAjaxR.getInfluence = function(blockID) {
    if( !canRefresh[blockID] ) {
        return;
    }

    // Don't let a stray autorefresh overwrite the source.
    fetched[blockID] = true;

    // Disable new requests for 1 second
    canRefresh[blockID] = false;

    setTimeout( 'wgAjaxR.allowRefresh("' + blockID + '")', 1000 );

    // Do the ajax call to the server
    var Rbox = document.getElementById( 'ajax-r-results-' + blockID );
    sajax_do_call( "fetchInfluence", [ blockID, 1], Rbox );

    wgAjaxR.setStatusTime(blockID);
    document.getElementById('ajaxrresult-' + blockID).className = 'level1';
    document.getElementById('ajaxrscript-' + blockID).className = 'level1';
    document.getElementById('ajaxrlink-' + blockID).className = 'level1';
    document.getElementById('ajaxrinfluence-' + blockID).className = "level1 active";
    document.getElementById('ajaxrinfluencexml-' + blockID).className = "level1";
};

// Fetch the R input and update with result
wgAjaxR.getInfluencexml = function(blockID) {
    if( !canRefresh[blockID] ) {
        return;
    }

    // Don't let a stray autorefresh overwrite the source.
    fetched[blockID] = true;

    // Disable new requests for 1 second
    canRefresh[blockID] = false;

    setTimeout( 'wgAjaxR.allowRefresh("' + blockID + '")', 1000 );

    // Do the ajax call to the server
    var Rbox = document.getElementById( 'ajax-r-results-' + blockID );
    if (!(Rbox.innerHTML.indexOf('code') > 0)) sajax_do_call( "fetchInfluence", [ blockID], Rbox);
    setTimeout( 'wgAjaxR.highlight("' + blockID + '")', 1000 );
    wgAjaxR.setStatusTime(blockID);
    document.getElementById('ajaxrresult-' + blockID).className = 'level1';
    document.getElementById('ajaxrscript-' + blockID).className = 'level1';
    document.getElementById('ajaxrlink-' + blockID).className = 'level1';
    document.getElementById('ajaxrinfluence-' + blockID).className = "level1";
    document.getElementById('ajaxrinfluencexml-' + blockID).className = "level1 active";
};

wgAjaxR.highlight = function(blockID) {
    var Rbox = document.getElementById( 'ajax-r-results-' + blockID );
    if (!(Rbox.innerHTML.indexOf('code') > 0)) setTimeout( 'wgAjaxR.highlight("' + blockID + '")', 1000 );
    else
    {
            editor = CodeMirror.fromTextArea('code', {
        height: "500px",
        parserfile: ["parsexml.js", "parsecss.js", "tokenizejavascript.js", "parsejavascript.js", "parsehtmlmixed.js"],
        stylesheet: [wgServer + wgScriptPath + "/extensions/AjaxR/codecolour/css/xmlcolors.css", wgServer + wgScriptPath + "/extensions/AjaxR/codecolour/css/jscolors.css", wgServer + wgScriptPath + "/extensions/AjaxR/codecolour/css/csscolors.css"],
        path: wgServer + wgScriptPath + "/extensions/AjaxR/codecolour/js/"//,
        //indentUnit: 4

      });
    }
}
// Fetch the R input and update with result
wgAjaxR.getLink = function(blockID) {
    if( !canRefresh[blockID] ) {
        return;
    }

    // Don't let a stray autorefresh overwrite the source.
    fetched[blockID] = true;

    // Disable new requests for 1 second
    canRefresh[blockID] = false;

    setTimeout( 'wgAjaxR.allowRefresh("' + blockID + '")', 1000 );

    // Do the ajax call to the server
    var Rbox = document.getElementById( 'ajax-r-results-' + blockID );
    sajax_do_call( "fetchRBox", [ blockID, 1 ], Rbox );

    document.getElementById('ajaxrresult-' + blockID).className = 'level1';
    document.getElementById('ajaxrscript-' + blockID).className = 'level1';
    document.getElementById('ajaxrlink-' + blockID).className = 'level1 active';
    document.getElementById('ajaxrinfluence-' + blockID).className = "level1";
    document.getElementById('ajaxrinfluencexml-' + blockID).className = "level1";
};

// callback to allow refresh
wgAjaxR.allowRefresh = function(elementID) {
    canRefresh[elementID] = true;
};

wgAjaxR.setStatusTime = function(blockID)
{
    var Rbox = document.getElementById( 'ajax-r-results-' + blockID );
    var timeElement = document.getElementById('Recalculate-' + blockID);
    if(Rbox.innerHTML.indexOf('retrieved') > 0 || Rbox.innerHTML.indexOf('Execution halted') > 0)
    {
        var currentTime = new Date();
        var hours = currentTime.getHours();
        var minutes = currentTime.getMinutes();
        var seconds = currentTime.getSeconds();

        if (minutes < 10) minutes = "0" + minutes
        if (seconds < 10) seconds = "0" + seconds

        timeElement.innerHTML = "Retrieved at " + hours + ":" + minutes + ":" + seconds+"";
    }
    else timeElement.innerHTML = "Still processing";
}
wgAjaxR.setStatus = function(blockID, text)
{
    var timeElement = document.getElementById('Recalculate-' + blockID);
    timeElement.innerHTML = text;
}
// Register our initialization function.
hookEvent( "load", wgAjaxR.onLoad);

