/*
** =======================================================
** Copyright © 2006 Beware of Design. All rights reserved. 
** -------------------------------------------------------
** Site:     www.bewareofdesign.com
** File:     src_rain.css
** Purpose:  Display rain in a region.
** Author:   Greg Gurr
**
** Acknowledgements:
**   Portions of this code are from Snowmaker
**   Copyright (c) 2003 Peter Gehrig. All rights reserved.
**   http://www.24fun.com info@24fun.com
**
** -------------------------------------------------------
** 2006-Nov-10 Created
** 2006-Nov-11 Added .countdown to have drop's initial
**             appearance occur randomly from the top.
** 2006-Dec-31 Repurposed Snowmaker as a Rainmaker
**             Req'd ofs_h*3 bottom edge detector.
** =======================================================
*/

/*
** For raindrop colours and fonts, provide a list of as many as you'd like.
*/
var rainfnum = 35  // number of raindrops (recommended: <= 40)
var rainchar = "'" // raindrop character (recommended: *)
var rainsink = 6   // rainfall speed (recommended: 0.3 to 2)
var rainfmin = 8   // minimum raindrop size (pixels)
var rainfmax = 14  // maximum raindrop size (pixels)
var rainmsec = 50  // timer period between drop movement recalcs (msec)
var rainfcol = new Array('#aaa','#888','#666') // rain colors (in CSS RGB shorthand)
var rainfont = new Array('Arial Black','Arial Narrow','Times','Comic Sans MS','Verdana','Georgia','Trebuchet MS')

var div_w // rain region width
var div_h // rain region height
var ofs_x // optional inset x adjustment within rain region
var ofs_y // optional inset y adjustment within rain region
var rain = new Array() // array of raindrops
var x_mv = new Array() // some magic x movement values
var crds = new Array() // some magic x movement values
var ltrt = new Array() // some magic left/right adjustments

function randint(range) {		
  return Math.floor(range*Math.random())
}

/*
** p - parent container (DIV) for rain
** e - image element that rain covers (resides within p)
** ins_x see ofs_x
** ins_y see ofs_y
**
** Note: Call only once because DIV tags are created (but never destroyed)
*/
function raininit(p, e, ins_x, ins_y) {
  var i, drop, dropstyle, img_e = elm(e)
  div_w = img_e.width
  div_h = img_e.height
  ofs_x = ins_x
  ofs_y = ins_y
  for (i=0 ; i < rainfnum ; ++i) {
    rain[i] = elm(p).appendChild(document.createElement('DIV'))

    drop = rain[i]
    drop.id = 's'+i
    drop.className = 'drop'
    wrt(drop, rainchar)
    dropstyle = drop.style

    crds[i] = 0
    ltrt[i] = Math.random()* 15
    x_mv[i] = 0.03 + (Math.random() / 10)
    dropstyle.fontFamily = rainfont[randint(rainfont.length)]
    dropstyle.color = rainfcol[randint(rainfcol.length)]
    drop.size = randint(rainfmax-rainfmin) + rainfmin
    dropstyle.fontSize =   drop.size + 'px'
    dropstyle.lineHeight = dropstyle.fontSize
    drop.sink = rainsink * drop.size / 5
    drop.posx = ofs_x + randint(div_w-(2*ofs_x)-drop.offsetWidth)
    drop.posy = 0
    dropstyle.left = drop.posx + 'px' // units very important for Netscape
    dropstyle.top  = drop.posy + 'px' //
    drop.countdown  = randint(div_h)
  }
  rainfall()
}

function rainfall() {
  var i, drop, dropstyle
  for (i=0 ; i < rainfnum ; ++i) {
    drop = rain[i]
    dropstyle = drop.style

    crds[i] += x_mv[i]
    drop.posy += drop.sink
    if (drop.posy < ofs_y) drop.posy = ofs_y // ensure initial minimum y

    dropstyle.left = (drop.posx + (ltrt[i] * Math.sin(crds[i]))) + 'px' // don't forget units
    dropstyle.top  = drop.posy + 'px'                                   //

    if (drop.countdown >= 0) { // don't display drop initially until countdown expires
      drop.countdown -= drop.sink
      if (drop.countdown <= 0) drop.posy = 0
    } else {
      if ((parseInt(dropstyle.left) + drop.offsetWidth) > (div_w-ofs_x)) {
	dropstyle.visibility='hidden'
      } else {
	dropstyle.visibility = (parseInt(dropstyle.left) < ofs_x) ? 'hidden' : 'visible'
      }
    }

    if ((parseInt(dropstyle.top) + (drop.offsetHeight*0.6)) > (div_h-(ofs_y*3))) {
      drop.posx = randint(div_w)
      drop.posy = 0
    }
  }
  setTimeout('rainfall()', rainmsec)
}

// Start the rain blower...
//
function doInit() { 
  raininit('bod-seasonal', 'img-seasonal', 14, 9)
}

window.onload = doInit;
