// JavaScript Document

/* Add to body:

< b o d y onLoad="LCBC_setupSnowflakes();">

*/

/**************************************************************************************************/
/* Configuration Constants																		  */
/**************************************************************************************************/

// Adjust to taste.
var ANIMATION_DISTANCE = 10; //px
var ANIMATION_INTERVAL = 100; //ms
var INTERVAL_BETWEEN_FLAKES = 2000; //ms
var MAX_SNOWFLAKES = 10; //snowflakes!
var SOURCE_FILE = "GRAPHICS/flake2-20ph.gif";
var SOURCE_FILE_HEIGHT = "20px";
var SOURCE_FILE_WIDTH = "20px";
var X_RANGE = 20; //px

// Should not require adjustment.
var BOUNDARY_WIDTH = 30; //px
var BOUNDARY_HEIGHT = 30; //px
var WINDOW_HEIGHT_MAX = 800; //px
var WINDOW_WIDTH_MAX = 1200; //px

/**************************************************************************************************/
/* Global Variables																				  */
/**************************************************************************************************/

var i = 0;
var mouse_x = 0; //px
var mouse_y = 0; //px
var snowflake = new Array();
var snowfield_height = 0; //px
var snowfield_width = 0; //px
var source_file = "";
var new_snowflakes, move_snowflakes; // interval ids
var snow_is_falling;

var homedir = ""; /* Must be defined outside function to stop IE complaining. */

/**************************************************************************************************/
/* Object Constructor Functions																	  */
/**************************************************************************************************/

function Snowflake( id, x, y)
	{
	// Create browser object - an image of a snowflake. 
	this.image = document.createElement("img");
	
	// Set browser object properties. 
	this.image.id = 			id;
	this.image.src = 			source_file;
	this.image.style.width =	SOURCE_FILE_WIDTH;
	this.image.style.height =	SOURCE_FILE_HEIGHT;
	this.image.style.position =	"absolute";
	this.image.style.left =		x + "px";
	this.image.style.top =		y + "px";
	this.image.style.zIndex =		"0";
	}

/**************************************************************************************************/
/* Event Handler Functions																		  */
/**************************************************************************************************/

// Check
function LCBC_checkSnowflakes()
	{
	// Check for snow cookie 
	//window.alert( "checking cookie");
	var all_cookies = document.cookie;
	
	var snow_cookie = all_cookies.indexOf("snow=");
	if( snow_cookie == -1)
		{
		// No snow cookie, so start snowing
		LCBC_setupSnowflakes();
		}
	else
		{
		// Read snow cookie
		var snow_cookie_val_start = snow_cookie + 5;
		var snow_cookie_val_end = all_cookies.indexOf( ";", snow_cookie_val_start);
		if( snow_cookie_val_end == -1) snow_cookie_val_end = all_cookies.length;
		var snow_cookie_value = all_cookies.substring( snow_cookie_val_start, snow_cookie_val_end);
		snow_cookie_value = unescape( snow_cookie_value);
		
		if( snow_cookie_value == "true") 
			{
			LCBC_setupSnowflakes();
			}
		}
	}

// Setup & start
function LCBC_setupSnowflakes()
	{
	snow_is_falling = true;
	
	// Set snow cookie 
	//window.alert( "setting cookie");
	var cookie_expiry_date = new Date();
	cookie_expiry_date.setFullYear( cookie_expiry_date.getFullYear() + 1 );
/*	document.cookie = 
		"snow=true" +
		"; expires=" + cookie_expiry_date.toGMTString() +
		"; path=/" +
		"; domain=creativeretailsolutions.co.uk";*/
	document.cookie = 
		"snow=true" +
		"; expires=" + cookie_expiry_date.toGMTString() +
		"; path=/";
	
	/* Get home directory */
	var homelink_obj =		ML_findObj( "homelink");
	var homelink =			homelink_obj.href;
	var homedir_length =	homelink.lastIndexOf( "index.htm");
	homedir =				homelink.substr( 0, homedir_length);
	
	source_file = homedir + SOURCE_FILE;
	
	// Force variables to 0.
	i = 0;
	snowflake = new Array( 0);
	
	// Start straight away.
	updateSnowfield(); 
	createNewSnowflake();
	moveSnowflakes();
	
	new_snowflakes = setInterval( 'createNewSnowflake();', INTERVAL_BETWEEN_FLAKES); 
	move_snowflakes = setInterval( 'updateSnowfield(); moveSnowflakes();', ANIMATION_INTERVAL);	
	}

// Stop
function LCBC_stopSnowing()
	{
	snow_is_falling = false;

	// Set snow cookie 
	//window.alert( "setting cookie");
	var cookie_expiry_date = new Date();
	cookie_expiry_date.setFullYear( cookie_expiry_date.getFullYear() + 1 );
/*	document.cookie = 
		"snow=false" +
		"; expires=" + cookie_expiry_date.toGMTString() +
		"; path=/" +
		"; domain=creativeretailsolutions.co.uk";*/
	document.cookie = 
		"snow=false" +
		"; expires=" + cookie_expiry_date.toGMTString() +
		"; path=/";
	
	clearInterval( new_snowflakes);
	clearInterval( move_snowflakes);
	
	for( var j=0; j < snowflake.length; j++)
		{
		if( document.getElementById( snowflake[j].image.id ))	document.body.removeChild( snowflake[j].image );
		}
	}
	
// Get mouse coordinates 

function getMouseCoordsIE()
	{
	mouse_x = event.clientX + 
		(document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
	mouse_y = event.clientY + 
		(document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);	
	}

function getMouseCoordsNN(e)
	{
	mouse_x = e.pageX;
	mouse_y = e.pageY;
	}

// Create snowflakes
function createNewSnowflake()
	{
	var snowflake_id = "snowflake" + i;

	if( document.getElementById( snowflake_id ))	document.body.removeChild( snowflake[i].image );
	snowflake[i] = new Snowflake( snowflake_id, validX( mouse_x), validY( mouse_y) );
	document.body.appendChild( snowflake[i].image);
	
	i++;
	if( i > MAX_SNOWFLAKES) i = 0;
	}

// Move snowflakes
function moveSnowflakes()
	{
	for( var j=0; j < snowflake.length; j++)
		{
		var snowflake_obj = document.getElementById( snowflake[j].image.id );
		
		var current_x = parseInt( snowflake_obj.style.left);
		var current_y = parseInt( snowflake_obj.style.top);
		
		if( current_y < snowfield_height )
			{
			snowflake_obj.style.left = validX( current_x + parseInt( (Math.random() - 0.5) * X_RANGE) ) + "px";
			snowflake_obj.style.top = validY( current_y + ANIMATION_DISTANCE) + "px";
			}
		}
	}

// Update snowfield.

// Since there's no way to detect scroll changes, and scroll dimensions are crucial to snowfield, 
// update the snowfield every time animation is required.
function updateSnowfield()
	{
	snowfield_width = windowWidth() + scrollWidth() - BOUNDARY_WIDTH;
	snowfield_height = windowHeight() + scrollHeight() - BOUNDARY_HEIGHT;
	
	// Sweep up stray snow
	for( var j=0; j < snowflake.length; j++)
		{
		// Test for existence of element
		if( document.getElementById( snowflake[j].image.id ))
			{
			var snowflake_obj = document.getElementById( snowflake[j].image.id );
			
			var current_x = parseInt( snowflake_obj.style.left);
			var current_y = parseInt( snowflake_obj.style.top);
			
			if( current_x > snowfield_width )	snowflake_obj.style.left = snowfield_width + "px";
			if( current_y > snowfield_height )	snowflake_obj.style.top = snowfield_height + "px";
			}
		}
	}
	
/**************************************************************************************************/
/* Event Handlers 																				  */
/**************************************************************************************************/

// Get mouse moves
if( document.all)	document.onmousemove=getMouseCoordsIE;
else				document.onmousemove=getMouseCoordsNN;

// Get changes to window
// Not really necessary as changes are calculated on each animation.
//window.onresize = updateSnowfield;

/**************************************************************************************************/
/* Functions	 																				  */
/**************************************************************************************************/

function validX( x)
	{
	return Math.max( 0, Math.min( x, snowfield_width));
	}

function validY( y)
	{
	return Math.max( 0, Math.min( y, snowfield_height));
	}

// Following 4 functions adapted from alertSize() and getScrollXY() at
// http://www.howtocreate.co.uk/tutorials/index.php?tut=0&part=16.

function scrollHeight() 
	{
	// Netscape compliant
  	if( typeof( window.pageYOffset ) == 'number' ) 
		return window.pageYOffset;
	// DOM compliant
	else if( document.body && document.body.scrollTop ) 
		return document.body.scrollTop;
	// IE6 standards compliant mode
	else if( document.documentElement && document.documentElement.scrollTop )
		return document.documentElement.scrollTop;
	// Default
	return 0;
	}

function scrollWidth() 
	{
	// Netscape compliant
  	if( typeof( window.pageXOffset ) == 'number' ) 
		return window.pageXOffset;
	// DOM compliant
	else if( document.body && document.body.scrollLeft ) 
		return document.body.scrollLeft;
	// IE6 standards compliant mode
	else if( document.documentElement && document.documentElement.scrollLeft )
		return document.documentElement.scrollLeft;
	// Default
	return 0;
	}

function windowHeight() 
	{
	// Non-IE
  	if( typeof( window.innerHeight ) == 'number' ) 
		return window.innerHeight;
	// IE 6+ in 'standards compliant mode'
	else if( document.documentElement && document.documentElement.clientHeight )
		return document.documentElement.clientHeight;
	// IE 4 compatible
	else if( document.body && document.body.clientHeight ) 
		return document.body.clientHeight;
	// Default
	return WINDOW_HEIGHT_MAX;
	}

function windowWidth() 
	{
	// Non-IE
  	if( typeof( window.innerWidth ) == 'number' ) 
		return window.innerWidth;
	// IE 6+ in 'standards compliant mode'
	else if( document.documentElement && document.documentElement.clientWidth )
		return document.documentElement.clientWidth;
	// IE 4 compatible
	else if( document.body && document.body.clientWidth ) 
		return document.body.clientWidth;
	// Default
	return WINDOW_WIDTH_MAX;
	}

/**************************************************************************************************/