// This file makes it possible to dynamically add various javascript
// to the page without crowding my html

// dynamically load a javascript file
function load_script(url) {
	var e = document.createElement("script");
	e.src = url;
	e.type = "text/javascript";
	document.getElementsByTagName("head")[0].appendChild(e);
}

// dynamically load a css file
function load_css(url) {
	var e = document.createElement("link");
	e.href = url;
	e.type = "text/css";
	e.rel = "stylesheet";
	e.media = "screen";
	document.getElementsByTagName("head")[0].appendChild(e);
}

// dynamically add functions to the window.onload event
function addOnLoad(fn) {
	if (window.addEventListener) { // W3C standard
	  window.addEventListener('load', fn, false); // NB **not** 'onload'
	}
	else if (window.attachEvent) { // Microsoft
	  window.attachEvent('onload', fn);
	}
}

// create a random integer
function randomInt(min, max) {
	min = parseInt(min);
	max = parseInt(max);
	return Math.floor(Math.random( ) * (max - min)) + min;
}

// add some bling to the page on various dates
addOnLoad(function( ) {
	var date = new Date;
	var month = date.getMonth( ) + 1; // +1 due to index 0
	var day = date.getDate( );

	var monthDay = month+':'+day;

	// we only want snow between october and march
	// and cancel the snow on new year's eve and day for fireworks
	if ((10 <= month || 3 >= month)/* && ! ("12:31" == monthDay || "1:1" == monthDay)*/) {
		load_script("scripts/snow/storm.js");
	}

	// we only want fireworks on new year's (and new year's eve), and the 4th and 24th of july
	if ("12:31" == monthDay || "1:1" == monthDay || "7:4" == monthDay || "7:24" == monthDay) {
		load_css("scripts/fireworks/fireworks.css");
		load_script("http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js");
		load_script("scripts/fireworks/build_html.js");
		load_script("scripts/fireworks/fireworks.js");

		// launch a firework
		// (needs to be global for setTimeout to work)
		do_boom = function( ) {
			createFirework(randomInt(10, 50),randomInt(20, 100),randomInt(2, 5),null,null,null,null,null,true,true);
			setTimeout('do_boom( );', randomInt(250, 2000));
		}

		// because we've already passed the window.onload
		// event to get here, fire fc.init manually

		// give the script time to load...
		setTimeout('fc.init( ); do_boom( );', 500);
	}
});
