var windowWidth = null;
var windowHeight = null;

IE4 = (document.all);
NS4 = (document.layers);

var currentDisplay = "jcs";

var animationActive = false;
var sleepCount = 0;
var logoFlipActive = false;

var max_debris = 21;
var debris = null;
var debrises = new Array();
var debrisLaunchCounter = 0;

var logoHeight = 105;
var logoDelta = -2;
var logoTop = 20;
var logoTopDelta = 1;

var jcsLogo = new Image();
jcsLogo.src = "graphics/clock01.jpg";

var pixLogo = new Image();
pixLogo.src = "graphics/pixelsplash_logo_blackgold_circle.gif";

function fetchHeader() {
  var url = "TCHeader.html";
  submitData("GET", null, url, replaceHeader, true);
}

function Debris(status, x_velocity, y_velocity) {
  this.status = status;
  this.x_velocity = x_velocity;
  this.y_velocity = y_velocity;
}

function replaceHeader(response) {
  var obj = document.getElementById("tcHeader");
  obj.innerHTML = response;
}

function resizeLogo(height) {
  var theLogoDiv;
  var theLogo;
  if (currentDisplay == "pix") {
    theLogo = document.getElementById("altLogoImg");
    theLogoDiv = document.getElementById("altLogoDiv");
  }
  else {
    theLogo = document.getElementById("jcsLogoImg");
    theLogoDiv = document.getElementById("topLogoDiv");
  }

  theLogo.height = height;
  theLogo.width = 108;
  theLogoDiv.style.top = logoTop + "px";
}


function resetDebris() {
  for (i=0; i < max_debris; i++) {
    debrises[i] = new Debris("waiting", -100, -100);
    debris = document.getElementById("debris" + i);
    debris.style.top = '-100px';
    debris.style.left = '-100px';
    debris.style.display = "none";
  }
  debrisLaunchCounter = 0;
}


function startAnimation() {
  if (animationActive == true) return;
  else initAndStartAnimation();
}


function initAndStartAnimation() {
  IE4 = (document.all);
  NS4 = (document.layers);

  //var results = secureInit();
  //if (results != "secure") {
  //  window.location = results;
  //}

  windowWidth = ourWindowWidth();
  windowHeight = ourWindowHeight();

  animationActive = true;
  logoFlipActive = true;
  sleepCount = 0;

  logoDelta = -2;
  logoTop = 20;
  logoTopDelta = 1;

  resetDebris();

  animateLogo();
}


function animateLogo() {

  logoHeight += logoDelta;
  logoTop = logoTop + logoTopDelta;

  if (logoHeight > 105) {
    logoHeight = 105;
    logoDelta = 0;
    logoTop = 20;
    logoTopDelta = 0;

    if (currentDisplay == "pix") {
      sleepCount = 200;
    }
    else logoFlipActive = false;
  }

  if (sleepCount > 0) {
    sleepCount--;
    if (sleepCount == 0) {
      resetDebris();
      logoDelta = -2;
      logoTopDelta = 1;
    }
  }

  if (logoHeight <= 0) {
    logoHeight = 0;

    var jcsLogoObject = document.getElementById("topLogoDiv");
    var pixLogoObject = document.getElementById("altLogoDiv");

    if (currentDisplay == "pix") {
      jcsLogoObject.style.display = "block";    
      pixLogoObject.style.display = "none";    
      currentDisplay = "jcs";
    }
    else {
      currentDisplay = "pix";
      jcsLogoObject.style.display = "none";    
      pixLogoObject.style.display = "block";    
    }

    logoDelta = logoDelta * -1;
    logoTopDelta = logoTopDelta * -1;
  }

  //window.status = logoHeight;
  resizeLogo(logoHeight);



  debrisLaunchCounter++;
  if (debrisLaunchCounter == 3) {
    debrisLaunchCounter = 0;

    for (m=0; m < debrises.length; m++) {
      debris = document.getElementById("debris" + m);

      if (debrises[m].status == "waiting") {
        debrises[m].status = "alive";
        debrises[m].y_velocity = -4 - Math.round(8*Math.random());
        debrises[m].x_velocity = 0 + Math.round(5*Math.random());
        if ((100*Math.random()) < 50) debrises[m].x_velocity *= -1;

        debris.style.left = "180px";
        debris.style.top = "60px";
        debris.style.display = "block";
        break;
      }
    }
  }

  debrisAlive = false;
  for (i=0; i < debrises.length; i++) {
    debris = document.getElementById("debris" + i);
    if (debrises[i].status != "dead") debrisAlive = true;
    
    if (debrises[i].status == "alive") {
      // If alive, let's animate and process:

      // Boundary check:
      tempX = parseInt(debris.style.left);
      if ((tempX > windowWidth) || (tempX < -17)) {
        debrises[i].status = "dead";
        debris.style.display = "none";
      }
      tempY = parseInt(debris.style.top);
      if (tempY > (windowHeight - 100)) {
        debrises[i].status = "dead";
        debris.style.display = "none";
      }

      if (debrises[i].status != "dead") {
        debris.style.left = parseInt(debris.style.left) + debrises[i].x_velocity + 'px';
        debris.style.top = parseInt(debris.style.top) + debrises[i].y_velocity + 'px';
        if (debrisLaunchCounter == 2) debrises[i].y_velocity++;
      }

    }
  }

  if ((logoFlipActive == false) && (debrisAlive == false)) animationActive = false;


  if (animationActive == true) {
    if (IE4) setTimeout(animateLogo, 4);
    else setTimeout(animateLogo, 8);
  }

}

