/**
* WaitingClass will display a loading image in the center of the screen and black out the screen until wait_end is called.
*	WaitingClass is a replacement for wait_start() and wait_end() in the windowEffects file.It does the same 
*	things BUT there is no flicker if wait_end() gets called before wait_start() has finished.
*
*	DateDev: 2009-02-16 ADM
*	
*	USAGE: 	
*		window.addEvent('domready', function()
*		{
*			my_waiting = new WaitingClass();
*			my_waiting.wait_start(); //start the loading image.
*		});
*
*	Methods:
*		wait_start(); //starts the loading image (with a fade in)
*		wait_end(); //removes the loading image (with a fade out)
*
*	Options:
*		max_opacity: //sets the max opacity for the background overlay. values can range between 0 and 1 (defaults to .8)
*/


var WaitingClass = new Class(
{
	overlay:$empty,
	in_transition: false,
	current_opacity: 0,

	overlay_fade_in_fx: null,
	overlay_fade_out_fx: null,
	waiting_fade_in_fx: null,
	waiting_fade_out_fx: null,


	waiting_box: $empty,
	Implements: [Options,Events],
	options: {
		max_opacity: ".8"
	},

	initialize: function(options)
	{
		this.generateOverlay();
		this.generateLoadingBox();
	},
	generateOverlay : function ()
	{
		this.overlay = new Element('div', {
		'id': 'newOverlay',
		'styles': {
		'display': 'block',
		'position': 'fixed',
		'width': '100%',
		//'position': 'absolute',
		'top': 0,
		'left': 0,
		'opacity': '0',
		'background': '#3c3c3c',
		'z-index': '1004'
		}
		});

		this.updateOverLaySize();

		document.body.appendChild(this.overlay);
	},
	generateLoadingBox: function()
	{
		this.waiting_box = new Element('div', {
		'id': 'waitBox',
		'html': '<BR><BR><center><img src="include/img/sliderBar.gif" border="0"><p style="color: #cd3700; padding: 0px; margin: 0px; font-weight: bold;">Please Wait...</p></center>',
		'styles': {
		'display': 'block',
		'width': '250px',
		'height': '75px',
		'position': 'absolute',
		'background': '#ffffff',
		'border': '2px solid #030303',
		'z-index': '1006',
		'opacity': 0
		}
		});

		document.body.appendChild(this.waiting_box);
	},
	centerWaitingDiv: function ()
	{
		docHeight = (this.waiting_box.style.height).replace("px","");
		docWidth = (this.waiting_box.style.width).replace("px","");
		var X = 0, Y = 0;

		var windowSize = window.getScrollSize();
		var windowScroll = window.getScroll();

		X = ((windowSize.x / 2) - docWidth) + windowScroll.x;
		Y = ((windowSize.y / 2) - docHeight) + windowScroll.y;

		this.waiting_box.style.top = Y - (docHeight/2) + 'px';
		this.waiting_box.style.left = X + (docWidth/2) + 'px';
	},
	updateOverLaySize: function ()
	{
		var windowSize = window.getScrollSize();
		this.overlay.style.height = windowSize.y + "px";
	},
	wait_start: function()
	{
		this.centerWaitingDiv();
		this.easeOverlayIn();
		my_waiting_box = this.waiting_box;
		if(this.waiting_fade_out_fx)
		{
			this.waiting_fade_out_fx.cancel();
			this.overlay_fade_out_fx.cancel();
		}

		this.waiting_fade_in_fx = new Fx.Tween(this.waiting_box, {
			duration: 500,
			wait: false,
			transition: Fx.Transitions.Quad.easeOut,
			onCancel : function()
			{
				this.current_opacity = my_overlay.style.opacity;
			}
		});

		this.waiting_fade_in_fx.start('opacity', this.overlay.style.opacity, 1);

	},
	wait_end: function()
	{
		this.easeOverlayOut();
		my_waiting_box = this.waiting_box;
		if(this.waiting_fade_in_fx)
		{
			this.waiting_fade_in_fx.cancel();
			this.overlay_fade_in_fx.cancel();
		}

		this.waiting_fade_out_fx = new Fx.Tween(this.waiting_box, {
			duration: 500,
			wait: false,
			transition: Fx.Transitions.Quad.easeOut,
			onCancel : function()
			{
				this.current_opacity = my_overlay.style.opacity;
			}
		});

		this.waiting_fade_out_fx.start('opacity', this.overlay.style.opacity,0);
	},
	easeOverlayIn: function()
	{
		my_overlay = this.overlay;
		if(this.overlay_fade_out_fx)this.overlay_fade_out_fx.cancel();

		this.overlay_fade_in_fx = new Fx.Tween(this.overlay, {
			duration: 500,
			wait: false,
			transition: Fx.Transitions.Quad.easeOut,
			onCancel : function()
			{
				this.current_opacity = my_overlay.style.opacity;
			}
		});

		this.overlay_fade_in_fx.start('opacity', this.overlay.style.opacity, this.options.max_opacity);

	},
	easeOverlayOut: function()
	{
		if(this.overlay_fade_in_fx)this.overlay_fade_in_fx.cancel();
		my_overlay = this.overlay;
		this.overlay_fade_out_fx = new Fx.Tween(this.overlay, {
			duration: 500,
			wait: false,
			transition: Fx.Transitions.Quad.easeOut,
			onCancel : function()
			{
				this.current_opacity = my_overlay.style.opacity;
			}
		});
		this.overlay_fade_out_fx.start('opacity', this.overlay.style.opacity, 0);
	}
});
