window.currentMenuItem = 'mnuItem0';
window.currentItem = 'item0';

function gt(fileURL) {
  _uacct = "UA-3449474-1";
  if (typeof(fileURL)=="undefined") {
    urchinTracker ();
  } else {
    if (fileURL[0]!='/') {
      fileURL='/'+fileURL;
    }
    urchinTracker (fileURL);
  }
}

function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
} 

function goToSection(sectionid, title) {
   t = 300;
   opacity(window.currentItem, 100, 0, t);
   $(window.currentItem).addClassName("hidden");
   $('mainpagefade').show();
   opacity(sectionid, 100, 0, 10);
   $(sectionid).removeClassName("hidden");
   window.currentItem = sectionid;
   opacity(window.currentItem, 0, 100, t);
   $('mainpagefade').hide();
   if (typeof(title)!="undefined") {
     gt("Sezioni/" + title);
   }
}

function selectMenu(menuid) {
   $(currentMenuItem).removeClassName("menuselected");
   $(menuid).addClassName("menuselected");
   window.currentMenuItem = menuid;
}

Event.observe(window, "load", function() {
  var items = $A($('mainpage').getElementsByTagName('div'));
  var ids = 0;
  if (items.length>1) {
    var container = $('container');
    var leftbar = document.createElement('div');
    leftbar.setAttribute("id", "leftbar"); 
    var menu = document.createElement('ul');
    menu.setAttribute("id", "menuitems"); 
    leftbar.appendChild(menu);
    container.appendChild(leftbar);
    items.each(function(node) {
      var menuid = "mnuItem" + ids; 
      var itemid = "item" + ids;
      ids++;
      var li = document.createElement('li');
      var a = document.createElement('a');
      a.id = menuid;
      var title = (node.getElementsByTagName('h1'))[0].innerHTML;
      Element.update(a, title);
      node.id = itemid;
      a.setAttribute("href", "#");
      Event.observe(a, 'click', function(event){ 
        selectMenu(a.id)
        goToSection(node.id, title);
      });
      li.appendChild(a);
      menu.appendChild(li);
    });
    goToSection(currentItem);
    selectMenu(currentMenuItem);
  }
});

