var windowWidth = null;
var windowHeight = null;

var animationActive = false;
var sleepCount = 0;
var animationWaiting = false;

var max_debris = 21;
var debris = null;
var debrises = new Array();

var logoHeight = 109;
var logoDelta = -3;

var jcsLogo = new Image();
jcsLogo.src = "graphics/j&cstudios_logo_blackgold_circle.gif";

var pixLogo = new Image();
pixLogo.src = "graphics/pixelsplash_logo_blackgold_circle.gif";

function fetchHeader() {
  var url = "JCSHeader.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("jcsHeader");
  obj.innerHTML = response;
}

function resizeLogo(height) {
  var theLogo = document.getElementById("topLogo");
  theLogo.height = height;
  theLogo.width = 108;
}

function startResetLogoAnimation() {
  startAnimation();
}

function startLogoAnimation() {
  if (animationWaiting == true) {
    return;
  }
  else {
    animationWaiting = true;
    startAnimation();
  }
}

function startAnimation() {

  var results = secureInit();
  if (results != "secure") {
    window.location = results;
  }

  windowWidth = ourWindowWidth();
  windowHeight = ourWindowHeight();

  animationActive = true;

  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";
  }

  animateLogo();
}

function animateLogo() {
  logoHeight += logoDelta;
  resizeLogo(logoHeight);

  if (sleepCount > 0) {
    sleepCount--;
    if (sleepCount == 0) {
      logoDelta = 3;
      logoHeight = 1;
    }
  }

  var theLogo = document.getElementById("topLogo");

  if ((sleepCount == 0) && (logoHeight <= 0)) {
    if (theLogo.src == pixLogo.src) theLogo.src = jcsLogo.src;
    else theLogo.src = pixLogo.src;

    /*
    var now = new Date();
    var startingMSeconds = now.getTime();
    var sleeping = true;
    var alarm;

    while (sleeping) {
      alarm = new Date();
      if ((alarm.getTime() - startingMSeconds) > 125) sleeping = false;
    }
    */

    sleepCount = 10;
    logoDelta = 0;
  }

  if (logoHeight >= 109) {
    loogoHeight = 109;
    logoDelta = -3;
    resizeLogo(logoHeight);
    animationActive = false;
  }

  for (m=0; m < debrises.length; m++) {
    debris = document.getElementById("debris" + m);

    if (animationActive == false) {
      debrises[m].status == "dead";
      debris.style.display = "none";
    }
    else {
      if (debrises[m].status == "waiting") {
        debrises[m].status = "alive";
        debrises[m].y_velocity = -4 - Math.round(16*Math.random());
        debrises[m].x_velocity = 0 + Math.round(10*Math.random());
        if ((100*Math.random()) < 50) debrises[m].x_velocity *= -1;

        debris.style.left = "230px";
        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 == "alive") {
      debrisAlive = true;
      // If alive, let's animate and process:
      debris.style.left = parseInt(debris.style.left) + debrises[i].x_velocity + 'px';
      debris.style.top = parseInt(debris.style.top) + debrises[i].y_velocity + 'px';
      debrises[i].y_velocity++;

      // Boundary check:
      tempX = parseInt(debris.style.left);
      if ((tempX > windowWidth) || (tempX < -20)) {
        debrises[i].status = "dead";
        debris.style.display = "none";
      }
      tempY = parseInt(debris.style.top);
      if (tempY > windowHeight) {
        debrises[i].status = "dead";
        debris.style.display = "none";
      }
    }
  }

  if (animationActive == true) {
    setTimeout(animateLogo, 1);
  }
  else {
    if (theLogo.src == pixLogo.src) {
      setTimeout(startResetLogoAnimation, 2000);
    }
    else {
      animationWaiting = false;
    }
  }
}

