
// July 3 2000 - Willem Jongman, Akris bv.
// Script to change image appearance of generated buttons
// in reaction on mouseover- and click-events
//
// The image and the href are associated by their name property
// (so they _must_ have the same name for this script to work)
//
// The format of the image source name should be of the form:
// <navpath>_lo-<buttontext>.<ext> for normal text resp.
// <navpath>_hi-<buttontext>.<ext> for highlited text
//
// where <navpath> is the qualified BiS path
//       _lo- resp _hi- are literal
//       <buttontext> is the text on the generated button with spaces replaced by underscores
//       <ext> is the imagefile extension, typically "gif" or "jpg"
//
// example HTML syntax to use this script:
//
// <a href="some_url"
//    name="nav-1-1" onmouseover="HiLite(this);"
//                   onmouseout="LoLite(this);"
//                   onclick="LockHiLite(this);">
// <img  name="nav-1-1" src="nav_topic_1_lo-topic_one.gif">
// </a>
//
// Note again that the name-property of the <a> element is
// identical to that of the <img> element
//--------------------------------

// Global variables

var g_LockFlags = new Array();      // Array to track which images remain fixed
var g_hilightedname = "";           // Name of currently hilighted image
var g_ImageSequences = new Array(); // Array of ImageArrays
var g_nsequences = 0;               // The number of ImageArrays on this page
var g_imgs = new Array();           // Array to hold preloaded images
var g_imgcount = 0;                 // Number of preloaded images

// for BMW layers
var userAgent = window.navigator.userAgent;
var bVers = parseInt(userAgent.charAt(userAgent.indexOf("/")+1),10);

// cross-frame access is loosened up for IE
//var domain = document.domain;
//while(domain.length - domain.indexOf('.') > 4){
//  domain = domain.substring(domain.indexOf('.') + 1, domain.length);
//}
//document.domain = domain;

//--------------------------------
function PreLoadImages()
// Load all images, even the ones that are not visible (yet),
// so they will be in the cache when they are needed.
{
  if (!document.all && !document.getElementById)
  {
    // Preload images in layers
    for (i=0; i<document.layers.length; i++)
      for (j=0; j<document.layers[i].document.images.length; j++)
        if (document.layers[i].document.images[j].name)
          DoPreload(document.layers[i].document.images[j])
  }

  // Preload images in document
  for (i=0; i<document.images.length; i++)
    if (document.images[i].name)
      DoPreload(document.images[i])

  if (g_nsequences > 0)
    // Start the animation
    window.setTimeout('NextImage();',5000);
}

//--------------------------------
function DoPreload(img)
{
  var imgsrc = img.src;
  var imgname = img.name;
  var altimgurl;
  if ((altimgurl = ReplaceInString(imgsrc, "_lo.", "_hi.")) != "")
  {
    // we have a '_hi-' image to load.
    g_imgs[g_imgcount] = new Image();
    g_imgs[g_imgcount++].src = altimgurl;
  }
  else if ((altimgurl = ReplaceInString(imgsrc, "_hi.", "_lo.")) != "")
  {
    // we have a '_lo-' image to load.
    g_imgs[g_imgcount] = new Image();
    g_imgs[g_imgcount++].src = altimgurl;
  }
  else if (imgsrc.indexOf("_rot") != -1)
  {
    // we have to preload an image-sequence
    g_ImageSequences[g_nsequences++] = new ImageArray(imgname, imgsrc);
  }
}

//--------------------------------
function ImageArray(imagename, lastimgsrc)
// Constructor for object of type ImageArray
// A sequence of images can be indicated in the image sourcename
// The sourcename is supposed to have the following format:
//
// <navpath>_rot<N>.<ext>
//
// where <navpath> is the qualified BiS path
//       _rot should be there literal
//       <N> is an integer in {1..9} that indicates the last image in the sequence
//       <ext> is the imagefile extension, typically "gif" or "jpg"
//
// This function attempts to preload all images in the
// range <navpath>_rot1.<ext> to <navpath>_rotN.<ext>
//
{
  this.name = imagename;
  // Extract info from lastimgsrc
  var splitpos = lastimgsrc.indexOf("_rot");
  // (splitpos + 4) is now supposed to point at a numeric character.
  // Make very sure we have an integer here, we don't want to end up
  // in an infinite loop and cause certain browsers to freeze..
  var seqlen = parseInt(lastimgsrc.charAt(splitpos + 4));
  if (isNaN(seqlen) || seqlen == 0)  // This means that _rot0 is illegal
  {
    alert(imgsrc + " is not a valid image-sequence name !");
    return 0;
  }
  // Setup the member variables
  this.currentindex = seqlen;
  // Save the number (N)
  this.imagecount = seqlen;
  // Prepare the strings needed to build the sequence of sourcenames
  // Split into head and tail at splitpos+4 and discard the number (N)
  var head = lastimgsrc.substring(0, splitpos+4);
  var tail = lastimgsrc.substring(splitpos+5, lastimgsrc.length);
  this.images = new Array();
  // Fill the array of images
  for (i=1; i<=seqlen; i++)
  {
    // Construct image-name
    var seqname = head + i + tail;
    // Preload the image
    this.images[i] = new Image();
    this.images[i].src = seqname;
  }
}

//--------------------------------
function NextImage()
{
  // There may be several sequences
  for (i=0; i < g_nsequences; i++)
  {
    var curseq = g_ImageSequences[i];
    var currentname = curseq.name;
    // Increment index
    if (++curseq.currentindex > curseq.imagecount)
      curseq.currentindex = 1;
    // Swap the image
    document.images[currentname].src = curseq.images[curseq.currentindex].src;
  }
  window.setTimeout('NextImage();',5000);
}
  
//--------------------------------
function LoLite(currentname, layername)
// Set non-hilighted image associated with this object
{
  // If it is locked, nothing to do
  if (g_LockFlags[currentname])
    return;

  // Find image with the same name
  if(layername && document.layers){
    var imageObject = eval("NSStyle('"+layername+
                           "').document.images['"+currentname+"']");
  }else{
    var imageObject = document.images[currentname];
  }
  // Return if the image doesn't exist
  if(! imageObject) return;
  // Get source url of this image
  var imgsrc = imageObject.src;
  // Convert source url to url of lolighted image
  loname = ReplaceInString(imgsrc, "_hi.", "_lo.");
  if (loname != "")
    // Make the image change
    imageObject.src = loname;
}

//--------------------------------
function HiLite(currentname, layername)
// Set hilighted image associated with this object
{
  // Find image with the same name
  if(layername && document.layers){
    var imageObject = eval("NSStyle('"+layername+
                           "').document.images['"+currentname+"']");
  }else{
    var imageObject = document.images[currentname];
  }
  // Return if the image doesn't exist
  if(! imageObject) return;
  // Get source url of this image
  var imgsrc = imageObject.src;
  // Convert source url to url of hilighted image
  hiname = ReplaceInString(imgsrc, "_lo.", "_hi.");
  if (hiname != "")
    // Make the image change
    imageObject.src = hiname;
  else
    g_LockFlags[currentname] = true;
}

//--------------------------------
function LockHiLite(currentname, layername)
// Lock current object in the hilighted state
{
  // Reset all lock-flags
  for (i in g_LockFlags)
    g_LockFlags[i] = false;

  // Lock the hilight-status of this object
  g_LockFlags[currentname] = true;
  // Reset hilighted object (if any)
  if (g_hilightedname)
    LoLite(g_hilightedname, layername);
  // And hilight current one
  HiLite(currentname, layername);
  // Save this object
  g_hilightedname = currentname;
}

//--------------------------------
function ReplaceInString(targetstring, searchstring, replacestring)
// Helper function to reduce redundant code
// Replaces the first occurance of 'searchstring'
// within 'targetstring' by 'replacestring'
// in params:
// targetstring  - string to convert
// searchstring  - substring to be replaced
// replacestring - string to replace 'searchstring' with
//
// returns:
// converted string or empty string on failure
//
{
  var splitpos = targetstring.indexOf(searchstring);
  if (splitpos != -1)
  {
    // Split at splitpos and remove 'searchstring'
    var head = targetstring.substring(0, splitpos);
    var tail = targetstring.substring(splitpos + searchstring.length, targetstring.length);
    // Rebuild with 'replacestring' inserted
    var resultstring = head + replacestring + tail;
    return resultstring;
  }
  else
  {
    return "";
  }
}

//--------------------------------
function PreloadSequence(imgsrc)
// A sequence of images can be indicated in the image sourcename
// The sourcename is supposed to have the following format:
//
// <navpath>_rot<N>.<ext>
//
// where <navpath> is the qualified BiS path
//       _rot should be there literal
//       <N> is an integer in {1..9} that indicates the last image in the sequence
//       <ext> is the imagefile extension, typically "gif" or "jpg"
//
// This function attempts to preload all images in the
// range <navpath>_rot1.<ext> to <navpath>_rotN.<ext>
//
{
  var RotArray = new Array();
  var splitpos = imgsrc.indexOf("_rot");
  // (splitpos + 4) is now supposed to point at a numeric character.
  // Make very sure we have an integer here, we don't want to end up
  // in an infinite loop and cause certain browsers to freeze..
  var seqlen = parseInt(imgsrc.charAt(splitpos + 4));
  if (isNaN(seqlen) || seqlen == 0)  // This means that _rot0 is illegal
  {
    alert(imgsrc + " is not a valid image-sequence name !");
    return;
  }
  // Prepare the strings needed to build the sequence of sourcenames
  // Split into head and tail at splitpos+4 and discard the number (N)
  var head = imgsrc.substring(0, splitpos+4);
  var tail = imgsrc.substring(splitpos+5, imgsrc.length);
  // For full sequence
  for (i=1; i<seqlen; i++)
  {
    // Construct image-name
    var seqname = head + i + tail;
    // Preload the image
    RotArray[i] = new Image();
    RotArray[i].src = seqname;
  }
}

//--------------------------------


// added for BMW (copied from bmw_scriptlib.js)
var curLayer='';
//--BEGIN visibility switch for layers  --//
function vSwitch(aLayer,aBool) {
  if (document.getElementById) {
   DOMStyle(aLayer).visibility = (aBool == 0) ? "hidden" : "visible";
   if (aBool==1) curLayer = aLayer;
  } else {
    if (document.all) {
      IEStyle(aLayer).visibility = (aBool == 0) ? "hidden" : "visible";
      if (aBool==1) curLayer = aLayer;
    } else {
      NSStyle(aLayer).visibility = (aBool == 0) ? 'hide' : 'show';
    }
  }
}
//--END visibility switch for layers  --//

//--BEGIN DOM style layer access  --//
function DOMStyle(s)
  {
  return document.getElementsByTagName("div")[s].style;
  }
//--END DOM style layer access  --//

//--BEGIN Internet Explorer layer access  --//
function IEStyle(s)
  {
  return document.all.tags("div")[s].style;
  }
//--END Internet Explorer layer access  --//

//--BEGIN Netscape layer access  --//
function NSStyle(s)
  {
  return findElement(s,0);
  }
//--END Netscape layer access  --//

//--BEGIN Netscape layer functionality  --//
function findElement(n,ly)
  {
  var curDoc = ly ? ly.document : document;
  var elem = curDoc[n];
  if (bVers < 4)
    return document[n];
  if (!elem)
    {
    for (var i=0;i<curDoc.layers.length;i++)
      {
      elem = findElement(n,curDoc.layers[i]);
      if (elem)
        return elem;
      }
    }
  return elem;
  }
//--END Netscape layer functionality  --//

function hideAll() {
  vSwitch('naviLayer1',0);
  vSwitch('naviLayer2',0);
  vSwitch('naviLayer3',0);
}

function updateIt(evt) {
  if (document.all || document.getElementById) {
    if (curLayer== '') return;
    var theel=document.getElementById(curLayer);
    var lxtop= theel.offsetLeft;
    var rxbottom= lxtop + theel.offsetWidth;
    var lytop= theel.offsetTop;      
    var rybottom= lytop + theel.offsetHeight;
    var clickX, clickY; 
    if (isNav) {
      var clickX = evt.pageX;
      var clickY = evt.pageY;
    } else {
      var clickX = window.event.clientX + document.body.scrollLeft;
      var clickY = window.event.clientY + document.body.scrollTop;
    }
    if (clickX < lxtop || clickX > rxbottom) {hideAll(); return; }
    if (clickY < lytop || clickY > rybottom) {hideAll(); return; }
  }
}
function popup(Link,Name){
  var breedte=718;
  var hoogte=468;
  var iMyWidth = (window.screen.width/2) - (breedte/2 + 10);
  var iMyHeight = (window.screen.height/2) - (hoogte/2 + 50);

  var win3 = window.open(Link,Name,"location=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=" + breedte + ",height=" + hoogte + ",left=" + iMyWidth + ",top=" + iMyHeight + ",screenX=" + iMyWidth + ",screenY=" + iMyHeight);
  win3.focus();
}

function PopupPrintWindow(Link) {
  var breedte=550;
  var hoogte=510;
  var iMyWidth = (window.screen.width/2) - (breedte/2 + 10);
  var iMyHeight = 10; //(window.screen.height/2) - (hoogte/2);

  var win3 = window.open(Link,'Print',"location=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=" + breedte + ",height=" + hoogte + ",left=" + iMyWidth + ",top=" + iMyHeight + ",screenX=" + iMyWidth + ",screenY=" + iMyHeight);
  win3.focus();

}
function PopupCentered(url, winWidth, winHeight,scrl) {
  var name='letter';
  var scrollbars='no';
  if (scrl) scrollbars='1';
  var posLeft = (window.screen.width /2)-(winWidth /2+10);
  var posTop  = (window.screen.height/2)-(winHeight/2+50);
  var winNew = window.open(url,name,'toolbar=no,location=no,status=no,menubar=no,scrollbars=' + scrollbars + ',resizable=no,width='+winWidth+',height='+winHeight+',left='+posLeft+',top='+posTop+',screenX='+posLeft+',screenY='+posTop);
  winNew.blur();
  winNew.focus();
}

function setRandomImage()
{
  var r = Math.random() * 2;  var s = 1 + Math.round(r);
  var randimg = document.images['random'];
  if(! randimg) return;
  randimg.src = '/img_site/h' + s + '.jpg'; 
}

function HideCoverDiv()
{
  document.getElementById('cover').style.display='none'; 
}

function showCoveredDiv() {
  window.setTimeout('HideCoverDiv();',2500);
}

function cleanTitle() {
  var newtitle='';
  var chars= 'abcdefghijklmnnopqrstuvwxyz0123456789';
  if (document.getElementById('title') ) {
   var strippedtitle=document.getElementById('title').innerText.toLowerCase().split('');
   
   for (var i=0; i<strippedtitle.length; i++) {
    if (chars.indexOf(strippedtitle[i])>-1) newtitle+= strippedtitle[i] 
    else newtitle+= '_';
   }
  }
  return newtitle;  
}
function checkSearchBox(n) {
  var boxes=document.forms[0].source;
  var bCh=false;
  var mother=0;
  for (var i=0;i<boxes.length; i++) {
   if (boxes[i].value == '21,29,30,31,32,33' || boxes[i].value=='51,59,60,61,62,63') {
    mother=i;
    if (boxes[i].checked) { bCh=true; } else {bCh=false; }
   }
  }
  for (var i=0;i<boxes.length; i++) {
   if ( boxes[i].value == 21 || (boxes[i].value>28 && boxes[i].value<34) ||
        boxes[i].value == 51 || (boxes[i].value>58 && boxes[i].value<64)) {
     if (n==0) {
      boxes[i].checked=bCh;
     } else {
      if (!(boxes[i].checked)) boxes[mother].checked=false;
     }
   }
  }
}

