/******************************************
         Exploding fireworks script
 Written by Mark Wilton-Jones, 24-25/5/2002
*******************************************

Please see http://www.howtocreate.co.uk/jslibs/ for details and a demo of this script
Please see http://www.howtocreate.co.uk/jslibs/termsOfUse.html for terms of use

To use this script, simply put the line:
<script type="text/javascript" language="javascript1.2" src="PATH TO SCRIPT/fireworks.js"></script>
just before the </body> tag. The body of your document must be a dark colour. That's all!

*/

var rocketFire, rocketUpSpd, glitHSpd = [], glitVSpd = [];
function getRefToGlitter( divID, oDoc ) {
	if( !oDoc ) { oDoc = document; }
	if( document.layers ) {
		if( oDoc.layers[divID] ) { return oDoc.layers[divID]; } else {
			for( var x = 0, y; !y && x < oDoc.layers.length; x++ ) {
				y = getRefToDivNest(divID,oDoc.layers[x].document); }
			return y; } }
	if( document.getElementById ) { return document.getElementById(divID); }
	if( document.all ) { return document.all[divID]; }
	return document[divID];
}
//give a random glitter colour
//This odd looking line creates an array using the [array contents] syntax and in the same line randomly selects a cell
function getBlastCol() { return ['#f49f98','#faf94e','#87f95b','#68f8de','#68fc68','#d174f6','#f674b6'][Math.round( Math.random() * 6 )]; }
function explodeRocket( resetAtHeight ) {
  //glitter progresses outwards
  for( var glitx = 0; glitx < 20; glitx++ ) {
    var glitPiece = getRefToGlitter( 'fireGlit' + glitx );
    //change background colour to make glitter sparkle (not very well but better than nothing)
    if( glitPiece.style ) { glitPiece = glitPiece.style; if( glitPiece.backgroundColor ) {
    	glitPiece.backgroundColor = getBlastCol(); } else { glitPiece.background = getBlastCol(); }
    } else { glitPiece.bgColor = getBlastCol(); } var oPix = document.childNodes ? 'px' : 0;
    //advance glitter to next position unless too far, whereas stop and reset
    if( Math.round( 10 * glitVSpd[glitx] ) + parseInt( glitPiece.top ) >= resetAtHeight ) { resetRocket(); return; }
    glitPiece.top = ( Math.round( 10 * glitVSpd[glitx] ) + parseInt( glitPiece.top ) ) + oPix;
    glitPiece.left = ( Math.round( 10 * glitHSpd[glitx] ) + parseInt( glitPiece.left ) ) + oPix;
    //glitter should accelerate downwards until it reaches terminal velocity
    //glitter must not spread infinitely
    glitVSpd[glitx] += ( glitVSpd[glitx] < 2 ) ? 0.2 : 0; glitHSpd[glitx] *= 0.96;
  }
}
function shootRocket( glitMaxHeight, minGlitHeight ) {
  for( var glitx = 0; glitx < 20; glitx++ ) {
    //move upwards
    var glitPiece = getRefToGlitter( 'fireGlit' + glitx );
    if( glitPiece.style ) { glitPiece = glitPiece.style; }
    glitPiece.top = ( parseInt( glitPiece.top ) - Math.ceil( rocketUpSpd * ( minGlitHeight - glitMaxHeight ) ) ) + ( document.childNodes ? 'px' : 0 );
  }
  rocketUpSpd *= 0.91; //decelerate
  if( parseInt( glitPiece.top ) <= glitMaxHeight ) {
    //max height reached, time to explode
    window.clearInterval( rocketFire );
    rocketFire = window.setInterval('explodeRocket(' + minGlitHeight + ');',50);
  }
}
function resetRocket() {
  if( !getRefToGlitter( 'fireGlit19' ) ) { return; } //not loaded yet or not supported
  window.clearInterval( rocketFire );
  rocketUpSpd = 0.1;
  var myWidth = 0, myHeight = 0, myXoff = 0, myYoff = 0, myHeightToBang = 35 + Math.round( Math.random() * 120 );
  //get new screen settings to position rocket
	if( typeof( window.innerWidth ) == 'number' ) { myWidth = window.innerWidth; myHeight = window.innerHeight; } else {
		if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			myWidth = document.documentElement.clientWidth; myHeight = document.documentElement.clientHeight; } else {
			if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
				myWidth = document.body.clientWidth; myHeight = document.body.clientHeight; } } }
	if( typeof( window.pageYOffset ) == 'number' ) { myYoff = pageYOffset; myXoff = pageXOffset; } else {
		if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) { myYoff = document.body.scrollTop; myXoff = document.body.scrollLeft; } else {
			if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) { myYoff = document.documentElement.scrollTop; myXoff = document.documentElement.scrollLeft; } }
	}
  //position glitter and reset the color to white
  var myXStart = 185 + Math.round( Math.random() * ( myWidth - 370 ) ) + myXoff;
  for( var glitx = 0; glitx < 20; glitx++ ) {
    var glitPiece = getRefToGlitter( 'fireGlit' + glitx );
    if( glitPiece.style ) { glitPiece = glitPiece.style; if( glitPiece.backgroundColor ) {
 			glitPiece.backgroundColor = '#ffffff'; } else { glitPiece.background = '#ffffff'; }
   	} else { glitPiece.bgColor = '#ffffff'; } var oPix = document.childNodes ? 'px' : 0;
    glitPiece.top = ( ( myYoff + myHeight ) - 20 ) + oPix; glitPiece.left = myXStart + oPix;
    //set up the glitter to fly in not quite a circle - for realism
    glitHSpd[glitx] = Math.cos( ( 2 * Math.PI * glitx ) / 20 ) + ( 0.1 * Math.random() );
    glitVSpd[glitx] = Math.sin( ( 2 * Math.PI * glitx ) / 20 ) + ( 0.1 * Math.random() );
  }
  rocketFire = window.setInterval('shootRocket(' + ( myHeightToBang + myYoff ) + ',' + ( ( myYoff + myHeight ) - 20 ) + ');',50);
}
for( var glitx = 0; glitx < 20; glitx++ ) { //create the glitter using tiny squares of background colour
  if( document.layers ) { //relieve NS4 bug
    document.write('<layer top="0" left="0" height="2" width="2" id="fireGlit'+glitx+'" bgcolor="#000000"></layer>');
  } else {
    document.write('<div style="position:absolute;top:0px;left:0px;height:2px;width:2px;clip:rect(0px 2px 2px 0px);font-family:sans-serif;font-size:7px;background-color:#000000;" id="fireGlit'+glitx+'"></div>');
  }
}
rocketFire = window.setInterval('resetRocket();',100);