// global variables
var is_css, is_w3c, is_ie6_css, is_ie;
var root_folder = "xportpro";
var current_popup_ifrm = null;
var popup_limits = { min_width: 50, min_height: 50, max_width: 400}

// coordinate object
function Coordinate(_x, _y)
{
  this.x = _x;
  this.y = _y;
}

// size object
function Size(_x, _y)
{
  this.x = _x;
  this.y = _y;
}

// term placement object
function TermPlacement()
{
  this.x = 0;
  this.y = 0;
  this.width = 0;
  this.height = 0;
  this.horizontal_placement = "left";
  this.vertical_placement = "bottom";
  this.link_wrapped = false;
}

// set event handler to initialize API
window.onload = init_dhtml_api;
document.onmousedown = check_for_popup;


// Initialize upon load to let all browsers establish content objects
function init_dhtml_api()
{ 
  if (document.images) {
    is_css = (document.body && document.body.style) ? true : false;
    is_w3c = (is_css && document.getElementById) ? true : false;
    is_ie6_css = (document.compatMode && document.compatMode.indexOf("CSS1") >= 0) ? true : false;
    is_ie = (navigator.appName == "Microsoft Internet Explorer");
    init_term_popups();
    window.onclick=check_for_popup;
  }
}

// initializes popups and gets current doc location depth
function init_term_popups() 
{
  var depth = get_location_depth();
  var span_elems = document.getElementsByTagName("span");
  for (var i = 0; i < span_elems.length; ++i) {
    var elem = span_elems[i];
    if (elem.className == "popup") {
      init_popup(elem, depth);
    }
  }
}

// gets the depth of current document
function get_location_depth()
{
  var path = new String(window.location);
  var i = path.lastIndexOf(root_folder);
  var count = 0;
  for (; i < path.length; ++i) {
    if (path.charAt(i) == '/') {
      ++count;
    }
  }
  return count - 1;
}

// initializes popup link
function init_popup(elem, depth)
{
  var term_path = "";
  for (var i = 0; i < depth; ++i) {
    term_path += "../";
  }
  term_path += "common/terms/" + elem.id + ".htm";
  var coords = get_element_coordinates(elem);
  var ifrm = document.createElement("iframe");
  ifrm.style.position = "absolute";
  ifrm.style.left = coords.x;
  ifrm.style.top = coords.y;
  ifrm.style.visibility = "hidden";
  ifrm.width = "0px";
  ifrm.height = "0px";
  ifrm.setAttribute("id", elem.id + "_popup");
  ifrm.src = term_path;
  document.body.appendChild(ifrm);
}


// checks for and removes current popup
function check_for_popup()
{
  if (current_popup_ifrm != null) {
    current_popup_ifrm.style.width = 0;
    current_popup_ifrm.style.height = 0;
    current_popup_ifrm.style.visibility = 'hidden';
    current_popup_ifrm = null;
  }
}

// click handler to show popup
function show_popup(elem, evt)
{
  // check if there's a popup showing
  check_for_popup();
  evt = (evt) ? evt : ((window.event) ? event : null);
  var ifrm =  document.getElementById(elem.id + "_popup");   

  if (ifrm) {
    elem.event = evt;
    place_term_popup(elem, ifrm);
  }
  current_popup_ifrm = ifrm;
  if (evt) {
    evt.cancelBubble = true;
  }
}

// places popup
function place_term_popup(elem, ifrm)
{ 
//  ifrm.src = ifrm.url;
  ifrm.placement = new TermPlacement();
  get_vertical_placement(elem, ifrm);
  get_horizontal_placement(elem, ifrm);
  ifrm.placement.width = calc_popup_width();
  calc_popup_xpos(elem, ifrm);
  calc_popup_height_and_ypos(elem, ifrm);
  ifrm.style.top = ifrm.placement.y + "px";
  ifrm.style.left = ifrm.placement.x + "px";
  ifrm.style.width = ifrm.placement.width + "px";
  ifrm.style.height = ifrm.placement.height + "px";
  ifrm.style.visibility = "visible";
}

// determines if popup should be placed on top or bottom
function get_vertical_placement(elem, ifrm)
{
  var elem_coords = get_element_coordinates(elem);

  var elem_size = get_element_size(elem);
  var wnd_size = get_inside_window_size();
  ifrm.placement.vertical_placement = null;
  var required_height = get_required_height(ifrm);

  var scroll_top =  get_scroll_top();
  while (ifrm.placement.vertical_placement == null) {
    if (required_height < wnd_size.y - (elem_coords.y - scroll_top + elem_size.y)) {
      ifrm.placement.vertical_placement = "bottom";
    } else if (required_height < elem_coords.y - scroll_top) {
      ifrm.placement.vertical_placement = "top";
    }
    required_height -= 20;
  }
}

// determines if popup should be placed on left or right
function get_horizontal_placement(elem, ifrm)
{
  var elem_coords = get_element_coordinates(elem);
  var elem_size = get_element_size(elem);
  var wnd_size = get_inside_window_size();
  ifrm.placement.link_wrapped = is_link_wrapped(elem);
  if (!ifrm.placement.link_wrapped) {
    ifrm.placement.horizontal_placement = (elem_coords.x > wnd_size.x - (elem_coords.x + elem_size.x)) ? "left" : "right";
  } else {
    ifrm.placement.horizontal_placement = (elem.event.x < wnd_size.x / 2) ? "right" : "left";
  }
}

// determines popup width
function calc_popup_width()
{
  var wnd_size = get_inside_window_size();
  return(popup_limits.max_width < wnd_size.x - 20) ? popup_limits.max_width : wnd_size.x - 20;
}

// determines popup horiz position
function calc_popup_xpos(elem, ifrm)
{
  var elem_coords = get_element_coordinates(elem);
  if (ifrm.placement.horizontal_placement == "left") {
    ifrm.placement.x = elem_coords.x - ifrm.placement.width - 10;
    if (ifrm.placement.x < 0) {
      ifrm.placement.x = 10;
    }
  } else if (ifrm.placement.link_wrapped && ifrm.placement.horizontal_placement == "right") {
    ifrm.placement.x = 10;
  } else {
    var elem_size = get_element_size(elem);
    var wnd_size = get_inside_window_size();
    ifrm.placement.x = elem_coords.x + elem_size.x + 10;
    if (ifrm.placement.x + ifrm.placement.width >= wnd_size.x) {
      ifrm.placement.x = wnd_size.x - ifrm.placement.width - 10;
    }
  }
}

// determines popup verticaL position and height
function calc_popup_height_and_ypos(elem, ifrm)
{ 
  ifrm.style.width = ifrm.placement.width - 5 + "px";
  var doc = iframe_doc(ifrm);
  
  if (!is_ie) {
    doc.body.style.width = ifrm.placement.width - 30 + "px";
  }
  
  // set height to default value
  if (doc.body.offsetHeight) {
    // w3c
    ifrm.placement.height = doc.body.offsetHeight + (is_ie ? 20 : 30);
  } else {
    // check for ie
    ifrm.placement.height = doc.body.scrollHeight + 20;
  }
  var elem_coords = get_element_coordinates(elem);
  var elem_size = get_element_size(elem);
  var wnd_size = get_inside_window_size();
  // adjust height
  var scroll_top = get_scroll_top();
  if (ifrm.placement.vertical_placement == "bottom") {
    // check if popup too high
    if (elem_coords.y - scroll_top + elem_size.y + ifrm.placement.height + 20 > wnd_size.y) {
      // check for min height
      if (elem_coords.y + elem_size.y + popup_limits.min_height - scroll_top + 20 < wnd_size.y) {
        // over min height.  don't have to raise up
        ifrm.placement.y = elem_coords.y + elem_size.y + 10;
        ifrm.placement.height = wnd_size.y - elem_coords.y - elem_size.y + scroll_top + 20;
      } else {
        // over min height.  have to raise up
        ifrm.placement.y = wnd_size.y - popup_limits.min_height - 5; 
        ifrm.placement.height = popup_limits.min_height;
      }
    } else {
      // plenty of room to show max height
      ifrm.placement.y = elem_coords.y + elem_size.y + 5;
    }
    if (ifrm.placement.link_wrapped && ifrm.placement.horizontal_placement == "right") {
      ifrm.placement.y += elem_size.y;
    }
  } else {
    // check if popup too high
    if (ifrm.placement.height + scroll_top + 20 > elem_coords.y) {
      // check for min height
      if (popup_limits.min_height + scroll_top + 20 > elem_coords.y) {
        ifrm.placement.y = 5 + scroll_top;
        ifrm.placement.height = popup_limits.min_height;
      } else {
        ifrm.placement.y = 5 + scroll_top;
        ifrm.placement.height = elem_coords.y - scroll_top - 5;
      }
    } else {
      // plenty of room
      ifrm.placement.y = elem_coords.y - ifrm.placement.height - 10;
    }
  }
}

// returns the document object in the iframe
function iframe_doc(ifrm)
{
  var doc = (ifrm.contentWindow || ifrm.contentDocument);
  if (doc.document) {
    doc = doc.document;
  }
  return doc;
}


// convert object name or reference into a valid element object reference
function get_raw_object(obj)
{
  var the_obj;
  if (typeof obj == "string") {
    if (is_w3c) {
      the_obj = document.getLementById(obj);
    }
  } else {
    the_obj = obj;
  }
  return the_obj;
}

// returns the pixels scrolled left
function get_scroll_left() {
  return filter_results (
                        window.pageXOffset ? window.pageXOffset : 0,
                        document.documentElement ? document.documentElement.scrollLeft : 0,
                        document.body ? document.body.scrollLeft : 0
                        );
}

// returns the pixels scrolled down
function get_scroll_top() {
  return filter_results (
                        window.pageYOffset ? window.pageYOffset : 0,
                        document.documentElement ? document.documentElement.scrollTop : 0,
                        document.body ? document.body.scrollTop : 0
                        );}

function filter_results(n_win, n_docel, n_body) {
  var n_result = n_win ? n_win : 0;
  if (n_docel && (!n_result || (n_result > n_docel)))
    n_result = n_docel;
  return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

// returns the element's coords
function get_element_coordinates(elem)
{
  var left = elem.offsetLeft; 
  var top = elem.offsetTop;  
  var parent = elem.offsetParent;     
  while (parent != null) {
    left += parent.offsetLeft;
    top += parent.offsetTop;      
    parent = parent.offsetParent;  
  }
  var coord = new Coordinate(left, top);
  return coord;                             
}

// return the rendered size of an object
function get_element_size(obj)
{
  var elem = get_raw_object(obj);
  var width = 0;
  var height = 0;
  if (elem.offsetWidth) {
    width = elem.offsetWidth;
    height = elem.offsetHeight;
  } else if (elem.clip && elem.clip.width) {
    width = elem.clip.width;
    height = elem.clip.height;
  } else if (elem.style && elem.style.pixelWidth) {
    width = elem.style.pixelWidth;
    height = elem.style.pixelHeight;
  }
  var size = new Size();
  size.x = parseInt(width);
  size.y = parseInt(height);
  return size;
}

// return the available content size in browser window
function get_inside_window_size()
{
  var width = 0;
  var height = 0;
  if (window.innerWidth && window.innerHeight) {
    // w3c
    width = window.innerWidth;
    height = window.innerHeight;
  } else if (is_ie6_css) {
    width = document.body.parentElement.clientWidth;
    height = document.body.parentElement.clientHeight;
  } else if (document.body && document.body.clientWidth) {
    width = document.body.clientWidth;
    height = document.body.clientHeight;
  }

  var size = new Size(parseInt(width), parseInt(height));
  return size;
}

// returns the source of the event
function get_event_src(evt)
{
  var elem = null;
  evt = (evt) ? evt : ((window.event) ? event : null);
  if (evt) {
    elem = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
  }
  return elem;
}


// returns the required height of popup
function get_required_height(ifrm) 
{
  var old_width = ifrm.style.width;
  ifrm.style.width = popup_limits.max_width;
  
  var doc = iframe_doc(ifrm);
  var old_doc_width = doc.body.style.width;
  doc.body.style.width = popup_limits.max_width;

  var required_height = 0;
  if (doc.body.offsetHeight) {
    // w3c
    required_height = doc.body.offsetHeight;
  } else {
    required_height = doc.body.scrollHeight;
  }
  ifrm.style.width = old_width;
  doc.body.style.width = old_doc_width;

  return required_height;
}

// determines of link is wrapped
function is_link_wrapped(elem) 
{
  var link_coords = get_element_coordinates(elem);
  var link_size = get_element_size(elem);
  var window_size = get_inside_window_size();
  return(link_coords.x + link_size.x > window_size.x);
}




