// ==UserScript==
// @name	Provveditorato Studi Enna Fix
// @description	Fix the site from the Provveditorato agli Studi di Enna
// @namespace	http://oblomov.myopenid.com
// @include	http://provvstudienna.it/*
// @include	http://*.provvstudienna.it/*
// @author	Giuseppe "Oblomov" Bilotta
// @version	20100819
// ==/UserScript==

if (typeof(unsafeWindow) == 'undefined') unsafeWindow = window;

String.prototype.ltrim = function() { return this.replace(/^\s+/,'') };
String.prototype.rtrim = function() { return this.replace(/\s+$/,'') };
String.prototype.trim = function() { return this.ltrim().rtrim() };

(function () {

 /* replace the non-standard startDownload() behavior with standard XMLHttpRequest */
 var marq = unsafeWindow.document.getElementById('externalmarquee');
 if (marq) {
  var url = marq.getAttribute('src').replace(/\\/g,'/');
  xhr = new XMLHttpRequest();
  xhr.open("GET", url, true);
  xhr.onreadystatechange = function() {
   if (xhr.readyState == 4) {
    if (xhr.status == 200) {
     marq.innerHTML = xhr.responseText;
    } else {
     marq.innerHTML = 'impossibile caricare le news';
    }
   }
  }
  xhr.send();
 }

 /* Replace the unneeded Java menu applet with a standard menu */
 var menu = unsafeWindow.document.getElementsByTagName('applet')[0];
 if (typeof(menu) == 'object') {
  var pnode = menu.parentNode;
  var neib = menu.nextSibling;
  while (neib) {
   pnode.removeChild(neib);
   neib = menu.nextSibling;
  }
  var nodes = menu.getElementsByTagName('param');
  var ops = [];
  /* The following code expects menu items to appear in sequence:
     label1, sublabel1.1, url1.1, sublabel1.2, url1.2, label2, etc
     (as they are on the processed site. If params are given off-sequence,
     it will break. Beware if you port the code to other sites too */
  for (var i = 0; i < nodes.length; ++i) {
   var param = nodes[i];
   var match = /^(\d+)-Label$/.exec(param.name);
   if (match) {
    ops[parseFloat(match[1])-1] = {label: param.value.trim(), entry: []};
    continue;
   }
   match = /^(\d+)-Entry(\d+)$/.exec(param.name);
   if (match) {
    ops[parseFloat(match[1])-1].entry[parseFloat(match[2]-1)] = {label: param.value.trim()};
    continue;
   }
   match = /^(\d+)-URL(\d+)$/.exec(param.name);
   if (match) {
    ops[parseFloat(match[1])-1].entry[parseFloat(match[2]-1)].url = param.value.replace(/\\/g, '/');
    continue;
   }
  }
  /* DEBUG
  if (typeof(JSON) == 'object') {
   alert(JSON.stringify(ops));
  }
  */
  if (ops.length > 0) {
   pnode.removeChild(menu);
   menu = document.createElement('ul');
   menu.id = 'hypermenu';
   for (var i = 0; i < ops.length; ++i) {
    menu.appendChild(document.createElement('li'));
    var item = menu.lastChild;
    item.id = ops[i].label.replace(/\./g,'').replace(/\W/g,'-').toLowerCase();
    item.appendChild(document.createElement('p'));
    item.lastChild.appendChild(document.createTextNode(ops[i].label));
    item.appendChild(document.createElement('ul'));
    item = item.lastChild;
    for (var j = 0; j < ops[i].entry.length; ++j) {
     item.appendChild(document.createElement('li'));
     item.lastChild.appendChild(document.createElement('a'));
     link = item.lastChild.lastChild;
     link.href = ops[i].entry[j].url;
     link.appendChild(document.createTextNode(ops[i].entry[j].label));
    }
   }
   pnode.appendChild(menu);
  }
 }

 /* Styling for the menu */
 /* we could grab most of these from the params to the java applet, but I'm too lazy to write that code for now */
 /* TODO: metal effect */
 style  = '#hypermenu { font-family: Dialog, sans-serif; font-size: 12px; list-style-type: none; text-align: left; background-color: #e2e2e2; margin:0 auto; padding:0;width: 100%}';
 style += '#hypermenu > li { margin:0; padding:0}';
 style += '#hypermenu > li > p { font-weight: bold; background-color: #284828; margin:0; padding: 6px 0 6px 3px; }';
 style += '#hypermenu > li > p:hover { color: #00FF00; }';
 style += '#hypermenu ul { color:black; font-weight:normal; list-style-type: none; margin:0; padding:0; background-color: #C0C0C0}';
 style += '#hypermenu ul > li { padding: 3px}';
 style += '#hypermenu ul > li:hover { color: #CC0000; font-weight: bold}';
 style += '#hypermenu a { color:inherit; text-decoration:none }' ;

 /* rolling effect */
 style += '#hypermenu > li > p { border: 1px solid gray }';
 style += '#hypermenu > li > ul {display:none}';
 style += '#hypermenu > li:hover > ul {display:block}';

 stel = unsafeWindow.document.createElement('style');
 stel.appendChild(document.createTextNode(style));
 unsafeWindow.document.getElementsByTagName('head')[0].appendChild(stel);


})();

