/*
Todo: 	oncomplete method should not have mySlideList hardcoded
		check how oncomplete handles an array rather than single image
		put flag in constructor to handle preloading of images singularly or as group
		re-implement callback function
*/
var baseURL = 'http://www.directproperty.co.nz';
var mySlideList = [
	baseURL+'/images/home/1.jpg',
	baseURL+'/images/home/2.jpg',
	baseURL+'/images/home/3.jpg',
	baseURL+'/images/home/4.jpg',
	baseURL+'/images/home/6.jpg',
	baseURL+'/images/home/09.jpg',
	baseURL+'/images/home/10.jpg',
	baseURL+'/images/home/11.jpg',
	baseURL+'/images/home/12.jpg',
	baseURL+'/images/home/13.jpg'
];
//	baseURL+'/images/home/5.jpg',
var swaptime = 5000; //milliseconds

function ImagePreloader(images)
{
	// initialize internal state.
	this.nLoaded = 0;
	this.nProcessed = 0;
	this.aImages = new Array;
	
	if (images instanceof Array) {
		// record the number of images.
		this.nImages = images.length;
	
		// for each image, call preload()
		for ( var i = 0; i < images.length; i++ ) {
			this.preload(images[i]);
		}
	} else {
		this.nImages = 1;
		this.preload(images);
	}
}
ImagePreloader.prototype.preload  = preload;  
function preload(image)
{
	// create new Image object and add to array
	var oImage = new Image;
	this.aImages.push(oImage);
	
	oImage.onload = protoLoad;
	oImage.onerror = protoError;
	oImage.onabort = protoAbort;
	
	// assign pointer back to this.
	oImage.oImagePreloader = this;
	oImage.bLoaded = false;
	oImage.source = image;
	
	// assign the .src property of the Image object
	oImage.src = image;
}
ImagePreloader.prototype.onComplete = onComplete;  
function onComplete(error)
{

	this.nProcessed++;
	if ( this.nProcessed == this.nImages ) {
		if ( this.nImages == 1 ) {
			//alert('loaded: ');
			for ( var i = 0; i < mySlideList.length; i++ )  {
				if (this.aImages[0].src == mySlideList[i] && !error) {
					imageLoaded[i] = true;
				}
			}
		} else {
			for ( var j = 0; j < this.aImages.length; j++ )  {
				for ( var i = 0; i < mySlideList.length; i++ )  {
					if (this.aImages[j] == mySlideList[i] && !error) {
						imageLoaded[i] = true;
					}
				}
			}
		}
	}
}

function protoLoad()
{
	this.bLoaded = true;
	this.oImagePreloader.nLoaded++;
	this.oImagePreloader.onComplete();
}

function protoError()
{
	this.bError = true;
	this.oImagePreloader.onComplete();
}

function protoAbort()
{
	this.bAbort = true;
	this.oImagePreloader.onComplete();
}


var imageLoaded = Array();

function SlideShow(slideList, image, speed, name) {
  this.slideList = slideList;
  this.image = image;
  this.speed = speed;
  this.name = name;
  this.current = 0;
  this.timer = 0;
  for ( var i = 0; i < this.slideList.length; i++ ) {
  	imageLoaded[i] = false;
  }
  for ( var i = 0; i < this.slideList.length; i++ )  {
  	new ImagePreloader(slideList[i], 'setLoaded');
  }
}
SlideShow.prototype.play = SlideShow_play;

function SlideShow_play() {
  with(this) {
		if(current++ == slideList.length-1) {
			current = 0;
		}
		if (imageLoaded[current]) {
			switchImage(image, slideList[current]);
		} else {
			//alert('skipping : ' + current + ' : ' + imageLoaded[current] + ' : ' + imageLoaded);
		}
		clearTimeout(timer);
		timer = setTimeout(name+'.play()', speed);
  }
}

function switchImage(imgName, imgSrc) {
  if (document.images)
  {
    if (document.images[imgName] && imgSrc != "none")
    {
      document.images[imgName].src = imgSrc;
    }
  }
}

function start_swap() {
	setTimeout('mySlideShow.play()', 2000);
}



// THIS KICKS IT ALL OFF
var mySlideShow = new SlideShow(mySlideList, 'rotate', swaptime, "mySlideShow");
if (window.addEventListener) {
	window.addEventListener("load", start_swap, false);
} else if (window.attachEvent) {
	window.attachEvent("onload", start_swap);
}
