/**
 * Progress indicator.
 */

Busy = new Class(/** @lends Busy.prototype */{

	//Implements: [NCMS.IDealloc, NCMS.IHasClassName],

	Binds: [
		'mouseMoveListener'
	],

	/**
	 * Constructor
	 */
	initialize:function() {

		// Number of people who want us to look busy
		this.busyCount = 0;

		this.spinner = new Element('div');
		this.spinner.addClass(Busy.CSSClass);
		this.spinner.inject(document.getElement('body'), 'bottom');
		this.spinner.setStyle('display', 'none');
		//console.log(this.spinner);
		document.addEvent('mousemove', this.mouseMoveListener);
	},

	/**
	 * I increment the busy count. If I previously wasn't looking busy, I make
	 * the spinner gif visible.
	 */
	lookBusy:function() {
		
		//console.log(this);
		//console.log(this.spinner);
		if(this.busyCount <= 0) {

			// Become busy
			this.spinner.setStyle('display', 'block');
			this.busyCount = 0;
		}

		this.busyCount++;
	},

	/**
	 * I decrement the busy count until no-one else wants me to look busy. Then
	 * I hide the spinner gif
	 */
	relax:function() {

		this.busyCount--;

		if(this.busyCount <= 0) {

			// Relax
			this.spinner.setStyle('display', 'none');
			this.busyCount = 0;
		}
	},

	/**
	 * I listen for when the mouse moves, and update the coordinates of the
	 * spinner gif to wherever the mouse is
	 *
	 * @private
	 */
	mouseMoveListener:function(evt) {
		
		// Windows has big mouse pointer
		if(Browser.Platform.win) {

			this.spinner.setStyles({
				'top':evt.page.y + 16,
				'left':evt.page.x + 15
			});

		} else {

			this.spinner.setStyles({
				'top':evt.page.y + 10,
				'left':evt.page.x + 10
			});
		}
	},

	/**
	 * I deallocate this instance
	 */
	dealloc:function() {

		document.removeEvent('mousemove', this.mouseMoveListener);

		this.spinner.destroy();
		this.spinner = null;
	}
});

$extend(Busy, /** @lends NCMS.Busy */{

	/**
	 * The CSS class of elements this class creates or uses
	 * @property {String} CSSClass
	 */
	CSSClass: 'busy'
});
