

/*********************************
 ************ Barrettes **********
 *********************************/
(function() {
	var window = this,
			document = window.document,
			navigator = window.navigator,
			WRT = 'writeln',
			snf_getHTML = window.snf_getHTML,
			snf_setHTML = window.snf_setHTML,
			snf_setCursor = window.snf_setCursor,
			snf_getLeft = window.snf_getLeft,
			refreshDisplay = window.refreshDisplay,
			mouseEventManager = window.mouseEventManager;

	function noop() { }

	//YG: objet Barrette (old style)
	window.BarretteMax = function(imId,imSz,minLblId,maxLblId,minLbl) {
		this.imId = imId;
		this.minLblId = minLblId;
		this.maxLblId = maxLblId;
		this.minLbl = minLbl;
		this.imSz = imSz;
		this.arrBrnMaxi = [];
		this.arrBrnMaxiCnt = 0;
		this.arrBrnMaxiIndex = 0;

		this.eventValueActivated = function(value) { return; };
		this.eventValueDesactivated = function(value) { return; };
		this.formatValue = function(value) { return value; };

		this.addBorneMaxi = function(val) {
			this.arrBrnMaxi[this.arrBrnMaxiCnt++] = val;
			this.setBorneMaxi();
		};
		this.update = function() {
			var cnt = this.arrBrnMaxiCnt - 1, i;
			if (cnt > 0) {
				for (i = 0; i < this.arrBrnMaxiIndex; ++i) { try { this.eventValueActivated(this.arrBrnMaxi[i]); }catch (e) {} }
				for (i = this.arrBrnMaxiIndex; i < cnt; ++i) { try { this.eventValueDesactivated(this.arrBrnMaxi[i]); }catch (e2) {} }
				try { snf_setHTML(this.maxLblId, this.formatValue(this.arrBrnMaxi[this.arrBrnMaxiIndex])); }catch (e3) {}
				var curImgSz = parseInt(((this.arrBrnMaxiIndex + 1) * parseInt(this.imSz, 10)) / this.arrBrnMaxiCnt, 10);
				var imageElt = document.getElementById(this.imId);
				if (imageElt) {
					imageElt.style.width = curImgSz + 'px';
				}
				try { refreshDisplay(); }catch (e4) {}
			}
		};
		this.setBorneMini = function() {
			this.arrBrnMaxiIndex = 0;
			this.update();
		};
		this.setBorneMaxi = function() {
			this.arrBrnMaxiIndex = Math.max(this.arrBrnMaxiCnt - 1, 0);
			this.update();
		};
		this.incBorne = function(n) {
			if (!n) {
				n = 1;
			}
			this.arrBrnMaxiIndex += n;
			if (this.arrBrnMaxiIndex < 0) {
				this.setBorneMini();
			} else if (this.arrBrnMaxiIndex > this.arrBrnMaxiCnt - 1) {
				this.setBorneMaxi();
			} else {
				this.update();
			}
		};
		this.initValues = function(arrInitValues) {
			if (arrInitValues) {
				for (var i in arrInitValues) {
					if (arrInitValues.hasOwnProperty(i)) {
						this.addBorneMaxi(arrInitValues[i]);
					}
				}
			}
		};
		try { snf_setHTML(this.minLblId, this.minLbl); }catch (e) {}
	};

	//YG: objet Barrette2 (new style)
	var MARGIN_PATTERN = /^\s*([0-9]+)\s+([0-9]+)\s+([0-9]+)\s+([0-9]+)\s*$/;
	window.Barrette2 = function(id,txt,way,left,top,width,btnWidth,btnHeight,margin,debug) {
		debug = false;
		this.dbg = noop;
		this.debug = debug;
		if (this.debug) {
			this.dbg = function(dbgtxt) {
				snf_setHTML(this.filterId + 'dbg', snf_getHTML(this.filterId + 'dbg') + this.filterId + ' - ' + dbgtxt + '<br>');
			};
		}
		this.filterId = id;
		this.lblId = this.filterId + 'Lbl';
		this.lblLeftId = this.filterId + 'LblLeft';
		this.lblRightId = this.filterId + 'LblRight';
		this.btnId = this.filterId + 'Btn';
		this.btn2Id = this.filterId + 'Btn2';
		this.bkgOnId = this.filterId + 'BkgOn';
		this.bkgOffId = this.filterId + 'BkgOff';
		this.bkgGradId = this.filterId + 'BkgGrad';
		if (!margin) {
			this.marginT = this.marginR = this.marginB = this.marginL = 2;
		} else {
			this.marginT = parseInt(margin.replace(MARGIN_PATTERN, '$1'), 10);
			this.marginR = parseInt(margin.replace(MARGIN_PATTERN, '$2'), 10);
			this.marginB = parseInt(margin.replace(MARGIN_PATTERN, '$3'), 10);
			this.marginL = parseInt(margin.replace(MARGIN_PATTERN, '$4'), 10);
		}
		this.lblTxt = txt;
		this.filterWay = way;
		this.btnOffsetX = 0;
		this.isDown = false;
		this.isSecond = false;
		this.top = top;

		this.spacing = 5;

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

		this.writeHTML = function() {
			var html = '<div id="' + this.filterId + '" class="css_filterBarrette2"';
			html += '  style="left:' + this.left + 'px;top:' + this.top + 'px;';
			html += '  width:' + this.width + 'px;height:' + this.height + 'px;"\n';
			html += '  onmousedown="javascript:filterBarrette2Down(\'' + this.filterId + '\');">\n';
			if (this.debug) {
				var dbgLeft = this.marginL + this.left + this.width + this.marginR, dbgTop = this.top + this.marginT;
				html += '  <div id="' + this.filterId + 'dbgMain" style="position:absolute;';
				html += '    left:' + dbgLeft + 'px;top:' + dbgTop + 'px;z-index:410;background-color:#DDFFDD;">\n';
				html += '   <div style="width:160px;background-color:#4444FF;cursor:pointer;font-weight:bold;color:#FFFFFF;">\n';
				html += '     <span onclick="javascript:snf_switch(\'' + this.filterId + 'dbg\');">';
				html += this.filterId;
				html += '   [DEBUG]</span>';
				html += '     <span onclick="javascript:snf_setHTML(\'' + this.filterId + 'dbg\',\'\');"><u>clear</u></span>';
				html += '   </div>\n';
				html += '   <div id="' + this.filterId + 'dbg" style="display:none;color:#555588;width:400px;"></div>\n';
				html += '  </div>\n';
			}
			html += ' <div id="' + this.lblId + '" class="css_filterBarrette2_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_filterBarrette2_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_filterBarrette2_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.bkgGradId + '" class="css_filterBarrette2_bkgGrad"';
			html += '   style="left:' + this.leftBkgOff + 'px;top:' + this.topBkgOff + 'px;';
			html += '   width:' + this.bkgWidth + 'px;height:6px;"></div>\n';
			html += ' <div id="' + this.bkgOffId + '" class="css_filterBarrette2_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.bkgOnId + '" class="css_filterBarrette2_bkgOn"';
				html += '   style="left:' + this.leftBtn + 'px;top:' + (this.topBtn + 1) + 'px;';
				html += '   width:' + this.bkgWidth + 'px;height:' + this.btnHeight + 'px;"></div>\n';
				html += ' <div id="' + this.btnId + '" class="css_filterBarrette2_btn"';
				html += '   style="left:' + (this.leftBtn + this.xMax) + 'px;top:' + (this.topBtn - 3) + 'px;';
				html += '   width:' + this.btnWidth + 'px;height:' + this.btnHeight + 'px;"></div>\n';
			}else if (this.filterWay === 'min') {
				html += ' <div id="' + this.bkgOnId + '" class="css_filterBarrette2_bkgOn"';
				html += '   style="left:' + this.leftBtn + 'px;top:' + (this.topBtn + 1) + 'px;';
				html += '   width:' + this.bkgWidth + 'px;height:' + this.btnHeight + 'px;"></div>\n';
				html += ' <div id="' + this.btnId + '" class="css_filterBarrette2_btn"';
				html += '   style="left:' + (this.leftBtn + this.xMin) + 'px;top:' + (this.topBtn - 3) + 'px;';
				html += '   width:' + this.btnWidth + 'px;height:' + this.btnHeight + 'px;"></div>\n';
			}else {//both
				html += ' <div id="' + this.bkgOnId + '" class="css_filterBarrette2_bkgOn"';
				html += '   style="left:' + this.leftBtn + 'px;top:' + (this.topBtn + 1) + 'px;';
				html += '   width:' + this.bkgWidth + 'px;height:' + this.btnHeight + 'px;"></div>\n';
				html += ' <div id="' + this.btnId + '" class="css_filterBarrette2_btn"';
				html += '   style="left:' + (this.leftBtn + this.xMin) + 'px;top:' + (this.topBtn - 3) + 'px;';
				html += '   width:' + this.btnWidth + 'px;height:' + this.btnHeight + 'px;"></div>\n';
				html += ' <div id="' + this.btn2Id + '" class="css_filterBarrette2_btn"';
				html += '   style="left:' + (this.leftBtn + this.xMax) + 'px;top:' + (this.topBtn - 3) + 'px;';
				html += '   width:' + this.btnWidth + 'px;height:' + this.btnHeight + 'px;"></div>\n';
			}
			document[WRT](html + '</div>\n');
			//this.dbg('margin-top = "'+this.marginT+'"');
			//this.dbg('margin-right = "'+this.marginL+'"');
			//this.dbg('margin-bottom = "'+this.marginB+'"');
			//this.dbg('margin-left = "'+this.marginL+'"');
		};

		var isOpera = navigator.userAgent.toLowerCase().indexOf('opera') > 0;
		var isFirefox = navigator.userAgent.toLowerCase().indexOf('firefox') > 0;
		var isNetscape = false;
		var isIE = !(isOpera || isFirefox || isNetscape);

		if (isFirefox) {
			this.getNavigatorName = function(e) { return 'FF'; };
			this.getEvent = function(e) { return e; };
			this.getSrcElement = function(e) { return e.target; };
			this.getToElement = function(e) { return e.relatedTarget; };
			this.getParentElement = function(e) { return e.rangeParent; };
			this.getToParentElement = function(e) { return e.relatedTarget.offsetParent; };
			this.getOffsetX = function(e) { return e.layerX; };
			this.firstButton = function(e) { return !e.button; };
		}else if (isIE) {
			this.getNavigatorName = function(e) { return 'IE'; };
			this.getEvent = function(e) { return window.event; };
			this.getSrcElement = function(e) { return e.srcElement; };
			this.getToElement = function(e) { return e.toElement; };
			this.getParentElement = function(e) { return e.srcElement ? e.srcElement.parentElement : undefined; };
			this.getToParentElement = function(e) { return e.toElement ? e.toElement.parentElement : undefined; };
			this.getOffsetX = function(e) { return e.offsetX; };
			this.firstButton = function(e) { return !!e.button; };
		}else if (isOpera) {
			this.getNavigatorName = function(e) { return 'OP'; };
			this.getEvent = function(e) { return e; };
			this.getSrcElement = function(e) { return e.target; };
			this.getToElement = function(e) { return e.relatedTarget; };
			this.getParentElement = function(e) { return e.rangeParent; };
			this.getOffsetX = function(e) { return e.layerX; };
			this.firstButton = function(e) { return !e.button; };
		}else {
			this.getNavigatorName = function(e) { return 'NN'; };

		}

		this.onFilterChange = function(posBtn1,posBtn2) { return; };
		this.onFilterRefresh = noop;

		//this.testEvent=function(e){ recordEvent('<br>testEvent ['+this.getNavigatorName(e)+']: '+show(e));return ; }
		//  this.showVar=function(v,p1,p2,p3,p4){this.dbg(v+"="+eval(v));};
		this.updateDisplay = function(e,first) {
			var offsetLeft = this.getOffsetX(e) - this.btnOffsetX;
			var amplitude = this.xMax - this.xMin;
			if (this.getSrcElement(e).id == this.btnId || this.getSrcElement(e).id == this.btn2Id || (this.getSrcElement(e).id == this.bkgOnId && this.filterWay != 'max')) {
				offsetLeft += snf_getLeft(this.getSrcElement(e).id) - this.leftBtn;
			} else if (!first) {
				offsetLeft += snf_getLeft(this.getSrcElement(e).id);
			}
			if (this.filterWay != 'both') {
				offsetLeft = Math.max(Math.min(offsetLeft, this.xMax), this.xMin);
				document.getElementById(this.btnId).style.left = this.leftBtn + offsetLeft + 'px';
				document.getElementById(this.bkgOnId).style.width = this.btnWidth + offsetLeft + 'px';
				if (this.filterWay == 'max') {
					document.getElementById(this.bkgOnId).style.width = this.btnWidth + offsetLeft + 'px';
					if (this.onFilterChange) {
						try { this.onFilterChange(parseInt((snf_getLeft(this.btnId) - this.leftBtn) * 100 / amplitude, 10)); }catch (e1) {}
					}
				}else if (this.filterWay == 'min') {
					document.getElementById(this.bkgOnId).style.left = this.leftBtn + offsetLeft + 'px';
					document.getElementById(this.bkgOnId).style.width = this.xMax - offsetLeft + this.btnWidth + 'px';
					if (this.onFilterChange) {
						try { this.onFilterChange(parseInt((snf_getLeft(this.btnId) - this.leftBtn) * 100 / amplitude, 10)); }catch (e2) {}
					}
				}
			}else {
				if (this.isSecond) {
					offsetLeft = Math.max(Math.min(offsetLeft, this.xMax), snf_getLeft(this.btnId) - this.leftBtn + 5);
					document.getElementById(this.btn2Id).style.left = this.leftBtn + offsetLeft + 'px';
				}else {
					offsetLeft = Math.max(Math.min(offsetLeft, snf_getLeft(this.btn2Id) - this.leftBtn - 5), this.xMin);
					document.getElementById(this.btnId).style.left = this.leftBtn + offsetLeft + 'px';
				}
				document.getElementById(this.bkgOnId).style.width = snf_getLeft(this.btn2Id) - snf_getLeft(this.btnId) + this.btnWidth + 'px';
				document.getElementById(this.bkgOnId).style.left = snf_getLeft(this.btnId) + 'px';
				if (this.onFilterChange) {
					try { this.onFilterChange(parseInt((snf_getLeft(this.btnId) - this.leftBtn) * 100 / amplitude, 10), parseInt((snf_getLeft(this.btn2Id) - this.leftBtn) * 100 / amplitude, 10)); }catch (e3) {}
				}
			}
			return;
		};
		this.mouseDown = function(e) {
			e = this.getEvent(e);
			if (this.firstButton(e)) {
				if (this.getSrcElement(e) && (this.getSrcElement(e).id == this.btnId || this.getSrcElement(e).id == this.btn2Id)) {
					this.btnOffsetX = this.getOffsetX(e);
				} else {
					this.btnOffsetX = this.btnWidth / 2;
				}
				if (this.getSrcElement(e) && this.getSrcElement(e).id == this.btnId) {
					this.isSecond = false;
				} else if (this.getSrcElement(e) && this.getSrcElement(e).id == this.btn2Id) {
					this.isSecond = true;
				} else {
					var xMoy = (snf_getLeft(this.btn2Id) + snf_getLeft(this.btnId) + this.btnWidth) / 2;
					var posCur = snf_getLeft(this.getSrcElement(e).id) + this.getOffsetX(e);
					this.isSecond = posCur > xMoy;
				}
				this.isDown = true;
				snf_setCursor(this.btnId, 'e-resize');
				if (this.filterWay === 'both') {
					snf_setCursor(this.btn2Id, 'e-resize');
				}
				snf_setCursor(this.bkgOnId, 'e-resize');
				snf_setCursor(this.bkgOffId, 'e-resize');
				this.updateDisplay(e, true);
			}
			return;
		};
		this.mouseMove = function(e) {
			e = this.getEvent(e);
			if (this.isDown) {//&&this.getSrcElement(e)&&this.getSrcElement(e).id==this.filterId){
				this.updateDisplay(e, false);
			}
			return;
		};
		this.mouseRelease = function(e) {
			e = this.getEvent(e);

			//this.dbg("-----------------------------------");
			//this.showVar("this.getParentElement(p1).id",e);
			//this.showVar("this.getSrcElement(p1).id",e);
			//this.showVar("this.firstButton(p1)",e);
			//this.showVar("this.filterId");
			//this.showVar("this.btnId");
			//this.showVar("this.bkgOffId");
			//this.showVar("this.bkgOnId");
			//this.dbg("-----------------------------------");

			if (this.isDown &&
					(e.type == 'mouseout' && this.getSrcElement(e) &&
					((!this.getToElement(e)) ||
					(this.getToElement(e).id != this.filterId &&
					this.getToElement(e).id != this.btnId &&
					this.getToElement(e).id != this.btn2Id &&
					this.getToElement(e).id != this.bkgOffId &&
					this.getToElement(e).id != this.bkgOnId &&
					this.getToElement(e).id != this.bkgGradId &&
					this.getToElement(e).id != this.lblId &&
					this.getToElement(e).id != this.lblLeftId &&
					this.getToElement(e).id != this.lblRightId)) ||
					e.type == 'mouseup' && this.firstButton(e))) {
				snf_setCursor(this.btnId, 'pointer');
				if (this.filterWay === 'both') {
					snf_setCursor(this.btn2Id, 'pointer');
				}
				snf_setCursor(this.bkgOnId, 'default');
				snf_setCursor(this.bkgOffId, 'default');
				this.isDown = false;
				if (this.onFilterRefresh) {
					try { this.onFilterRefresh(); }catch (e1) {}
				}
				window.filterBarrette2Id = null;
			}
			return;
		};

		this.writeHTML();
	};

	window.filterBarrette2Id = null;
	window.barretteArray = [];
	window.filterBarrette2Down = function(id) { try { window.filterBarrette2Id = id; }catch (e) {} };
	window.filterBarrette2IsOk = function() { return window.filterBarrette2Id && window.barretteArray && window.barretteArray[window.filterBarrette2Id]; };
	window.Barrette2Manager = function() {
		if (!mouseEventManager) { return; }
		this.ml = new window.MouseListener();
		this.ml.mouseDown = function(e) { if (window.filterBarrette2IsOk()) { window.barretteArray[window.filterBarrette2Id].mouseDown(e); } };
		this.ml.mouseMove = function(e) { if (window.filterBarrette2IsOk()) { window.barretteArray[window.filterBarrette2Id].mouseMove(e); } };
		this.ml.mouseUp = function(e) { if (window.filterBarrette2IsOk()) { window.barretteArray[window.filterBarrette2Id].mouseRelease(e); } };
		this.ml.mouseOut = function(e) { if (window.filterBarrette2IsOk()) { window.barretteArray[window.filterBarrette2Id].mouseRelease(e); } };
		mouseEventManager.addEventListener(this.ml);
	};
	window.initBodyEvents = function() { var barrette = new window.Barrette2Manager(); };
}());

