/* Aragon Online - Copyright 2003-2010 by Russell Shilling */
/* popmenu.js */

var menu_timer = [];
var menu_drag = [];

/**
 *  Position menu 'obj' at 'me'
 *  loc 0:below, 1:right-down, 2:left-down, 3:right-up, 4:left-up
 */
function move_menu(me, obj, loc) {

  var div_map = $('div_map');
  var x = 0;
  var y = 0;
  var o = me;
  do {
    x += o.offsetLeft || 0;
    y += o.offsetTop || 0;
    if (o.scrollLeft) {
      x -= o.scrollLeft;
    }
    if (o.scrollTop) {
      y -= o.scrollTop;
    }
    // Safari fix
    if (o.offsetParent == document.body)
      break;
      //if (o.style.position == 'absolute')
      //  break;
  } while (o = o.offsetParent)

  // Position menu
  if (loc == 0) {
    x -= 2;
    y += me.offsetHeight + 1;
  } else if (loc == 1) {
    x += me.offsetWidth + 1;
    y -= 2;
  } else if (loc == 2) {
    x -= obj.offsetWidth + 1;
    y -= 2;
  } else if (loc == 3) {
    x += me.offsetWidth + 1;
    y += me.offsetHeight - obj.offsetHeight;
  } else if (loc == 4) {
    x -= obj.offsetWidth + 1;
    y += me.offsetHeight - obj.offsetHeight;
  } else {
    x -= 2;
    y -= obj.offsetHeight + 1;
  }
//  if (window.innerWidth) {
//    if (x + obj.offsetWidth > window.innerWidth) x = window.innerWidth - obj.offsetWidth;
//    if (y + obj.offsetHeight > window.innerHeight) y = window.innerHeight - obj.offsetHeight;
//  } else {
//    if (x + obj.offsetWidth > document.body.offsetWidth) x = document.body.offsetWidth - obj.offsetWidth;
//    if (y + obj.offsetHeight > document.body.offsetHeight) y = document.body.offsetHeight - obj.offsetHeight;
//  }
  if (x < 0) x = 0;
  if (y < 0) y = 0;
  
  obj.style.left = x + 'px';
  obj.style.top = y + 'px';
}



// Set context for click event
function menu_update_context(unit_id, action_id, spell_id, action_txt) {
  click_unit_id = unit_id;
  click_action_id = action_id;
  click_spell_id = spell_id;
  click_action_txt = action_txt;
  if (click_unit_id) {
    if (action_id == 5) // MOVETO
      setHTML('curr_action['+unit_id+']', 'Destination...');
    else
      setHTML('curr_action['+unit_id+']', 'Target...');
  }
}

// Update the positions for each of the actions that use them
function update_coord(uid) {
  var action = $('do_action['+uid+']');
  var ux = $('unit_x['+uid+']'), uy = $('unit_y['+uid+']');
  var ex = $('dest_x['+uid+']'), ey = $('dest_y['+uid+']');
  var dx = $('x').innerHTML, dy = $('y').innerHTML;
  if (ux) {
    var x = ux.value;
    var y = uy.value;

    show_hilite(x, y, 1);
    hide_hilite(2);
    if (ex && action) {
      if (action.value >= 4 && action.value <= 9)
        show_hilite(ex.value, ey.value, 2);
    }
  }
}

function warn_spell(action_id, spell_id, action_txt) {

  if (action_id == 8 && spell_id > 0) {
    return confirm('Changing your action will abandon '+action_txt+
      '.  Do you wish to abandon this spell?');
  }
  return true;
}

// Show menu 'obj' at object 'me'
function show_menu(me, obj, loc) {

  if (obj) {
    obj = $(obj);
    // Reset the currently active menu timer, if any
    un_timer(obj);

    if (! obj.style.display || obj.style.display == 'none') {
      obj.style.display = 'block';
    }
    move_menu(me, obj, loc);

    if ($(obj.id + "_handle") != null) {
      menu_drag[obj.id] = new dragObject(obj, obj.id + "_handle");
    }
  }
}

// Hide the menu
function hide_menu(obj) {
  if (obj) {
    obj = $(obj);
    if (typeof obj != "undefined") {
      un_timer(obj);
      obj.style.display = 'none';

      if (menu_drag[obj.id]) {
        menu_drag[obj.id].Dispose();
        menu_drag[obj.id] = null;
      }
    }
  }
}

// Toggle the menu
function toggle_menu(me, obj, loc) {
  if (obj) {
    obj = $(obj);
    if (typeof obj != "undefined") {
      if (obj.style.display == 'none')
        show_menu(me, obj, loc);
      else
        hide_menu(obj);
    }
  }
}

// Set the menu to hide after 1.5 seconds
function hide_timed(obj) {
  obj = $(obj);
  un_timer(obj);
  menu_timer[obj.id] = setTimeout('hide_menu("'+obj.id+'");', 1500);
}

// Remove the timer
function un_timer(obj) {
  obj = $(obj);
  if (obj && menu_timer && obj.id && typeof menu_timer[obj.id] != "undefined")
    clearTimeout(menu_timer[obj.id]);
}

// Update the timer when active
function re_timer(obj) {
    un_timer(obj);
    hide_timed(obj);
}

