/*------------------------------------------------------------------------------------
	Image picker - Copyright 2008 © Lonnie Mendez
     
	Returns a smaller subset of an array with semi random
	members.  First image is always the same.
  
 	Usage:
	
    imagePicker.pickImages(array, start, limit)
    
      pickImages(imgFiles, start, limit):	an array of image file paths.  returns a new array of
		random members from array.
		
      
------------------------------------------------------------------------------------*/

var imagePicker = {
	imgstore: [],
	imgCount: 0,
	prevIndexes: [],
	
	// returns true if index passed in is unique
	isUnique: function (aIndex) {
		for (x = 0; x < this.prevIndexes.length; x++) {
			if (this.prevIndexes[x] == aIndex)
				return false;
		}
		
		return true;
	},
	
	pickImages: function (imgFiles, imgStart, imgLimit) {
		
		if (typeof imgFiles != 'array' && typeof imgFiles != 'object')
			return;
		
		if (!imgFiles.length)
			return;
		
		// set our actual limit
		imgLimit = (imgLimit > imgFiles.length) ? imgFiles.length : imgLimit;

		// set start image
		if (imgStart > imgLimit)
			imgStart = 0;
		
		this.imgstore.push(imgFiles[imgStart]);
		++(this.imgCount);
		this.prevIndexes.push(imgStart);

		// add random members to new array while being careful to select unique members
		while (this.imgCount < imgLimit) {
			r = (Math.floor(Math.random() * (imgFiles.length - 1)) + 1);
			if (this.isUnique(r)) {
				this.imgstore.push(imgFiles[r]);
				this.prevIndexes.push(r);
				++(this.imgCount);
			}
		}
		
		return this.imgstore;
	}
}