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 delay = navigator.appVersion.toLowerCase().indexOf('windows nt')>=0?20:2;
var doStop = 0;
var snowLimit = isMozilla?16:32;
var b = 'image/';
var cSections = [];
var px = 'px';
var xmasLights = [];
var xmasLightsDoAnimate = 0;
var glassExplosions = [];
var glassYOffset = 0;
var flash = null;
var smashCounter = 0;
var oFeatured = null;
var preO = [];
var preI = ['arkanoid_1','summer02_1','snowflake','nav_bg_1','feature_0.jpg','feature_1.jpg','feature_2.jpg','feature_3.jpg','glass/blue','glass/green','glass/none','glass/red','glass/white','glass/yellow'];
for (var i=0; i<preI.length; i++) {
  preO[i] = new Image();
  preO[i].src = 'image/' + preI[i] + (preI[i].indexOf('.jpg')>=0?'':'.gif');
}

function plusMinusOne() {
  return (Math.random()>0.5?1:-1);
}

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);
}

Glass.prototype.animate = function() {
  this.counter++;
  if (this.counter >= this.counterMax) {
    this.counter = 0;
    this.status = !this.status;
    this.o.src = 'image/glass/' + (this.status?this.type:'none')+'.gif';
    this.randomizeFlicker();
  }
  this.vX *= 0.33;
  this.o.style.left = parseInt(this.o.style.left) + this.vX + px;
  this.o.style.top = parseInt(this.o.style.top) + this.vY + px;
  if (parseInt(this.o.style.top) > midY + 4 - glassYOffset) {
    this.die();
    return 1;
  } else {
    return 0;
  }
}

Glass.prototype.die = function() {
  this.o.style.top = (midY+5-glassYOffset) + px;
  this.active = 0;
}

Glass.prototype.randomizeFlicker = function() {
  this.counterMax = 1+parseInt(Math.random()*10);
}

function Glass(x,y,vX,vY,type,ciao) {
  this.active = 1;
  this.counter = 0;
  this.status = 1;
  this.randomizeFlicker();
  this.x = parseInt(x + ((Math.random()*5)*plusMinusOne()));
  this.y = parseInt(y + ((Math.random()*5)*plusMinusOne()));
  this.vX = vX;
  this.vY = vY;
  this.type = type;
  this.o = document.createElement('img');
  with (this.o.style) {
    display = 'block';
    position = 'absolute';
    left = this.x + px;
    top = this.y + px;
    width = height = 2 + px;
  }  
  this.o.src = 'image/glass/'+this.type+'.gif';
  oS.appendChild(this.o);
}

GlassExplosion.prototype.animate = function() {
  if (this.active) {
    for (this.i=0; this.i<this.glass.length; this.i++) {
      this.dead += this.glass[this.i].animate();
    }
    if (this.dead >= this.glass.length-1) this.active = 0;
  }
}

function GlassExplosion(x,y,type,ciao,bulbHeight) {
  this.active = 1;
  this.dead = 0;
  this.glass = [];
  this.n = ciao?15:5;
  for (this.i=0; this.i<this.n; this.i++) {
    this.glass[this.i] = new Glass(x,y+parseInt(Math.random()*(parseInt(bulbHeight))),-20+(80*this.i/this.n),4,type,ciao);
  }
  this.sine = Math.random()*3;
}

ChristmasLight.prototype.smash = function() {
  if (this.state < 2) {
    this.setState(2);
    setTimeout("xmasLights["+this.index+"].setState(3)",20);
    glassExplosions[glassExplosions.length] = new GlassExplosion(this.x+(parseInt(this.o.offsetWidth)/2),this.y,this.type,this.index>=15?1:0,this.o.offsetHeight);
    playSound('smash'+parseInt(Math.random()*6));
    this.o.className = 'noButton';
    smashCounter++;
    if (smashCounter >= xmasLights.length) {
      document.getElementById('xmasReset').style.display = 'block';
    }
  }
}

ChristmasLight.prototype.setState = function(s) {
  this.state = s;
  this.o.src = 'image/lights/'+this.index+'_'+this.state+'.gif';
}

ChristmasLight.prototype.animate = function() {
  if (this.state < 2) {
    this.counter++;
    if (this.counter >= this.counterMax) {
      this.counter = 0;
      this.setState(!this.state?1:0);
    }
  }
}

ChristmasLight.prototype.preload = function() {
  this.preI = [];
  for (this.i=0; this.i<4; this.i++) {
    this.preI[this.i] = new Image();
    this.preI[this.i].src = 'image/lights/' + this.index + '_' + this.i + '.gif';
  }
}

ChristmasLight.prototype.reset = function() {
  this.setState(Math.random()>0.5?1:0);
  this.o.onclick = function() { xmasLights[this.index].smash() }
  this.o.className = 'button';
}

function ChristmasLight(x,y,i,type,w,h) {
  this.x = x;
  this.y = y;
  this.index = i;
  this.type = type;
  this.counter = 0;
  this.counterMax = parseInt(Math.floor(Math.random()*10));
  this.o = document.createElement('img');
  this.o.index = this.index;
  with (this.o.style) {
    display = 'block';
    position = 'absolute';
    left = x + px;
    top = y + px;
    if (w && h) {
      width = w;
      height = h;
    }
  }
  this.preload();
  this.reset();
  oS.appendChild(this.o);
}

function init() {
  oFeature = document.getElementById('featureImage');
  cSections = [document.getElementById('cMain'),document.getElementById('cAbout'),document.getElementById('cNotes'),document.getElementById('cCredits'),document.getElementById('cInspiration')];
  window.onresize = getClientCoords;
/*
  if (!isMozilla) {
    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';
  }
*/
  initFlash();
  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';
  xmasLights = [
   new ChristmasLight(57,153,0,'red',10,10),
   new ChristmasLight(92,151,1,'white',8,11),
   new ChristmasLight(110,151,2,'yellow',12,10),
   new ChristmasLight(81,154,3,'yellow',9,12),
   new ChristmasLight(70,165,4,'blue',8,12),
   new ChristmasLight(114,162,5,'green',9,12),
   new ChristmasLight(129,165,6,'white',9,11),
   new ChristmasLight(58,175,7,'green',8,11),
   new ChristmasLight(90,179,8,'green',11,11),
   new ChristmasLight(120,177,9,'blue',8,11),
   new ChristmasLight(76,181,10,'red',8,12),
   new ChristmasLight(130,187,11,'red'),
   new ChristmasLight(88,202,12,'yellow'),
   new ChristmasLight(109,203,13,'white'),
   new ChristmasLight(104,214,14,'blue')
  ];
  for (var i=0; i<snowLimit; i++) {
    setTimeout("snowSomeMore()",delay*(i*10));
  }
  resumeAnimation();
}

function initFlash() {
  document.getElementById('flashContainer').innerHTML = '<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0" id="flashSFX" WIDTH="1" HEIGHT="1"><PARAM NAME=movie VALUE="xmaslights.swf"><PARAM NAME=quality VALUE=high></OBJECT>'
  flash = document.getElementById('flashSFX');
}

function pauseAnimation() {
  doStop = 1;
  document.getElementById('rAni').style.display = 'block';
}

function resumeAnimation() {
  mainLoop();
  xmasLightsAnimate();
}

function xmasLightsAnimate() {
  for (var i=0; i<xmasLights.length; i++) {
    xmasLights[i].animate();
  }
  xmasLightsDoAnimate = 1;
}

function xmasLightsReset() {
  for (var i=0; i<xmasLights.length; i++) {
    if (!xmasLights[i].active) {
      xmasLights[i].reset();
    }
  }
  document.getElementById('xmasReset').style.display = 'none';
  smashCounter = 0;
  initFlash();
}

function snowSomeMore() {
  snowFlakes[snowFlakes.length] = new SnowFlake(parseInt(oC.style.left)+50-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 (var 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 = 250;
  doPositioning();
}

function mainLoop() {
  for (var i=0; i<snowFlakes.length; i++) {
    if (snowFlakes[i].active) snowFlakes[i].animate();
  }
  if (doStop)
    doStop = 0;
  else {
    setTimeout("mainLoop()",delay);
    if (xmasLightsDoAnimate) {
      xmasLightsDoAnimate = 0;
      setTimeout("xmasLightsAnimate()",1000+parseInt(Math.floor(Math.random()*500)));
    }
    for (var i=0; i<glassExplosions.length; i++) {
      if (glassExplosions[i].active) {
        glassExplosions[i].animate();
      }
    }
  }
}

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');
  }
}

var tmp = '';
function asdf() {
  if (!event) return false;
  var c = String.fromCharCode(event.keyCode);
  tmp += c;
  if (tmp.indexOf('boom')>=0) {
    boom();
    tmp = '';
  } else if (tmp.indexOf('ciao')>=0) {
    ciao();
    tmp = '';
  }
}

var boomCounter = 0;
var boomIndex = 0;

function boom() {
  if (boomCounter < xmasLights.length-1) {
    do {
      boomIndex = parseInt(Math.random()*xmasLights.length);
    } while (!xmasLights[boomIndex].state > 2);
    xmasLights[boomIndex].smash();
    setTimeout("boom()",20+Math.random()*180);
  }
}

function ciao() {
  // quick hack :)
  var c = document.getElementById('elCiao');
  if (c.style.display != 'block') {
    c.style.display = 'block';
    // ciao! lights
    var i = 15;
     xmasLights[i++] = new ChristmasLight(38,3,15,'white',22,31);
     xmasLights[i++] = new ChristmasLight(331,18,16,'green',33,19);
     xmasLights[i++] = new ChristmasLight(86,28,17,'green',17,31);
     xmasLights[i++] = new ChristmasLight(41,57,18,'blue',30,23);
     xmasLights[i++] = new ChristmasLight(183,35,19,'green',18,32);
     xmasLights[i++] = new ChristmasLight(208,31,20,'yellow',17,31);
     xmasLights[i++] = new ChristmasLight(229,42,21,'green',25,28);
     xmasLights[i++] = new ChristmasLight(318,42,22,'red',22,30);
     xmasLights[i++] = new ChristmasLight(370,59,23,'blue',32,21);
     xmasLights[i++] = new ChristmasLight(-10,80,24,'red',31,17);
     xmasLights[i++] = new ChristmasLight(112,82,25,'blue',32,17);
     xmasLights[i++] = new ChristmasLight(156,84,26,'red',32,19);
     xmasLights[i++] = new ChristmasLight(45,97,27,'yellow',28,28);
  }
}

function playSound(audioTarget,volume) {
  if (!volume && volume != 0) volume = -1;
  if (flash.ReadyState == 4) {
    flash.TGotoLabel("/" + audioTarget, "start")
    flash.TPlay("/" + audioTarget)
    if (volume != -1) playFrame(audioTarget,'v'+volume);
    else playFrame(audioTarget,'v50');
  }
}

function playFrame(audioTarget, label) {
  if (flash.ReadyState == 4) flash.TGotoLabel("/"+audioTarget,label);
}

function showFeature(n) {
  if (n == -1) {
    if (oFeature.style.display != 'none') oFeature.style.display = 'none';
  } else {
    if (oFeature.style.display != 'block') oFeature.style.display = 'block';
    oFeature.src = 'image/featured_'+n+'.jpg';
  }
}

function mozCheck() {
  if (isMozilla) {
    alert('Requires IE. (Sorry).');
    return false;
  } else {
    return true;
  }
}

if (!isMozilla) document.onkeypress = asdf;
window.onload = init;