// uncomment for initial animation sequence
//Event.observe(window, 'load', hsquares_animate, false);

// uncomment to make middle square turn on by default
Event.observe(window, 'load', hsquares_activate_default, false);

var anim = false;
var activesq = 0;
var sq = 0;
var sqarr;

function hsquares_animate()
{
    // tell mouseover functions to wait until we finish animation
    anim = true;
 
    // randomly light up each squares
    sqarr = new Array();
    for (var i=0; i < 9; i++) {
        sqarr[i] = i+1;
    }
    
    setTimeout(hsquare_anim, 500)
}

function hsquare_anim()
{
    if ( sq > 0 ) hsquare_swapImgRestore();

    if ( ++sq < 10 ) {
        // pick a random square to animate and remove square from our array
        var n = Math.floor(Math.random() * sqarr.length);
        var anisq = sqarr[n];
        sqarr.splice(n,1);
        // animate chosen square
        hsquare_swapImage('hsquares'+anisq, '', '../images/hsquares'+anisq+'_ro.jpg', 1);
        setTimeout(hsquare_anim, 250)
    } else {
        anim = false;
    }
}

function hsquares_activate( id, image_on, boxnum )
{
  hsquare_swapImage(id, '', image_on, 1);
  if ( boxnum > 0 && boxnum < 10 ) {
    if (x = document.getElementById('hsquares_quote'+boxnum)) {
      x.style.display = 'inline';
      x.style.visibility = 'visible';
    }
  }
}

function hsquares_deactivate()
{
  hsquare_swapImgRestore();
  for (n = 1; n < 10; n++) {
    if (x = document.getElementById('hsquares_quote' + n)) {
      x.style.display = 'none';
      x.style.visibility = 'hidden';
    }
  }
}

function hsquares_activate_default()
{
    if (activesq == 0) {
      hsquares_deactivate();
      hsquares_activate( 'hsquares5', '../images/hsquares5_ro.jpg', 5 );
    }
}

function hsquare_rollover( id, image_on, boxnum )
{
    var x,t;

    if (anim) return;

    activesq = boxnum
    hsquares_deactivate();
    hsquares_activate( id, image_on, boxnum );
}

function hsquare_rollout()
{
    var x, n;
    
    if (anim) return;
    
    activesq = 0
    // set timer to check if we should restore default
    setTimeout(hsquares_activate_default, 1000)
}

// copy of basic image swapping functions so squares have their own restore array

function hsquare_swapImgRestore() {
  var i,x,a=document.hsquare_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function hsquare_findObj(n, d) {
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=hsquare_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function hsquare_swapImage() {
  var i,j=0,x,a=hsquare_swapImage.arguments; document.hsquare_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=hsquare_findObj(a[i]))!=null){document.hsquare_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

