/**
 *----------------------------------------------------------*-----------------*
 *     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
 */

/*
 * custom functions
 */
 
function down(file_id) {
  var obj = element("down_" + file_id);
  cardShowDownload(file_id, obj, 'CARD_DOWNLOAD', '/ajax.php');
}

function check_download(obj) {
  if (!element("instant").checked && (!element("user_email").value || element("user_email").value == '@')) {
    alert('You must fill in your e-mail address or tick "Instant download" checkbox.');
    element("user_email").focus();
    
    return false;
  }

  var obj = element("down_" + element("file_id").value);
  cardShowDownloadByEmail(element("file_id").value, obj, 'CARD_DOWNLOAD_EMAIL', '/ajax.php');
}

function instant_file(obj) {
  element("instant_file").className = (obj.checked ? "dis_block" : "dis_none");
}
 
function checkNewsletterForm() {
  if (!element("email").value) {
    alert("You must fill in your e-mail.");
    element("email").focus();
    
    return false;
  }
  
  if (!element("name").value) {
    alert("You must fill in your name.");
    element("name").focus();
    
    return false;
  }

  return true;
} 

function visibility(obj_id, folder_obj) {
  var obj = document.getElementById("folder_content_" + obj_id);

  if (obj) {
    if (obj.className == "dis_block") {
      obj.className  = "dis_none";
    } else {
      obj.className = "dis_block";
    }
  }

  if (folder_obj.className == "hand folder_closed") {
    folder_obj.className = "hand folder_open";
  } else {
    folder_obj.className = "hand folder_closed";
  }
}
  
/*
 * 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");
}

/**
 * find out whether HTML element is visible
 *    
 * @param mixed obj    object or object's id
 * @return boolean     false if element is not visible, true otherwise
 */
function elementIsVisible(obj) {
  if (element(obj)) {    // object identified by its id
    obj = element(obj);
  }

  if (obj.style && obj.style.display == "none") {
    return false
  }
  
  return true;
}

/**
 * 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 event editing card
 *
 * @param integer id         document 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 cardShowDownload(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", "download");
  ajax.set("file_id", id);
  ajax.set("card_template", template);
  ajax.set("left", left + "px");
  ajax.set("top", top + "px");
  ajax.run();
} 

function cardShowDownloadByEmail(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", "download_email");
  ajax.set("file_id", id);
  ajax.set("user_email", element("user_email").value);
  ajax.set("card_template", template);
  ajax.set("left", left + "px");
  ajax.set("top", top + "px");
  ajax.run();
} 
 
/**
 * display deliverable card
 *
 * @param integer id         deliverable 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 cardShowDeliverable(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", "deliverable");
  ajax.set("id", id);
  ajax.set("card_template", template);
  ajax.set("left", left + "px");
  ajax.set("top", top + "px");
  ajax.run();	  
}

/**
 * display document editing card
 *
 * @param integer id         document 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 cardShowDocument(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", "document");
  ajax.set("id", id);
  ajax.set("card_template", template);
  ajax.set("left", left + "px");
  ajax.set("top", top + "px");
  ajax.run();	  
}

/**
 * display event editing card
 *
 * @param integer id         document 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 cardShowNews(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", "news");
  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;
}

/**
 * display message dialog. If confirmed, redirect to address stored in link's 'href' attribute
 *    
 * @param string msg    message to be displayed
 * @param object obj    link object to use href from
 * @return boolean      true if message has been confirmed, false otherwise
 */
function dialogConfirmRedirect(msg, obj) {
  var status = confirm(msg);
  if (status) {
    obj.href;
  }
  
  return status;
}

/*
 * card functions / END
 */

