/**
 * Basic C4U Javascript library
 * 
 * Copyright (c) 1999-2010 by Community4you GmbH
 */

/**
 * Centers an element on the screen or at the parent.
 * 
 * @param theParentID if null, the element will be centered on the screen,
 * otherwise on the parent
 * @param aPositionAbsolute only for parent id - if true, the element
 * will be positioned abolsutely 
 */
jQuery.fn.center = function(theParentID, aPositionAbsolute)
{
  if (theParentID)
  {
    var _id = '#' + theParentID;
    var _parent = jQuery(_id);

    if (_parent.size() == 1)
    {
      if (aPositionAbsolute)
      {
        this.css("position", "absolute");
        
        var _top = (_parent.offset().top + ( _parent.height() - this.height()) / 2 );
        if (_top < 0)
          _top = 0;
        
        this.css("top", _top + "px");
        
        var _left = (_parent.offset().left + (_parent.width() - this.width()) / 2 );
        if (_left < 0)
          _left = 0;
        
        this.css("left", _left + "px");
      }
      else
      {
        this.css("margin-top", (_parent.height() - this.height()) / 2
            + _parent.scrollTop() + "px");
        this.css("margin-left", (_parent.width() - this.width()) / 2
            + _parent.scrollLeft() + "px");
      }
    }
  }
  else
  {
    this.css("position", "absolute");
    
    this.css("top", (jQuery(window).height() - this.height()) / 2
        + jQuery(window).scrollTop() + "px");
    this.css("left", (jQuery(window).width() - this.width()) / 2
        + jQuery(window).scrollLeft() + "px");
  }

  return this;
}

/**
 * Focusses a field.
 * 
 * @param theID the id of the field
 */
function focus_field(theID)
{
  if (!theID)
    return;

  var _id = '#' + theID;

  jQuery(document).ready(function()
  {
    _obj = jQuery(_id);

    if (_obj.size() == 1)
    {
      _obj.focus();
    }
  });
}

/**
 * Clears the dummy input of an element.
 * 
 * This method clears the elements value, if the value matches
 * the passed string.
 */
function clearDummyInput(theElement, theDefaultValue)
{
  if (!theElement || !theDefaultValue)
    return;
  
  if (theElement.value == theDefaultValue)
    theElement.value = '';
}

/**
 * Sets the dummy input of an element.
 * 
 * This method sets the elements value, if there is no value.
 */
function setDummyInput(theElement, theDefaultValue)
{
  if (!theElement || !theDefaultValue)
    return;
  
  if (!theElement.value || theElement.value == '')
    theElement.value = theDefaultValue;
}

/**
 * Opens a link in a different frame (or the same frame).
 * 
 * @param theFrameID the id of the frame
 * @param theURL the url
 * @param theWaitDialog on of the wait dialogs or null, if nothing to display
 */
function reloadFrame(theFrameID, theURL, theWaitDialog)
{
  var _window;
  
  if (!theFrameID)
    _window = window;
  else
  {
    if (window.parent)
    {
      if ((theFrameID == '_top') || (theFrameID == 'main'))
        _window = window.parent;
      else
      {
        if (window.parent.frames[theFrameID] != null)
          _window = window.parent.frames[theFrameID];
        else
          _window = window;
      }
    }
    else
      _window = window;
  }
  
  if (!_window)
    return;
  
  _window.document.location.href = theURL;
  
  if (theWaitDialog)
    _window.showProgressDialog(theWaitDialog);
}

/**
 * Encodes an url parameter.
 * 
 * This function uses encodeURI to escape all non ascii characters according to the current charset.
 * Additionally, it escapes & and ?.
 *
 */
function encodeURLParameter(theParameter)
{
  if (!theParameter)
    return null;
  
  var _param = encodeURI(theParameter);
  
  // escape & and ?
  _param = _param.replace(/[&]{1,1}/g, "%26");
  _param = _param.replace(/[?]{1,1}/g, "%3F");
  
  return _param;
}

/**
 * Adds or removes a class.
 * 
 * This function toggles the div classes.
 * 
 * @param theElement the optional element (string or object).
 * @param anAdd if true add, otherwise remove
 */
function toggleClass(theElement, theClass, anAdd)
{
  var _element;
  
  if ((typeof theElement) == 'string')
    _element = jQuery('#' + theElement);
  else
    _element = jQuery(theElement);
  
  if (anAdd)
    _element.addClass(theClass);
  else
    _element.removeClass(theClass);
}

/**
 * Sets a class.
 * 
 * @param theElement the optional element (string or object).
 * @param theClass the class
 */
function setClass(theElement, theClass)
{
  var _element;
  
  if ((typeof theElement) == 'string')
    _element = jQuery('#' + theElement);
  else
    _element = jQuery(theElement);
  
  _element.removeClass();
  _element.addClass(theClass);
}

/**
 * Checks, if an element had been clicked.
 * 
 * @param theElementID the element id
 * @param theEvent the event (containing pageX and pageY)
 * @return true, if clicked
 */
function elementClicked(theElementID, theEvent)
{
  var _element = jQuery('#' + theElementID);
  if (_element.size() == 0)
    return false;
  
  if ((_element.offset().left > theEvent.pageX) ||
      (_element.offset().top > theEvent.pageY) ||
      ((_element.offset().top + _element.height()) < theEvent.pageY) ||
      ((_element.offset().left + _element.width()) < theEvent.pageX))
    return false;
  
  return true;
}

/**
 * sets the display property of the given element to show.
 * 
 * @param theElementID the element id
 */
function showElement(theElementID)
{
	jQuery('#' + theElementID).show();
}