var imageTransitions = {
  tmpInfo: [],
  tmpInfo2: [],
  tmpURL: "",
  tmpPage: ""
};

/*************************************************************************************
* This function was put in here because in order for the new image transitions to
*  work you must have the HTTPRequest function as well. In an attempt to get away
*  using the server so much I have made an async request..
*
**************************************************************************************/
imageTransitions.getImages = function(url, callback, page) {
  var request;
  try { request = new XMLHttpRequest(); }catch(e){};
  try { request = new ActiveXObject("Msxml2.XMLHTTP"); }catch(e){};
  try { request = new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){};

  request.onreadystatechange = function() {
    if (request.readyState == 4 && request.status == 200) {
      callback(request.responseText, url, page);
    }
  }
  request.open("GET", url);
  request.send(null);
};

imageTransitions.rollImages = function(v, url, page) {
  var info = v.split(/\r|\n/);
  imageTransitions.tmpURL = url;
  imageTransitions.tmpPage = page;
  for (var i=1;i < info.length; ++i){
    var matchIT = info[i].split(":")[0];
    if (matchIT.match(RegExp(page, "i"))){
      imageTransitions.tmpInfo = info[i];
    }
  }
  //The speed on this matters because when you speed it up it eats up the amount of the ram that the system uses.
  //So with that in mind don't try to go too fast.
  doTrans = setInterval("imageTransitions.rollIt()", 2500);
}

imageTransitions.rollIt = function() {
  var tmpPage = imageTransitions.tmpPage;
  var tmpInfo2 = imageTransitions.tmpInfo.split(":")[1];
  tmpInfo2 = tmpInfo2.split(",");
  for (var i=1;i < tmpInfo2.length; ++i){
    var thisImage = Math.round(Math.random(1)*(tmpInfo2.length-1));
    obj("img_transition2").src = "../../images/shopimages/"+tmpPage+"/"+tmpInfo2[thisImage];
  }
  imageTransitions.tmpInfo2 = tmpInfo2;
};

imageTransitions.seeImages = function(v, url, page) {
 var info = v.split(/\r|\n/);
  imageTransitions.tmpURL = url;
  imageTransitions.tmpPage = page;
 var tmpPage = imageTransitions.tmpPage;
  for (var i=1;i < info.length; ++i) {
    var matchIT = info[i].split(":")[0];
    if (matchIT.match(RegExp(page,"i"))){
      imageTransitions.tmpInfo = info[i];
    }
   }
  imageTransitions.tmpInfo2 = imageTransitions.tmpInfo.split(":")[1];
  imageTransitions.tmpInfo2 = imageTransitions.tmpInfo2.split(",");
  var tmpInfo = imageTransitions.tmpInfo;
  var tmpInfo2 = imageTransitions.tmpInfo2;

  var serlen = tmpInfo2.length;
  if (serlen >= 4) var collen = 4;
  else if (serlen < 4) collen = serlen;

  var cellcount = 1;
  var galInfo = "<table border=0 bordercolor=ffcc33 align=center cellpadding=3 cellspacing=0 style='width:605;font-size: 10.5pt;' cols=collen>";
  galInfo += " <tr>";
  for (var i = 0; i < tmpInfo2.length; i++){
    if (cellcount == 1) galInfo += " </tr><tr>";
    galInfo += "  <td align=center><img width=145 height=100 src='../../images/shopimages/" +tmpPage+"/"+tmpInfo2[i]+"' onClick='imageTransitions.displayImage(this.src);' style='cursor:pointer;' alt='Click to view larger image.'><br>";
    if (cellcount < 4) cellcount++;
    else if (cellcount == 4) cellcount = 1;
  }
  galInfo += "  </tr>";
  galInfo += " </table>";

  obj("img1").innerHTML=galInfo;
}

imageTransitions.displayImage = function(img) {
  //imageTransitions.tmpPage = page;
  var tmpPage = imageTransitions.tmpPage;
  img = img.split("-")[0];
  if (tmpPage != "swift" && tmpPage != "genser" && tmpPage != "construction" && tmpPage != "agriculture" && tmpPage != "transport") {
    obj("img1").innerHTML="<img src='"+img+".jpg' onclick=document.location='"+tmpPage+".html?gallery' style='cursor:pointer;' alt='Click to see Image Gallery'>";
  }
  else {
    obj("img1").innerHTML="<img src='"+img+".jpg' onclick=document.location='index.html?gallery' style='cursor:pointer;' alt='Click to see Image Gallery'>";
  }
}

