

/*********************************
 ******* Barrettes (suite) *******
 *********************************/
(function() {
	var window = this,
			document = window.document,
			WRT = 'writeln',
			snf_getWidth = window.snf_getWidth,
			getObjectLeft = window.getObjectLeft,
			findObjectLeft = window.findObjectLeft,
			LEFT_BUTTON = window.LEFT_BUTTON,
			mouseEventManager = window.mouseEventManager,
			MouseListener = window.MouseListener;

	//YG: objet Barrette3 (new style)
	var MARGIN_PATTERN = /^\s*([0-9]+)\s+([0-9]+)\s+([0-9]+)\s+([0-9]+)\s*$/;
	window.Barrette3 = function(id,txt,way,left,top,width,btnWidth,btnHeight,margin,debug) {
		if (!mouseEventManager) { return; }
		this.ml = new MouseListener();

		this.ml.filterId = id;
		this.ml.lblId = id + 'Lbl';
		this.ml.lblLeftId = id + 'LblLeft';
		this.ml.lblRightId = id + 'LblRight';
		this.ml.btnId = id + 'Btn';
		this.ml.btn2Id = id + 'Btn2';
		this.ml.bkgOffId = id + 'BkgOff';
		if (!margin) {
			this.ml.marginT = this.ml.marginR = this.ml.marginB = this.ml.marginL = 2;
		} else {
			this.ml.marginT = parseInt(margin.replace(MARGIN_PATTERN, '$1'), 10);
			this.ml.marginR = parseInt(margin.replace(MARGIN_PATTERN, '$2'), 10);
			this.ml.marginB = parseInt(margin.replace(MARGIN_PATTERN, '$3'), 10);
			this.ml.marginL = parseInt(margin.replace(MARGIN_PATTERN, '$4'), 10);
		}
		this.ml.lblTxt = txt;
		this.ml.filterWay = way;
		this.ml.btnOffsetX = 0;
		this.ml.mouseIsDown = false;
		this.ml.isSecond = false;
		this.ml.top = top;

		this.ml.spacing = 5;

		this.ml.lblTop = this.ml.marginT;
		this.ml.lblHeight = 16;
		this.ml.topBkgOff = this.ml.lblTop + this.ml.lblHeight + this.ml.spacing;
		this.ml.topBtn = this.ml.topBkgOff;
		this.ml.left = left;
		this.ml.leftBkgOff = this.ml.marginL;
		this.ml.leftBtn = this.ml.leftBkgOff;
		this.ml.btnWidth = btnWidth;
		this.ml.btnHeight = btnHeight;
		this.ml.bkgWidth = width;
		this.ml.bkgHeight = this.ml.btnHeight - 2;
		this.ml.width = this.ml.marginL + this.ml.bkgWidth + this.ml.marginR;
		this.ml.height = this.ml.marginT + this.ml.lblHeight + this.ml.spacing + this.ml.btnHeight + this.ml.marginB;
		this.ml.xMax = this.ml.bkgWidth - this.ml.btnWidth;
		this.ml.xMin = 0;

		this.ml.writeHTML = function() {
			var html = '';
			html += '<div id="' + this.filterId + '" class="css_filterBarrette3"';
			html += '  style="left:' + this.left + 'px;top:' + this.top + 'px;';
			html += '  width:' + this.width + 'px;height:' + this.height + 'px;">\n';
			html += ' <div id="' + this.lblId + '" class="css_filterBarrette3_lbl"';
			html += '   style="left:' + (this.leftBkgOff + 50) + 'px;top:' + this.lblTop + 'px;';
			html += '   height:' + this.lblHeight + 'px;">' + this.lblTxt + '</div>\n';
			html += ' <div id="' + this.lblLeftId + '" class="css_filterBarrette3_lblLeft"';
			html += '   style="left:' + this.leftBkgOff + 'px;top:' + this.lblTop + 'px;';
			html += '   width:' + this.bkgWidth + 'px;height:' + this.lblHeight + 'px;"></div>\n';
			html += ' <div id="' + this.lblRightId + '" class="css_filterBarrette3_lblRight"';
			html += '   style="left:' + this.leftBkgOff + 'px;top:' + this.lblTop + 'px;';
			html += '   width:' + this.bkgWidth + 'px;height:' + this.lblHeight + 'px;"></div>\n';
			html += ' <div id="' + this.bkgOffId + '" class="css_filterBarrette3_bkgOff"';
			html += '   style="left:' + this.leftBkgOff + 'px;top:' + this.topBkgOff + 'px;';
			html += '   width:' + this.bkgWidth + 'px;height:' + (this.btnHeight) + 'px;"></div>\n';
			if (this.filterWay == 'max') {
				html += ' <div id="' + this.btnId + '" class="css_filterBarrette3_btn"';
				html += '   style="left:' + (this.leftBtn + this.xMax) + 'px;top:' + this.topBtn + 'px;';
				html += '   width:' + this.btnWidth + 'px;height:' + this.btnHeight + 'px;"></div>\n';
			}else if (this.filterWay == 'min') {
				html += ' <div id="' + this.btnId + '" class="css_filterBarrette3_btn"';
				html += '   style="left:' + (this.leftBtn + this.xMin) + 'px;top:' + this.topBtn + 'px;';
				html += '   width:' + this.btnWidth + 'px;height:' + this.btnHeight + 'px;"></div>\n';
			}else {//both
				html += ' <div id="' + this.btnId + '" class="css_filterBarrette3_btn"';
				html += '   style="left:' + (this.leftBtn + this.xMin) + 'px;top:' + this.topBtn + 'px;';
				html += '   width:' + this.btnWidth + 'px;height:' + this.btnHeight + 'px;"></div>\n';
				html += ' <div id="' + this.btn2Id + '" class="css_filterBarrette3_btn"';
				html += '   style="left:' + (this.leftBtn + this.xMax) + 'px;top:' + this.topBtn + 'px;';
				html += '   width:' + this.btnWidth + 'px;height:' + this.btnHeight + 'px;"></div>\n';
			}
			document[WRT](html + '</div>\n');
		};

		this.ml.onFilterChange = function(posBtn1,posBtn2) {};
		this.setOnFilterChange = function(filterChangeEventMethod) {
			this.ml.onFilterChange = filterChangeEventMethod;
			if (this.ml.filterWay === 'both' && this.ml.onFilterChange) {
				try { this.ml.onFilterChange(0, 100); }catch (e1) {}
			} else if (this.ml.filterWay === 'max' && this.ml.onFilterChange) {
				try { this.ml.onFilterChange(100); }catch (e2) {}
			} else if (this.ml.filterWay === 'min' && this.ml.onFilterChange) {
				try { this.ml.onFilterChange(0); }catch (e3) {}
			}
		};
		this.ml.onFilterRefresh = function() {};
		this.setOnFilterRefresh = function(filterRefreshEventMethod) { this.ml.onFilterRefresh = filterRefreshEventMethod; };
		this.ml.updateBtn = function(id,x) {
			var width = snf_getWidth(id);
			var widthTotal = snf_getWidth(this.bkgOffId);
			var minX = getObjectLeft(this.bkgOffId);
			var maxX = widthTotal + minX - width;
			var amplitude = maxX - minX;
			if (this.filterWay === 'both') {
				var pct1, pct2;
				if (this.isSecond) {
					x = parseInt(Math.max(Math.min(x, maxX), getObjectLeft(this.btnId)), 10);
					pct1 = parseInt((getObjectLeft(this.btnId) - minX) * 100 / amplitude, 10);
					pct2 = parseInt((x - minX) * 100 / amplitude, 10);
				}
				else {
					x = parseInt(Math.max(Math.min(x, getObjectLeft(this.btn2Id)), minX), 10);
					pct1 = parseInt((x - minX) * 100 / amplitude, 10);
					pct2 = parseInt((getObjectLeft(this.btn2Id) - minX) * 100 / amplitude, 10);
				}
				if (this.onFilterChange) {
					try {
						//console.info('calling method "onFilterChange('+pct1+','+pct2+')"... [id:'+id+']')
						this.onFilterChange(pct1, pct2);
					}catch (e1) {}
				}
			}
			else {
				x = Math.max(Math.min(x, maxX), minX);
				var pct = parseInt((x - minX) * 100 / amplitude, 10);
				if (this.onFilterChange) {
					try { this.onFilterChange(pct); }catch (e2) {}
				}
			}
			document.getElementById(id).style.left = x + 'px';
		};
		this.ml.mouseRelease = function(id, x) {
			this.mouseIsDown = false;

			/// TEMP TEST
			var width = snf_getWidth(id);
			var widthTotal = snf_getWidth(this.bkgOffId);
			var minX = getObjectLeft(this.bkgOffId);
			var maxX = widthTotal + minX - width;
			var amplitude = maxX - minX;
			if (this.filterWay === 'both') {
				var pct1, pct2;
				if (this.isSecond) {
					x = parseInt(Math.max(Math.min(x, maxX), getObjectLeft(this.btnId)), 10);
					pct1 = parseInt((getObjectLeft(this.btnId) - minX) * 100 / amplitude, 10);
					pct2 = parseInt((x - minX) * 100 / amplitude, 10);
				}
				else {
					x = parseInt(Math.max(Math.min(x, getObjectLeft(this.btn2Id)), minX), 10);
					pct1 = parseInt((x - minX) * 100 / amplitude, 10);
					pct2 = parseInt((getObjectLeft(this.btn2Id) - minX) * 100 / amplitude, 10);
				}
				if (this.onFilterRefresh) {
					try {
						//console.info('calling method "onFilterRefresh('+pct1+','+pct2+')"... [id:'+id+']')
						this.onFilterRefresh(pct1, pct2);
					}catch (e1) {}
				}
			}
			else {
				x = Math.max(Math.min(x, maxX), minX);
				var pct = parseInt((x - minX) * 100 / amplitude, 10);
				if (this.onFilterRefresh) {
					try {
						//console.info('calling method "onFilterRefresh('+pct+')"... [id:'+id+']')
						this.onFilterRefresh(pct);
					}catch (e2) {}
				}
			}
			//TEMP TEST

			//    if(this.onFilterRefresh){ try{ this.onFilterRefresh(); }catch(e){} }
		};
		this.ml.mouseDown = function(e) {
			if (e.mouseButton == LEFT_BUTTON && e.srcElement && (e.srcElement.id == this.btnId || e.srcElement.id == this.btn2Id || e.srcElement.id == this.bkgOffId)) {
				this.mouseIsDown = true;
				if (e.srcElement.id == this.btnId || e.srcElement.id == this.btn2Id) {
					this.btnOffsetX = e.offsetX;
				} else {
					this.btnOffsetX = snf_getWidth(this.btnId) / 2;
				}

				if (this.filterWay !== 'both' || e.srcElement.id == this.btnId) {
					this.isSecond = false;
				} else if (e.srcElement.id == this.btn2Id) {
					this.isSecond = true;
				} else {//both
					var xMoy = parseInt((getObjectLeft(this.btn2Id) + getObjectLeft(this.btnId) + snf_getWidth(this.btnId)) / 2, 10);
					var posCur = parseInt(getObjectLeft(e.srcElement.id) + e.offsetX, 10);
					this.isSecond = posCur > xMoy;
				}
				this.mouseMove(e);
			}
			return false;
		};
		this.ml.mouseUp = function(e) {
			if (this.mouseIsDown && e.mouseButton == LEFT_BUTTON) {
				var tmpX;
				if (window.scrollX === undefined) {
					tmpX = e.clientX - findObjectLeft(this.filterId) - this.btnOffsetX;
				} else {
					tmpX = window.scrollX + e.clientX - findObjectLeft(this.filterId) - this.btnOffsetX;
				}
				this.mouseRelease(this.isSecond ? this.btn2Id : this.btnId, tmpX);
			}
		};
		this.ml.mouseOut = function(e) {
			if (this.mouseIsDown && !e.toElement) {
				var tmpX;
				if (window.scrollX === undefined) {
					tmpX = e.clientX - findObjectLeft(this.filterId) - this.btnOffsetX;
				} else {
					tmpX = window.scrollX + e.clientX - findObjectLeft(this.filterId) - this.btnOffsetX;
				}
				this.mouseRelease(this.isSecond ? this.btn2Id : this.btnId, tmpX);
			}
		};
		this.ml.mouseMove = function(e) {
			var tmpX;
			//      if(window.scrollX===undefined) tmpX=e.x-this.btnOffsetX;
			//TODO : trouver un moyen pour IE (decalage en cas de scroll > 0 en X)
			//       pb : window.scrollX === undefined et document.[body.]scrollLeft toujours 0
			if (window.scrollX === undefined) {
				tmpX = e.clientX - findObjectLeft(this.filterId) - this.btnOffsetX;
			} else {
				tmpX = window.scrollX + e.clientX - findObjectLeft(this.filterId) - this.btnOffsetX;
			}
			if (this.mouseIsDown && e.srcElement) {
				this.updateBtn(this.isSecond ? this.btn2Id : this.btnId, tmpX);
			}
		};
		mouseEventManager.addEventListener(this.ml);
		this.ml.writeHTML();
	};
}());

