preloadedimages=new Array();
timeouts=new Array();

function isloaded(img) {
  if (!img.complete)
	return false;
  if (typeof img.naturalWidth != "undefined" && img.naturalWidth == 0)
	return false;
  return true;
}

function loadimage(path, id, cnt, num, file_name){
  if (typeof(preloadedimages[id][num])!='undefined')
	return;
  next_pic = (num% cnt) + 1;	
  pic = new Image();
  //alert(path + id + file_name + next_pic + ".jpg");
  pic.src = path + file_name + next_pic + ".jpg";
  preloadedimages[id][num]= pic;
}

function showimage(path, id, cnt, num, file_name){
  if (!isloaded(preloadedimages[id][num])){
	timeouts[id]=setTimeout("showimage('"+path+"', "+id+", "+cnt+", "+num+", '" + file_name + "')", 10);
	return;
  }
  document.getElementById('pic'+id).src=preloadedimages[id][num].src;
  
  if(++num==cnt) 
	num = 0;
  loadimage(path, id, cnt, num, file_name);
  timeouts[id]=setTimeout("showimage('"+path+"', "+id+", "+cnt+", "+num+", '" + file_name + "')", 500);
}

function startPreview(id, cnt, start, path, file_name){
//alert(id);
  ind= start -1;
  if (path == null)
  {
	 path="http://static.brazzers.com/scenes/" + id + "/";
	 
  }
  else
  {
	  path = path + "/" + id + "/";
  }
  
  if (file_name == null)
  {
	file_name = "br";
  }

  //alert(path);
  if (typeof(preloadedimages[id])=='undefined')
	preloadedimages[id]=new Array();
  loadimage(path, id, cnt, ind, file_name );
  timeouts[id]=setTimeout("showimage('" + path + "', "+id+", "+cnt+", "+ ind +", '" + file_name + "')", 0);
}

function stopPreview(id, start, file_name){
  clearTimeout(timeouts[id]);
  if (file_name == null)
  {
	file_name = "bz";
  }
  if (typeof(preloadedimages[id][start-1])=='undefined') {
	ind = start +1;
	pic = new Image();
	pic.src = path+file_name+ind+".jpg";
	preloadedimages[id][start-1]= pic;
  } else {
	pic = preloadedimages[id][start-1];
  }
  document.getElementById('pic'+id).src=pic.src;
}



