var stop = false;
var bbgallery = new Array();
var i= 0;

function preLoadImages()
{
  bbgallery[0] = "images/house0.jpg";
  bbgallery[1] = "images/house1.jpg";
  bbgallery[2] = "images/house20.jpg";
  bbgallery[3] = "images/house2.jpg";
  bbgallery[4] = "images/house3.jpg";
  bbgallery[5] = "images/house4.jpg";
  bbgallery[6] = "images/house5.jpg";
  bbgallery[7] = "images/house6.jpg";
}

function moveElement(elementID,interval) {
  if (!document.getElementById) return false;
  if (!document.getElementById(elementID)) return false;
       
  var elem = document.getElementById(elementID);
  if (stop) { 
      return true;
      }
  elem.setAttribute("src",bbgallery[i]);
  i++;
  if (i >7)
  {i=0;}
  var repeat = "moveElement('"+elementID+"',"+interval+")";
  movement = setTimeout(repeat,interval);
}

function positionMessage() {
  if (!document.getElementById) return false;
  if (!document.getElementById("myImage")) return false;
  if (!document.getElementById("stopbtn")) return false;
  if (!document.getElementById("startbtn")) return false;

  var elem = document.getElementById("myImage");
  var stopnow = document.getElementById("stopbtn");
  stopnow.onclick = function() {
         stop = true;
         }

  var startnow = document.getElementById("startbtn");
  startnow.onclick = function() {
         clearTimeout(movement);
         stop = false;
         i = 0;
         moveElement("myImage",2500);
         }
      

  preLoadImages();
  moveElement("myImage",2500);
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

addLoadEvent(positionMessage);