/**
 * A handful of global support functions.
 *
 * These are available to any template that inherits from base.mako.
 */

/**
 * Provide a function for trimming whitespace.
 */
String.prototype.trim = function() {
  return this.replace(/^\s+|\s+$/g, "");
}

/**
 * Create a string interpolater.  I saw this on the internet.
 *
 * Example usage:
 *   // Gotta create the vMap closure every time.
 *   var vMap = function ( _x_ ) { return eval( _x_ ); };
 *   var var1 = "SomeValue";
 *   "My string {var1}".interp(vMap) -> 'My string SomeValue'.
 */
String.prototype.interp = function ( vMap ) {
   return this.replace( /\{([^}]+)\}/g,
       function ( dummy, v ) {
                 return vMap( v );
       } );
};

/**
 * Allows arrays to be appended to.
 */
Array.prototype.append = function(val) {
    this[this.length] = val;
}

/**
 * Navigate to url.
 */
function goto_url(url) {
  location.href = url;
}

/*
 * Adds the ability to enable / disable controls.
 *
 * $('#myselect').disable();
 * $('#myselect').enable();
 */
(function($) {
  $.fn.enable = function() {
       return this.each(function() {
            $(this).removeAttr('disabled');
       });
  }})(jQuery);

(function($) {
  $.fn.disable = function() {
       return this.each(function() {
            $(this).attr("disabled", "true");
       });
  }})(jQuery);


/*
 * Create a dialog for jquery object jqDlg.
 *
 * The dialog will be constrained to fit in the window.
 */
function dialog(jqDlg, options) {
    if (options == undefined) {
        options = {};
    }

    // 50 pixel fudge factor to account for the titlebar of the dlg
    var fudge_factor_height = 50;

    // If there are buttons, add another 35 pixels to the fudge factor.
    if (options.buttons != undefined) {
        fudge_factor_height += 35;
    }

    var fe_h = $('#fisheye-bottom').length ?
               $('#fisheye-bottom').height() : 0;

    var max_height = $(window).height() - fe_h - fudge_factor_height;

    // Use as much space as possible, so grab the maximum height.
    var dlg_height_estm = jqDlg.height() + fudge_factor_height;
    max_height = Math.min(max_height, dlg_height_estm);

    options = jQuery.extend({height : max_height, maxHeight: max_height,
                             width : 'auto'}, options);
    if (options.height != 'auto') {
        options.position = ['center', 'top'];
    }

    jqDlg.dialog(options);
}


/**
 * Removes the closing 'x' from dialogs and disables the esc to close
 * functionality.
 */
$.fn.rm_dialogx = function() {
    $(this).removeAttr('style');

    $(this).parents(".ui-dialog:first")
        .find(".ui-dialog-titlebar-close").remove();

    return $(this).dialog('option', 'closeOnEscape', false);
}

/**
 * Logout of the system.
 */
function logout() {
    // Update the score, then show test results.
    $.blockUI({ message: '<h1><img src="/images/throbber.gif" /> Logging Out' +
                         '...</h1>' });
    $.getJSON('/logout', function(x) { goto_url('/'); });
}


/**
 * Simple input blockign, throbberized message window.
 */
function wait_msg(msg) {
    $.blockUI.defaults.baseZ = 2000;
    $.blockUI.defaults.css.width = 'auto';
    $.blockUI({ message: '<h2><img src="/images/throbber.gif" />' + msg +
                         '</h2>' });
}
