/*
 *  When this page is done loading, attach events to the input boxes at the
 *  bottom so that the links created by the user-defined "launch" tag are
 *  automatically updated with parameters.  Right now I'm just updating all
 *  of the tool links.  It could be done such that the click() event does the
 *  update, but then the user can't bookmark the link and use it later.
 */
var allLinks = { };

// http://download.rptools.net/webstart.php/maptool-development.jnlp?...
var baseURL = 'http://download.rptools.net/webstart.php/';

/*
 *  This function expects there to be an array ("maptoolVers") that exists
 *  at execution time which holds all valid link names for MapTool versions.
 *  This allows the automatic creation of a dropdown list that the user can
 *  select from and modify the memory parameters for.
 */
$( function() {
    // Keep track of the original URL on all anchor elements.
    $('.tool').each( function() {
        var a = $(this);
        allLinks[ a.attr("id") ] = a.attr("href");   // Save the original URL
    } );
    // Fill in the dropdown list with passed in version names.
    if (typeof maptoolVers != 'undefined') {
	var regex = /^.*[/](.*?)[.](zip|jar|txt|jnlp)$/i;
	// This is ugly.  Puts the leading "0" in for single-digit
	// builds so that the array will sort properly, then we take
	// it back out again for display purposes.  Fortunately, the
	// user will never see this crappy code. :)
	for (i in maptoolVers) {
	    var file = maptoolVers[i].replace(regex, "$1");
	    file = file.replace(/b(\d)$/, "b0$1");
	    maptoolVers[i] = file;
	}
	maptoolVers.sort();
	var select = $('select.altversion');
	for (i in maptoolVers) {
	    var file = maptoolVers[i].replace(/b0(\d)$/, "b$1");
	    var option = $('<option/>')
		.val(baseURL + file)
		.text(file);
	    select.append(option);
	}
    } else {
	$('div.altversion').hide();
	allLinks[ "altversion" ] = null;
    }
    $('a.customization').click( function() {
	$('div.javascript_on').toggle();
	return false;	// Disables the normal 'click' processing
    });
    $('div.javascript_on').hide();
    $('div.javascript_off').hide();
    $('.jvm').change(updateLink);
    updateLink();    // Gotta "prime the pump", so to speak.
} );

var maxmem, minmem, stack;

/*
 *  This function is invoked whenever one of the input fields is
 *  changed.  That currently means the maximum, minimum, and stack size.
 *  This function determines what the correct URL is for the given
 *  parameters and updates the 'href' attributes of the anchor elements.
 *  The original 'href' values have been stored in "allLinks" so that
 *  they can be retrieved and new text added to the end.  This allows
 *  the page to function properly when javascript is not enabled, albeit
 *  without the ability to customize the memory sizes.
 */
function updateLink() {
    // Retrieve the text currently stored in the dropdown box.
    // It is combined with 'baseURL' and used as the URL.
    var maptool = $('select.altversion option:selected');
    if (maptool != null) {
	// val() returns VALUE attribute, or content if VALUE is empty
	allLinks[ "altversion" ] = maptool.val();
	$("#altversion").html(maptool.text());
    }
    maxmem = $('#maxmem').val();
    minmem = $('#minmem').val();
    stack = $('#stack').val();
    $('.tool').each( function() {
        var id = $(this).attr("id");
        var newURL = allLinks[ id ] +
	    ".jnlp?vmargs=-Xmx" + maxmem +
	    "m%20-Xms" + minmem +
	    "m%20-Xss" + stack + "m";
        $(this).attr("href", newURL);
    } );
}
