//window.onDomReady = DomReady;
function DomReady(fn)
{
	if(document.addEventListener){document.addEventListener("DOMContentLoaded", fn, false);} // W3C
	else {document.onreadystatechange = function(){readyState(fn)}}	// IE
}
function readyState(fn){if(document.readyState == "interactive"){fn();}}


roze1 = new Image();
roze1.src = 'images/galleries/flowers/rose1_1.png';
roze2 = new Image();
roze2.src = 'images/galleries/flowers/rose2_1.png';

var maxRoses = 3;
var rFrame = Array();
var rUp = Array();
var rlastX = Array();
var rlastY = Array();
var rlastXv = Array();
var rWhich = Array();
var winWidth = 800;
var winHeight = 600;

function CreateRoseDivs(divID)
{
	var divTag = document.createElement("div");
	divTag.id = divID;
	divTag.className ="ani_rose";
	divTag.style.width = 50;
	divTag.style.height = 50;
	document.body.appendChild(divTag);
}
function InitRoses()
{
	for (i=0; i<maxRoses; i++)
	{
		CreateRoseDivs('rose' + i);
	}
	
	if(typeof(window.innerWidth) == 'number'){winWidth = window.innerWidth;winHeight = window.innerHeight;}
	else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)){winWidth = document.documentElement.clientWidth;winHeight = document.documentElement.clientHeight;}
	else if(document.body && (document.body.clientWidth || document.body.clientHeight)){winWidth = document.body.clientWidth;winHeight = document.body.clientHeight;}
	if (winHeight < 400){winHeight = 400;} // can happen due to timing with onDomLoad. Corrects itself eventually
	for (i = 0; i < maxRoses; i++){InitRose(i);}
	Roses();
}
function InitRose(idx)
{
	rFrame[idx] = 1;
	if (idx/2 == Math.floor(idx/2)){rUp[idx]=true;}else{rUp[idx]=false;}
	rWhich[idx] = 2 - Math.floor(Math.random() * 2);			// value is either 1 or 2, to select which rose is being displayed
	rlastX[idx] = Math.floor(Math.random() * (winWidth - roze1.width));
	rlastY[idx] = -300 + Math.floor(Math.random() * 300);		// try to space them out by setting the start points wildly apart
	rlastXv[idx] = 0;
	
	var aRose = document.getElementById('rose'+i);
	if (aRose)
	{
		var tagg = '<img src="images/galleries/flowers/rose' + rWhich[i] + '_1.png" alt=""/>';
		aRose.innerHTML = tagg;
	}
}
function Roses()
{
	for (i = 0; i < maxRoses; i++)
	{
		rlastXv[i] += (-6 + (Math.floor(Math.random()*13)));	// wavering left-to-right 
		if (rlastXv[i] > 30){rlastXv[i] = 30;}
		if (rlastXv[i] < -30){rlastXv[i] = -30;}
		
		rlastX[i] += rlastXv[i];
		if (rlastX[i] < 0){rlastX[i] = 0;}
		if (rlastX[i] > (winWidth - roze2.width - 90)){rlastX[i] = winWidth - roze2.width - 90;}
		rlastY[i] += (-2 + Math.floor(Math.random()*26));		// amount to "fall" each frame
		if (rlastY[i] > document.body.clientHeight + roze1.height + 5){InitRose(i);}

		var aRose = document.getElementById('rose'+i);
		if (aRose)
		{
			aRose.style.top = rlastY[i] + 'px';
			aRose.style.left = rlastX[i] + 'px';
		}
	}
	setTimeout('Roses()', 200);
}
window.onDomReady(InitRoses);
