
/*********************************/
/************ Barrettes **********/
/*********************************/
//function show(arr){ var r=""; for(var i in arr) r+=","+i+"="+arr[i]+"<br>"; if(r.length>1) r="["+r.substring(1,r.length-1)+"]"; return r; }
function noop(){ return void(0); }

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

  this.eventValueActivated=function(value){ return void(0); };
  this.eventValueDesactivated=function(value){ return void(0); };
  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;
    if(cnt>0){
      for(var i=0;i<this.arrBrnMaxiIndex;++i){ try{ this.eventValueActivated(this.arrBrnMaxi[i]); }catch(e){} }
      for(var i=this.arrBrnMaxiIndex;i<cnt;++i){ try{ this.eventValueDesactivated(this.arrBrnMaxi[i]); }catch(e){} }
      try{ snf_setHTML(this.maxLblId,this.formatValue(this.arrBrnMaxi[this.arrBrnMaxiIndex])); }catch(e){}
      var curImgSz=parseInt(((this.arrBrnMaxiIndex+1)*parseInt(this.imSz))/this.arrBrnMaxiCnt);
      var imageElt=document.getElementById(this.imId);
      if(imageElt) imageElt.style.width=curImgSz+'px';
      try{ refreshDisplay(); }catch(e){}
    }
  }
  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==undefined) 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){ 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*$/;
function Barrette2(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"));
    this.marginR=parseInt(margin.replace(MARGIN_PATTERN,"$2"));
    this.marginB=parseInt(margin.replace(MARGIN_PATTERN,"$3"));
    this.marginL=parseInt(margin.replace(MARGIN_PATTERN,"$4"));
  }
  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="";
    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;
      var 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.writeln(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==0; };
  }else if(isIE){
    this.getNavigatorName=function(e){ return "IE"; };
    this.getEvent=function(e){ return 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==1; };
  }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==0; };
  }else{
    this.getNavigatorName=function(e){ return "NN"; };

  }

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

  //this.testEvent=function(e){ recordEvent('<br>testEvent ['+this.getNavigatorName(e)+']: '+show(e));return void(0); }
//  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)); }catch(e){} }
      }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)); }catch(e){} }
      }
    }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), parseInt((snf_getLeft(this.btn2Id)-this.leftBtn)*100/amplitude)); }catch(e){} }
    }
    return void(0);
  }
  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 void(0);
  }
  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 void(0);
  }
  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
//          (!this.getParentElement(e))
//          || this.getParentElement(e).id!=this.filterId
        )
//        || (!this.getToParentElement(e))
//        || this.getToParentElement(e).id!=this.filterId
      )
      || 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(e){} }
      filterBarrette2Id=null;
    }
    return void(0);
  }

  this.writeHTML();
}

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