/***
  IMAGE-SCROLL.JS
  ===============
    2/19/10
    -------
    This module takes an array of images and scrolls them at intervals. It scrolls either left or right and
    either continuously or at preset intervals.

    The array, named iArray0, elements hold the scrolling image data.
         0 = image location
         1 = start order
         2 = current order in line
         3 = object ID
         4 = upper left start pos (default pos) of object
         5 = description for popup
         6 = product supplier URL

    This data is in an additional script module which must be declared before this by the parent HTML. That data must
    include the following global variables.

    iArray0 = the data array
    iAsize = number of elements in the array
    innerTime = length of the waits while images are "sliding"
    outerTime = length of the "pause" between image slides
    hlit = this is the leftmost position where an image is visible (but not shown--hidden by a higher element)
    hfin = this is the rightmost position where an image is visible (but not shown--hidden by a higher element)
    hbgn = this is the end of the queue point (note it is off-screen and NOT visible)
    roof = current top of the images
    lside = left side of vertically sliding images
    ispeed = speed (in pixels) during the inner loop
    
    * Function names using "slide" imply a slide show where images are shown for a period of time
    * Function names using "scroll" imply a scrolling banner with continuous motion
***/

var slideID=0;      // used for the horizontal scrolling    // numbering them would allow for future expansion in order to
                                                            // manage several scrollers (god forbid!)

function slideImgOuter(){
  clearTimeout(slideID);
  return slideID=setTimeout("slideImgInner()", innerTime);
}

function slideImgInner(){
  var dun=-1;
  clearTimeout(slideID);
  for (i=0; i<iAsize; i++){
    var e=iArray0[i][3];
    switch (dir){
      case 0:                 // moving right
        var lft=getObjLeft(e);
        if (lft>=hfin){
          var s=""+hbgn+"px";
          hideObj(e);
          moveObj(e, s, roof);
          dun=i;
        }
        else {
          lft+=ispeed;
          if (lft>=hlit) showObj(e);
          var s=""+lft+"px";
          moveObj(e, s, roof);
        }
        break;
      case 1:                 // moving left
        var lft=getObjLeft(e);
        if (lft<=hbgn){
          var s=""+(hfin)+"px";
          moveObj(e, s, roof);
          showObj(e);
          dun=i;
        }
        else {
          lft-=ispeed;
          if (lft<=hlit) hideObj(e);
          var s=""+lft+"px";
          moveObj(e, s, roof);
        }
        break;
      case 2:                 // moving down
        var tp=getObjTop(e);
        if (tp>=vfin){
          var s=""+vbgn+"px";
          moveObj(e, lside, s);
          hideObj(e);
          dun=i;
        }
        else {
          tp+=ispeed;
          var s=""+tp+"px";
          moveObj(e, lside, s);
          if (tp>=vlit) showObj(e);
        }
        break;
      case 3:                 // moving up
        var tp=getObjTop(e);
        if (tp<=vbgn){
          var s=""+(vfin-2)+"px";
          moveObj(e, lside, s);
          hideObj(e);
          dun=i;
        }
        else {
          tp-=ispeed;
          if (tp<=vlit) hideObj(e);          
          var s=""+tp+"px";
          moveObj(e, lside, s);
          if (tp<=vfin) showObj(e);
        }
        break;
    }
  }
  if (dun>=0) return slideID=setTimeout("slideImgOuter()", outerTime);
  else return slideID=setTimeout("slideImgInner()", innerTime);
}

function scrollImgs(){
  clearTimeout(slideID);
  for (i=0; i<iAsize; i++){
    var e=iArray0[i][3];
    switch (dir){
      case 0:                 // moving right
        var lft=getObjLeft(e);
        if (lft>=hfin){
          var s=""+hbgn+"px";
          hideObj(e);
          moveObj(e, s, roof);
        }
        else {
          lft+=ispeed;
          if (lft>=hlit) showObj(e);
          var s=""+lft+"px";
          moveObj(e, s, roof);
        }
        break;
      case 1:                 // moving left
        var tp=getObjTop(e);
        if (tp<=vbgn){
          var s=""+vfin+"px";
          moveObj(e, lside, s);
          showObj(e);
        }
        else {
          tp-=ispeed;
          if (tp<=vlit) hideObj(e);
          var s=""+tp+"px";
          moveObj(e, lside, s);
        }
        break;
      case 2:                 // moving down
        var tp=getObjTop(e);
        if (tp>=vfin){
          var s=""+vbgn+"px";
          hideObj(e);
          moveObj(e, lside, s);
        }
        else {
          tp+=ispeed;
          if (tp>=vlit) showObj(e);
          var s=""+tp+"px";
          moveObj(e, lside, s);
        }
        break;
      case 3:                 // moving up
        var tp=getObjTop(e);
        if (tp<=vbgn){
          var s=""+vfin+"px";
          moveObj(e, lside, s);
          showObj(e);
        }
        else {
          tp-=ispeed;
          if (tp<=vlit) hideObj(e);
          var s=""+tp+"px";
          moveObj(e, lside, s);
        }
        break;
    }
  }
  return slideID=setTimeout("scrollImgs()", innerTime);
}
/*****************************
function beginScrollImgR(){
  clearTimeout(slideID);
  dir=0;
  for (i=0; i<iAsize; i++){
    var e=iArray0[i][3];
    var lft=getObjLeft(e);
    if (lft>=hfin){
      var s=""+hbgn+"px";
      hideObj(e);
      moveObj(e, s, roof);
    }
    else {
      lft+=ispeed;
      if (lft>=hlit) showObj(e);
      var s=""+lft+"px";
      moveObj(e, s, roof);
    }
  }
  return slideID=setTimeout("beginScrollImgR()", innerTime);
}

function beginScrollImgU(){
  clearTimeout(slideID);
//  dir=3;
  for (i=0; i<iAsize; i++){
    var e=iArray0[i][3];
    var tp=getObjTop(e);
    if (tp<=vbgn){
      var s=""+vfin+"px";
      moveObj(e, lside, s);
      showObj(e);
    }
    else {
      tp-=ispeed;
      if (tp<=vlit) hideObj(e);
      var s=""+tp+"px";
      moveObj(e, lside, s);
    }
  }
  return slideID=setTimeout("beginScrollImgU()", innerTime);
}

function beginScrollImgD(){
  clearTimeout(slideID);
//  dir=2;
  for (i=0; i<iAsize; i++){
    var e=iArray0[i][3];
    var tp=getObjTop(e);
    if (tp>=vfin){
      var s=""+vbgn+"px";
      hideObj(e);
      moveObj(e, lside, s);
      dun=i;
    }
    else {
      tp+=ispeed;
      if (tp>=vlit) showObj(e);
      var s=""+tp+"px";
      moveObj(e, lside, s);
    }
  }
  return slideID=setTimeout("beginScrollImgD()", innerTime);
}

function beginScrollImgL(){
  clearTimeout(slideID);
  dir=1;
  for (i=0; i<iAsize; i++){
    var e=iArray0[i][3];
    var lft=getObjLeft(e);
    if (lft<=hbgn){
      var s=""+(hfin)+"px";
      moveObj(e, s, roof);
      showObj(e);
    }
    else {
      lft-=ispeed;
      if (lft<=hlit) hideObj(e);
      var s=""+lft+"px";
      moveObj(e, s, roof);
    }
  }
  return slideID=setTimeout("beginScrollImgL()", innerTime);
}
********************************************************/

function stopScroll(){
  clearTimeout(slideID);
  return slideID=setTimeout("slideImgInner()", innerTime);    // this allows the queue to align correctly onscreen
//  return slideID=setTimeout("slideImgOuter()", outerTime);
}

function slPause(){
  return clearTimeout(slideID);
}

function slRestart(){
  return slideID=setTimeout("slideImgInner()", innerTime);
}

function initSlide(){
  return slideID=setTimeout("slideImgOuter()", outerTime);
}
function initImgsH(){
  var r0=getSty("block0");
  var r1=getSty("block1");
  
/******************/
  var d0=hspacer*hshow;
  var w=getObjWidth("block0");
  var ls=parseInt(lside)
  var lf=ls-w;
  r0.left=""+lf+"px";
  r1.left=""+(ls+d0)+"px";
  hfin=ls+d0;
  hlit=ls-hspacer;
  hbgn=hfin-(iAsize*hspacer);
/******************/  
  
  r0.top=""+(parseInt(roof)-10)+"px";  
  r1.top=r0.top;
  
  showObj("block0");
  showObj("block1");  
  var st=hfin;
  for (i=0; i<iAsize; i++){
    st-=hspacer;
    var s=""+st+"px";
    var e=iArray0[i][3];
    if (st<hlit){
      var y=getSty(e);
      y.visibility="hidden";
    }
    moveObj(e,s,roof);
  }
  return 0;
}
function initImgsV(){
  var d0=vspacer*vshow;
  var rf=parseInt(roof);
  vfin=rf+d0;
  moveObj('blk_dn',""+0+"",""+vfin+"");
  
  if (!ie){
    eu=getSty('up');
    ed=getSty('dn');
    eu.top="-3px";
    ed.top="-4px";
  }
//  vlit=rf-vspacer;
  vbgn=vfin-(iAsize*vspacer);
/******************/    
  
  var lf=parseInt(lside)-5;
  var lft=""+lf+"px";
  var st=vfin;
  for (i=0; i<iAsize; i++){
    st-=vspacer;
    var s=""+st+"px";
    var e=iArray0[i][3];
    if (st<vlit){
      var y=getSty(e);
      y.visibility="hidden";
    }
    moveObj(e,lside,s);  
  }
  return 0;
}
function initImgs(){ 
  if (dir>1) initImgsV(); else initImgsH();
  return initSlide();
}

// The following swaps scroll arrows and manages direction changes
var arrows=new Array(4);
for (i=0; i<4; i++) arrows[i]=new Array(3);
arrows[0][0]="img/rt_idle.png";
arrows[0][1]="img/rt_hover.png";
arrows[0][2]="img/rt_active.png";
arrows[1][0]="img/lft_idle.png";  
arrows[1][1]="img/lft_hover.png"; 
arrows[1][2]="img/lft_active.png";
arrows[2][0]="img/dn_idle2.png";  
arrows[2][1]="img/dn_hover2.png"; 
arrows[2][2]="img/dn_active2.png";
arrows[3][0]="img/up_idle.png";  
arrows[3][1]="img/up_hover.png"; 
arrows[3][2]="img/up_active.png";
var RT = 0;
var LF = 1;
var DN = 2;
var UP = 3;
var IDL = 0;
var HOV = 1;
var ACT = 2;

function dirArrowsV3(n,m){  // n = direction, m = mouseover (1), mouseout (0), mousedown (2) or mouseup (3)
  if (dir>1){       // vertical stuff
    if (m==0) 
      if (n==dir) return (n==DN)?arrows[DN][ACT]:arrows[UP][ACT]; // down arrow?
      else return (n==DN)?arrows[DN][IDL]:arrows[UP][IDL];
    if (m==2) dir=n; return (n==DN)?arrows[DN][HOV]:arrows[UP][HOV];
    if (m==1) return (n==DN)?arrows[DN][HOV]:arrows[UP][HOV];   // down arrow?
  }
  else {            // horizontal
    if (m==0) 
      if (n==dir) return (n==RT)?arrows[RT][ACT]:arrows[LF][ACT]; // right arrow?
      else return (n==RT)?arrows[RT][IDL]:arrows[LF][IDL];
    if (m==2) dir=n; return (n==RT)?arrows[RT][HOV]:arrows[LF][HOV];
    if (m==1) return (n==RT)?arrows[RT][HOV]:arrows[LF][HOV];   // right arrow?
  }   
}

