/**	Wheather system.
*
*	@author Dinu Florin.
*/

/** Rain class */
function Rain()
{
	var stropi = new Array();
	var ww = getWindowWidth();
	var wh = getWindowHeight();

	this.initialize = function ()
	{
		for(var i = 0; i<70; i++)
		{
			var strop = document.createElement('div');
			strop.className = "HideFromPrint";
			if(navigator.userAgent.indexOf('IE') != -1)
			{
				strop.innerHTML = '<img style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+LINK_BASE+'images/weather/raindrop.png\');" src="'+LINK_BASE+'blank.png" alt=" ">'
			}
			else
			{
				strop.innerHTML = '<img src="'+LINK_BASE+'images/weather/raindrop.png" alt=" ">';
			}
			strop.style.position = 'absolute';

			strop.Y = Math.round(wh*Math.random());
			strop.X = Math.round(ww*Math.random());
			strop.style.top = strop.Y + 'px';
			strop.style.left = strop.X + 'px';

			stropi[i] = strop;
			document.getElementById('Body').appendChild(strop);
		}
		window.setInterval(timer, 80);
	}
	function timer()
	{
		try {
			for(var i in stropi)
			{
				stropi[i].Y += 12;

				if(stropi[i].Y > wh)
				{
					stropi[i].Y = 0;
					stropi[i].X = ww*Math.random();
				}
				stropi[i].style.top = stropi[i].Y + 'px';
				stropi[i].style.left = stropi[i].X + 'px';
			}
		} catch(e) {}
	}
}
/** Snow class */
function Snow()
{
	var fulgi = new Array();
	var ww = getWindowWidth();
	var wh = getWindowHeight();

	this.initialize = function ()
	{
		for(var i = 0; i<70; i++)
		{
			var fulg = document.createElement('div');
			fulg.className = "HideFromPrint";
			if(navigator.userAgent.indexOf('IE') != -1)
			{
				fulg.innerHTML = '<img style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+LINK_BASE+'images/weather/snowflake.png\');" src="'+LINK_BASE+'blank.png" alt=" ">'
			}
			else
			{
				fulg.innerHTML = '<img src="'+LINK_BASE+'images/weather/snowflake.png" alt=" ">';
			}
			fulg.style.position = 'absolute';

			fulg.Y = Math.round(wh*Math.random());
			fulg.X = Math.round(ww*Math.random());
			fulg.style.top = fulg.Y + 'px';
			fulg.style.left = fulg.X + 'px';

			fulgi[i] = fulg;
			document.getElementById('Body').appendChild(fulg);
		}
		window.setInterval(timer, 100);
	}
	function timer()
	{
		try {
			for(var i in fulgi)
			{
				fulgi[i].Y += 4;
				fulgi[i].X += Math.round(2-Math.random()*4);

				if(fulgi[i].Y > wh)
				{
					fulgi[i].Y = 0;
					fulgi[i].X = ww*Math.random();
				}
				fulgi[i].style.top = fulgi[i].Y + 'px';
				fulgi[i].style.left = fulgi[i].X + 'px';
			}
		} catch(e) {}
	}
}
/** Make a date object from an unix timestamp
*
*	@ts integer, thimestamp in seconds
*	@return Date, a date object
*/
function makeDateFromStamp(ts)
{
	var date = new Date(ts*1000);
	var offset = -date.getTimezoneOffset()/60 - 2;

	date.setHours(date.getHours() - offset);
	return date;
}

/** Get a date object set gmt time, it sinchronizes with the server
*
*	@return Date, a date object
*/
function getGMTDate()
{
	var date = new Date();
	ajax = new AjaxQuery();
	ajax.addEvent("loaded", function (ret)
			{
				date = makeDateFromStamp(ret);
			});
	ajax.load(LINK_BASE+"ajax_handlers/get_gmt_time.php", false);
	try {console.log(date.getTimezoneOffset()/60, date);} catch(e) {}
	return date;
}

/** Moon class */
function Moon()
{
	var sunrise = 0;
	var sunset = 0;
	this.x = 0;
	this.y = 0;
	this.width = 156;
	this.height = 156;
	var luna;

	/** Initialize the class.
	*
	*	@param sunr integer, sunrise time (unix tymestamp - number of seconds).
	*	@param suns integer, sunset time (unix tymestamp - number of seconds).
	*	@return void.
	*/
	this.initialize = function(sunr, suns)
	{
		sunrise =  makeDateFromStamp(sunr);
		sunset =  makeDateFromStamp(suns);

		luna = document.createElement('div');
		luna.className = "HideFromPrint";
		document.getElementById('Body').appendChild(luna);
		if(navigator.userAgent.indexOf('IE') != -1)
		{
			luna.innerHTML = '<img style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+LINK_BASE+'images/weather/moon.png\');" src="'+LINK_BASE+'blank.png" alt=" ">'
		}
		else
		{
			luna.innerHTML = '<img src="'+LINK_BASE+'images/weather/moon.png" alt=" ">';
		}
		luna.style.position = 'absolute';
		luna.classInstance = this;

		window.setInterval(timer, 60000);
		timer();
	}
	/* Set the x position of the moon */
	var setX = function(_x)
	{
		this.x = _x;
		luna.style.left = this.x + 'px';
	}
	/* Set the y position of the moon */
	var setY = function(_y)
	{
		if(_y < 210) {luna.style.visibility = "hidden"; return;}
		this.y = _y;
		luna.style.bottom = this.y + 'px';
	}
	var timer = function()
	{
		var date = getGMTDate();
		var h = date.getHours();
		var m = h*60 + date.getMinutes();
		var maxm = sunset.getHours()*60+sunset.getMinutes();
		var minm = sunrise.getHours()*60+sunrise.getMinutes();
		var offset = 0; /* corectie initiala cu PI/2*/

		var x = Math.round(getWindowWidth()/2 - luna.classInstance.width/2 + Math.cos((m*3.14)/(maxm-minm)+offset)*500);
		var y = Math.round(300 + Math.sin((m*3.14)/(maxm-minm)+offset)*200);

		if(m < maxm || m > minm) luna.style.visibility = 'visible';
		else luna.style.visibility = 'hidden';
		setX(x); setY(y);

		date = null;
	}
}
/** Sun class */
function Sun()
{
	var sunrise = 0;
	var sunset = 0;

	this.x = 0;
	this.y = 0;
	this.width = 156;
	this.height = 156;
	var soare;

	/** Initialize the class.
	*
	*	@param sunr integer, sunrise time (unix tymestamp - number of seconds).
	*	@param suns integer, sunset time (unix tymestamp - number of seconds).
	*	@return void.
	*/
	this.initialize = function(sunr, suns)
	{
		sunrise =  makeDateFromStamp(sunr);
		sunset =  makeDateFromStamp(suns);

		soare = document.createElement('div');
		soare.className = "HideFromPrint";
		document.getElementById('Body').appendChild(soare);

		if(navigator.userAgent.indexOf('IE') != -1)
		{
			soare.innerHTML = '<img style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+LINK_BASE+'images/weather/sun.png\');" src="'+LINK_BASE+'blank.png" alt=" ">'
		}
		else
		{
			soare.innerHTML = '<img src="'+LINK_BASE+'images/weather/sun.png" alt=" ">';
		}

		soare.style.position = 'absolute';
		soare.classInstance = this;

		window.setInterval(timer, 60000);
		timer();

	}
	/* Set the x position of the sun */
	var setX = function(_x)
	{
		this.x = _x;
		soare.style.left = this.x + 'px';
	}
	/* Set the y position of the sun */
	var setY = function(_y)
	{
		if(_y < 160) {soare.style.visibility = "hidden"; return;}
		this.y = _y;
		soare.style.bottom = this.y + 'px';
	}

	var timer = function()
	{
		var date = getGMTDate();

		var h = date.getHours();
		var m = h*60 + date.getMinutes();
		var maxm = sunset.getHours()*60+sunset.getMinutes();
		var minm = sunrise.getHours()*60+sunrise.getMinutes();
		var offset = 3.6; /* corectie initiala cu PI/2*/


		var x = Math.round(getWindowWidth()/2 - soare.classInstance.width/2 + Math.cos((m*3.14)/(maxm-minm)+offset)*500);
		var y = Math.round(300 + Math.sin((m*3.14)/(maxm-minm)+offset)*200);

		if(m > minm && m < maxm) soare.style.visibility = 'hidden';
		else soare.style.visibility = 'visible';
		setX(x); setY(y);
		date = null;
	}
}
/** Storm clouds */
function StormCloud()
{
	var nor;
	var ww = getWindowWidth();
	var wh = getWindowHeight();

	this.y = 0;
	this.x = 0;
	this.width = 0;
	this.height = 0;

	/* MouseUp handler. This is private */
	var onMouseUp = function(e)
	{
		if(!e) e = window.event;

		this.canDrag = false;
		return false;
	}
	/* MouseDown handler. This is private */
	var onMouseDown = function(e)
	{
		ww = getWindowWidth();
		wh = getWindowHeight();
		this.classInstance.width = getElementWidth(nor);
		this.classInstance.height = getElementHeight(nor);
		if(!e) e = window.event;

		this.canDrag = true;
		return false;
	}
	/* MouseMove handler. This is private */
	var onMouseMove = function(e)
	{
		if(this.canDrag)
		{
			if(!e) e = window.event;

			this.classInstance.setX(ww - e.clientX - this.classInstance.width/2);
			this.classInstance.setY(wh - e.clientY - this.classInstance.height/2);
		}
		return false;
	}
	/** Change the y position.
		@param _y integer, the new position.
		@return void.
	*/
	this.setY = function(_y)
	{
		this.y = _y;
		nor.style.bottom = this.y + 'px';
	}
	/** Change the x position.
		@param _x integer, the new position.
		@return void.
	*/
	this.setX = function(_x)
	{
		this.x = _x;
		nor.style.right = this.x + 'px';
	}
	/** Initialize the cloud.
		@param fx integer, if not undefined sets the x position of the cloud, else x is random .
		@return void.
	*/
	this.initialize = function(fx)
	{
		if(fx != undefined) this.x = fx;
		nor = document.createElement('div');
		nor.className = "HideFromPrint";
		if(navigator.userAgent.indexOf('IE') != -1)
		{
			nor.innerHTML = '<img style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+LINK_BASE+'images/weather/storm_cloud.png\');" src="'+LINK_BASE+'blank.png" alt=" ">'
		}
		else
		{
			nor.innerHTML = '<img src="'+LINK_BASE+'images/weather/storm_cloud.png" alt=" ">';
		}
		document.getElementById('Body').appendChild(nor);

		nor.style.position = 'absolute';
		nor.style.zIndex = '50500';
		nor.style.cursor = "pointer";

		this.width = getElementWidth(nor);
		this.height = getElementHeight(nor);
		this.y = 550 + Math.round(Math.random()*(wh-650));
		this.x = Math.round(Math.random()*(ww/2));


		nor.onmouseup = onMouseUp;
		nor.onmousedown = onMouseDown;
		nor.onmousemove = onMouseMove;
		nor.classInstance = this;

		this.setX(this.x);
		this.setY(this.y);
	}

}
/** Dark clouds */
function DarkCloud()
{
	var nor;
	var ww = getWindowWidth();
	var wh = getWindowHeight();

	this.y = 0;
	this.x = 0;
	this.width = 0;
	this.height = 0;

	/* MouseUp handler. This is private */
	var onMouseUp = function(e)
	{
		if(!e) e = window.event;

		this.canDrag = false;
		return false;
	}
	/* MouseDown handler. This is private */
	var onMouseDown = function(e)
	{
		ww = getWindowWidth();
		wh = getWindowHeight();
		this.classInstance.width = getElementWidth(nor);
		this.classInstance.height = getElementHeight(nor);
		if(!e) e = window.event;

		this.canDrag = true;
		return false;
	}
	/* MouseMove handler. This is private */
	var onMouseMove = function(e)
	{
		if(this.canDrag)
		{
			if(!e) e = window.event;

			this.classInstance.setX(ww - e.clientX - this.classInstance.width/2);
			this.classInstance.setY(wh - e.clientY - this.classInstance.height/2);
		}
		return false;
	}
	/** Change the y position.
		@param _y integer, the new position.
		@return void.
	*/
	this.setY = function(_y)
	{
		this.y = _y;
		nor.style.bottom = this.y + 'px';
	}
	/** Change the x position.
		@param _x integer, the new position.
		@return void.
	*/
	this.setX = function(_x)
	{
		this.x = _x;
		nor.style.right = this.x + 'px';
	}
	/** Initialize the cloud.
		@param fx integer, if not undefined sets the x position of the cloud, else x is random .
		@return void.
	*/
	this.initialize = function(fx)
	{
		if(fx != undefined) this.x = fx;
		nor = document.createElement('div');
		nor.className = "HideFromPrint";
		if(navigator.userAgent.indexOf('IE') != -1)
		{
			nor.innerHTML = '<img style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+LINK_BASE+'images/weather/dark_cloud.png\');" src="'+LINK_BASE+'blank.png" alt=" ">'
		}
		else
		{
			nor.innerHTML = '<img src="'+LINK_BASE+'images/weather/dark_cloud.png" alt=" ">';
		}
		document.getElementById('Body').appendChild(nor);

		nor.style.position = 'absolute';
		nor.style.zIndex = '50500';
		nor.style.cursor = "pointer";

		this.width = getElementWidth(nor);
		this.height = getElementHeight(nor);
		this.y = 550 + Math.round(Math.random()*(wh-650));
		this.x = Math.round(Math.random()*(ww/2));


		nor.onmouseup = onMouseUp;
		nor.onmousedown = onMouseDown;
		nor.onmousemove = onMouseMove;
		nor.classInstance = this;

		this.setX(this.x);
		this.setY(this.y);
	}

}
/** Normal summer clouds */
function NormalCloud()
{
	var nor;
	var ww = getWindowWidth();
	var wh = getWindowHeight();

	this.y = 0;
	this.x = 0;
	this.width = 0;
	this.height = 0;

	/* MouseUp handler. This is private */
	var onMouseUp = function(e)
	{
		if(!e) e = window.event;

		this.canDrag = false;
		return false;
	}
	/* MouseDown handler. This is private */
	var onMouseDown = function(e)
	{
		ww = getWindowWidth();
		wh = getWindowHeight();
		this.classInstance.width = getElementWidth(nor);
		this.classInstance.height = getElementHeight(nor);
		if(!e) e = window.event;

		this.canDrag = true;
		return false;
	}
	/* MouseMove handler. This is private */
	var onMouseMove = function(e)
	{
		if(this.canDrag)
		{
			if(!e) e = window.event;

			this.classInstance.setX(ww - e.clientX - this.classInstance.width/2);
			this.classInstance.setY(wh - e.clientY - this.classInstance.height/2);
		}
		return false;
	}
	/** Change the y position.
		@param _y integer, the new position.
		@return void.
	*/
	this.setY = function(_y)
	{
		this.y = _y;
		nor.style.bottom = this.y + 'px';
	}
	/** Change the x position.
		@param _x integer, the new position.
		@return void.
	*/
	this.setX = function(_x)
	{
		this.x = _x;
		nor.style.right = this.x + 'px';
	}
	/** Initialize the cloud.
		@param fx integer, if not undefined sets the x position of the cloud, else x is random .
		@return void.
	*/
	this.initialize = function(fx)
	{
		if(fx != undefined) this.x = fx;
		nor = document.createElement('div');
		nor.className = "HideFromPrint";
		if(navigator.userAgent.indexOf('IE') != -1)
		{
			nor.innerHTML = '<img style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+LINK_BASE+'images/weather/normal_cloud.png\');" src="'+LINK_BASE+'blank.png" alt=" ">'
		}
		else
		{
			nor.innerHTML = '<img src="'+LINK_BASE+'images/weather/normal_cloud.png" alt=" ">';
		}
		document.getElementById('Body').appendChild(nor);

		nor.style.position = 'absolute';
		nor.style.zIndex = '50500';
		nor.style.cursor = "pointer";

		this.width = getElementWidth(nor);
		this.height = getElementHeight(nor);
		this.y = 550 + Math.round(Math.random()*(wh-650));
		this.x = Math.round(Math.random()*(ww/2));


		nor.onmouseup = onMouseUp;
		nor.onmousedown = onMouseDown;
		nor.onmousemove = onMouseMove;
		nor.classInstance = this;

		this.setX(this.x);
		this.setY(this.y);
	}

}
/** Cloud system class. */
function CloudSystem()
{
	var speed = 200;
	var clouds = new Array;
	var ww = getWindowWidth();
	var timerId = 0;

	var timer = function()
	{
		for(var i in clouds)
		{
			try {	//firebug reports "clouds[i].setX not a function" when on a page with lightbox, so.. hide that :P
				clouds[i].setX( (clouds[i].x > (ww + clouds[i].width))? (-clouds[i].width) : (clouds[i].x + 1) );
			} catch(e) {}
		}
	}

	/** Add a new cloud
		@param c Cloud class (NormalClowd or StormClowd).
		@return void.
	*/
	this.addCloud = function(c)
	{
		clouds.push(c);
	}
	/** Initialize the cloud system
		@param s integer, clowd speed
		@return void.
	*/
	this.initialize = function(s)
	{
		speed = s;
		timerId = window.setInterval(timer, speed);
	}
	/** Destructor */
	this.disroy = function()
	{
		window.clearInterval(timerId);
		clouds = null;
	}
}
