/**
 * Class handles display of WSOD people.
 * Incorporates list management and display effects
 */
var WSODPeople = function() {
	
	var people = [];
	var PEOPLE_COUNT = 312;
	var doNotDisplay = [ 292 ];
	
	var arrayRandomize = function(array) {
		// seems like this method should be part of a util class somewhere else
		
		var tempArray = [];
		while (array.length) {
			var random = Math.floor(Math.random() * array.length);
			tempArray.push(array.splice(random, 1)[0]);
		}
		return tempArray;
	};
	
	/**
	 * todo: replace this with buffered call to some sort of data store
	 * 		 probably should tie into whatever the intranet uses
	 */
	var doNotDisplayCopy = doNotDisplay.slice(0);  // Duplicate, so the original list can be used later
	for (var i=0; i<PEOPLE_COUNT; i++) {
		var hidden = false;
		for (var j=0; j<doNotDisplayCopy.length; j++) {
			if (doNotDisplayCopy[j] == i) {
				hidden = true;
				doNotDisplayCopy.splice(j, 1);
				j--;
				break;
			}
		}
		
		if (!hidden) {
			people.push({ id: i, name: "", comment: "" })
		}
	}
	people = arrayRandomize(people);
		
	this.displayedPersonIdx = 0;
	this.fader = new Fader();
	this.timeouts = [];
	
	this.getPeopleCount = function() {
		return PEOPLE_COUNT;
	};
	
	this.getDoNotDisplay = function() {
		return doNotDisplay;
	};
	
	this.getPeople = function() {
		return people;
	};
};

WSODPeople.prototype.EL_CONTAINER_SELECTOR = "ul.people li";		// css selector to grab spots that need to be filled
WSODPeople.prototype.IMAGE_PATH = "common/img/photos/";				// path to images
WSODPeople.prototype.IMAGE_TYPE = ".jpg";							// image file type
WSODPeople.prototype.MIN_EMPTY_FACE_TIME = 0;						// min time (in seconds) before space is filled with first picture
WSODPeople.prototype.MAX_EMPTY_FACE_TIME = 10;						// max time (in seconds) before space is filled with first picture
WSODPeople.prototype.MIN_FACE_TIME = 5;								// min time (in seconds) that a person can be displayed
WSODPeople.prototype.MAX_FACE_TIME = 45;							// max time (in seconds) that a person can be displayed

/**
 * @return next person in list, move back to beginning at end of list
 */
WSODPeople.prototype.getNextPerson = function() {
	var people = this.getPeople();
	
	var person = people[this.displayedPersonIdx++ % people.length];
	// preload image old school style!
	new Image().src = this.getImageName(person);
	
	return person;
};

/**
 * @return random time(ms) between min and max times that image should be displayed
 */
WSODPeople.prototype.getFaceTime = function() {
	return ((this.MAX_FACE_TIME - this.MIN_FACE_TIME) * Math.random() + this.MIN_FACE_TIME) * 1000;
};

/**
 * @return random time(ms) between min and max times before intial image should be displayed
 */
WSODPeople.prototype.getEmptyFaceTime = function() {
	return ((this.MAX_EMPTY_FACE_TIME - this.MIN_EMPTY_FACE_TIME) * Math.random() + this.MIN_EMPTY_FACE_TIME) * 1000;
};

/**
 * @return fully qualified path to image file
 */
WSODPeople.prototype.getImageName = function(person) {
	return this.IMAGE_PATH + person.id + this.IMAGE_TYPE;
};

WSODPeople.prototype.setSpeed = function(speed) {
	this.fader.INTERVAL = speed.interval;
	this.fader.FRAME_TIME = speed.frameTime;
	this.MIN_EMPTY_FACE_TIME = speed.peopleMinFaceTime;
	this.MAX_EMPTY_FACE_TIME = speed.peopleMaxFaceTime;
	this.MIN_FACE_TIME = speed.peopleMinFaceTime;
	this.MAX_FACE_TIME = speed.peopleMaxFaceTime;			
	
	this.reset();
};


/**
 * Essentially private method as there isn't much benefit to calling from another interface.
 * @return closure function to show a person.
 */
WSODPeople.prototype.getShowFunctionClosure = function(person, container, preventFade) {
	var theObject = this;
	return function() {
		theObject.show(person, container, preventFade);
	};
};

/**
 * display one image for each available container, prevent fade
 */
WSODPeople.prototype.showAll = function() {
	var elContainers = Element.parseSelector(this.EL_CONTAINER_SELECTOR);
	var theObject = this;
	
	for (var i=0; i<elContainers.length; i++) {
		this.timeouts.push(setTimeout(this.getShowFunctionClosure(this.getNextPerson(), elContainers[i]), this.getEmptyFaceTime()));
	};
};

/**
 * show person in container
 * set up timeout for person to be replaced by next one in list
 * todo: think about noscript here?
 */
WSODPeople.prototype.show = function(person, container, preventFade) {
	if (undefined != person) {
		var imgSelection = Element.parseSelector("img", container);
		if (imgSelection.length) {
			this.fader.fadeOut(imgSelection[0], true);
		}
		
		var elImg = Element.create("img", {
			src: this.getImageName(person),
			title: person.name
		});	
		container.appendChild(elImg);
		
		if (preventFade) {
			Element.setOpacity(elImg, this.fader.FINISH_OPACITY);
		} else {
			this.fader.fadeIn(elImg);			
		}
		
		this.timeouts.push(setTimeout(this.getShowFunctionClosure(this.getNextPerson(), container), this.getFaceTime()));
	}
};

/**
 * Stub at this point
 * Assuming we add the ability to display comments when a person is mousedover
 */
WSODPeople.prototype.reset = function() {
	for (var i=0; i<this.timeouts.length; i++) {
		clearTimeout(this.timeouts[i]);
	}
	
	this.timeouts = [];
	this.showAll();
};