var nImage = -1;
var obj = null;
var isImageReady = true;

//Constructor
function ImageRotator() {
	this.images = new Array();
	this.texts = new Array();
	this.urls = new Array();
	this.width = null;
	this.height = null;
	this.timeout = null;
	this.build = ImageRotatorBuildContent;
	this.next = ImageRotatorShowNext;
	this.init = ImageRotatorInit;
	this.start = ImageRotatorStart;
	this.addImage = ImageRotatorAddImage;
	//this.showImage = ImageDisplayShowImage;
}

// Methods.

function ImageRotatorInit(name,width,height,timeout) {
	this.name = name;
	obj = eval(name);
	this.width = width;
	this.height = height;
	this.timeout = timeout;

	if (document.layers){
		document.writeln ('<ilayer id="divImageRotatorContainer" width="'+ width +'" height="'+ (height+40) +'"><layer id="divImageRotator" top="0" left="0" width="'+ width +'"></layer></ilayer>');
	}else{
		document.writeln ('<div id="divImageRotator" style="position:relative; width:' + width + '; height:' + height + '"></div>');
	}
}

function ImageRotatorStart() {
	if(obj.images.length > 0){
		obj.images[0] = CacheImage(obj.images[0]);
		obj.next();
	}
}

function ImageRotatorShowNext() {
	nImage++;
	if(obj.images.length <= nImage) nImage = 0;

	if (typeof(obj.images[nImage])=='string'){ //downloaded?
	    isImageReady = false;
	    self.defaultStatus = "Henter billede ..." + nImage;
	    obj.images[nImage] = CacheImage(obj.images[nImage]);
	    Download();
	}

	if(isImageReady){
		WriteContent(obj.build(nImage));
	}else{
		nImage--;	
	}

	strFunction = this.name + ".next()";
	setTimeout(strFunction,this.timeout);
}

function ImageRotatorBuildContent(n) {
	strHTML = '';
	strHTML += '<a href="' + this.urls[n] + '"><img src="' + this.images[n].src + '" alt="' + this.texts[n] + '" width="' + this.width + '" height="' + this.height + '" border="0"></a><br>';
	// dont show deskription below picture SLA strHTML += '<a href="' + this.urls[n] + '" class="PictureLink">' + this.texts[n]+ '</a><br>';
	return(strHTML);
}

function ImageRotatorAddImage(image, linktxt, url) {
  this.images[this.images.length] = image;
  if (linktxt==null) this.texts[this.texts.length] = '';
  else this.texts[this.texts.length] = linktxt;
  if (url==null) this.urls[this.urls.length] = null;
  else this.urls[this.urls.length] = url;
}

function WriteContent(strHTML){
	if (document.layers){
		document.layers.divImageRotatorContainer.document.layers.divImageRotator.document.open();
		document.layers.divImageRotatorContainer.document.layers.divImageRotator.document.write(strHTML);
		document.layers.divImageRotatorContainer.document.layers.divImageRotator.document.close();
	}else{
		document.getElementById("divImageRotator").innerHTML = strHTML;
	}
}

//------------------------------------------

function CacheImage(ImageSource) { // TURNS THE STRING INTO AN IMAGE OBJECT
   var ImageObject = new Image();
   ImageObject.src = ImageSource;
   return ImageObject;
}

function Download() {
	//alert(obj.images[nImage].length);
	if (obj.images[nImage].complete) {
		isImageReady = true;
		self.defaultStatus = ""
	}else{
		setTimeout("Download()", 100); // CHECKS DOWNLOAD STATUS EVERY 100 MS
	}
	return true;
}

//------------------------------------------

