/**
 * this function will animate the element provided with the text and colour provided.
 *
 * The amimation will finish in 6 seconds.  Assumes background is white.
 * */
function notifyStatus(color, text, element)
{
  //color to change to
  var toColorAtt = {
    backgroundColor: { to: color }
  };

  //white to change back to
  var whiteAtt = {
    backgroundColor: { to: '#ffffff' }
  };

  //animations - first defaults to 1 second, second is 5 secs
  var toColor = new YAHOO.util.ColorAnim(element, toColorAtt);
  var toWhite = new YAHOO.util.ColorAnim(element, whiteAtt, 5);

  //chain the animations together
  toColor.onComplete.subscribe(function()
  {
    toWhite.animate();
  });

  //reset the html of the element
  toWhite.onComplete.subscribe(function()
  {
    element.innerHTML = '';
  });

  //perform the animation
  element.style.color = '#ffffff';
  element.style.textAlign = 'center';
  element.innerHTML = text;
  toColor.animate();
}


/*
 * Function will animate the element provided once the page has loaded
 * */
function notifyStatusOnLoad(color, text, elementName)
{
  var oldOnLoad;

  oldOnLoad = window.onload;
  if (typeof window.onload != 'function')
  {
    window.onload = function()
    {
      var element = document.getElementById(elementName);
      notifyStatus(color, text, element)
    };
  }
  else
  {
    window.onload = function()
    {
      oldOnLoad();
      var element = document.getElementById(elementName);
      notifyStatus(color, text, element)
    };
  }
}


/**
 * This function will get a parameter from the query string if it exists
 * */
function queryString(parameter)
{
  var loc = location.search.substring(1, location.search.length);
  var param_value = false;

  var params = loc.split("&");
  for (i = 0; i < params.length; i++)
  {
    param_name = params[i].substring(0, params[i].indexOf('='));
    if (param_name == parameter)
    {
      param_value = params[i].substring(params[i].indexOf('=') + 1)
    }
  }
  if (param_value)
  {
    return param_value;
  }
  else
  {
    return null;
  }
}

/**
 * Function to print a pretty dialog box for the user to confirm something.
 * If the user confirms the text they will be forwarded to the URL
 * */
function userConfirm(dialogText, url)
{
  // Define various event handlers for Dialog
  var handleYes = function()
  {
    window.location = url;
    this.hide();
  };
  var handleNo = function()
  {
    this.hide();
  };

  // Instantiate the Dialog
  var simpledialog1 = new YAHOO.widget.SimpleDialog("si",
  { width: "300px",
    fixedcenter: true,
    visible: false,
    draggable: false,
    close: true,
    text: dialogText,
    icon: YAHOO.widget.SimpleDialog.ICON_HELP,
    constraintoviewport: true,
    buttons: [
      {
        text:"Yes",
        handler:handleYes,
        isDefault:true
      },
      {
        text:"No",
        handler:handleNo
      }
    ]
  });
  simpledialog1.setHeader("Are you sure?");

  // Render the Dialog
  simpledialog1.render("dialog");

  //display the dialog
  simpledialog1.show();
}

//function to override the normal JS alert with one that looks nicer
(function()
{
  YAHOO.namespace('widget.alert');

  alert_old = window.alert;
  window.alert = function(str)
  {
    YAHOO.widget.alert.dlg.setBody(str);
    YAHOO.widget.alert.dlg.cfg.queueProperty('icon', YAHOO.widget.SimpleDialog.ICON_WARN);
    YAHOO.widget.alert.dlg.cfg.queueProperty('zIndex', 9999);
    YAHOO.widget.alert.dlg.render(document.body);
    if (YAHOO.widget.alert.dlg.bringToTop)
    {
      YAHOO.widget.alert.dlg.bringToTop();
    }
    YAHOO.widget.alert.dlg.show();
  };


  YAHOO.util.Event.on(window, 'load', function()
  {

    var handleOK = function()
    {
      this.hide();
    };

    YAHOO.widget.alert.dlg = new YAHOO.widget.SimpleDialog('widget_alert', {
      visible:false,
      width: '20em',
      zIndex: 9999,
      close: false,
      fixedcenter: true,
      modal: false,
      draggable: true,
      constraintoviewport: true,
      icon: YAHOO.widget.SimpleDialog.ICON_WARN,
      buttons: [
        {
          text: 'OK',
          handler: handleOK,
          isDefault: true
        }
      ]
    });
    YAHOO.widget.alert.dlg.setHeader("Alert!");
    YAHOO.widget.alert.dlg.setBody('Alert body passed to window.alert'); // Bug in panel, must have a body when rendered
    YAHOO.widget.alert.dlg.render(document.body);
  });
})();

// Function to add the relevant file icon to the node in a treeview.
function addIcon(node)
{
  var label = node.label;

  var tokens = label.split('.');
  var suffix = tokens[tokens.length - 1];

  // Generic
  node.labelStyle = 'fileIcon icon-gen';

  // Zip files
  if (suffix == 'cxd' || suffix == 'zip')
  {
    node.labelStyle = 'fileIcon icon-zip';
  }
  // Doc files
  if (suffix == 'doc' || suffix == 'docx' || suffix == 'DOC')
  {
    node.labelStyle = 'fileIcon icon-doc';
  }
  // PPT
  if (suffix == 'ppt' || suffix == 'pptx')
  {
    node.labelStyle = 'fileIcon icon-ppt';
  }
  // PDF
  if (suffix == 'pdf' || suffix == 'ps')
  {
    node.labelStyle = 'fileIcon icon-prv';
  }
  // DMG
  if (suffix == 'dmg' || suffix == 'iso')
  {
    node.labelStyle = 'fileIcon icon-dmg';
  }
  // jar
  if (suffix == 'jar')
  {
    node.labelStyle = 'fileIcon icon-jar';
  }
}

function toggleDiv(scrolldiv, nextMenu)
{
  if (document.getElementById(scrolldiv).style.display == 'none')
  {
    document.getElementById(scrolldiv).style.display = 'block';
    if (nextMenu !== '')
    {
      document.getElementById(nextMenu).style.borderTop = '1px solid black';
    }
  }
  else
  {
    document.getElementById(scrolldiv).style.display = 'none';
    if (nextMenu !== '')
    {
      document.getElementById(nextMenu).style.borderTop = '';
    }
  }
}

