  // prevent IE page loading flicker (IE5 & 6)
	try {	
	 document.execCommand("BackgroundImageCache", false, true);	
	} catch(err) {}

  // initialise buttons
  var b0, b1, b2, b3, b4, b5, b6;

  // each button displays one of 7 possible images
  buttons = new Array(7);	
  for (i = 0; i < 7; i++)
  {
     buttons[i] = new Image();
     buttons[i].src = "/images/buttons/sky/b" + i + ".gif";
  }

  // initialise button index and timer IDs
  var n = 0;
  var buttonID = 0;
  var dialTimer = 0;
  var danceTimer = 0;
  var previewTimer = 0;
  var direction = 1;

  // initialise pool applet variables
  var poolImage = 0;        // current pool image
  var tempPoolImage = 0;    // temporary pool image holder
  var poolReady = false;    // applet ready flag

  // init and menu functions ////////////////////////////////////////////
  function init(defaultPoolImage)
  {  
//    java.lang.System.out.println("[JS] in init(" + defaultPoolImage + ")");

    // initialise the current pool image
    poolImage = defaultPoolImage;
    
    if (poolReady)
      setPoolImage(poolImage);
  }
  
  function toggleItem(id)
  {
    // toggle the item
    var item = document.getElementById(id);

    if (item.style.display != "block")
      showItem(id);
    else
      hideItem(id);      
  }
  
  function showItem(id)
  {
    var item = document.getElementById(id);
    
    // show the item immediately
    item.style.display = "block";
    
    // and save the change to the server
    request = new XMLHttpRequest();
    request.onreadystatechange = onResponse; // (see common/js/ajax.js)
    url = "/ajax/show_item.php?id=" + id;

//    java.lang.System.out.println("request: show " + id);
    request.open("get", url, true);
    request.send(null);
  }

  function hideItem(id)
  {
    var item = document.getElementById(id);

    // hide the item immediately
    item.style.display = "none";
    
    // and save the change to the server
    request = new XMLHttpRequest();
    request.onreadystatechange = onResponse; // (see common/js/ajax.js)
    url = "/ajax/hide_item.php?id=" + id

//    java.lang.System.out.println("request: hide " + id);
    request.open("get", url, true);
    request.send(null);
  }

  // logo functions ///////////////////////////////////////////////////////
  // the logo has two possible states
  logo1 = new Image(); logo1.src = "/images/logo/logosky.gif";
  logo2 = new Image(); logo2.src = "/images/logo/logoblack.gif";

  function logoover()
  {
    // change the logo
    document.getElementById("logoImg").src = logo2.src;
    
    // drip a drop
    makeDrop(); 

    // blank the pool image
//    setPoolImage(0);
  }

  function logoout()
  {
    // change the logo back
    document.getElementById("logoImg").src = logo1.src;
    
    // drip a drop
    makeDrop(); 

    // restore the pool image
//    setPoolImage(poolImage);
  }

  function logodown()
  {
    // drip a drop
    setTimeout('makeDrop()', 100);
    
    // jump to the home page
    document.location = "/index.php";

//    // toggle the pool image
//    // - it may be 6 (index page) or 7 (about page)
//    //   or 0 if the image is currently 'blank'
//    if (poolImage > 0)
//    {
//      tempPoolImage = poolImage;
//      poolImage = 0;
//      setPoolImage(0);
//    }
//    else
//    {
//      poolImage = tempPoolImage;
//      setPoolImage(tempPoolImage);
//    }
  }

  // sky dial functions //////////////////////////////////////////////////////////////////
  function skydialover(e)
  {
    // drip a drop
    makeDrop(); 

   // spin the dial
//    clearInterval(dialTimer);	
//    dialTimer = setInterval('document.dial.src = buttons[(n^=1)+4].src', 40);

    // update the pool image
    setPoolImage(1);

//    // cycle through all the pool images
//    previewTimer = setInterval(poolflasher, 500);
  }

  function skydialout(e)
  {
//    // kill the dial spin and preview flick timers
//    clearInterval(dialTimer);	
//    clearInterval(previewTimer);

    // reset the current pool image
    setPoolImage(poolImage);
  }

  function skydialdown()
  {
    // drip a single drop into the pool
//    makeDrop();
//    setTimeout('makeDrop()', 100);
  }

  function poolflasher()
  {
    // update the pool image
    setPoolImage(buttonID + 1);

    // advance to the next pool image
    buttonID += 1; buttonID %= 7; 

    // drip a single drop into the pool
//    makeDrop();
  }

  // sky button functions //////////////////////////////////////////////////////////////////
  function skybuttonover(buttonID)
  {
    // drip a drop
     makeDrop(); 
 
    // arm the button
    setTimeout('document.button' + buttonID + '.src = buttons[1].src', 40);
    setTimeout('document.button' + buttonID + '.src = buttons[2].src', 80);

    // update the pool image
    setPoolImage(buttonID + 2);
  }

  function skybuttonout(buttonID)
  {
    // kill any current dial timer
    skydialout();	

    // disarm the button
    setTimeout('document.button' + buttonID + '.src = buttons[1].src', 40);
    setTimeout('document.button' + buttonID + '.src = buttons[0].src', 80);

    // reset the current pool image
    setPoolImage(poolImage);
  }

  function skybuttondown(buttonID)
  {
    // press the button
    setTimeout('document.button' + buttonID + '.src = buttons[1].src', 40);

    // drip a single drop into the pool
//    makeDrop(); 
//	  setTimeout('makeDrop()', 100);

    // now spin the dial
//    skydialover();
  }

  // sky button dance functions //////////////////////////////////////////////////////////////////
  function dance1on()
  {
    // arm all the buttons once
    skybuttonover(0); skybuttonover(1); skybuttonover(2); skybuttonover(3); skybuttonover(4); skybuttonover(5); 
  }

  function dance1off()
  {
    // disarm all the buttons
    skybuttonout(0); skybuttonout(1); skybuttonout(2); skybuttonout(3); skybuttonout(4); skybuttonout(5); 
  }

  function dance2on()
  {
    // spin the dial and cycle the buttons back and forward
    skydialover();
    danceTimer = setInterval(dance2step, 200);
  }

  function dance2step()
  {
    // open the current button
    setTimeout('document.button' + buttonID + '.src = buttons[1].src', 40);
    setTimeout('document.button' + buttonID  + '.src = buttons[2].src', 80);

    // close the current button
    setTimeout('document.button' + buttonID  + '.src = buttons[1].src', 120);
    setTimeout('document.button' + buttonID  + '.src = buttons[0].src', 160);

    // advance to the next button
    buttonID += direction; buttonID %= 5; 

    // toggle the direction of the dance
    if (buttonID == 0) { direction = 1 } // go clockwise
    if (buttonID == 4) { direction = -1 } // go anticlockwise         
  }

  function dance2off()
  {
    // stop the dance amd dial timers
    clearInterval(danceTimer); skydialout();
  }

  function dance3on()
  {
    // spin the dial and cycle the buttons round and round
    skydialover();
    danceTimer = setInterval(dance3step, 200);
  }

  function dance3step()
  {
    // open the current button
    setTimeout('document.button' + buttonID + '.src = buttons[1].src', 40);
    setTimeout('document.button' + buttonID  + '.src = buttons[2].src', 80);

    // close the current button
    setTimeout('document.button' + buttonID  + '.src = buttons[1].src', 120);
    setTimeout('document.button' + buttonID  + '.src = buttons[0].src', 160);

    // advance to the next button (go clockwise)
    buttonID += 1; 

    // bypass any gaps
    if (buttonID == 5 || buttonID == 7)  buttonID++;

    buttonID %= 8; 
  }

  function dance3off()
  {
    // stop the dance and dial timers
    clearInterval(danceTimer); skydialout();
  }

  // skin button functions //////////////////////////////////////////////////////////////////
  function skinbuttonover()
  {
    // drip a drop
    makeDrop(); 

    // blank the pool image
    setPoolImage(0);
  }

  function skinbuttonout()
  {
    // drip a drop
    makeDrop(); 

    // restore the pool image
    setPoolImage(poolImage);
  }

  // ordinary button functions ///////////////////////////////////////////////////////////////////////
  function buttonover(button)
  {
    // called by ordinary HTML buttons on a mouse over event
    if (button.className == '')
      button.className='hover';
    else
    if (button.className == 'alt')
      button.className='alt hover';    
  }

  function buttonout(button)
  {
    // called by ordinary HTML buttons on a mouse out event
    if (button.className == 'hover')
      button.className='';
    else
    if (button.className == 'alt hover')
      button.className='alt';    
  }

  // pool applet functions //////////////////////////////////////////////////////////////////
  function setPoolReady(state)
  {
//    java.lang.System.out.println("[JS] in setPoolReady()");

    // (this method is called by the pool applet once it has been fully loaded)
    poolReady = state;
    
    // now we can set the current pool image
    setPoolImage(poolImage);
    
    // drip a drop
    setTimeout('makeDrop()', 100);    
  }
  
  function makeDrop() 		
  {
//    java.lang.System.out.println("[JS] in makeDrop()");
    if (poolReady) 
      document.pool.drop();
//      document.getElementById("pool").drop();
  }

  function setPoolImage(imageNum)
  { 
//    java.lang.System.out.println("[JS] in setPoolImage(" + imageNum + ")");

    if (poolReady)
    {
      document.pool.setPoolImage(imageNum);
//      document.getElementById("pool").setPoolImage(imageNum);
//      makeDrop();
    }
  }

  function pressButton()
  {
    // this function is called by the Pool applet on a mouse down event
//    setTimeout('document.magicbutton.src = buttons[1].src', 0);   <!-- middle button  -->
//    setTimeout('document.magicbutton.src = buttons[2].src', 100); <!-- bright button  -->
  }

  function releaseButton()
  {
    // this function is called by the Pool applet on a mouse up event
//    setTimeout('document.magicbutton.src = buttons[1].src', 0);   <!-- middle button  -->
//    setTimeout('document.magicbutton.src = buttons[0].src', 100); <!-- dull button    -->
  }
  
  function imposeMaxLength(obj, maxlen)
  {
    // impose a maximum length on text areas   
    var msg = document.getElementById("invalid_msg");

    if (obj.value.length > maxlen)
    {
      obj.value = obj.value.substring(0, maxlen);
      msg.innerHTML = "<span class=\"invalid\">Maximum message length exceeded</span>";
    }
    else
      msg.innerHTML = "&nbsp;";    
  }
  
  