/*
*	Studyfieldmixer, version 0.1
*	(c) 2009 Steuerung B GmbH
*
*	Authors:	Henning F. Mettge
*	Website:	http://www.steuerungb.de
*
*/

Array.prototype.shuffle = function() {
		return this.sortBy(Math.random);
};

var StudyfieldMixer = Class.create();
StudyfieldMixer.prototype = {
	
	initialize: function(studyfields, fontSize, lineHeight, durationPerField, delay) {

		// config
		if (fontSize) {
			this.fontSize = fontSize;
		}
		else {
			this.fontSize = 12;	
		}					
		
		if (fontSize) {
			this.lineHeight = lineHeight;
		}
		else {
			this.lineHeight = 16;	
		}	
		
		if (durationPerField) {
			this.durationPerField = durationPerField;
		}
		else {
			this.durationPerField = 0.2;	
		}					
		
		if (delay) {
			this.delay = delay;
		}
		else {
			this.delay = 0.9;	
		}
		
		this.studyfields = studyfields;
		
		// field 1
		this.studyfields1 = studyfields.shuffle();
		
		// field 2					
		this.studyfields2 = studyfields.shuffle();
		
		document.getElementById('studyfieldlist1').innerHTML = '<li>' + this.studyfields1[this.studyfields1.length-1] + '</li>';
		document.getElementById('studyfieldlist2').innerHTML = '<li>' + this.studyfields2[this.studyfields2.length-1] + '</li>';
		
	},
		
	getRandomStudyfields: function() {							
		
		fieldlist1 = document.getElementById('studyfieldlist1');
		fieldlist1.innerHTML = '';
		for (i = 0; i < this.studyfields1.length; i++) {
			var tempChild = fieldlist1.appendChild(document.createElement('LI'));
			tempChild.style.fontSize = this.fontSize + 'px';
			tempChild.innerHTML = this.studyfields1[i];
		}
		fieldlist1.style.position = 'relative';
		fieldlist1.style.top = '-' + (this.studyfields.length * (this.lineHeight)) + 'px';
		fieldlist1.style.left = '0px';
						
		move1 = (this.studyfields.length * (this.lineHeight)) + 1;
		duration1 = this.studyfields1.length * this.durationPerField;					
		
		new Effect.Move(fieldlist1, {
			x: 0,
			y: +move1,
			mode: 'relative', 
			duration: duration1, 
			transition: Effect.Transitions.sinoidal
		});
		
		
		
		fieldlist2 = document.getElementById('studyfieldlist2');
		fieldlist2.innerHTML = '';
		for (i = 0; i < this.studyfields2.length; i++) {
			//console.log(this.studyfields2[i] + '!=' + this.studyfields1[0])
			if (this.studyfields2[i] != this.studyfields1[0]) {						
				var tempChild = fieldlist2.appendChild(document.createElement('LI'));
				tempChild.style.fontSize = this.fontSize + 'px';
				tempChild.innerHTML = this.studyfields2[i];
			}
			else {
				//console.log(this.studyfields2[i] + '==' + this.studyfields1[0])
			}
		}
		fieldlist2.style.position = 'relative';
		fieldlist2.style.top = '-' + ((this.studyfields.length - 1) * (this.lineHeight)) + 'px';
		fieldlist2.style.left = '0px';
		
		move2 = ((this.studyfields2.length - 1) * (this.lineHeight)) + 1;
		duration2 = this.studyfields2.length * this.durationPerField;
		
		new Effect.Move(fieldlist2, {
			x: 0,
			y: +move2,
			mode: 'relative',						
			duration: 2.0,
			transition: Effect.Transitions.sinoidal,
			delay: this.delay			
		});
		
		// shuffle for the next round
		// field 1
		this.studyfields1 = studyfields.shuffle();
		
		// field 2					
		this.studyfields2 = studyfields.shuffle();
		
	}	
	
} 