// Only inserted by PHP when needed.
var ajaxInterval;
var ajaxStaticRequestSpeed=100;
var scrollInterval;
var thumbsfolderUrl;
var nameIdArray = new Array();
var imgNameIdArray = new Array();
var idArray = new Array();
var imgObj = new Array();
var bannerWidth = 950;   // = content width.
var tempImgObject;
var thumbs = new Array();

function b (imageNum, prodNum, text, thumbName, width, nextProdNum, stock_level) {
this.imageNum = imageNum;
this.tempImgObject=false;
this.prodNum = prodNum;
this.thumbName = thumbName;
this.width = width;
this.left=0;
this.text = text;
this.nextProdNum = nextProdNum;
this.stock_level = stock_level;
this.setSource = setSource;
this.setSource ();
}

function setSource () {
  idArray[this.imageNum] = document.getElementById(nameIdArray[this.imageNum]);
 // idArray[this.imageNum].style.height = + thumbHeightsfromPHP + 2 + "px";
 // idArray[this.imageNum].style.width = this.width + 2 + "px";
  imgObj[this.imageNum] = document.getElementById(imgNameIdArray[this.imageNum]);
  loadSource(this.width,this.thumbName);
  return true;
}

function loadSource(xwidth ,xname) {
  tempImgObject = new Image(xwidth, thumbHeightsfromPHP);
  tempImgObject.src = thumbsfolderUrlfromPHP + xname;
    if (!browserIE) {
    tempImgObject.onload = sourceLoaded();
    }
}

function sourceLoaded() {
fireLoad = true;
}

function setPosition () {
this.gap=thumb_spacer;
this.scrolling = false;
this.nextImage_x = this.firstImage_x = 2;
this.nextStaticImage_left=2;
this.nextScrolledImage_left = this.nextStaticImage_left -= 2;
this.firstScrolledVisibleImage = 0;
this.firstVisibleWidth = 0;
this.lastImageNum = 0;
this.image_x = 0;
this.adjust = adjust;
}

function adjust (imageToAdjust) {

  if (this.scrolling) {

    if ((thumbs[this.firstScrolledVisibleImage].width + thumbs[this.firstScrolledVisibleImage].left)<0) {

    // Save first image number & deactivate other.
    idArray[this.firstScrolledVisibleImage].style.display = "none";
    this.firstScrolledVisibleImage = nextPos(this.firstScrolledVisibleImage,imageNum);
    this.nextScrolledImage_left = thumbs[this.firstScrolledVisibleImage].left;
    }

 // Prepare for next SCROLL interval.
  var saveFirstImagePosition = this.nextScrolledImage_left;
  ii = this.firstScrolledVisibleImage;

    while (this.nextScrolledImage_left < bannerWidth) {
    idArray[ii].style.display = "inline";
    idArray[ii].style.left = this.nextScrolledImage_left + "px";
    thumbs[ii].left = this.nextScrolledImage_left;
    this.nextScrolledImage_left += thumbs[ii].width + this.gap;
    ii = nextPos(ii,imageNum);
    }

    // Also prevents calls for new image on following cycles.
    if (this.lastImageNum < ii) {
     this.lastImageNum = ii;
     ajaxNextThumb();
    }

 // Prepare for next SCROLL interval.
  this.nextScrolledImage_left = saveFirstImagePosition - 1;
  } else {
  idArray[imageToAdjust].style.display = "inline";
  idArray[imageToAdjust].style.left = this.nextStaticImage_left  + "px";
  thumbs[imageToAdjust].left = this.nextStaticImage_left;
  this.nextStaticImage_left += thumbs[imageToAdjust].width + this.gap;
  }
}

function nextPos(ii,maxItems) {
  if (ii<(maxItems-1))	{return ii+1;} else {return 0;}
}

position = new setPosition();

// imageNum is always a reference to the last image loaded by AJAX.
imageNum=0;
imagesStartWidth=0;

function canStart() {
// boolean until loaded !
getName(0, firstProdId);
imageNum = -1;
ajaxInterval = setInterval("ajaxNextThumb()",ajaxStaticRequestSpeed);
}

var fireLoad = false;
var clearOnComplete = false;
var count=-1;

function ajaxNextThumb() {

  // Object not instance.
  if (!thumbs[imageNum+1]) {return 0}

  // Wait on image to fully load, and resolve inconsistances..
  if (!browserIE) {
    if (!fireLoad) {
    return 0;
    }
  } else {
    if (!tempImgObject.complete) {
    return 0;
    }
  }

  // Does not set new imageNum until object is loaded.
  imageNum++;
  count++;
 // window.status="loaded "+ imageNum + " - "+count;
 imgObj[imageNum].width=tempImgObject.width;
 imgObj[imageNum].height=tempImgObject.height;
 imgObj[imageNum].src = tempImgObject.src;

  // Ready for next !
 fireLoad = false;

  // Request may be for a single image only.
  if (clearOnComplete) {
   clearInterval(ajaxInterval);
   return 0;
  }

  if (!position.scrolling) {

    // Line up images - no scrolling yet.
    position.adjust(imageNum);

    // Obtain enough images to fill the screen.
    imagesStartWidth += thumbs[imageNum].width;

      if (imagesStartWidth > (bannerWidth*2)) {
      position.scrolling=true;
      window.clearInterval(scrollInterval);  
	  clearInterval(ajaxInterval);
			
		if (!scrollDisable) {
        sto="scrollInterval = setInterval(\"position.adjust()\","+scrollSpeed+")";
        setTimeout (sto, waitBeforScroll);
        }
      }
    }

  if (imageNum==(numFilesToget-1)) {
  clearInterval(ajaxInterval);
  } else {
    if (thumbs[imageNum] < thumbs[imageNum].nextProdNum) {
    alert(thumbs[imageNum].nextProdNum);
    }
  // Previous image is complete - Get BIOS for the next one from server.
  getName(imageNum+1, thumbs[imageNum].nextProdNum);
  }
}



function makeHttpRequest(url, callback_function, async) {
var http_request = false;

  if (window.XMLHttpRequest) { // Mozilla, Safari,...
  http_request = new XMLHttpRequest();
  // If not XML do the following .. If method exists .. else Firefox error ..!
    if (http_request.overrideMimeType) {
    http_request.overrideMimeType('text/html');
    }
  } else if (window.ActiveXObject) { // IE
    try {
    http_request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
      http_request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {}
    }
  }

  if (!http_request) {
  alert('Unfortunately you browser doesn\'t support this feature.');
  return false;
  }

  http_request.onreadystatechange = function() {
    if (http_request.readyState == 4) {
      if (http_request.status == 200) {
    // alert(callback_function + http_request.responseText+')');
      eval(callback_function + http_request.responseText+')');
      } else {
      window.status = "Lost contact with server, if this message persists please check your internet connection !";
      //alert('There was a problem with the request.(Code: ' + http_request.status + ')');
      }
    }
  }

http_request.open('POST', url, async);
http_request.send(null);
}

function getName(imageNum, firstProdId) {
  f0 = "thumbs["+imageNum+"] = new b("+imageNum+",";
  makeHttpRequest(".\/various-mods\/ajaxBanner\/AjaxRequest3.php?PHPSESSID="+mySession+"&inputText="+firstProdId, f0, true);
}

scrollDisable = false;
function stop(){
  scrollDisable = true;
  if (!position.scrolling) {return 0};
  window.clearInterval(scrollInterval)
}

function go() {
  scrollDisable = false;
  if (!position.scrolling) {return 0};
  window.clearInterval(scrollInterval);
  scrollInterval = setInterval("position.adjust("+(imageNum)+")",scrollSpeed);
}

function goToURL(prodNum) {
  window.open(prodPathUrlfromPHP + thumbs[prodNum].prodNum,"_self");
}

var disable=false;
var lastRef;
function showText(show,elementNumber) {
  if (disable) {return 0}

  if (elementNumber=="disable"){
  disable=true;
  ref.style.display="block";
  stop();
  return 0;
  } else if (elementNumber=="enable") {
    disable=false;
    };

ref = document.getElementById("hcBannerText");

  if (show) {
  // var inStock = Math.max(0,thumbs[elementNumber].stock_level);
  var inStock = (thumbs[elementNumber].stock_level > 0)?"In Stock " + Math.min(50,thumbs[elementNumber].stock_level):"<span style=\"color:#ff0000\">Out of Stock</span>";
  var stockCount = "/" + inStock;
  var text = thumbs[elementNumber].text+ "<span class='codedesc'>&nbsp;(Code " +thumbs[elementNumber].prodNum + stockCount +")</span>"

    if ((thumbs[elementNumber].left + thumbs[elementNumber].width) < (bannerWidth/2)) {
    ref.innerHTML = "&larr;&nbsp;" + text;
    ref.style.left = thumbs[elementNumber].left + thumbs[elementNumber].width - 3 + "px";
    } else {
    ref.style.display="block";
    ref.innerHTML = "&nbsp;" + text + "&rarr;";
    ref.style.left = thumbs[elementNumber].left - ref.offsetWidth + 3 + "px";
    }

  ref.style.display="block";
  } else {
  ref.style.display="none";
  }

}
