var isMozilla = (navigator.appName == 'Microsoft Internet Explorer'?0:1);
var midX = 0;
var midY = 0;
var oB; // bar
var oC; // content
var oS; // splash
var snowFlakes = [];
var i;
var delay = navigator.appVersion.toLowerCase().indexOf('windows nt')>=0?20:2;
var doStop = 0;
var snowLimit = isMozilla?16:32;
var a1 = new Image();
var a2 = new Image();
var a3 = new Image();
var a4 = new Image();
var b = 'image/';
var cSections = [];
var px = 'px';
a1.src = b+'arkanoid_1.gif';
a2.src = b+'summer02_1.gif';
a3.src = b+'snowflake.gif';
a4.src = b+'nav_bg_1.gif';

SnowFlake.prototype.animate = function() {
  this.sine += this.sV;
  this.y += this.sVY;
  if (this.y > midY + 6) {
    this.active = 0;
    if (snowFlakes.length >= 128) {
      this.active = 1;
      this.sVY = 0.5+Math.random()*2;
      this.y = 0;
    } else {
      snowSomeMore();
      return false;
    }
  }
  if (this.sine >= 6.2) this.sine = 0;
  this.o.style.left = parseInt(this.baseX) + Math.sin(this.sine)*this.sX + px;
  this.o.style.top = this.y + px;
}

function SnowFlake(sLeft,sX,sV,sVY) {
  this.o = document.createElement('img');
  this.active = 1;
  this.sine = Math.random()*3; // sine value
  this.sX = sX;  // sine multiplier
  this.sV = sV;  // sine velocity
  this.sVY = sVY;
  this.baseX = sLeft;
  this.y = 0;
  this.o.src = 'image/snowflake.gif';
  this.o.width = 2;
  this.o.height = 2;
  this.o.style.display = 'block';
  this.o.style.position = 'absolute';
  this.o.style.left = this.baseX + px;
  this.o.style.top = 0 + px;
  oS.appendChild(this.o);
}

function init() {
  cSections = [document.getElementById('cMain'),document.getElementById('cAbout'),document.getElementById('cNotes'),document.getElementById('cCredits'),document.getElementById('cInspiration')];
  window.onresize = getClientCoords;
  if (!isMozilla) {
    document.getElementById('mainLinks').style.display = 'block';
    document.getElementById('arkLaunch').title = 'Click to launch.. [ Insert pop-up warning here ]';
  } else {
    document.getElementById('arkLaunch').title = 'Current version works under IE. This link will point to an earlier compatible test version. Apologies.. read below!';
    document.getElementById('objectList').style.fontSize = '9px';
  }
  oB = document.getElementById('hBar');
  oC = document.getElementById('mainContent');
  oS = document.getElementById('mainSplash');
  getClientCoords();
  oB.style.display = 'block';
  oC.style.display = 'block';
  oS.style.display = 'block';
  for (i=0; i<snowLimit; i++) {
    setTimeout("snowSomeMore()",delay*(i*10));
  }
  mainLoop();
}

function pauseAnimation() {
  doStop = 1;
  rAni.style.display = 'block';
}

function snowSomeMore() {
  snowFlakes[snowFlakes.length] = new SnowFlake(parseInt(oC.style.left)-16+parseInt(Math.random()*320),5+Math.random()*48,0.05,0.5+Math.random()*2);
}

function doPositioning() {
  oB.style.top = midY + 7 + px;
  oC.style.top = midY - 102 + px;
  oS.style.left = midX - 202 + px;
  for (i=0; i<snowFlakes.length; i++) {
    if (!snowFlakes[i].active) snowFlakes[i].o.style.top = midY + 4 + px;
  }
}

function getClientCoords() {
  midX = parseInt((!isMozilla?document.body.clientWidth:window.innerWidth)/2);
  // midY = parseInt((!isMozilla?document.body.clientHeight:window.innerHeight)/2);
  midY = 250;
  doPositioning();
}

function mainLoop() {
  for (i=0; i<snowFlakes.length; i++) {
    if (snowFlakes[i].active) snowFlakes[i].animate();
  }
  if (doStop)
    doStop = 0;
  else
    setTimeout("mainLoop()",delay);
}

function launchArkanoid(forceMozilla) {
  if (!isMozilla && !forceMozilla) {
    window.open('/arkanoid/arkanoid.html','schillmaniaDHTMLArkanoid','width=468,height=500,toolbar=no,status=0');
  }
  else {
    window.open('/arkanoid/index-mozilla.html','schillmaniaDHTMLArkanoid','width=495,height=500,toolbar=no,status=1');
  }
  pauseAnimation();
}

function showSection(s) {
  for (var i=0; i<cSections.length; i++) {
    cSections[i].style.display = (i!=s?'none':'block');
  }
}

window.onload = init;