﻿// General Variables
var nextEstateNo, lastEstateNo;
var stepDelay, imageCount, scrollStepCount;
var srcContainer, destContainer, destContainerPosition;
var timer, interval, scrollPaceChange1, scrollPaceChange2;
var isFastForwarding, canScroll = false, scrollPaused = false, pendingFastForward = false, pendingFastForwardDirection = 0;

// Config Variables
var topImage = 0;
var scrollStep = 1;
var autoScroll = true;
var scrollHeight = 90;
var visibleImages = 4;
var isScrolling = false;
var autoScrollDelay = 2500;

function initialiseScroller(estateCount, dScrollerID) {
  imageCount = estateCount;
  srcContainer = document.layers ? document.layers.dEstateLibrary : document.all ? document.all.dEstateLibrary : document.getElementById ? document.getElementById('dEstateLibrary') : null;
  destContainer = document.layers ? document.layers[dScrollerID] : document.all ? document.all[dScrollerID] : document.getElementById ? document.getElementById(dScrollerID) : null;

  // Remove extra white space
  var estateNodes = srcContainer.childNodes;
  for (var i = 0; i < estateNodes.length; i++) {
    if (estateNodes[i].nodeType != 1) {
      srcContainer.removeChild(estateNodes[i]);
    }
  }
  estateNodes = destContainer.childNodes;
  for (var i = 0; i < estateNodes.length; i++) {
    if (estateNodes[i].nodeType != 1) {
      destContainer.removeChild(estateNodes[i]);
    }
  } 

  if (srcContainer && destContainer && (imageCount > visibleImages)) {
    canScroll = true;
    destContainerPosition = destContainer.style ? destContainer.style : destContainer;
    scrollPaceChange2 = (scrollHeight / scrollStep) / 3;
    scrollPaceChange1 = scrollPaceChange2 * 2;
    if (autoScroll) {
      interval = setInterval(function () { startScroll(-1); }, autoScrollDelay);
    }
  }
}

function startScroll(direction) {
  if (!isScrolling) {
    isScrolling = true;
    if (isFastForwarding) {
      stepDelay = 5;
    } else {
      stepDelay = 25;
    }
    scrollStepCount = scrollHeight / scrollStep;
    if (direction === -1) {
      nextEstateNo = (topImage + visibleImages) % imageCount;
      destContainer.appendChild(srcContainer.childNodes[nextEstateNo]);
      destContainerPosition.top = "";
      destContainerPosition.bottom = "-" + (scrollHeight + 1) + "px";
    } else {
      nextEstateNo = (topImage === 0 ? srcContainer.childNodes.length : topImage) - 1;
      destContainer.insertBefore(srcContainer.childNodes[nextEstateNo], destContainer.childNodes[0]);
      destContainerPosition.top = "-" + scrollHeight + "px";
      destContainerPosition.bottom = "";
    }
    if (nextEstateNo === srcContainer.childNodes.length) {
      srcContainer.appendChild(document.createElement("div"));
    } else {
      srcContainer.insertBefore(document.createElement("div"), srcContainer.childNodes[nextEstateNo]);
    }
    scrollImage(direction);
  }
}

function fastForward(direction) {
  if (canScroll) {
    if (direction === -1) {
      document.getElementById('dFastForwardUp').className += " fastForwardUpOn";
    } else {
      document.getElementById('dFastForwardDown').className += " fastForwardDownOn";
    }
    if (isScrolling && (((direction === -1) && (destContainerPosition.bottom === '')) || ((direction === 1) && (destContainerPosition.top === '')))) {
      pendingFastForward = true;
      pendingFastForwardDirection = direction;
      return;
    }
    clearTimeout(timer);
    clearInterval(interval);
    isFastForwarding = true;
    if (isScrolling) {
      scrollImage(direction);
    } else {
      startScroll(direction);
    }
  }
}

function cancelFastForward() {
  if (document.getElementById('dFastForwardUp').className === "fastForwardUp fastForwardUpOn") {
    document.getElementById('dFastForwardUp').className = "fastForwardUp";
  } else {
    document.getElementById('dFastForwardDown').className = "fastForwardDown";
  }
  clearInterval(interval);
  if (autoScroll && canScroll && isFastForwarding && !scrollPaused) {
    interval = setInterval(function () { startScroll(-1); }, autoScrollDelay);
  }
  isFastForwarding = false;
  pendingFastForward = false;
}

function pauseScroll() {
  scrollPaused = true;
  clearInterval(interval);
}

function resumeScroll() {
  scrollPaused = false;
  clearInterval(interval);
  if (autoScroll && canScroll) {
    interval = setInterval(function () { startScroll(-1); }, autoScrollDelay);
  }
}

function scrollImage(direction) {
  if (direction === -1) {
    destContainerPosition.bottom = (parseInt(destContainerPosition.bottom, 10) + scrollStep) + 'px';
  } else {
    destContainerPosition.top = (parseInt(destContainerPosition.top, 10) + scrollStep) + 'px';
  }
  scrollStepCount -= 1;
  if (scrollStepCount === 0) {
    isScrolling = false;
    if (direction === -1) {
      srcContainer.insertBefore(destContainer.childNodes[0], srcContainer.childNodes[topImage]);
      srcContainer.removeChild(srcContainer.childNodes[topImage + 1]);
    } else {
      lastEstateNo = ((topImage + visibleImages) - 1) % imageCount;
      srcContainer.insertBefore(destContainer.childNodes[visibleImages], srcContainer.childNodes[lastEstateNo]);
      srcContainer.removeChild(srcContainer.childNodes[lastEstateNo + 1]);
    }
    switchLightOff(topImage);
    topImage -= direction;
    if (topImage === imageCount) {
      topImage = 0;
    } else if (topImage === -1) {
      topImage = imageCount - 1;
    }
    switchLightOn(topImage);
    if (isFastForwarding) {
      startScroll(direction);
    }
    if (pendingFastForward) {
      isFastForwarding = true;
      pendingFastForward = false;
      startScroll(pendingFastForwardDirection);
    }
  } else {
    if (isFastForwarding) {
      if (isFastForwarding && stepDelay > 5) {
        stepDelay -= 2;
      }
    } else if ((scrollStepCount > scrollPaceChange1) && stepDelay > 5) {
      stepDelay -= 2;
    } else if ((scrollStepCount < scrollPaceChange2) && stepDelay < 25) {
      stepDelay += 1;
    }
    timer = setTimeout("scrollImage(" + direction + ")", stepDelay);
  }
}

function switchLightOn(lightNo) {
  document.getElementById('scrollLight' + lightNo).className += " scrollLightOn";
}

function switchLightOff(lightNo) {
  document.getElementById('scrollLight' + lightNo).className = "dScrollLight";
}
