/*
 *  Global Javascripts, used throughout the website.
 *
 *  @version $Id: main.js 94 2010-09-20 13:20:54Z jpeloquin_apperian $
 */

/*
 *  Extend Prototype.js
 *  Add the ability to access an iFrame's DOM
 *  using the standard $() syntax in Prototype.
 */
Element.addMethods('iframe', {
document: function(element) {
  element = $(element);
  if (element.contentWindow)
      return element.contentWindow.document;
  else if (element.contentDocument)
      return element.contentDocument;
  else
      return null;
},
$: function(element, frameElement) {
  element = $(element);
  var frameDocument = element.document();
  if (arguments.length > 2) {
      for (var i = 1, frameElements = [], length = arguments.length; i < length; i++)
          frameElements.push(element.$(arguments[i]));
      return frameElements;
  }
  if (Object.isString(frameElement))
      frameElement = frameDocument.getElementById(frameElement);
  return frameElement || element;
}
});

/*  HIDE WITH js-hide
 *	jsHide
 *  Removes the "js-hide" class form elements containing it, and
 *  replaces it with an inline style of "display:none".
 *  This should alleviate the "flashing" that occurs when JS tries to
 *  hide elements after the DOM is loaded, while allowing Prototype/Scriptaculous
 *  the ability to control the element's visability.
 */
function jsHide()
{
    var elms = $$('[class~=js-hide]');
    elms.invoke('setStyle','display:none').invoke('removeClassName','js-hide');
}
document.observe("dom:loaded", jsHide);



/*  URL PARAMS
 *  Retrieves a particular parameter from the URL string.
 *  @param  string  name: parameter name
 */
function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if ( results == null )
    return "";
  else
    return results[1];
}


/*
 *  watchTooltips()
 *  Set an observer to watch any Tooltips on a page
 *  for clicks, triggering their tips to be toggled.
 *
 */
function watchTooltips()
{
    var tips = $$('[class~=tooltip-img]');

    tips.each(function(s){
        var tiptip = s.next('[class~=tooltip-tip]');
        s.observe('click', function(){
            Effect.toggle(tiptip, 'appear', {duration:0.3});
        });
    });
}
document.observe("dom:loaded", watchTooltips);



/*
 *  Confirm Delete
 *  Confirm the deletion of a database record
 *  param   string  m : Confirmation Message string
 */
function confirmDelete(m)
{
    if ( (m == null)   ||  (typeof m == 'undefined') )    { var m = 'Are you sure you want to Delete this?'; }
    var c = confirm(m);
    return c;
}


/*
 *  Display the Spinner
 *  displaySpinner()
 *  Displays an animated graphic upon submission
 *  of the upload form.
 *  @param  object the form $(object) we wish to watch.
 */
function displaySpinner(FORM)
{
    FORM.observe('submit', function(e){
        //Event.stop(e);
        $('OverlaySpinner').show();
    });
}

