/**
 * @author Damien Churchill
 * @version 0.1
 */
var Testimonials = new Class({
	
	Binds: ['rotate'],

	initialize: function(div) {
		this.div = div;
		
		// ensure that all overflowing elements are hidden.
		this.div.setStyle('overflow', 'hidden');
		
		// see how much space we have to use, we need this when adding
		// testimonials.
		this.size = this.div.getSize();
		
		// empty the div as we don't care about whats in there at the
		// moment.
		this.div.empty();
		
		// create an array so we have easy access to our testimonial
		// elements
		this.testimonials = new Array();
		
		// this effects
		this.effects = new Array();
		
		// set the currentIndex to 0
		this.currentIndex = 0;
	},
	
	addTestimonial: function(html) {
		// create a container element for the testimonial to ensure that it
		// has a wrapper element
		var elm = new Element('div', {
			html: html,
			style: 'height: ' + this.size.y + 'px; width: ' + this.size.x + 'px;'
		});
		
		// add the element to the easy access array
		this.testimonials.push(elm);
		
		// add the created element to the testimonials div
		this.div.grab(elm);
		
		// create a slide effect for the element, naming the div as the wrapper.
		this.effects.push(new Fx.Tween(elm, {property: 'margin-top'}));
		
		// return this to allow for chaining
		return this;
	},
	
	rotate: function() {
		// set up a few local vars that are required in the clean up func
		var elm = this.testimonials[this.currentIndex];
		
		// start scrolling the visible element out of display.
		this.effects[this.currentIndex++].start(-this.size.y).chain(function() {
			// remove the div from the dom
			elm.dispose();
			
			// reset the margin-top style.
			elm.setStyle('margin-top', 0);
			
			// re-add the element to the dom so it ends up at the end of the div.
			this.div.grab(elm);
		}.bind(this));
		
		// if we've reached our limit then reset the currentIndex
		if (this.currentIndex >= this.testimonials.length) this.currentIndex = 0;
	},
	
	start: function(interval) {
		// don't want to start calling rotate again so just
		// return if it we are running
		if (this.running) return;
		
		// check to see if we have a passed in interval
		interval = $pick(interval, 7000)
		
		// use the periodical extension that mootools adds to start the
		// rotate function calling every 7 seconds or the passed in interval.
		this.running = this.rotate.periodical(interval);
	},
	
	stop: function() {
		if (!this.running) return;
		$clear(this.running);
	}
});

document.addEvent('domready', function(e) {
	var testimonials = new Testimonials($('quotes'));
	testimonials
		.addTestimonial("<p>'The project was particularly exciting as it allowed for joint work with leading academics at the University of Reading and Research scientists at @UK PLC.  This is one of the largest and most innovative KTP projects ever undertaken and we are very excited by the results.  We believe that the new tool is groundbreaking and has come at exactly the right time to help public and private sector organizations achieve savings.'</p><H4>Professor Mark Bishop, Department of Computing<br>Goldsmiths </H4>")
		.addTestimonial("<p>'It's really useful because we can look at the information from all different angles and it's so easy to use,' says Ms Hamilton. 'It will help us rationalise and standardise our suppliers and products, which in turn means we can get better prices.'</p><H4>Vicki Hamilton, The Head of Procurement and Supplies for the Outer South East London Shared Procurement<br>London Procurement Program</H4>")
		.addTestimonial("<p>'The project was particularly exciting as it allowed for joint work with leading academics at the University of Reading and Research scientists at @UK PLC.  This is one of the largest and most innovative KTP projects ever undertaken and we are very excited by the results.  We believe that the new tool is groundbreaking and has come at exactly the right time to help public and private sector organizations achieve savings.'</p><H4>Professor Mark Bishop, Department of Computing<br>Goldsmiths </H4>")
		.addTestimonial("<p>'It's really useful because we can look at the information from all different angles and it's so easy to use,' says Ms Hamilton. 'It will help us rationalise and standardise our suppliers and products, which in turn means we can get better prices.'</p><H4>Vicki Hamilton, The Head of Procurement and Supplies for the Outer South East London Shared Procurement<br>London Procurement Program</H4>")
		.addTestimonial("<p>'The project was particularly exciting as it allowed for joint work with leading academics at the University of Reading and Research scientists at @UK PLC.  This is one of the largest and most innovative KTP projects ever undertaken and we are very excited by the results.  We believe that the new tool is groundbreaking and has come at exactly the right time to help public and private sector organizations achieve savings.'</p><H4>Professor Mark Bishop, Department of Computing<br>Goldsmiths </H4>")
		.addTestimonial("<p>'It's really useful because we can look at the information from all different angles and it's so easy to use,' says Ms Hamilton. 'It will help us rationalise and standardise our suppliers and products, which in turn means we can get better prices.'</p><H4>Vicki Hamilton, The Head of Procurement and Supplies for the Outer South East London Shared Procurement<br>London Procurement Program</H4>")
		.addTestimonial("<p>'The project was particularly exciting as it allowed for joint work with leading academics at the University of Reading and Research scientists at @UK PLC.  This is one of the largest and most innovative KTP projects ever undertaken and we are very excited by the results.  We believe that the new tool is groundbreaking and has come at exactly the right time to help public and private sector organizations achieve savings.'</p><H4>Professor Mark Bishop, Department of Computing<br>Goldsmiths </H4>")
		.addTestimonial("<p>'It's really useful because we can look at the information from all different angles and it's so easy to use,' says Ms Hamilton. 'It will help us rationalise and standardise our suppliers and products, which in turn means we can get better prices.'</p><H4>Vicki Hamilton, The Head of Procurement and Supplies for the Outer South East London Shared Procurement<br>London Procurement Program</H4>")
		.start();
});
