/* Aragon Online - Copyright 2003-2010 by Russell Shilling */
/* world.js */

function do_click(x, y) {

  place_cursor(x, y);

  var xx = origin_x + x, yy = origin_y + y;
  setHTML('x', xx);
  setHTML('y', yy);
  var wx = $('world_x');
  var wy = $('world_y');
  if (wx) {
    wx.value = xx;
    wy.value = yy;
  }

  if (click_unit_id) {
    menu_action_click(click_unit_id, click_action_id, click_spell_id);
    click_unit_id = 0;
  }
}

function do_city_data(items, title, tag, extra) {
    var nodes = items.getElementsByTagName(tag);
    var content = get_content(nodes[0]);
    var html = title.length > 0 ? "<td "+extra+">"+title+"</td>" : "";
    return html + "<td "+extra+">"+content+"</td>";
}

/*
*function make_ajax_submit(msg,fn,action,other_id) {
*    var btn = '/button.php?txt=' . urlencode($fn) . '&sz=0&on=1';
*    return "<input type='image' name='{action}' style='vertical-align: middle' " +
*        "src='{btn}' " +
*        "onclick='if (confirm(\"{msg}\")) " +
*            "AjaxRequest.post(" +
*                "{parameters: { action:\"{action}\", other_id:{other_id}, " +
*                "r: 0, s:\"{$vbulletin->session->vars['sessionhash']}\"}," +
*                "onSuccess: function(req) { ajax_update_page(req.responseXML); }" +
*                "}); return false;'>";
*}
*/

// Get city data and display it in a pop-up menu
function do_city(id, x, y) {

  do_click(x, y);

  AjaxRequest.post(
    {
      'url': 'world.php',
      'parameters': {
        'action': 'get_city',
        's': vb_session_hash,
        'other_id': id,
        'r': 0 },
      'onSuccess': function(req) {
        items = req.responseXML;
        // Title
        var nodes = items.getElementsByTagName('city_txt');
        var title = get_content(nodes[0]);
        //var nodes = items.getElementsByTagName('message');
        //var message = get_content(nodes[0]);

        var txt = "<table class='tborder' width='100%'><tr>";
        txt += do_city_data(items, "", "avatar", "width='50%' colspan='2' class='alt1'");
        nodes = items.getElementsByTagName('heraldry');
        content = get_content(nodes[0]);
        txt += content;
        txt += "</tr><tr>" +
            "<th class='tcat' colspan='2'>INFORMATION</th>" +
            "<th class='tcat' colspan='2'>INTRIGUE</th></tr>";
        txt += "<tr>" + do_city_data(items, "Race, Gender", "race_gender_txt", "class='alt2'") +
                do_city_data(items, "Information", "information", "class='alt2'") +
                "</tr>";
        nodes = items.getElementsByTagName("state_txt");
        content = get_content(nodes[0]);
        txt += "<tr>" + do_city_data(items, "Population", "population_total", "class='alt1'") +
                "<td class='alt1'>State</td>" +
                "<td class='alt1'><div id='state_0'>" + content +
                "</div></td></tr>";
        nodes = items.getElementsByTagName("scout_active");
        content = get_content(nodes[0]);
        txt += "<tr>" + do_city_data(items, "Army", "army_total", "class='alt2'") +
                "<td class='alt2'>Scout</td>" +
                "<td class='alt2'><div id='scout_0'>" + content +
                "</div></td></tr>";
        nodes = items.getElementsByTagName("spy_active");
        content = get_content(nodes[0]);
        txt += "<tr>" + do_city_data(items, "Power", "power", "class='alt1'") +
                "<td class='alt1'>Spy</td>" +
                "<td class='alt1'><div id='spy_0'>" + content +
                "</div></td></tr>";
        nodes = items.getElementsByTagName("saboteur_active");
        content = get_content(nodes[0]);
        txt += "<tr>" + do_city_data(items, "Win-Loss", "win_loss", "class='alt2'") +
                "<td class='alt2'>Saboteur</td>" +
                "<td class='alt2'><div id='sabotage_0'>" + content +
                "</div></td></tr>";
        nodes = items.getElementsByTagName("diplomacy_txt");
        content = get_content(nodes[0]);
        txt += "<tr>" + do_city_data(items, "Distance", "cost", "class='alt1'") +
                "<td class='alt1'>Diplomacy</td>" +
                "<td class='alt1'><div id='diplo_0'>" + content +
                "</div></td></tr>";
        txt += "<tr>" + do_city_data(items, "Activity", "last_active", "class='alt2'") +
                "<td class='alt2' colspan='2' align='center'><div_id='progress'></div></td></tr>";
        txt += "</table>";
        message_alert(title, txt);
      },
      'onError': function(req) {
        var title = "Error do_city: " + req.statusText;
        var txt = req.responseText;
        message_alert(title, txt);
      }
    }
  );
}

