

/**
 * Fichier obsolète : utiliser ev/gui/progressBar.js à la place.
 */
(function() {
	var window = this,
			document = window.document;

	if (window.console && window.console.error) {
		window.console.error('FATAL : effects/progressBar.js est DEPRECATED !! => Utiliser ev/gui/progressBar.js à la place.');
	}

	/**
	 *
	 */
	var progressBarsManager;

	/**
	 */
	window.ProgressBar = function(paneNode, _minValue, _maxValue) {
		// Constante stockant le noeud DOM à utiliser
		var pn = document.getElementById(paneNode);
		if (!pn) {throw 'paneNode is not defined [' + paneNode + ']';}
		if (_minValue >= _maxValue) {throw '_minValue is superior to _maxValue [' + paneNode + ', min:' + _minValue + ', max:' + _maxValue + ']';}

		/**
		 * This private property defines thisinstance reference in progressBarsList
		 * that can be used in getProgressBar(int) function.
		 */
		var id = progressBarsManager().addProgressBar(this);
		id = null;// Used to remove "not used warning"


		/**
		 * This private field refers to the ProgressBarListener used by this instance
		 * to manage its behaviour. Each delay ms, #throwEvent(ProgressBarListener)
		 * method of that instance is invoked, providing this progressbar is running.
		 */
		var progressBarListener = null;

		/**
		 * Defines the event produced by a Progressbar instance when it invokes
		 * ProgressbarListener#throwEvent().
		 * @param _executionCount Step number from Progressbar's instance last #start()
		 *  invokation.
		 * @param _type Instance of ProgressbarEventType holding this ProgressbarEvent's type.
		 * @param _source The Progressbar instance who produced this event.
		 */
		window.ProgressBarEvent = function(_element, _value, _minValue, _maxValue, _source) {
			var source = _source;
			var element = _element;
			var value = _value;
			//var minValue=_minValue;
			//var maxValue=_maxValue;

			this.getSource = function() {
				return source;
			};
			this.getElement = function() {
				return element;
			};
			this.getValue = function() {
				return value;
			};
		};

		/**
		 * Singleton declared to hold progressbar's events type enumeration, i.e.
		 * START, RUNNING, and STOP.
		 */
		function ProgressBarEventType() {
		}
		var instance = new ProgressBarEventType();
		instance = null;

		/**
		 * This method adds a ProgressbarListener instance as a control to this
		 * Progressbar.
		 * @param listener The ProgressbarListener that will control this
		 * Progressbar.
		 * @throws If listener is not defined.
		 * @throws If listener is not an instance of ProgressbarListener.
		 */
		this.addProgressBarListener = function(listener) {
			if (!listener) {throw 'progressBarListener is not defined';}
			if (!(listener instanceof window.ProgressBarListener)) {throw 'progressBarListener is not a ProgressBarListener listener';}
			progressBarListener = listener;
		};
		// Cette constante fixe la div dont la largeur est amenée à être modifiée.
		var paneNodeFC = window.firstChildByNodeName(pn, 'div') || window.firstChildByNodeName(pn, 'DIV');

		// Ces constantes sont fixées à 0 par défaut
		// value est la valeur courante
		// widthFC est la largeur appliquée à paneNodeFC
		var value = 0;
		var widthFC = 0;

		// minValue est la valeur minimale
		// par défaut elle vaut 0, hormis si on la déclare en paramètre
		var minValue = _minValue || 0;

		// maxValue est la valeur maximale
		// par défaut elle vaut 100, hormis si on la déclare en paramètre
		var maxValue = _maxValue || 100;

		// La Largeur équivalent à 100%
		var maxWidth = window.getStyleValue(pn, 'width');
		if (maxWidth !== 'none' && maxWidth.match(/[0-9]+px/)) {
			maxWidth = parseInt(maxWidth.replace(/px/, ''), 10);
		}

		// On récupère les paddings et bordures horizontals quand IE est en quirk mode
		// car ils sont pris en compte dans le calcul de la largeur
		if (window.genericNavigator.navigator.id === window.MSIE && document.compatMode === 'BackCompat') {
			var paddingLeft = window.getStyleValue(pn, 'paddingLeft');
			if (paddingLeft !== 'none' && paddingLeft.match(/[0-9]+px/)) {
				paddingLeft = parseInt(paddingLeft.replace(/px/, ''), 10);
			}
			var paddingRight = window.getStyleValue(pn, 'paddingRight');
			if (paddingRight !== 'none' && paddingRight.match(/[0-9]+px/)) {
				paddingRight = parseInt(paddingRight.replace(/px/, ''), 10);
			}
			this.paddingHor = paddingLeft + paddingRight;

			var borderLeftWidth = window.getStyleValue(pn, 'borderLeftWidth');
			if (borderLeftWidth !== 'none' && borderLeftWidth.match(/[0-9]+px/)) {
				borderLeftWidth = parseInt(borderLeftWidth.replace(/px/, ''), 10);
			}
			var borderRightWidth = window.getStyleValue(pn, 'borderRightWidth');
			if (borderRightWidth !== 'none' && borderRightWidth.match(/[0-9]+px/)) {
				borderRightWidth = parseInt(borderRightWidth.replace(/px/, ''), 10);
			}
			this.borderHorWidth = borderLeftWidth + borderRightWidth;
		}
		this.getMinValue = function() {
			return minValue;
		};
		this.getMaxValue = function() {
			return maxValue;
		};
		/**
		 * On fixe la valeur de la largeur de paneNodeFC
		 */
		this.setValue = function(_value) {
			value = _value;

			//Si on tente de dépasser les 100%
			if (value > maxValue) {
				if (window.genericNavigator.navigator.id === window.MSIE && document.compatMode === 'BackCompat') {
					maxWidth = maxWidth - this.paddingHor - this.borderHorWidth;
				}
				paneNodeFC.style.width = maxWidth + 'px';
				value = maxValue;
			}
			//Si on tente d'aller en dessous de 0%
			else if (value <= minValue) {
				paneNodeFC.style.width = 0;
				value = minValue;
			}
			// On recadre la barre de progression
			else {
				widthFC = maxWidth * ((value - minValue) / (maxValue - minValue));
				/* On gère ici la différence de traitement des modèles de boîtes sous IE qui intervient quand il passe en mode Quirks */
				if (window.genericNavigator.navigator.id === window.MSIE && document.compatMode === 'BackCompat') {
					widthFC = widthFC - this.paddingHor - this.borderHorWidth;
				}
				paneNodeFC.style.width = widthFC + 'px';
			}
			progressBarListener.throwEvent(new window.ProgressBarEvent(pn, value, minValue, maxValue, this));
		};
		/**
		 * Cette méthode permet de redéfinir la valeur minimale
		 */
		this.setMinValue = function(_minValue) {
			minValue = _minValue;
			this.setValue(value);
		};
		/**
		 * Cette méthode permet de redéfinir la valeur maximale
		 */
		this.setMaxValue = function(_maxValue) {
			maxValue = _maxValue;
			this.setValue(value);
		};
	};

	/**
	 * Abstract class designed to control a Progressbar, this controls is a listener
	 * whom #throwEvent(ProgressbarEvent) abstract method is invoked periodically by
	 * the Progressbar instance it controls. All ProgressbarListener subclass must
	 * implement this method.
	 */
	window.ProgressBarListener = function() {
		/**
		 * The method periodically invoked by the controled Progressbar, it defines the
		 * Progressbar's behaviour.
		 * @param timlineEvent Contains a ProgressbarEvent instance produced by the
		 *  controled Progressbar.
		 * @throws If the method has not been implemented.
		 */
		this.throwTimelineEvent = function(progressBarEvent) {
			throw ('#throwEvent() has not been implemented');
		};
	};

	var ProgressBarsManagerInstance;
	/**
	 * Singleton declared to hold the list of Progressbar instances generaly used in
	 * eval(), or setTimeout(), or setInterval() string commands.
	 */
	progressBarsManager = function() {
		// both two following lines defines the singleton.
		if (ProgressBarsManagerInstance) {
			return ProgressBarsManagerInstance;
		}
		ProgressBarsManagerInstance = this;
		/**
		 * This private property holds the list of Progressbar instances and used by
		 * functions addProgressbar(Progressbar) or getProgressbar(int) in order to find a
		 * specific instance.
		 */
		var progressBarsList = [];
		/**
		 * Method designed to reference each Progressbar returning the reference which
		 * can be used in method getProgressbar(int) to retreive the matching instance.
		 * This is the only mean to reference an instance from an eval string for
		 * example.
		 * @param progressbar a Progressbar instance to be referenced.
		 * @return the index of the given Progressbar instance that can be used in
		 *getProgressbar(int) in order to get back the instance.
		 * @throws If progressbar parameter is not defined.
		 * @throws If progressbar parameter is not a Progressbar instance.
		 */
		this.addProgressBar = function(progressBar) {
			if (!progressBar) {throw 'progressBar is not defined';}
			if (!(progressBar instanceof window.ProgressBar)) {throw 'progressBar is not a ProgressBar';}
			progressBarsList.push(progressBar);
			return progressBarsList.length - 1;
		};
		/**
		 * Method designed to return an instance given its index in the
		 * progressbarsList.
		 * @param index the index of the instance to retrieve.
		 * @return the matching Progressbar instance.
		 * @throws If index is undefined, null or not a number.
		 * @throws If index is negative.
		 * @throws If index is out of bounds (is not a valid reference)
		 */
		this.getProgressBar = function(index) {
			if (typeof(index) !== 'number') {throw 'index is not a number (maybe null or undefined as well) [' + index + ']';}
			if (index < 0 || index >= progressBarsList.length) {throw 'index is out of bounds [0 ; ' + progressBarsList.length + '[';}
			return progressBarsList[index];
		};
		return this;
	};

	/***/
	ProgressBarsManagerInstance = progressBarsManager();
}());

