function prepare_balloon_object() {
  var balloon_div = document.createElement("div");
  balloon_div.id = "balloon_div";
  balloon_div.setAttribute("id", "balloon_div");
  balloon_div.style.position = "absolute";
  document.body.appendChild(balloon_div);
}

function create_message_balloon_tip(element, message, priority) {
  var balloon_tip = create_balloon_tip(element);
  
  var message_node = document.createElement("span");
  var font_node = document.createElement("font");
  font_node.appendChild(document.createTextNode(message));
  message_node.appendChild(font_node);
  message_node.font = font_node;
  balloon_tip.content.appendChild(message_node);
  balloon_tip.content.text = message_node;
  
  switch (priority) {
  case 1:
    font_node.color = "red";
    break;
  case 2:
    font_node.color = "blue";
    break;
  case 3:
  default:
    font_node.color = "black";
  }
  
  element.onmouseover = show_balloon_tip;
  element.onmouseout = hide_balloon_tip;
  return balloon_tip;
}

function create_template_balloon_tip(element, action) {
  var balloon_tip = create_balloon_tip(element, true);
  new Ajax.Updater(balloon_tip.content, action, {asynchronous:true, evalScripts:true});
  element.onclick = show_balloon_tip;
  return balloon_tip;
}

function create_balloon_tip(element, auto_size) {
  var balloon_tip = document.createElement("div");
  balloon_tip.className = "balloon";
  balloon_tip.style.display = "block";
  
  var balloon_tip_table = document.createElement("table");
  balloon_tip_table.border = "0";
  balloon_tip_table.cellSpacing = "0";
  balloon_tip_table.cellPadding = "0";
  balloon_tip.appendChild(balloon_tip_table);
  
  var top_row = balloon_tip_table.insertRow(balloon_tip_table.rows.length);
  var top_left   = top_row.insertCell(top_row.cells.length);
  var top_center = top_row.insertCell(top_row.cells.length);
  var top_right  = top_row.insertCell(top_row.cells.length);
  top_left.className  = "top_left";
  top_left.innerHTML  = "&nbsp;";
  top_right.className = "top_right";
  top_right.innerHTML = "&nbsp;";
  var top_center_table = document.createElement("table");
  top_center_table.width = "100%";
  top_center_table.border = "0";
  top_center_table.cellSpacing = "0";
  top_center_table.cellPadding = "0";
  top_center.appendChild(top_center_table);
  var top_center_row = top_center_table.insertRow(0);
  var top_center_left  = top_center_row.insertCell(top_center_row.cells.length);
  var top_center_right = top_center_row.insertCell(top_center_row.cells.length);
  top_center_left.className  = "top_center_left";
  top_center_right.className = "top_center_right";
  top_center_right.innerHTML = "&nbsp;";
  if (!auto_size) {
    top_center_right.width = "109";
  }
  
  var content_row = balloon_tip_table.insertRow(balloon_tip_table.rows.length);
  var content_left  = content_row.insertCell(content_row.cells.length);
  var content       = content_row.insertCell(content_row.cells.length);
  var content_right = content_row.insertCell(content_row.cells.length);
  content_left.className  = "content_left";
  content.className       = "content";
  content_right.className = "content_right";
  balloon_tip.content = content;
  content.balloon_tip = balloon_tip;
  
  var bottom_row = balloon_tip_table.insertRow(balloon_tip_table.rows.length);
  var bottom_left   = bottom_row.insertCell(bottom_row.cells.length);
  var bottom_center = bottom_row.insertCell(bottom_row.cells.length);
  var bottom_right  = bottom_row.insertCell(bottom_row.cells.length);
  bottom_left.className   = "bottom_left";
  bottom_center.className = "bottom_center";
  bottom_right.className  = "bottom_right";
  
//  var balloon_tip_top = document.createElement("div");
//  balloon_tip_top.className = "top";
//  balloon_tip_top.style.display = "block";
//  balloon_tip.appendChild(balloon_tip_top);
//  
//  var balloon_tip_content = document.createElement("div");
//  balloon_tip_content.className = "content";
//  balloon_tip_content.style.display = "block";
//  balloon_tip.appendChild(balloon_tip_content);
//  balloon_tip.content = balloon_tip_content;
//  
//  var balloon_tip_bottom = document.createElement("div");
//  balloon_tip_bottom.className = "bottom";
//  balloon_tip_bottom.style.display = "block";
//  balloon_tip.appendChild(balloon_tip_bottom);
  
  set_opacity(balloon_tip);
  element.balloon_tip = balloon_tip;
  return balloon_tip;
}

function show_balloon_tip(element) {
  var balloon_div = document.getElementById("balloon_div");
  if (this.balloon_tip.display) {
    return;
  }
  for (var i = 0; i < balloon_div.childNodes.length; i++) {
    hide_balloon_tip(balloon_div.childNodes[i]);
  }
  balloon_div.appendChild(this.balloon_tip);
  locate(element);
  this.balloon_tip.display = true;
}

function hide_balloon_tip(balloon_tip) {
  if (!balloon_tip) {
    balloon_tip = this.balloon_tip;
  }
  if (!balloon_tip) {
    return;
  }
  var balloon_div = document.getElementById("balloon_div");
  for (var i = 0; i < balloon_div.childNodes.length; i++) {
    if (balloon_div.childNodes[i] == balloon_tip) {
      balloon_div.removeChild(balloon_tip);
      break;
    }
  }
  balloon_tip.display = false;
}

function set_opacity(element) {
  element.style.filter = "alpha(opacity:95)";
  element.style.KHTMLOpacity = "0.95";
  element.style.MozOpacity = "0.95";
  element.style.opacity = "0.95";
}

function locate(element) {
  var pos_x = 0, pos_y = 0;
  if (element == null || !document.all) {
    var mouse_position = null;
    if (!document.all) {
      mouse_position = get_mouse_position(element);
    } else {
      mouse_position = get_mouse_position(window.event);
    }
    pos_x = mouse_position.x - 20;
    pos_y = mouse_position.y + 10;
  } else {
    var em_position = Position.cumulativeOffset(element);
    if (!document.all) {
      // FireFox対策
      pos_x = em_position[0];
      pos_y = em_position[1];
    }　else {
      // IE対策
      pos_x = em_position[0] + 2;
      pos_y = em_position[1] + 2;
    }
  }
  // はみ出したらさらに調整
  temp_x = pos_x + document.getElementById("balloon_div").offsetWidth;
  if (temp_x > document.documentElement.clientWidth) {
    pos_x = document.documentElement.clientWidth - document.getElementById("balloon_div").offsetWidth;
  }
  temp_y = pos_y + document.getElementById("balloon_div").offsetHeight;
  if (temp_y > document.documentElement.clientHeight) {
//    pos_y = document.documentElement.clientHeight - document.getElementById("balloon_div").offsetHeight;
  }
  document.getElementById("balloon_div").style.top = pos_y + "px";
  document.getElementById("balloon_div").style.left = pos_x + "px";
}

function set_message(balloon_tip, message) {
  if (!balloon_tip.content.text) {
    return;
  }
  balloon_tip.content.text.font.replaceChild(document.createTextNode(message), balloon_tip.content.text.font.firstChild);
  alert(balloon_tip.content.text.font.firstChild);
}

function subscribe_balloon_form(balloon_tip, form_id, action) {
  new Ajax.Updater(balloon_tip.content, action, {asynchronous:true, evalScripts:true, parameters:Form.serialize(form_id)});
  hide_balloon_tip(balloon_tip);
}