/*
  Multi directional scroller
  Multiple scrollers can run on same page
  
  Currently works for nav4up, ie4up (nav4 doesn't work when scroller viewed in a layer)
  Not currently working on Opera

*/

// Browser Sniffer
function ClientSnifferJr() {
  this.ua = navigator.userAgent.toLowerCase();
  this.major = parseInt(navigator.appVersion);
  this.minor = parseFloat(navigator.appVersion);
  // Opera
  this.opera = this.ua.indexOf('opera') != -1;
  if (this.opera) return;
  // MSIE
  this.ie = this.ua.indexOf('msie') != -1;
  if (this.ie) {
    this.ie3 = this.major == 2;
    this.ie4 = (
      this.major == 4 && this.ua.indexOf('msie 5.0') == -1 && this.ua.indexOf('msie 5.5') == -1 && this.ua.indexOf('msie 6.0') == -1
    );
    this.ie4up = this.major >= 4;
    this.ie5up = !this.ie3 && !this.ie4;
    return;
  }
  // Gecko, NN4+, and NS6
  this.gecko = this.ua.indexOf('gecko') != -1;
  this.nav = (
    this.ua.indexOf('mozilla') != -1
    && this.ua.indexOf('spoofer') == -1
    && this.ua.indexOf('compatible') == -1
  );
  if (this.nav) {
    this.nav4  = this.major == 4;
    this.nav4up= this.major >= 4;
    this.nav5up= this.major >= 5;
    return;
  }
  // Others
  this.hotjava = this.ua.indexOf('hotjava') != -1; 
  this.webtv = this.ua.indexOf('webtv') != -1;
  this.aol = this.ua.indexOf('aol') != -1; 
}

window.is = new ClientSnifferJr();

function showLayer(layer) {
  if (is.ie4up||is.gecko)
    layer.style.visibility = "inherit";
  if (is.opera)
    layer.style.visibility = "visible";
  if (is.nav4)
    layer.visibility = "show";
}

function moveLayerTo(layer, x, y) {
  if (is.ie4up||is.opera) {
    layer.style.left = x;
    layer.style.top  = y;
  }
  if (is.gecko) {
    layer.style.left = x + "px";
    layer.style.top  = y + "px";
  }
  if (is.nav4)
    layer.moveTo(x, y);
}

function moveLayerBy(layer, dx, dy) {
  if (is.ie4up||is.opera) {
    layer.style.pixelLeft += dx;
    layer.style.pixelTop  += dy;
  }
  if (is.gecko) {
    layer.style.left = (parseInt(layer.style.left) + dx) + "px";
    layer.style.top  = (parseInt(layer.style.top) + dy) + "px";
  }
  if (is.nav4)
    layer.moveBy(dx, dy);
}

function sizeBy(layer, dw, dh) {
	if (is.ie4up) {
	  layer.style.pixelWidth  = (getWidth(layer) + dw);
	  layer.style.pixelHeight = (getHeight(layer) + dh);
	}
	else if (is.gecko) {
		layer.style.width   = (parseInt(layer.style.width) + dw) + "px";
    layer.style.height  = (parseInt(layer.style.height) + dh) + "px";
  }
  else if (is.nav4) {
  	layer.document.width  = (layer.document.width + dw);
  	layer.document.height = (layer.document.height + dh);
  }
}

function getWidth(layer) {
  if (is.ie4up||is.opera) {
    if (layer.style.pixelWidth)
      return(layer.style.pixelWidth);
    else
      return(layer.clientWidth);
  }
  if (is.gecko) {
  	if (layer.style.width)
      return parseInt(layer.style.width);
  }
  if (is.nav4) {
    if (layer.document.width)
      return(layer.document.width);
    else
      return(layer.clip.right - layer.clip.left);
  }
  return(-1);
}

function getHeight(layer) {
  if (is.ie4up||is.opera) {
    if (false && layer.style.pixelHeight)
      return(layer.style.pixelHeight);
    else
      return(layer.clientHeight);
  }
  if (is.gecko) {
  	if (layer.style.height)
      return parseInt(layer.style.height);
  }
  if (is.nav4) {
    if (layer.document.height)
      return(layer.document.height);
    else
      return(layer.clip.bottom - layer.clip.top);
  }
  return(-1);
}

function setHeight(layer, newHeight) {
  if (is.ie4up||is.opera)
    layer.style.pixelHeight = newHeight;
  if (is.gecko)
  	layer.style.height = newHeight + "px";
  if (is.nav4)
    layer.document.height = newHeight;
}

function setzIndex(layer, z) {
  if (is.ie4up||is.gecko||is.opera)
    layer.style.zIndex = z;
  if (is.nav4)
    layer.zIndex = z;
}

//-----------------------------------------------------------------------------
// Layer clipping.
//-----------------------------------------------------------------------------

function clipLayer(layer, clipleft, cliptop, clipright, clipbottom) {
  if (is.ie4up)
    layer.style.clip = 'rect(' + cliptop + ' ' +  clipright + ' ' + clipbottom + ' ' + clipleft +')';
  if (is.gecko)
    layer.style.clip = 'rect(' + cliptop + 'px ' +  clipright + 'px ' + clipbottom + 'px ' + clipleft +'px)';
  if (is.opera)
    layer.style.clip = 'rect()';
  if (is.nav4) {
    layer.clip.left   = clipleft;
    layer.clip.top    = cliptop;
    layer.clip.right  = clipright;
    layer.clip.bottom = clipbottom;
  }
}

function getClipLeft(layer) {
  if (is.ie4up||is.gecko) {
    var str =  layer.style.clip;
    if (!str)
      return(0);
    var clip = getClipValues(layer.style.clip);
    return(clip[3]);
  }
  if (is.opera)
    return(0);
  if (is.nav4)
    return(layer.clip.left);
  return(-1);
}

function getClipTop(layer) {
  if (is.ie4up||is.gecko) {
    var str =  layer.style.clip;
    if (!str)
      return(0);
    var clip = getClipValues(layer.style.clip);
    return(clip[0]);
  }
  if (is.opera)
    return(0);
  if (is.nav4)
    return(layer.clip.top);
  return(-1);
}

function getClipRight(layer) {
  if (is.ie4up||is.gecko) {
    var str =  layer.style.clip;
    if (!str)
      return(layer.style.pixelWidth);
    var clip = getClipValues(layer.style.clip);
    return(clip[1]);
  }
  if (is.opera)
    return(0);
  if (is.nav4)
    return(layer.clip.right);
  return(-1);
}

function getClipBottom(layer) {
  if (is.ie4up||is.gecko) {
    var str =  layer.style.clip;
    if (!str)
      return(layer.style.pixelHeight);
    var clip = getClipValues(layer.style.clip);
    return(clip[2]);
  }
  if (is.opera)
    return(0);
  if (is.nav4)
    return(layer.clip.bottom);
  return(-1);
}

function getClipValues(str) {
  var clip = new Array();
  var i;

  // Parse out the clipping values for IE layers.
  i = str.indexOf("(");
  clip[0] = parseInt(str.substring(i + 1, str.length), 10);
  i = str.indexOf(" ", i + 1);
  clip[1] = parseInt(str.substring(i + 1, str.length), 10);
  i = str.indexOf(" ", i + 1);
  clip[2] = parseInt(str.substring(i + 1, str.length), 10);
  i = str.indexOf(" ", i + 1);
  clip[3] = parseInt(str.substring(i + 1, str.length), 10);
  return(clip);
}

//-----------------------------------------------------------------------------
// Layer scrolling.
//-----------------------------------------------------------------------------

function scrollLayerTo(layer, x, y, bound) {
  var dx = getClipLeft(layer) - x;
  var dy = getClipTop(layer) - y;

  scrollLayerBy(layer, -dx, -dy, bound);
}

function scrollLayerBy(layer, dx, dy, bound) {
  var cl = getClipLeft(layer);
  var ct = getClipTop(layer);
  var cr = getClipRight(layer);
  var cb = getClipBottom(layer);

  if (bound) {
    if (cl + dx < 0)
      dx = -cl;
    else if (cr + dx > getWidth(layer))
      dx = getWidth(layer) - cr;

    if (ct + dy < 0)
      dy = -ct;
    else if (cb + dy > getHeight(layer))
      dy = getHeight(layer) - cb;
  }

  clipLayer(layer, cl + dx, ct + dy, cr + dx, cb + dy);
  moveLayerBy(layer, -dx, -dy);
}

//-----------------------------------------------------------------------------

function setBgColor(layer, color) {
  if (is.ie4up||is.gecko)
    layer.style.backgroundColor = color;
  if (is.opera)
    layer.style.background = color;
  if (is.nav4)
    layer.bgColor = color;
}

function getLayer(name) {
  if (is.ie5up || is.gecko || is.opera)
    return document.getElementById(name);
  else if (is.ie4up)
    return document.all[name];
  else if (is.nav4)
    return findLayer(name, document);

  return null;
}

function findLayer(name, doc) {
  var i, layer;

  for (i = 0; i < doc.layers.length; i++) {
    layer = doc.layers[i];
    if (layer.name == name)
      return layer;
    if (layer.document.layers.length > 0) {
      layer = findLayer(name, layer.document);
      if (layer != null)
        return layer;
    }
  }
  return null;
}

//-----------------------------------------------------------------------------
// For IE4 Bug
//-----------------------------------------------------------------------------

function getPageWidth() {
  if (is.ie4up||is.gecko)
    return(document.body.scrollWidth);
  if (is.nav4)
    return(document.width);
  return(-1);
}

function getPageHeight() {
  if (is.ie4up||is.gecko)
    return(document.body.scrollHeight);
  if (is.nav4)
    return(document.height);
  return(-1);
}

function getPageScrollX() {
  if (is.nav4)
    return(window.pageXOffset);
  if (is.ie4up||is.gecko)
    return(document.body.scrollLeft);
  return(-1);
}

function getPageScrollY() {
  if (is.nav4)
    return(window.pageYOffset);
  if (is.ie4up||is.gecko)
    return(document.body.scrollTop);
  return(-1);
}