/**
 * @name ProgressbarControl
 * @version 1.0
 * @author Bjorn BRala
 * @copyright (c) 2008 SWIS BV - www.geostart.nl
 * @fileoverview Creates a progress bar control for usage in google maps. 
 *   It can be used to show the progress of loading markers, for example.
 */

/**
 * @name ProgressbarOptions
 * @class This class represents optional arguments to 
 *   {@link ProgressbarControl}, 
 * @property {Number} [width=176] Specifies, in pixels, the width of the bar.
 * @property {String} [loadstring=Loading...] Specifies the string displayed 
 *  when first starting the control, before any update.
 */


/**
 * Custom progress bar control, for placement on map.
 * 
 * @private
 * @return {GControl}
 */    
function ProgressbarMapControl($map, $width) { 
	this._map    = $map; 
	this._width  = $width;
	this._offset = { x: 95, y: 7 };

	this.IDS     = { container : "geoProgressContainer",
					 text : "geoProgressText",
					 bar : "geoProgressBar"
				   };
}

/**
 * @private
 */
ProgressbarMapControl.prototype = new GControl(true, false);


/**
 * @private
 * @desc Initializes the GControl. Creates the HTML and styles.
 * @return {Element}
 */
ProgressbarMapControl.prototype.initialize = function () {

	var container = document.createElement('div'),
		domHtml   = ['<div id="', this.IDS.text, '"></div>',
					 '<div id="', this.IDS.bar, '"></div>'];

	container.id          = this.IDS.container;
	container.style.width = this._width + 'px';
	container.innerHTML   = domHtml.join ('');

	// Append GControl to Map
	this._map.getContainer().appendChild(container);

	return container;
};


/**
 * @private 
 * @desc Return the default position for the control
 * @return {GControlPosition}
 */
ProgressbarMapControl.prototype.getDefaultPosition = function () {
	return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(this._offset.x, this._offset.y));
};


/**
 * Creates a progress bar control on the given map, with the given options.
 *
 * @constructor
 * @param {GMap2} Map object
 * @param  {ProgressbarOptions} opt_opts
 */
function ProgressbarControl($map, $opts) {
	this._map          = $map;
	this._options      = $opts || {};
	this._defaultWidth = 120;
	this._defaultText  = 'Loading... ';
	this._compleleText = 'Loading Complete';

	this._width        = this._options.width || this._defaultWidth;
	this._loadString   = this._options.loadString || this._defaultText;

	this._control      = new ProgressbarMapControl(this._map, this._width);
	this._map.addControl(this._control);

	// After add Control to Map, we can Select the Dom
	this._div          = document.getElementById(this._control.IDS.bar);
	this._text         = document.getElementById(this._control.IDS.text);
	this._container    = document.getElementById(this._control.IDS.container);

	this._initialized  = false;
	this._operations   = 0;
	this._current      = 0;

}


/**
 * @desc Start the progress bar. 
 * @param {Number} operations Total amount of operations that will be executed.
 */
ProgressbarControl.prototype.addPending = function ($operations) {

	if ( this._initialized ) {
		this._operations += $operations;
	}
	else {
		this._initialized = true;
		this._operations = $operations || 0;
		this._current = 0;

		this._div.style.width = '0%'; 
		this._text.innerHTML = this._loadString;
		$(this._container).fadeIn();
	}
//	HyperCities.debug("Init : " + this._current + ' / ' + this._operations);
};


/**
 * @desc  Update the progress with specified number of operations.
 * @param {Number} step Number of operations to add to bar.
 */
ProgressbarControl.prototype.addFinished = function ($step) {
	var self = this, percentage;
	
	// If Progressbar is not initialized, ignore request
	if ( this._initialized ) {
		this._current += $step;

//		HyperCities.debug("Step : " + this._current + ' / ' + this._operations);

		if (this._current > 0) {
			percentage = Math.ceil((this._current / this._operations) * 100);
			if (percentage > 99) { 
				percentage = 99; 
			}

			this._div.style.width = percentage + '%'; 

			// Update text of Procressbar, and remove it after it's complete 
			if (this._current >= this._operations) {
				this._current = this._operations;
				setTimeout(function() {
						self.remove.call(self);		
					}, 1500);

				this._text.innerHTML = this._compleleText;
			} 
			else { 
				this._text.innerHTML = this._loadString + this._current + ' / ' + this._operations;
			}
		}
	}
};

/**
 * @desc Remove control.
 */
ProgressbarControl.prototype.remove = function () {
	$(this._container).fadeOut();
	this._initialized = false;
};
