﻿// local vars
var ish_startpos;
var ish_scroller;
var ish_finalLeft = -100;
var ish_slideby = 0;
var ish_direction = 0;
var ish_initialised = 0


function copyElement(source, target) {
  // get the source style element
  var styles = source.style;
  // iterate
  target.innerHTML = source.innerHTML;
  for (var s in styles) {
    // wathc for exceptions
    try {
      // copy the style elements
      target.style[s] = source.style[s]
    }
    catch (e) {
      // suppress
    }
  }
}

function ish_Select(sender) {
//  // stop zooms
//  MagicZoom_stopZooms();
//  // get the zoom images
//  var bigzoom = document.getElementById('imageZoomBig');
//  var smlzoom = bigzoom.firstChild;
//  // set the new src and href
//  bigzoom.href = sender.src.replace('t.jpg','l.jpg');
//  smlzoom.src = sender.src.replace('t.jpg', 'm.jpg');
//  // re-init zoom
//  MagicZoom_findZooms();
}

function ish_slide_left() {
  // get the top div
  var leftDiv = ish_scroller.getElementsByTagName('SPAN')[0];
  // set the final top pos
  ish_finalleft = leftDiv.offsetWidth * -1;
  // set the direction
  ish_direction = 0;
  // set the amount to slide up by
  ish_slideby = -4;
}
function ish_slide_right() {
  // get the divs
  var divs = ish_scroller.getElementsByTagName('SPAN');
  // get the rightmost div
  var rightDiv = divs[divs.length - 1];
  // create a new div
  var addDiv = document.createElement('SPAN');
  // set the click hander
  addDiv.onclick = ish_slide_left;
  // copy the style
  copyElement(rightDiv, addDiv);
  // move the scroller up
  ish_scroller.style.left = (rightDiv.offsetWidth*-1)+'px';

  // remove the bottom div and append ready to loop
  ish_scroller.removeChild(rightDiv);
  ish_scroller.insertBefore(addDiv, ish_scroller.firstChild);
  // set the final top pos
  ish_finalleft = 0; 
  // set the direction
  ish_direction = 1;
  // set the amount to slide up by
  ish_slideby = 4;
}
function slideLeftDone() {
  // move the scroller down
  ish_scroller.style.left = '0px';
  // get the top most div
  var leftDiv = ish_scroller.getElementsByTagName('SPAN')[0];
  // create a new div
  var addDiv = document.createElement('SPAN');
  // set the click hander
  addDiv.onclick = ish_slide_left;
  // copy the style
  copyElement(leftDiv, addDiv);
  // remove and append ready to loop again
  ish_scroller.removeChild(leftDiv);
  ish_scroller.appendChild(addDiv);
}

function ish_doSlide() {
  // check the slide by
  if (ish_slideby != 0) {
    // get the position of the scrolling div
    var leftPos = ish_scroller.offsetLeft;
    // check bounds
    if ((leftPos <= ish_finalleft && ish_direction == 0) || (leftPos >= 0 && ish_direction == 1)) {
      // done
      ish_slideby = 0;
    }
    // move the panel
    ish_scroller.style.left = (leftPos + ish_slideby) + 'px';
    // check for slide up finishing
    if (ish_slideby == 0 && ish_direction == 0) slideLeftDone();
  }
  // set the timeout call value 
  setTimeout('ish_doSlide()', 20);
}
function ish_init() {
  if (ish_initialised == 0) {
    // assign event handlers
    document.getElementById('ish_LeftArrow').onclick = ish_slide_left;
    document.getElementById('ish_RightArrow').onclick = ish_slide_right;
    // get the scroller
    ish_scroller = document.getElementById('ish_Scroll');

    // get the start pos of the scroller
    ish_startpos = ish_scroller.offsetTop;
    // get all of the contained divs
    var divs = ish_scroller.getElementsByTagName('SPAN');
    // iterate
    for (var i = 0; i < divs.length; i++) {
      // set the onclick handler
      //divs[i].onclick = ish_slide_left;
    }

    // initialise the slide
    ish_doSlide();
    //ish_initialised = 1;
  }
}