var blurbs = new Array(
"if you wear a watch",
"changing the face of time",
"a new way to look at time",
"unique time pieces",
"times for sunrise and sunset",
"what's new is ancient",
"times for moonrise and moonset",
"your time",
"multiple time zones",
"pre-programmed for nearly 600 cities worldwide",
"programmed through the year 2099",
"automatic adjustment to daylight saving time",
"longitude and latitude",
"equinox and solstice alerts"
)

window.onload = function(){
  var numberOfPictures = 36;
  var randpic = Math.floor(Math.random()*numberOfPictures) +1;
  if(randpic == 20){ randpic = 1;};
  //document.getElementById('mastRotate').src = "/images/topimages/" + randpic + ".jpg";

  var randblurb = blurbs[ Math.round(Math.random()* 13) ];
  if(navigator.userAgent.indexOf("MSIE") != -1){
	document.getElementById('blurbRotate').innerHTML=randblurb;
  }
  else{
	var newnode = document.createTextNode(randblurb);
	document.getElementById('blurbRotate').appendChild(newnode);
  }
}


var millisec = 1000;
var direction = 0;
var slideCache = new Array();
var slideTotal = 36;
var slideNum = Math.floor(Math.random()* slideTotal)+1;

function opacity(id, opacStart, opacEnd, millisec) {
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;

	//determine the direction for the blending, if start and end are the same nothing happens
	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	} else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++)
			{
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
	var object = document.getElementById(id).style;
	if(navigator.userAgent.indexOf("MSIE") != -1){
		object.filter = "alpha(opacity=" + opacity + ")";
	}
	else{
		object.opacity = (opacity / 100);
		object.MozOpacity = (opacity / 100);
		object.KhtmlOpacity = (opacity / 100);
	}
}

function cacheIt(num){
	for(i = num; i <= num+4 ; i++) {
			if(slideCache[i] && slideCache[i].src){continue;};
			if(i>slideTotal){cacheIt(1);break;}
			slideCache[i] = new Image;
			slideCache[i].src = '/artwork/R' + i + '.jpg';
	}
}

function slideshow(){
	cacheIt(slideNum);
	document.getElementById('slide').src = slideCache[slideNum].src;
	setTimeout("swapslide()",4000);
}

function swapslide(){
	cacheIt(slideNum);
	document.getElementById('slide').src = slideCache[slideNum].src;
	if (direction == 1){
		opacity('slide', 0, 100, millisec);
		direction=0
		setTimeout("swapslide()",5000);
	}else{
		opacity('slide', 100, 0, millisec);
		direction=1
		slideNum++;
		if (slideNum > 36){slideNum = 1;}
		setTimeout("swapslide()",1250);
	};
}


function blendimage(divid, imageid, imagefile, milli){
  var speed = Math.round(milli / 10);
  var timer = 0;

  //set the current image as background
  document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")";

  //make image transparent
  changeOpac(0, imageid);

  //make new image
  document.getElementById(imageid).src = imagefile;

  //fade in image
  for(i = 0; i <= 100; i++)
  {
	setTimeout("changeOpac(" + i + ",'" + imageid + "')",(timer * speed));
	timer++;
  }

}

//var slideCache = new Array(); //use one already in this script
function RunSlideShow(divid,imageid,imageFiles,displaySecs)
{
  //changeOpac(0, imageid);

  var imageSeparator = imageFiles.indexOf(";");
  var nextImage = imageFiles.substring(0,imageSeparator);
  var futureImages= imageFiles.substring(imageSeparator+1,imageFiles.length)+ ';' + nextImage;

  blendimage(divid,imageid,nextImage,200);

  // Cache the next image to improve performance.
  imageSeparator = futureImages.indexOf(";");
  nextImage = futureImages.substring(0,imageSeparator);
  if (slideCache[nextImage] == null)
  {
	slideCache[nextImage] = new Image;
	slideCache[nextImage].src = nextImage;
  }


  setTimeout("RunSlideShow('"+divid+"','"+imageid+"','"+futureImages+"',"+displaySecs+")",displaySecs*1000);
}

if( location.href.indexOf("cozmo.html") < 0){
	slideshow();
}
