/*************
* Project : ScrolLabel 2
* File    : 
* Descr.  : DHTML scroller (compatibility test with IE6, NN7, IE5.1.6 (Mac), Safari)
* Author  : BS - 08.2003 / VR - 11.2003 / BS - 08.2008
*************/

//define constants and default values
var SL2_ARRAY=new Array(),
    SL2_BODY,
    SL2_STEP=10,      //default step
    SL2_INTERVAL=100, //default interval
    SL2_IMG_PATH='/_img/scrolLabel',
    SL2_SPACER_SRC='spacer.gif',
    SL2_BU_SRC='btnUp.gif',
    SL2_BU_HEIGHT=7,
    SL2_BD_SRC='btnDown.gif',
    SL2_BD_HEIGHT=7,
    SL2_BART_SRC='barTop.gif',
    SL2_BART_HEIGHT=2,
    SL2_BAR_SRC='barMiddle.gif',
    SL2_BARB_SRC='barBottom.gif',
    SL2_BARB_HEIGHT=2,
    SL2_BARBG_SRC='spacer.gif',
    SL2_BAR_WIDTH=10,
    SL2_SPACING=2,
    SL2_TXT_CLASS=null;
    
function ScrolLabel2(id,w,h,txt,v,x,y,p,bolHideWhenSmaller) {
  if (bolHideWhenSmaller==null) bolHideWhenSmaller=false;
  if(!document.getElementById || !document.getElementsByTagName) {
    if(typeof(oldBrowser)=="function") {
      oldBrowser();
    } else {
      alert("Your browser doesn't support this feature.");
    }
    return;
  }
  if(h<30 || w<30) {
    alert('Scroller is too small');
    return;
  }
  //SL2_BODY=document.getElementsByTagName("body")[0];
  SL2_BODY = document.getElementById(id);
  SL2_ARRAY[id]=this;
 
  //properties
  this.id=id;
  this.h=h;
  this.w=w;
  this.SI=null;  //setInterval ID
  this.pY=0;
  this.x=(x==null)?0:x;
  this.y=(y==null)?0:y;
  this.MS=false; //move slider
  this.eY=null;

  //properties from parameters array
  if(p === null || typeof(p)!="object") p=new Array();
  this.step=(p['step'])?p['step']:SL2_STEP;
  this.intvl=p['interval']?p['interval']:SL2_INTERVAL;
  this.imgPath=p['img_path']?p['img_path']:SL2_IMG_PATH;
  this.spacerSrc=this.imgPath+'/'+(p['spacer_src']?p['spacer_src']:SL2_SPACER_SRC);
  this.btnUpSrc=this.imgPath+'/'+(p['bu_src']?p['bu_src']:SL2_BU_SRC);
  this.btnUpH=p['bu_height']?p['bu_height']:SL2_BU_HEIGHT;
  this.btnDnSrc=this.imgPath+'/'+(p['bd_src']?p['bd_src']:SL2_BD_SRC);
  this.btnDnH=p['bd_height']?p['bd_height']:SL2_BD_HEIGHT;
  this.barTopSrc=this.imgPath+'/'+(p['bar_top_src']?p['bar_top_src']:SL2_BART_SRC);
  this.barTopH=p['bar_top_height']?p['bar_top_height']:SL2_BART_HEIGHT;
  this.barMSrc=this.imgPath+'/'+(p['bar_middle_src']?p['bar_middle_src']:SL2_BAR_SRC);
  this.barBtmSrc=this.imgPath+'/'+(p['bar_bottom_src']?p['bar_bottom_src']:SL2_BARB_SRC);
  this.barBtmH=p['bar_bottom_height']?p['bar_bottom_height']:SL2_BARB_HEIGHT;
  this.barBgSrc=this.imgPath+'/'+(p['bar_bg_src']?p['bar_bg_src']:SL2_BARBG_SRC);
  this.barW=p['bar_width']?p['bar_width']:SL2_BAR_WIDTH;
  this.spacing=p['spacing']?p['spacing']:SL2_SPACING;
  this.txtClass=p['txt_class']?p['txt_class']:SL2_TXT_CLASS
  
  //methods
  this.scroll=SL2_scroll;
  this.startScroll=SL2_startScroll;
  this.stopScroll=SL2_stopScroll;
  this.wheel=SL2_wheel;
  this.clickBar=SL2_clickBar;
  this.moveSlider=SL2_moveSlider;
  this.mouseupSlider=SL2_mouseupSlider;
  this.mousedownSlider=SL2_mousedownSlider;
  this.moveupSlider=SL2_moveupSlider;
  this.movedownSlider=SL2_movedownSlider;
  this.stopScrollSlider=SL2_stopMoveUpSlider;
  this.doMouseWheel=SL2_doMouseWheel;
  this.setXY=SL2_setXY;
  
  /*
    SAFARI
  */
  var divCtn = SL2_BODY.appendChild(document.createElement("DIV"));
  divCtn.setAttribute("id","divCtn_"+id);
  divCtn.style.position="absolute";
  divCtn.style.top=0;
  divCtn.style.left=0;
  divCtn.style.height=h;
  divCtn.style.width=(w+this.spacing+this.barW);
  divCtn.style.overflow="hidden";
  divCtn.style.visibility="hidden";
  
  var divTxt = divCtn.appendChild(document.createElement("DIV"));
  divTxt.setAttribute("id", "divTxt_"+id);
  divTxt.style.position="absolute";
  divTxt.style.top=0;
  divTxt.style.left=0;
  divTxt.style.width=w;
  divTxt.style.overflow="hidden";
  divTxt.setAttribute("onmousewheel","SL2_ARRAY['"+id+"'].wheel()");
  if (this.txtClass!=null){
    divTxt.setAttribute("class", this.txtClass);
  }
  divTxt.innerHTML = txt;
  
  var divBU = divCtn.appendChild(document.createElement("DIV"));
  divBU.setAttribute("id", "divBU_"+id);
  divBU.style.position="absolute";
  divBU.style.height=this.btnUpH;
  divBU.style.width=this.barW;
  var imgBU = divBU.appendChild(document.createElement("IMG"));  
  imgBU.setAttribute("src",this.btnUpSrc);
  imgBU.height=this.btnUpH;
  imgBU.width=this.barW;
  imgBU.border=0;
  
  var divBarBg = divCtn.appendChild(document.createElement("DIV"));
  divBarBg.setAttribute("id","divBarBg_"+id);
  divBarBg.style.position="absolute";
  divBarBg.style.height=h-this.btnUpH-this.btnDnH;
  divBarBg.style.width=this.barW;
  
  var imgBarBg=divBarBg.appendChild(document.createElement("IMG"));
  imgBarBg.setAttribute("src",this.barBgSrc);
  imgBarBg.height=h-this.btnUpH-this.btnDnH;
  imgBarBg.width=this.barW;
  imgBarBg.border=0;

  var divBar = divCtn.appendChild(document.createElement("DIV"));
  divBar.setAttribute("id", "divBar_"+id);
  divBar.style.position="absolute";
  divBar.style.height=h-this.btnUpH-this.btnDnH;
  divBar.style.width=this.barW;
  divBar.style.zIndex=256;
  divBar.ondrag=SL2_ReturnFalse;
  var imgBar=divBar.appendChild(document.createElement("IMG"));
  imgBar.setAttribute("src",this.spacerSrc);
  imgBar.height=h-this.btnUpH-this.btnDnH;
  imgBar.width=this.barW;
  imgBar.border=0;
  
  var divBD = divCtn.appendChild(document.createElement("DIV"));
  divBD.setAttribute("id", "divBD_"+id);
  divBD.style.position="absolute";
  divBD.style.height=this.btnDnH;
  divBD.style.width=this.barW;
  var imgBD = divBD.appendChild(document.createElement("IMG"));  
  imgBD.setAttribute("src",this.btnDnSrc);
  imgBD.height=this.btnDnH;
  imgBD.width=this.barW;
  imgBD.border=0;
  
  var divSlider=divCtn.appendChild(document.createElement("DIV"));
  divSlider.setAttribute("id", "divSlider_"+id);
  divSlider.style.position="absolute";
  divSlider.style.width=this.barW;
  
  /*
   END SAFARI
  */
  
  /*
  //create scroller
  var strHTML;
  strHTML =('<div id="divCtn_'+id+'" style="position:absolute;top:0;left:0;height:'+h+';width:'+(w+this.spacing+this.barW)+';overflow:hidden;visibility:hidden"><div id="divTxt_'+id+'" style="position:absolute;width:'+w+';overflow:hidden" onmousewheel="SL2_ARRAY[\''+id+'\'].wheel()" '+((this.txtClass!=null)?(' class="'+this.txtClass+'"'):'')+'>');
  strHTML+=(txt);
  strHTML+=('</div><div id="divBU_'+id+'" style="position:absolute;height:'+this.btnUpH+';width:'+this.barW+'"><img src="'+this.btnUpSrc+'" height="'+this.btnUpH+'" width="'+this.barW+'" border="0" alt="" onmousedown="SL2_ARRAY[\''+id+'\'].startScroll(1);" onmouseup="SL2_ARRAY[\''+id+'\'].stopScroll();" onmouseout="SL2_ARRAY[\''+id+'\'].stopScroll();"></div><div id="divBarBg_'+id+'" style="position:absolute;height:'+(h-this.btnUpH-this.btnDnH)+';width:'+this.barW+'"><img src="'+this.barBgSrc+'" height="'+(h-this.btnUpH-this.btnDnH)+'" width="'+this.barW+'" border="0" alt=""></div><div id="divBar_'+id+'" style="position:absolute;height:'+(h-this.btnUpH-this.btnDnH)+';width:'+this.barW+';z-index:256" ondrag="return false;"><img src="'+this.spacerSrc+'" height="'+(h-this.btnUpH-this.btnDnH)+'" width="'+this.barW+'" border="0" alt=""></div><div id="divBD_'+id+'" style="position:absolute;height:'+this.btnDnH+';width:'+this.barW+'"><img src="'+this.btnDnSrc+'" height="'+this.btnDnH+'" width="'+this.barW+'" border="0" alt="" onmousedown="SL2_ARRAY[\''+id+'\'].startScroll(-1);" onmouseup="SL2_ARRAY[\''+id+'\'].stopScroll();" onmouseout="SL2_ARRAY[\''+id+'\'].stopScroll();"></div><div id="divSlider_'+id+'" style="position:absolute;width:'+this.barW+'"></div></div>');
  if (ie4){
    SL2_BODY.insertAdjacentHTML("beforeEnd", strHTML);
  } else {
    SL2_BODY.innerHTML+=strHTML;
  }
  */
  
  //other properties
  this.dC=SL2_gE("divCtn_"+id);
  this.dT=SL2_gE("divTxt_"+id);
  this.dBU=SL2_gE("divBU_"+id);
  this.dBD=SL2_gE("divBD_"+id);
  this.dB=SL2_gE("divBar_"+id);
  this.dBB=SL2_gE("divBarBg_"+id);
  this.dS=SL2_gE("divSlider_"+id);
  this.tH=this.dT.offsetHeight;
  this.dT.style.height=h;
  
  //attach events to bar and slider
  this.dB.onclick=this.clickBar;
  this.dB.onmousemove=this.moveSlider;
  this.dB.onmousedown=this.mousedownSlider;
  this.dB.onmouseup=this.mouseupSlider;
  this.dB.onmouseout=this.mouseupSlider;
  this.dBU.onmousedown=this.moveupSlider;
  this.dBU.onmouseup=this.stopScrollSlider;
  this.dBU.onmouseout=this.stopScrollSlider;
  this.dT.onmousewheel=this.doMouseWheel;
  this.dBD.onmousedown=this.movedownSlider;
  this.dBD.onmouseup=this.stopScrollSlider;
  this.dBD.onmouseout=this.stopScrollSlider;
  
  
  //create slider
  this.sH=Math.min((h-this.btnUpH-this.btnDnH)*h/this.tH,h-this.btnUpH-this.btnDnH);
  this.noMove=(this.sH==h-this.btnUpH-this.btnDnH);
  this.sY=this.btnUpH;
  tSteps=this.tH-h;
  sSteps=(h-this.btnUpH-this.btnDnH)-this.sH;
  this.rST=sSteps/tSteps;
  
  if (bolHideWhenSmaller && this.tH < h){
    this.dBU.style.visibility="hidden";
    this.dBD.style.visibility="hidden";
  } else {
    this.dS.innerHTML='<img src="'+this.barTopSrc+'" height="'+this.barTopH+'" width="'+this.barW+'" border="0" alt=""><br><img src="'+this.barMSrc+'" height="'+(this.sH-this.barTopH-this.barBtmH)+'" width="'+this.barW+'" border="0" alt=""><br><img src="'+this.barBtmSrc+'" height="'+this.barBtmH+'" width="'+this.barW+'" border="0" alt="">';
  }
  
  //position items
  SL2_putAtXY(this.dBU,w+this.spacing,0);
  SL2_putAtXY(this.dBD,w+this.spacing,h-this.btnDnH);
  SL2_putAtXY(this.dB,w+this.spacing,this.btnUpH);
  SL2_putAtXY(this.dBB,w+this.spacing,this.btnUpH);
  SL2_putAtXY(this.dS,w+this.spacing,this.btnUpH);
  
  //position and show scroller
  SL2_putAtXY(this.dC,x,y);
  if(v) this.dC.style.visibility="visible";
  
}

function SL2_ReturnFalse(){
  return false;
}

function SL2_gE(s) {
  return document.getElementById(s);
}

function SL2_putAtXY(o,x,y) {
  if(!o) return;
  if(x!=null) o.style.left=x;
  if(y!=null) o.style.top=y;
}

function SL2_setXY(x,y) {
  if(x!=null) this.x=x;
  if(y!=null) this.y=y;
  SL2_putAtXY(this.dC,x,y);
}
function SL2_wheel() {
  if(event) this.scroll(Math.floor(event.wheelDelta/1));
}
function SL2_mousedownSlider(e) {
  //get object
  var o=SL2_ARRAY[this.id.substring(7,this.id.length)];
  o.MS=true;
}
function SL2_mouseupSlider(e) {
  //get object
  var o=SL2_ARRAY[this.id.substring(7,this.id.length)];
  o.eY=null;
  o.MS=false;
}
function SL2_moveupSlider(e) {
  //get object
  var o=SL2_ARRAY[this.id.substring(6,this.id.length)];
  o.startScroll(1);
}
function SL2_movedownSlider(e){
  //get object
  var o=SL2_ARRAY[this.id.substring(6,this.id.length)];
  o.startScroll(-1);
}
function SL2_stopMoveUpSlider(e){
  //get object
  var o=SL2_ARRAY[this.id.substring(6,this.id.length)];
  o.stopScroll();
}
function SL2_doMouseWheel(e){
  var o=SL2_ARRAY[this.id.substring(7,this.id.length)];
  o.wheel();
}
function SL2_moveSlider(e) {
  //get object
  var o=SL2_ARRAY[this.id.substring(7,this.id.length)];
  if(o.MS) {
    var yPos=(document.all)?(event.y-o.y):(e.pageY-o.y);
    if(o.eY) {
      var dY=o.eY-yPos;
      var i=(dY<0)?-1:1;
      var s=Math.ceil(dY*o.tH/o.sH);
      o.scroll(i,o.sY-(o.eY-yPos));
    }
    o.eY=yPos;
  }
}
function SL2_clickBar(e) {
  //get object
  var o=SL2_ARRAY[this.id.substring(7,this.id.length)];
  var yPos=(document.all)?(event.y-o.y):(e.pageY-o.y);
  var s=Math.ceil(o.tH*o.sH/(o.h-16));
  if(yPos-o.sY<0) {
    o.scroll(s);
  } else if(yPos-o.sY>o.sH) {
    o.scroll(-s);
  }
}
function SL2_startScroll(d) {
  this.SI=setInterval("SL2_ARRAY['"+this.id+"'].scroll("+(d*SL2_STEP)+");",SL2_INTERVAL);
}

function SL2_stopScroll() {
  clearInterval(this.SI);
}
function SL2_scroll(s,sY) {
  if(this.noMove) return;
  if(sY==null) {
    this.pY+=s;
    this.sY=Math.ceil(-this.pY*this.rST)+this.btnUpH;
  } else {
    this.pY=Math.ceil((this.btnUpH-this.sY)/this.rST);
    this.sY=sY;
  }
  if(s<0) {
    if(-this.pY+this.h>this.tH) {
      this.stopScroll();
      this.pY=this.h-this.tH;
      this.sY=Math.ceil(-this.pY*this.rST)+this.btnUpH;
      this.eY=null;
      this.MS=false;
    }
  } else {
    if(this.pY>0) {
      this.stopScroll();
      this.pY=0;
      this.sY=Math.ceil(-this.pY*this.rST)+this.btnUpH;
      this.eY=null;
      this.MS=false;
    }
  }
  //move text
  this.dT.style.height=-this.pY+this.h;
  SL2_putAtXY(this.dT,null,this.pY);
  
  //move slider
  SL2_putAtXY(this.dS,null,this.sY)
}
