/* slides.js

   Copyright (c) 2005 W3C (MIT, ERCIM, Keio), All Rights Reserved.
   W3C liability, trademark, document use and software licensing
   rules apply, see:

   http://www.w3.org/Consortium/Legal/copyright-documents
   http://www.w3.org/Consortium/Legal/copyright-software
*/
window.onload = startup; // equivalent to onload on body element
var multimedia = 0;
/*var detect = navigator.userAgent.toLowerCase();*/
var scrollhack = 0;   // IE work around for position: fixed
var sizeIndex = 0;
var sizes = new Array("10pt", "12pt", "14pt", "16pt", "18pt",
                      "20pt", "22pt", "24pt", "26pt");
var lastWidth = 0;
var khtml = ((navigator.userAgent).indexOf("KHTML") >= 0 ? true : false);
var ns_pos = (typeof window.pageYOffset!='undefined');
var prev= null;
/* general initialization */
function startup()
{
   // bind even handlers
   if(document.getElementsByTagName && document.getElementById){

	if(document.getElementById("approfondimento")){
		document.getElementById("approfondimento").className="hide";	
		}
	if(document.getElementById("navigation")){
		document.getElementById("navigation").className="jsenable";
		BuildList();
		}

    }
   content_resized();
   findimg();
   document.onkeydown = keyDown;
   window.onresize  = content_resized;
   handlePaths(collectPaths());
}
// hack to work around variation in point size to pixels as
// Opera/Linux displays fonts 30% smaller than other browsers

function isKHTML()
{
   var agent = navigator.userAgent;
   return (agent.indexOf("KHTML") >= 0 ? true : false);
}

function resized()
{
   var width = 0;

   if( typeof( window.innerWidth ) == 'number' )
   {
      width = window.innerWidth;  // Non IE browser
   }
   else if (document.documentElement && document.documentElement.clientWidth)
   {
      width = document.documentElement.clientWidth;  // IE6
   }
   else if (document.body && document.body.clientWidth)
   {
      width = document.clientWidth; // IE4
   }
   //alert(width);

	/* fede */ width = Math.round((width*66)/100);
	//alert(width);
   // IE fires onresize even when only font size is changed!
   // so we do a check to avoid blocking < and > actions
   if (width != lastWidth)
   {
      if (width > 1100)
         sizeIndex = 4;
      else if (width > 1000)
         sizeIndex = 3;
      else if (width > 800)
         sizeIndex = 2;
      else if (width > 400)
         sizeIndex = 1;
      else if (width)
         sizeIndex = 0;

      document.body.style.fontSize = sizes[sizeIndex];
	  //alert(document.body.style.fontSize);
      lastWidth = width;

      // force correct positioning of footer
      hideFooter();
      setTimeout(showFooter, 500);
   }
}



// these don't work well on Opera, for which
// we take advantage on @media projection
function smaller()
{
   if (sizeIndex > 0)
   {
      --sizeIndex;
   }
   document.body.style.fontSize = sizes[sizeIndex];
}

function bigger()
{
   if (sizeIndex < sizes.length - 1)
   {
      ++sizeIndex;
   }
   document.body.style.fontSize = sizes[sizeIndex];
}
function cancel(event)
{
  event.cancel = true;
  event.returnValue = false;

  if (event.preventDefault)
    event.preventDefault();

  return false;
}
//  See e.g. http://www.quirksmode.org/js/events/keys.html for keycodes
function keyDown(event)
{
    var key;

    if (!event)
      var event = window.event;

    // kludge around NS/IE differences 
    if (window.event)
       key = window.event.keyCode;
    else if (event.which)
       key = event.which;
    else
       return true; // Yikes! unknown browser

    // ignore event if key value is zero
    // as for alt on Opera and Konqueror
    if (!key)
       return true;

    // check for concurrent control/command/alt key
    // but are these only present on mouse events?

    if (event.ctrlKey || event.altKey)
       return true;

   
    else if (key == 188)  // < for smaller fonts
    {
       //smaller();
       return cancel(event);
    }
    else if (key == 190)  // > for larger fonts
    {
       //bigger();
       return cancel(event);
    }
    else if (key == 189 || key == 109)  // - for smaller fonts
    {
       smaller();
       return cancel(event);
    }
    else if (key == 187 || key == 191 || key == 107)  // = +  for larger fonts
    {
       bigger();
       return cancel(event);
    }
    else if (key == 86)  // V for smaller fonts
    {
       //smaller();
       return cancel(event);
    }
    else if (key == 66)  // B for larger fonts
    {
       //bigger();
       return cancel(event);
    }
    
    else if (key == 70)  // F for toggle toolbar
    {
       //toggleToolbar();
       return cancel(event);
    }
    
    else if (key == 75)  // toggle action of left click for next page
    {
       /*mouseClickEnabled = !mouseClickEnabled;
	   setCookie('click_enabled', mouseClickEnabled);
	   alert((mouseClickEnabled ? "hai abilitato" : "hai disabilitato") +  " l'avanzamento con un click del mouse");*/
       return cancel(event);
    }
    else if (key == 84)  // T for test
    {
       //test();
       return cancel(event);
    }
    //else alert("key code is "+ key);

    return true;
}

// the string str is a whitespace separated list of tokens
// test if str contains a particular token, e.g. "slide"
function hasToken(str, token)
{
   if (str)
   {
      // define pattern as regular expression
      var pattern = /\w+/g;

      // check for matches
      // place result in array
      var result = str.match(pattern);

      // now check if desired token is present
      for (var i = 0; i < result.length; i++)
      {
         if (result[i] == token)
            return true;
      }
   }

   return false;
}



function stopPropagation(e)
{
   if (window.event)
   {
      window.event.cancelBubble = true;
      //window.event.returnValue = false;
   }
   else
   {
      e.cancelBubble = true;
      e.stopPropagation();
      //e.preventDefault();
   }
}

// works with text/html and text/xhtml+xml with thanks to Simon Willison
function createElement(element)
{
   if (typeof document.createElementNS != 'undefined')
   {
      return document.createElementNS('http://www.w3.org/1999/xhtml', element);
   }

   if (typeof document.createElement != 'undefined')
   {
      return document.createElement(element);
   }

   return false;
}

// designed to work with both text/html and text/xhtml+xml
function getElementsByTagName(name)
{
   if (typeof document.getElementsByTagNameNS != 'undefined')
   {
      return document.getElementsByTagNameNS('http://www.w3.org/1999/xhtml', name);
   }

   if (typeof document.getElementsByTagName != 'undefined')
   {
      return document.getElementsByTagName(name);
   }

   return null;
}

// clean alternative to innerHTML method, but on IE6
// it doesn't work with named entities like &nbsp;
// which need to be replaced by numeric entities
function insertText(element, text)
{
   if (element.textContent)  // DOM3 only
      element.textContent = text;
   else
   {
      if (element.firstChild)
      {
         // remove current children
         while (element.firstChild)
            element.removeChild(element.firstChild);
      }

      element.appendChild(document.createTextNode(text));
   }
}

function content_resized()
{
   var newheight = 0;
   
   contentDiv = document.getElementById("presentation_box");
   if (!contentDiv) return;
   
   if ( typeof( window.innerHeight ) == 'number' )
	  height = window.innerHeight;  // Non IE browser
   
   else if (document.documentElement && document.documentElement.clientHeight)
	  height = document.documentElement.clientHeight;  // IE6
   else if (document.body && document.body.clientHeight)
	  height = document.body.clientHeight; // IE4
  
   newheight = height - 242;
   if (newheight < 0) newheight=0;
   contentDiv.style.height = newheight+"px";
   navDiv = document.getElementById("navigation_box");
   if (!navDiv) return;
   
   navDiv.style.height = newheight+"px";
}



function findimg()
{
 var imgs,i;
// loop through all images of the document
 imgs=document.getElementsByTagName('img');
 for(i=0;i<imgs.length;i++)
 {
// test if the class 'roll' exists
  if(/roll/.test(imgs[i].className))
  {
// add the function roll to the image onmouseover and onmouseout and send
// the image itself as an object
	if(imgs[i].parentNode.nodeName=="LI")continue;
   imgs[i].onmouseover=function(){roll(this);};
   imgs[i].onmouseout=function(){roll(this);};
  }
 }
}
function roll(o)
{
 var src,ftype,newsrc;
// get the src of the image, and find out the file extension
 src = o.src;
 ftype = src.substring(src.lastIndexOf('.'), src.length);
// check if the src already has an _on and delete it, if that is the case 
 if(/_sm/.test(src))
 {
  newsrc = src.replace('_sm','');
 }else{
// else, add the _on to the src 
  newsrc = src.replace(ftype, '_sm'+ftype);
 }
 o.src=newsrc;
}

function collectPaths()
{
    var p = document.getElementById("approfondimento");
	if(p) return p;
}

function handlePaths(p){ 
  
	   if(!p) return;
	   p.onclick=function(){
		   if(this.className!="show"){
            this.className="show";
            }
			else this.className="hide";
	   }
}


function BuildList(){
 
var active = document.getElementById("active");
if (active)	{
	active.parentNode.parentNode.className="show";
	prev = active.parentNode.parentNode;
}
var hs=document.getElementById("navigation").getElementsByTagName("span");

for(var i=0;i<hs.length;i++){

		hs[i].onclick=function(){

        if(this.parentNode.className!="show"){
            this.parentNode.className="show";
            if(prev && prev!=this.parentNode) prev.className="hide"; 
            prev=this.parentNode;
            }
        else this.parentNode.className="hide";

        }	
	}
   
}