//	Scripts used by the HTML pages to show and hide text and anmiate a picture slideshow.
//
//	HISTORY
//	19-Jun-2005	GenoPro			Created and written the PV_*() routines.
//	23-Jul-2005	Ron Prior       	Added the "Explorer Tree"
//	23-Nov-2005	Ron Prior       	Added the hide/show map functions
//


//	Core routine for the Picture Viewer (PV) to animate the slideshow.
function PV_PickCore(id, nDirection)
	{
	var oPicker = document.getElementById("idPVs_" + id);
	var cPictures = oPicker.options.length;	
	var iSelected = (oPicker.selectedIndex + cPictures + nDirection) % cPictures; 
	var strValue = oPicker.options[iSelected].value;
	var ich = strValue.indexOf('\x1f');
	document.getElementById("idPVp_" + id).innerHTML = strValue.substring(0, ich);
	var strValue = strValue.substring(ich + 1, strValue.length);
	var ich = strValue.indexOf('\x1f');
	document.getElementById("idPVc_" + id).innerHTML = strValue.substring(0, ich);
	var strDescription = strValue.substring(ich + 1, strValue.length);

	var oVisibility = document.getElementById("idPVv_" + id);
	if (oVisibility != null)
		{
		if (strDescription != "")
			{
			elementRemoveClass(oVisibility, "hide");
			elementAddClass(oVisibility, "show");
			}
		else
			{
			elementRemoveClass(oVisibility, "show");
			elementAddClass(oVisibility, "hide");
			}
		}
	var oDescription = document.getElementById("idPVd_" + id);
	if (oDescription != null)
		oDescription.innerHTML = strDescription;
	oPicker.selectedIndex = iSelected;
	} // PV_PickCore()

function PV_Info(id)
	{
	PV_PickCore(id, 0);
	}
function PV_Next(id)
	{
	PV_PickCore(id, +1);
	}
function PV_Prev(id)
	{
	PV_PickCore(id, -1);
	}

var g_idTimer;	// Global variable to store the id of the timer, so we can stop it later

function PV_Pause(id)
	{
	// The id is not used, but passed for code orthogonality
	if (g_idTimer != null)
		clearTimeout(g_idTimer);
	g_idTimer = null;
	}
 // Start the picture slideshow to display a new picture every 3 second
function PV_Play(id)
	{
	PV_Pause(id);
	PV_Next(id);
	g_idTimer = setTimeout("PV_Play('" + id + "');", 3000);
	}


// Set the text of the status bar
function ss(s){window.status=s;return true;}

// Clear the text of the status bar
function cs(){window.status='';}

/*

Explorer Tree 1.6
=================
by Andrew Gregory
http://www.scss.com.au/family/andrew/webdesign/explorertree/

This work is licensed under the Creative Commons Attribution License. To view a
copy of this license, visit http://creativecommons.org/licenses/by/1.0/ or send
a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305,
USA.

IMPORTANT NOTE:
Variables and functions with names starting with an underscore (_) are
'internal' and not to be used.

*/

var explorerTreeAutoCollapse = {'default':false};
var explorerTreeBulletWidth = {'default':10};


// Expand all explorer trees
function explorerTreeExpandAll() {
  var li, c, lis = getElementsByClass('li','xT-c');
  c = lis.length
  for (var lii = 0; lii < c; lii++) {
    li = lis[lii];
    if (li.nodeName.toLowerCase() == 'li') _explorerTreeOpen(li);
  }
}

// Collapse all explorer trees
function explorerTreeCollapseAll() {
  var li, lis = getElementsByClass('li','xT-o');
  for (var lii = 0; lii < lis.length; lii++) {
    li = lis[lii];
    if (li.nodeName.toLowerCase() == 'li') _explorerTreeClose(li);
  }
}

// Refresh the specified explorer tree
function explorerTreeRefresh(id) {
  _explorerTreeInitUL(document.getElementById(id));
}

// Get the root element (<ul>) of the tree the given element is part of.
function _explorerTreeGetRoot(element) {
  for (var e = element; e != null; e = e.parentNode) {
    if (e.nodeName.toLowerCase() == 'ul' && elementHasClass(e, 'xT')) {
      break;
    }
  }
  return e;
}

// Get the ID of the tree the given element is part of. Returns the ID or
// 'default' if there is no ID.
function _explorerTreeGetId(element) {
  var e = _explorerTreeGetRoot(element);
  var id = e ? e.getAttribute('id') : '';
  return (!id || id == '') ? 'default' : id;
}

// Initialise the given list
function _explorerTreeInitUL(ul) {
  if (window.IE7) return;
  if (navigator.userAgent.indexOf('Gecko') != -1) {
    addEvent(ul, 'mousedown', _explorerTreeStopGeckoSelect, false);
  }
  if (!ul.childNodes || ul.childNodes.length == 0) return;
  // Iterate LIs
  for (var itemi = 0; itemi < ul.childNodes.length; itemi++) {
    var item = ul.childNodes[itemi];
    if (item.nodeName.toLowerCase() == 'li') {
      addEvent(item, 'click', xTclk, false);
      // Iterate things in this LI
      var hassubul = false;
      for (var subitemi = 0; subitemi < item.childNodes.length; subitemi++) {
        var subitem = item.childNodes[subitemi];
        if (subitem.nodeName.toLowerCase() == 'a') {
          addEvent(subitem, 'click', xTclk, false);
        }
        if (subitem.nodeName.toLowerCase() == 'ul') {
          hassubul = true; 
          _explorerTreeInitUL(subitem);
        }
      }
      if (hassubul) {
        // item is expandable, but don't change it if it's already been set to
        // something else
        if (!elementHasClass(item, 'xT-o') &&
            !elementHasClass(item, 'xT-b')) {
          elementAddClass(item, 'xT-c');
        }
      } else {
        // item has no sub-lists, make sure it's non-expandable
        elementRemoveClass(item, 'xT-o');
        elementRemoveClass(item, 'xT-c');
        elementAddClass(item, 'xT-b');
      }
    }
  }
}

// Gecko selects text when bullets are clicked on - stop it!
function _explorerTreeStopGeckoSelect(evt) {
  if (!evt) var evt = window.event;
  if (evt.preventDefault) {
    evt.preventDefault();
  }
  return true;
}

// Handle clicking on LI and A elements in the tree.
function xTclk(evt) {
  var element = (evt.target) ? evt.target : evt.srcElement;

  if (element.nodeName.toLowerCase() == 'li') {
    // toggle open/closed state, if possible
    if (elementHasClass(element, 'xT-o')) {
      elementRemoveClass(element, 'xT-o');
      elementAddClass(element, 'xT-c');
    } else if (elementHasClass(element, 'xT-c')) {
      elementRemoveClass(element, 'xT-c');
      elementAddClass(element, 'xT-o');
    } else {
      return true;
    }

    if (explorerTreeAutoCollapse[_explorerTreeGetId(element)]) {
      _explorerTreeCollapseAllButElement(element);
    }
  } else if (element.nodeName.toLowerCase() == 'a') {
    // let hyperlinks work as expected
    // TO DO: target support untested!!!
    var href = element.getAttribute('href');
    if (href) {
      var target = element.getAttribute('target');
      if (target) {
        switch (target) {
          case '_blank':
            window.open(href);
            break;
          case '_self':
            window.location.href = href;
            break;
          case '_parent':
            window.parent.location.href = href;
            break;
          case '_top':
            window.top.location.href = href;
            break;
          default:
            window.open(href, target);
            break;
        }
      }
    }
  } else {
    return true;
  }
  // we handled the event - stop it from propagating any further
  evt.cancelBubble = true;
  if (evt.stopPropagation) {
    evt.stopPropagation();
  }
  return false;
}

// Open the specified tree branch
function _explorerTreeOpen(li) {
  if (!elementHasClass(li, 'xT-b')) {
    elementRemoveClass(li, 'xT-c');
    elementAddClass(li, 'xT-o');
  }
}

// Close the specified tree branch
function _explorerTreeClose(li) {
  if (!elementHasClass(li, 'xT-b')) {
    elementRemoveClass(li, 'xT-o');
    elementAddClass(li, 'xT-c');
  }
}

// Collapse the specified tree
function explorerTreeCollapse(id) {
  _explorerTreeSetState(document.getElementById(id), true, null);
}

// Fully expand the specified tree
function explorerTreeExpand(id) {
  if (!explorerTreeAutoCollapse[id]) {
    _explorerTreeSetState(document.getElementById(id), false, null);
  }
}

// Collapse all the branches of tree except for those leading to the specified
// element. 
function _explorerTreeCollapseAllButElement(e) {
  var excluded = new Array();
  var tree = null;
  for (var element = e; element != null; element = element.parentNode) {
    if (element.nodeName.toLowerCase() == 'li') {
      excluded[excluded.length] = element;
    }
    if (element.nodeName.toLowerCase() == 'ul' && elementHasClass(element, 'xT')) {
      tree = element;
    }
  }
  if (tree) {
    _explorerTreeSetState(tree, true, excluded)
  }
}

// Set the open/closed state of all the LIs under the tree.
// The excludedElements parameter is used to implement the auto-collapse feature
// that automatically collapses tree branches other than the one actively being
// opened by the user.
function _explorerTreeSetState(ul, collapse, excludedElements) {
  if (window.IE7) return;
  if (!ul.childNodes || ul.childNodes.length == 0) return;
  // Iterate LIs
  for (var itemi = 0; itemi < ul.childNodes.length; itemi++) {
    var item = ul.childNodes[itemi];
    if (item.nodeName.toLowerCase() == 'li') {
      var excluded = false;
      if (excludedElements) {
        for (var exi = 0; exi < excludedElements.length; exi++) {
          if (item == excludedElements[exi]) {
            excluded = true;
            break;
          }
        }
      }
      if (!excluded) {
        if (collapse) {
          _explorerTreeClose(item);
        } else {
          _explorerTreeOpen(item);
        }
      }
      for (var subitemi = 0; subitemi < item.childNodes.length; subitemi++) {
        var subitem = item.childNodes[subitemi];
        if (subitem.nodeName.toLowerCase() == 'ul') {
          _explorerTreeSetState(subitem, collapse, excludedElements);
        }
      }
    }
  }
}

// Open the tree out so the list item with the link with the specified HREF is
// visible. Optionally scrolls so the item is visible. Optionally opens the
// found branch. Returns the LI that contains the specified HREF, or null if
// unsuccessful.
function explorerTreeOpenTo(id, href, scroll, expand) {
  var li = _explorerTreeSearch(document.getElementById(id), _explorerTreeNormalizeHref(href));
  if (li) {
    if (!window.IE7) {
      if (explorerTreeAutoCollapse[id]) {
        _explorerTreeCollapseAllButElement(li);
      }
      if (expand) {
        _explorerTreeOpen(li);
      }
    }
    if (scroll) {
      // get height of window we're in
      var h;
      if (window.innerHeight) {
        // Netscape, Mozilla, Opera
        h = window.innerHeight;
      } else if (document.documentElement && document.documentElement.clientHeight) {
        // IE6 in 'standards' mode
        h = document.documentElement.clientHeight;
      } else if (document.body && document.body.clientHeight) {
        // other IEs
        h = document.body.clientHeight;
      } else {
        h = 0;
      }
      // scroll so the list item is centered on the window
      window.scroll(0, li.offsetTop - h / 2);
    }
  }
  return li;
}

// Search the list (and sub-lists) for the given href. Returns the LI object if
// found, otherwise returns null.
function _explorerTreeSearch(ul, href) {
  if (!ul.childNodes || ul.childNodes.length == 0) return null;
  // Iterate LIs
  for (var itemi = 0; itemi < ul.childNodes.length; itemi++) {
    var item = ul.childNodes[itemi];
    if (item.nodeName.toLowerCase() == 'li') {
      for (var subitemi=0; subitemi < item.childNodes.length; subitemi++) {
        var subitem = item.childNodes[subitemi];
        if (subitem.nodeName.toLowerCase() == 'a') {
          if (_explorerTreeNormalizeHref(subitem.getAttribute('href')) == href) {
            return item;
          }
        }
        if (subitem.nodeName.toLowerCase() == 'ul') {
          var found = _explorerTreeSearch(subitem, href);
          if (found) {
            _explorerTreeOpen(item);
            return found;
          }
        }
      }
    }
  }
  return null;
}

// When Opera performs HTMLElement.getAttribute('href'), it *doesn't* actually
// return the raw HREF like it's supposed to. It 'normalizes' it, adding in any
// missing protocol, host name/port, and converts relative HREFs (eg
// '../../index.html') into absolute HREFs (eg '/index.html'). It does exactly
// the same thing in CSS generated content for the attr(href) function. If all
// browsers did that it would make URL comparisons trivial. Unfortunately, other
// browsers don't, and they're probably doing the right thing too by returning
// the href as it appears in the HTML.
// What this function does is normalize HREFs so we can do a meaningful
// comparison in *all* browsers.
function _explorerTreeNormalizeHref(href) {
  var i, h = href, l = window.location;
  
  // immediately return explicit protocols
  if (href.substring(0, 7) == 'telnet:') return href;
  if (href.substring(0, 7) == 'mailto:') return href;
  if (href.substring(0, 7) == 'gopher:') return href;
  if (href.substring(0, 5) == 'http:'  ) return href;
  if (href.substring(0, 5) == 'news:'  ) return href;
  if (href.substring(0, 5) == 'rtsp:'  ) return href;
  
  // handle absolute references
  if (h.charAt(0) == '/') {
    return l.protocol + '//' + l.host + h;
  }
  
  // strip off the filename (if any) of the location to leave the folder we're in
  l = l.toString();
  i = l.lastIndexOf('/');
  if (i != -1) {
    l = l.substring(0, i + 1);
  }
  
  // handle any relative directory references, i.e. '../'
  while (h.substring(0, 3) == '../') {
    h = h.substring(3);
    i = l.lastIndexOf('/', l.length - 2);
    if (i != -1) {
      l = l.substring(0, i + 1);
    }
  }
  
  return l + h;
}

/*
CSS Utilities
by Andrew Gregory
http://www.scss.com.au/family/andrew/

I have placed this code in the public domain. Feel free to use it however you
wish.

v1.4 26-Aug-2004 [GenoPro.com] Speed improvements.  The function getElementsByClass() takes more than 2 minutes on a 3.0 GHz machine with 10,000 nodes.
v1.3  6-Oct-2004 Added el.className checks
v1.2  5-Aug-2004 Simplified code by using regular expressions.
v1.1 12-Apr-2004 Fixed bug in elementRemoveClass() which removed partially matching classnames.
v1.0 29-Mar-2004 Initial version. Allows non-destructive setting and removal of CSS class names.
*/
// Test if an element has the given CSS class
function elementHasClass(el,cl){return (el.className&&el.className.search(new RegExp('\\b'+cl+'\\b'))>-1);}
// Ensure an element has the given CSS class
function elementAddClass(el,cl){var c=el.className;if(!c)c='';if(!elementHasClass(el,cl))c+=((c.length>0)?' ':'')+cl;el.className=c;}
// Ensure an element no longer has the given CSS class 
function elementRemoveClass(el,cl){if(el.className)el.className=el.className.replace(new RegExp('\\s*\\b'+cl+'\\b\\s*'),' ').replace(/^\s*/,'').replace(/\s*$/,'');}

function getElementsByClass(elem, classname)
	{
	var classes, rx, alltags, ctags, i, tag
	classes = new Array();
	rx = new RegExp('\\b' + classname + '\\b')
	alltags = document.getElementsByTagName(elem);
	cTags = alltags.length
	for (i=0; i<cTags; i++)
		{
		tag=alltags[i]
		if (tag.nodeName.toLowerCase() == elem)
			{
			if (tag.className)
				{
				if (tag.className.search(rx) >= 0)
					classes[classes.length] = tag;
				}
			}
		}
	return classes;
	}

// Cross-browser event handling
// by Scott Andrew LePera
// http://www.scottandrew.com/weblog/articles/cbs-events

// Modified 2004-08-10 by Andrew Grgeory to work around Konqueror bug
// Modified 2004-06-04 by Andrew Gregory to support legacy (NS3,4) browsers
// http://www.scss.com.au/family/andrew/

// eg. addEvent(imgObj, 'mousedown', processEvent, false);
function addEvent(obj, evType, fn, useCapture) {
  // work around Konqueror bug #57913 which prevents
  // window.addEventListener('load',...) from working
  var ua = navigator.userAgent;
  var konq = ua.indexOf('KHTML') != -1 && ua.indexOf('Safari') == -1 && obj == window && evType == 'load';
  // don't use addEventListener for Konq, have Konq fall back to the old
  // obj.onload method
  if (obj.addEventListener && !konq) {
    obj.addEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.attachEvent) {
    return obj.attachEvent('on' + evType, fn);
  } else {
    if (!obj.cb_events) {
      obj.cb_events = new Object();
      obj.cb_ftemp = null;
    }
    var events = obj.cb_events[evType];
    if (!events) {
      events = new Array();
      obj.cb_events[evType] = events;
    }
    var i = 0;
    while ((i < events.length) && (events[i] != fn)) {
      i++;
    }
    if (i == events.length) {
      events[i] = fn;
      obj['on' + evType] = new Function("var ret=false,e=this.cb_events['"+evType+"'];if(e){for(var i=0;i<e.length;i++){this.cb_ftemp=e[i];ret=this.cb_ftemp()||ret;}return ret;}");
    }
    return true;
  }
}

// eg. removeEvent(imgObj, 'mousedown', processEvent, false);
function removeEvent(obj, evType, fn, useCapture) {
  // work around Konqueror bug #57913 which prevents
  // window.addEventListener('load',...) from working
  var ua = navigator.userAgent;
  var konq = ua.indexOf('KHTML') != -1 && ua.indexOf('Safari') == -1 && obj == window && evType == 'load';
  // don't use addEventListener for Konq, have Konq fall back to the old
  // obj.onload method
  if (obj.removeEventListener && !konq) {
    obj.removeEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.detachEvent) {
    return obj.detachEvent('on' + evType, fn);
  } else {
    var ret = false;
    if (obj.cb_events) {
      var events = obj.cb_events[evType];
      if (events) {
        // remove any matching functions from the events array, shuffling items
        // down to fill in the space before truncating the array
        var dest = 0;
        for (var src = 0; src < events.length; src++) {
          if (dest != src) {
            events[dest] = events[src];
          }
          if (events[dest] == fn) {
            ret = true;
          } else {
            dest++;
          }
        }
        events.length = dest;
      }
    }
    return ret;
  }
}

// AutoSize - launch auto-sized, centred window
// Sources --
// http://www.therotunda.net/code/autosized-popup-window.html
// most code taken from above, with addition of code to fit on screen if image larger than window

// http://javascript.internet.com/page-details/auto-resizable-pop-up.html
// ~~~ AND ~~~
// open SCREEN-CENTERED window .. kudos to Doc JavaScript
// www.webreference.com/js
// however, his version called for image height and width as args...


function viewPic(img)
{ picfile=new Image(); picfile.src=(img); fileCheck(img); }

function fileCheck(img)
{ if((picfile.width!=0)&&(picfile.height!=0))
	{ makeWindow(img); }
	else {
		funzione="fileCheck('"+img+"')"; intervallo=setTimeout(funzione,10); }}

function makeWindow(img)
{ ht=picfile.height + 20; wd=picfile.width + 20; 
	if (window.screen) 
	{ var avht=screen.availHeight-40; var avwd=screen.availWidth-20;
                if (ht > avht) {
			wd=wd*avht/ht
                        ht=avht
		}
		if (wd > avwd) {
			ht=ht*avwd/wd
			wd=avwd
		}
		var xcen=(avwd-wd)/2; var ycen=(avht-ht)/2;
		var args="height="+ht+",innerHeight="+ht;
		args+=",width="+wd+",innerWidth="+wd;
		args+=",left="+xcen+",screenX="+xcen;
		args+=",top="+ycen+",screenY="+ycen+",resizable=0,scrollbars=0"; }
		//alert(args);
		return window.open(img, '', args); 
}


function CenterMap(x,y,hlight)
{
document.cookie='CenterMap='+x+','+y+','+hlight
alert('Centermap cookie(s)='+document.cookie);
}

function showMapFrame()
{	if(parent.document.getElementById('rhs').rows=="*,0")      // don't change if already changed by user
	{	parent.document.getElementById('rhs').rows="37%, *";
	}
}
function hideMapFrame()
{	parent.document.getElementById('rhs').rows="*,0";
}
function getArgs(  ) {
    var args = new Object(  );
    var query = window.location.search.substring(1);     
      // Get query string
    var pairs = query.split(",");
     // Break at comma
    for(var i = 0; i < pairs.length; i++) {
        var pos = pairs[i].indexOf('=');
          // Look for "name=value"
        if (pos == -1) continue;
          // If not found, skip
        var argname = pairs[i].substring(0,pos);
          // Extract the name
        var value = pairs[i].substring(pos+1);
          // Extract the value
        args[argname] = unescape(value);
         // Store as a property
    }
    return args;     // Return the object
}

