/*
 * This set of Javascript code handles drawing all of the buttons in the system.
 * To change the look and feel of buttons, provide different definitions for
 * the functions in this file.
 */

/* Stack to store the buttons set by storeButton(). */
var _buttonStore = new Array();
/* Stack index for the buttons set by storeButton(). */
var _buttonStoreIndex = 0;
/* Holder for the state of buttons, as set by setActiveButton(). */
var _buttonState = new Object();
/* The default location of icons for buttons. */
var _buttonImageFolder = "modules/skin/images/buttons";
/* The default location of icons for small buttons. */
var _smallButtonImageFolder = "modules/skin/images/buttons/small";
/* Variable to hold the current location of icons for buttons. */
var _iconFolder = _buttonImageFolder;

/*
 * Called to change the location of icons for icon buttons. This is called before
 * a set of calls to drawIconButton().
 */
function setIconFolder(fldr) {
  _iconFolder = fldr;
}

/*
 * Set a button in the specified state (state=true means active; state=false means inactive).
 */
function setActiveButton(id, state) {
  _buttonState[id] = state;
  var im = document.images[id];
  if (im) {
    im.src = _iconFolder + "/" + id + (state ? ".gif" : "off.gif");
  }
}

/*
 * Determines whether the indicated button is active.
 */
function isActiveButton(id) {
  var flag = _buttonState[id];
  return flag == null || flag;
}

/*
 * Draw a button using a graphic icon. A button is inactive if it has been set 
 * inactive using setActiveButton(), or if it is drawn with an empty action.
 */
function drawIconButton(id, label, action) {
  if (action) {
    document.write("<a");
    document.write(" href=\"javascript:if(isActiveButton('" + id + "')) {" + action + "}\"");
    document.write(" onmouseover=\"_mouseOverButton('" + id + "', '" + _iconFolder + "')\"");
    document.write(" onmouseout=\"_mouseOutButton('" + id + "', '" + _iconFolder + "')\">");
    document.write("<img");
    document.write(" src=\"" + _iconFolder + "/" + id + ".gif\"");
    document.write(" border=\"0\"");
    document.write(" name=\"" + id + "\">");
    document.write("</a>");
  } else {
    document.write("<img");
    document.write(" src=\"" + _iconFolder + "/" + id + "off.gif\"");
    document.write(" border=\"0\"");
    document.write(" name=\"" + id + "\">");
  }
}

/*
 * Draw a button using CSS styles.
 */
function drawCSSButton(id, label, action, css) {
  if (action) {
    document.write("<a class=\"" + css + "\"");
    document.write(" href=\"javascript:if(isActiveButton('" + id + "')) {" + action + "}\"");
    document.write(" onmouseover=\"_mouseOverButton('" + id + "')\"");
    document.write(" onmouseout=\"_mouseOutButton('" + id + "')\">");
    document.write(label);
    document.write("</a>&nbsp;");
  } else {
    document.write("<span class=\"inactive\">");
    document.write("<span class=\"" + css + "\">");
    document.write(label);
    document.write("</span>");
    document.write("</span>&nbsp;");
  }
}

function drawCSSButtonLink(id, label, url, css) {
	document.write("<a class=\"" + css + "\"");
	document.write(" href=\"" + url + "\"");
	document.write(" onmouseover=\"_mouseOverButton('" + id + "')\"");
    document.write(" onmouseout=\"_mouseOutButton('" + id + "')\">");
    document.write(label);
    document.write("</a>&nbsp;");	
}

/*
 * This function is called back to whenever there is a mouseover on a 
 * button drawn by drawButton(). It is never called directly in code
 * written into modules files; it is only called by generated code
 * from drawButton().
 */
function _mouseOverButton(id, fldr) {
  if (! isActiveButton(id)) {
    return;
  } else if (document.getElementById && document.getElementById(id + "_comment")) {
    document.getElementById(id + "_comment").style.visibility = "visible";
  }
  if (fldr) {
    document.images[id].src = fldr + "/" + id + "on.gif";
  }
}

/*
 * This function is called back to whenever there is a mouseout on a 
 * button drawn by drawButton(). It is never called directly in code
 * written into modules files; it is only called by generated code
 * from drawButton().
 */
function _mouseOutButton(id, fldr) {
  if (! isActiveButton(id)) {
    return;
  } else if (document.getElementById && document.getElementById(id + "_comment")) {
    document.getElementById(id + "_comment").style.visibility = "hidden";
  }
  if (fldr) {
    document.images[id].src = fldr + "/" + id + ".gif";
  }
}

/*
 * Draw the popup for a button with the given id. The popup will appear at the
 * location in the page where this function is called. In the standard implementation
 * this is called from within the template frames in modules/skin.
 */
function drawButtonPopup(idtext, comment, offset) {
  var id = idtext + "_comment";
  document.writeln("<div id='" + id + "' class='popup'>" + comment + "</div>");
  var sty = document.getElementById(id).style;
  sty.visibility = "hidden";
  if (offset) {
    sty.left = offset;
  }
}

/*
 * Add a button to the store of buttons to be drawn by drawStoredButtons(). 
 * The popup text is optional. The kernel makes calls to storeButton() to
 * register control buttons on each individual page, and the skin
 * chooses where these should be placed, by the location of the call to
 * drawStoredButtons().
 */
function storeButton(id, label, action, comment, offset) {
  var b = new Object();
  b.id = id;
  b.label = label;
  b.action = action;
  b.comment = comment;
  b.offset = offset;
  _buttonStore[_buttonStoreIndex++] = b;	
}

/*
 * Draw all the buttons that have been registered by calls to storeButton().
 * The kernel makes calls to storeButton() to register control buttons on each 
 * individual page, and the skin chooses where these should be placed, by the 
 * location of the call to this function in the page. In the standard implementation
 * the calls to drawStoredButtons() are in the template frames in modules/skin.
 */
function drawStoredButtons() {
  for (var i=0; i<_buttonStore.length; i++) {
    var b = _buttonStore[i];
    drawButton(b.id, b.label, b.action);
  }
}

/*
 * Draw the popups for the buttons that have been registered by calls to storeButton().
 */
function drawStoredButtonPopups() {
  for (var i=0; i<_buttonStore.length; i++) {
    var b = _buttonStore[i];
    if (b.comment) {
      drawButtonPopup(b.id, b.comment, b.offset ? b.offset : (25 + i*70));
    }
  }
}





