/* Script che permette di monitorare ogni 5 secondi tutte le ultime modifiche */
var rcp_http;
var rcp_enabled;
var rcp_num_pages;
var rcp_refresh;
var rcp_num_idle_req;
var rcp_curr_idle_req;
 
var rcp_str_no_ajax = "Sembra esserci un problema nell'utilizzare lo script RecentChangesPatrol. Il tuo browser non è supportato.";
var rcp_str_box_title = "RC";
var rcp_str_box_title_updating = "RC (updating)";
var rcp_str_box_title_failed = "RC (update fallito)";
var rcp_str_enable = "abilita";
var rcp_str_disable = "disabilita";
 
$( rcp_init );
 
/* initalise */
function rcp_init() {
 
  // allow user settings through
  if (rcp_enabled == null) {
    rcp_enabled = false;
  }
  if (rcp_num_pages == null) {
    rcp_num_pages = 10;
  }
  if (rcp_refresh == null) {
    rcp_refresh = 5;
  }
  if (rcp_num_idle_req == null) {
    rcp_num_idle_req = 60;
  }
 
  // A few limits to be nice to the servers
  if (rcp_num_pages > 50) {
    rcp_num_pages = 50;
  }
  if (rcp_num_pages < 1) {
    rcp_num_pages = 1;
  }
  if (rcp_refresh < 2) {
    rcp_refresh = 2;
  }
  if (rcp_num_idle_req > 1000) {
    rcp_num_idle_req = 1000;
  }
  if (rcp_num_idle_req < 5) {
    rcp_num_idle_req = 5;
  }
 
  // get our cookie
  if (document.cookie.length > 0) {
    var c_start = document.cookie.indexOf("rcp_show_box=");
    if (c_start != -1) { 
      c_start = c_start + 13; 
      var c_end = document.cookie.indexOf(";", c_start);
      if (c_end == -1) {
        c_end = document.cookie.length;
      }
 
      if (document.cookie.substring(c_start, c_end) == "yes") {
        rcp_enabled = true;
      } else {
        rcp_enabled = false;
      }
    } 
  }
 
  // Either make a request or show nothing
  rcp_curr_idle_req = 0;
  if (rcp_enabled == true) {
    rcp_ajax_request();
  } else {
    rcp_draw_disabled_box();
  }
}
 
/* init ajax */
function rcp_create_request() {
  try {
    rcp_http = new XMLHttpRequest();
 
  } catch (e) {
    try {
      rcp_http = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        rcp_http = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        return false;
      }
    }
  }
 
  rcp_http.onreadystatechange = function() {
    if(rcp_http.readyState == 4) rcp_ajax_response();
  }
 
  return true;
}
 
/* make a request */
function rcp_ajax_request() {
  // if we have done too many requests, disable the box
  rcp_curr_idle_req++;
  if (rcp_curr_idle_req > rcp_num_idle_req) {
    rcp_disable_box();
  }
 
  // check we are enabled
  if (rcp_enabled == false) return;
 
  // firstly, inform the user
  var cur_box = document.getElementById('p-rcpatrol');
  if (cur_box != null) {
    cur_box.firstChild.firstChild.data = rcp_str_box_title_updating;
  }
 
  if (rcp_create_request () == false) {
    if (cur_box != null) {
      cur_box.firstChild.firstChild.data = rcp_str_box_title_failed;
    } else {
      alert (rcp_str_no_ajax);
    }
  }
 
  // Then make the request
  rcp_http.open("GET", "/w/api.php?action=query&list=recentchanges&format=xml&rcnamespace=0&rcprop=title|timestamp|ids|patrolled&rcshow=!bot&rcshow=!minor&rclimit=" + rcp_num_pages, true);
  rcp_http.send(null);
}
 
/* we have received a response */
function rcp_ajax_response() {
 
  var items = rcp_http.responseXML.getElementsByTagName('rc');
 
  // create the div that holds all the newpage links
  var link_div = document.createElement('div');
  link_div.className = 'pBody';
  var list = document.createElement('ul');
  link_div.appendChild(list);
 
  // populate the list with 10 links.
  for (var i = 0; i < items.length; i++) {
    var item_name = items[i].getAttribute('title');
    var rcid = items[i].getAttribute('rcid');
    var patrolled = items[i].getAttribute('patrolled') != null;
 
    item_name = item_name.replace(/&/, "%26");
    var item_url = '//it.wiktionary.org/w/index.php?title=' + item_name + '&rcid=' + rcid + '&redirect=no';
 
    a = document.createElement('a');
    a.setAttribute('href', item_url);
    a.appendChild(document.createTextNode(item_name));
 
    var li = document.createElement('li');
    li.appendChild(a);
    if (!patrolled) {
        li.setAttribute('class', 'not-patrolled');
    }
    list.appendChild(li);
  }
 
  // Container div
  var div = document.createElement('div');
  div.setAttribute('id', 'p-rcpatrol');
  div.className = 'portlet';
  var heading = document.createElement('h5');
  heading.appendChild(document.createTextNode(rcp_str_box_title));
  div.appendChild(heading);
  div.appendChild(link_div);
 
  // disable link
  var p = document.createElement('p');
  p.style.fontSize = 'x-small';
  p.style.margin = '0px';
  p.style.textAlign = 'right';
  a = document.createElement('a');
  a.appendChild(document.createTextNode(rcp_str_disable));
  a.onclick = rcp_disable_box;
  p.appendChild(a);
  link_div.appendChild(p);
 
  // now replace the div
  var old_div = document.getElementById('p-rcpatrol');
  var side_col = document.getElementById('column-one');
  if (old_div != null) {
    side_col.replaceChild(div, old_div);
  } else {
    var node = document.getElementById('p-search');
    side_col.insertBefore(div, node);
  }
 
  // and do it again in 5 secs
  setTimeout("rcp_ajax_request()", rcp_refresh * 1000);
}
 
function rcp_disable_box() {
  rcp_enabled = false;
  rcp_draw_disabled_box();
  document.cookie = "rcp_show_box=no; path=/";
}
 
function rcp_enable_box() {
  rcp_enabled = true;
  rcp_curr_idle_req = 0;
  document.cookie = "rcp_show_box=yes; path=/";
  rcp_ajax_request();
}
 
function rcp_draw_disabled_box() {
  // Container div
  var link_div = document.createElement('div');
  link_div.className = 'pBody';
  var div = document.createElement('div');
  div.setAttribute('id', 'p-rcpatrol');
  div.className = 'portlet';
  var heading = document.createElement('h5');
  heading.appendChild(document.createTextNode(rcp_str_box_title));
  div.appendChild(heading);
  div.appendChild(link_div);
 
  // enable link
  var p = document.createElement('p');
  p.style.fontSize = 'x-small';
  p.style.margin = '0px';
  var a = document.createElement('a');
  a.appendChild(document.createTextNode(rcp_str_enable));
  a.onclick = rcp_enable_box;
  p.appendChild(a);
  link_div.appendChild(p);
 
  // now replace the div
  var old_div = document.getElementById('p-rcpatrol');
  var side_col = document.getElementById('column-one');
  if (old_div != null) {
    side_col.replaceChild(div, old_div);
  } else {
    var node = document.getElementById('p-search');
    side_col.insertBefore(div, node);
  }
}