/**
 * This is a library of common functions used in multiple scripts.
 *
 * @author dittrich
 * @version $Revision$
 */
var utils = { };

utils.loadedLibs = new Array();

/**
 * Tries to attach the listener (function) fn to the given objects event evType. Do
 * not use "on" for event types (e.g. "load" instead of "onload").
 *
 * @param {Object} obj the object the event listener should be attached to
 * @param {String} evType the type of event the listener want to listen to. Attention: the event must not start
 * with "on" as there are some browsers that need the events without this.
 * @param {Function} fn the function to be called
 *
 * @return true or false depending on if the listener could successfully be attached.
 */
utils.addEvent = function (obj, evType, fn) {
  if (typeof fn == 'undefined' | typeof obj == 'undefined') return false;
  if (typeof obj.addEventListener != 'undefined'){
    // standard w3c conform method, .. gecko, safari, konqueror and standard
    obj.addEventListener(evType, fn, false);
    return true;
  } else if (typeof obj.attachEvent != 'undefined'){
    // IE does it different ...
    var r = obj.attachEvent("on"+evType, fn);
    return r;
  } else {
    return false;
  }
};

/**
 * Does the opposite of addEvent: tries to remove an event listener (function) from the objects event evType. Do
 * not use "on" for event types (e.g. "load" instead of "onload").
 *
 * @param {Object} obj the object the listener should be removed from
 * @param {String} svType the event type. Attention: the event must not start
 * with "on" as there are some browsers that need the events without this.
 * @param {Function} fn the function to be removed
 *
 * @return true or false depending on if the listener could successfully be attached.
 */
utils.removeEvent = function (obj, evType, fn) {
  if (typeof obj.removeEventListener != 'undefined'){
    // standard w3c conform method, .. gecko, safari, konqueror and standard
    obj.removeEventListener(evType, fn, false);
    return true;
  } else if (typeof obj.detachEvent != 'undefined'){
    // IE
    var r = obj.detachEvent("on"+evType, fn);
    return r;
  } else {
    return false;
  }
};

/**
 * Function used to manipulate css classes of elements. Can set, add, remove, swap or check for existance of css
 * classes depending on the command parameter 'a'. Possible values for 'a' are:
 * swap replaces class css1 with class c2 in object o.
 * add adds class css1 to the object o.
 * remove removes class css1 from the object o.
 * check test if class css1 is already applied to object o and returns true or false.
 *
 * @param {String} cmd can be one of the following: set, add, remove, swap, check
 * @param {Object} obj the object in question
 * @param {String} css1 the name of the first css class
 * @param {String} css2 the name of the second css class (optional)
 *
 * @return nothing or true/false (depending on the command)
 */
utils.jscss = function (cmd,obj,css1,css2) {
  switch (cmd){
    case 'set':
      obj.className = css1;
    break;
    case 'swap':
      obj.className=!utils.jscss('check',obj,css1)?obj.className.replace(css2,css1) : obj.className.replace(css1,css2);
    break;
    case 'add':
      if(!utils.jscss('check',obj,css1)){obj.className+=obj.className?' '+css1:css1;}
    break;
    case 'remove':
      var rep=obj.className.match(' '+css1)?' '+css1:css1;
      obj.className=obj.className.replace(rep,'');
    break;
    case 'check':
      return new RegExp('\\b'+css1+'\\b').test(obj.className);
  }
};

/**
 * Small helper function to calculate the current position (x, y coordinates) of a given object.
 * @param {Object} obj the object if which you want to find the current position.
 * @return An array of two integers: x and y coordinates (0,0 if the object has no coordinates)
 */
utils.findPos = function (obj) {
  var curleft = 0;
  var curtop = 0;
  if (obj.offsetParent) {
    curleft = obj.offsetLeft;
    curtop = obj.offsetTop;
    while (obj = obj.offsetParent) {
      curleft += obj.offsetLeft;
      curtop += obj.offsetTop;
    }
  }
  return [curleft,curtop];
}

/**
 * Introspection of a given object. Returns a string with all members of the object in
 * "name: value" pairs.
 */
utils.innerObject = function(obj) {
  var text = "";

  for(var i in obj) {
    text += i + ": " + obj[i] + "<br />\n";
  }

  return text;
}

/**
 * Checks (dom wise) if an element is contained within another.
 */
utils.containsDOM = function (container, containee) {
  var isParent = false;
  do {
    if ((isParent = container == containee))
      break;
    containee = containee.parentNode;
  }
  while (containee != null);
  return isParent;
}

/**
 * Dynamically load JavaScript files on runtime.
 * @param {string} libName the name of the javascript file to load (*excluding* path and ".js")
 * @param {string} dir (optional) absolut pathname where the js file is located (default "/js/")
 */
utils.require = function (libName, dir) {
  if (utils.loadedLibs[libName]) return;
  var e = document.createElement("script");
  var path = dir || "/js/";
  e.src = path + libName + ".js";
  e.type="text/javascript";
  document.getElementsByTagName("head")[0].appendChild(e);
  utils.loadedLibs[libName] = true;
}

/**
 * @author zerr
 * Read all Form Elements and create an GET Url String with
 * Elements Names and Values
 * @param {object} Form Object to get the Elements
 * @param {string} suffix to returnd url
 */
utils.convertForm2Url = function(form,suffix) {
  url = "?";
  fElm = form.elements;
  for(i=0; i<fElm.length;i++) {
    if(fElm[i].type == "select-one")
      url += fElm[i].name +"="+fElm[i].value+"&";
    else if(fElm[i].type == "checkbox" && fElm[i].checked)
      url += fElm[i].name +"=1&";
    else if(fElm[i].type == "radio" && fElm[i].checked)
      url += fElm[i].name +"="+fElm[i].value+"&";
    else
      url += fElm[i].name +"="+fElm[i].value+"&";
  }
  if(suffix)	url+=suffix;
  return url;
}

/**
 * Disable the Selection if you want dragNdrop Elements.
 * Special for InternetExplorer
 */
utils.selectionOff = function()	{
	if (document.selection)	document.onselectstart = function() { return false }
}

/**
 * Enable the Selection after the Disabling
 */
utils.selectionOn = function()	{
	if (document.selection) document.onselectstart = null; 
}

/**
 * @author k.seeger
 *
 * open a popup of any size. can be resizable or not.
 * after showing the popup it's centered on the screen.
 * then it gets the focus.
 *
 * @param "href" the url to open
 * @param "title" the title of the popup (no whitespaces allowed here)
 * @param "width" the width of the popup
 * @param "height" the height of the popup
 * @param "resize" boolean (simply pass an int 0 or 1) wheater or not the popup is resizable
 */
utils.openPW = function(href, title, width, height, resize) {
	var x = (screen.availWidth - width)/2;
	var y = (screen.availHeight - height)/2;
	var w = window.open(href, title, "width=" + width + ",height=" + height + ",top=" + y + ",left=" + x + (resize ? ",scrollbars=yes,resizable=yes" : ""));
	w.focus();
}

