/**
 *----------------------------------------------------------*-----------------*
 *     Web Project Manager                                  |   frontSystem   |
 *----------------------------------------------------------*-----------------*
 *     @category frontSystem
 *     @package javascript
 *     @version 0.1 [2008/11/29]
 *
 *     @author Tomáš Režňák <tomas.reznak@web-project-manager.com>
 *     @copyright Copyright © 2008, Tomáš Režňák
 *     @link http://www.web-project-manager.com/
 *----------------------------------------------------------------------------*
 *     definition of general javascript functions
 */

/*
 * HTML object manipulation functions / START
 */
 
/**
 * get HTML element defined by id
 *
 * @return obj    HTML element
 */
function element(id) {
  var obj = document.getElementById(id);
  
  return obj;
}
 
/**
 * hide HTML element
 *    
 * @param mixed obj    object or object's id
 * @return void
 */
function elementHide(obj) {
  if (element(obj)) {    // object identified by its id
    obj = element(obj);
  }
  
  obj.style.display = "none";
}

/**
 * show HTML element
 *    
 * @param mixed obj            object or object's id
 * @param boolean is_inline    [optional] if element is inline, pass true as second parameter
 * @return void
 */
function elementShow(obj, is_inline) {
  if (element(obj)) {    // object identified by its id
    obj = element(obj);
  }

  obj.style.display = (is_inline ? "inline" : "block");
}

/**
 * switch element class name - if element has className class_1, replace it with class_2 and vice versa
 *    
 * @param mixed obj         object or object's id
 * @param string class_1    name of class 1
 * @param string class_2    name of class 2
 * @return void
 */
function switchClass(obj, class_1, class_2) {
  if (element(obj)) {    // object identified by its id
    obj = element(obj);
  }
  
  if (obj.className == "") {    // class name empty -> set class name from class_1
    obj.className = class_1;
  } else {
    if (obj.className.indexOf(class_1) != -1) {    // class_1 exists -> replace with class_2
      obj.className = obj.className.replace(class_1, class_2);
    } else {
      if ((obj.className.indexOf(class_2) != -1) && (class_2 != "")) {    // class_2 exists -> replace with class_1
        obj.className = obj.className.replace(class_2, class_1);
      } else {    // if not found, add class_1 to existing
        obj.className = obj.className + " " + class_1;
      }
    }
  }
}

/*
 * HTML object manipulation functions / END
 */


/*
 * card functions / START
 */
 
/**
 * display user card
 *
 * @param integer id         user id  
 * @param object obj         reference object used for card positioning
 * @param string template    card template which generates card content
 * @param string script      URL leading to ajax script processing requests
 * @return void
 */
function cardShowUser(id, obj, template, script) {
  var width = "300";    // approximate card width (in pixels)
  var pos = elementPosition(obj);
  var left, top;
  if ((pos.x) > (width + 5)) {
    left = (pos.x - width);
  } else {
    left = pos.x;
  }
  top = pos.y;

  var ajax = new traxi();
  ajax.action_script = script;
  ajax.execute_response = true;
  ajax.set("action", "card");
  ajax.set("type", "user");
  ajax.set("id", id);
  ajax.set("card_template", template);
  ajax.set("left", left + "px");
  ajax.set("top", top + "px");
  ajax.run();	  
}

/**
 * hide card (typically when user clicks on 'close' link within the card)
 *    
 * @return void
 */
function cardClose() {
  var obj = element("card_div");
  if (obj) {
    obj.parentNode.removeChild(obj);
  }
}

/**
 * calculate element absolute position
 *    
 * @param object obj     reference object used for positioning
 * @param string type    [optional] type of coordinates to be returned
 *                         - 'bottomright' or not set: coordinates of bottom right corner
 *                         - 'topleft': coordinates of top left corner
 * @return object        element position (x, y)
 */
function elementPosition(obj, type) {
  var pos = {x:0, y:0};
	
  if (type == null) {
    type = "bottomright";
  }
	
  pos.x = obj.offsetLeft
  pos.y = obj.offsetTop
	
  if (type == "bottomright") {
    pos.x += obj.offsetWidth;
    pos.y += obj.offsetHeight;
  }

  while((obj = obj.offsetParent) != null) {
    pos.x += obj.offsetLeft;
    pos.y += obj.offsetTop;
  }
	
  return pos;
}

/*
 * card functions / END
 */

