/*

		Testimonials.js
		
		Mootools-based class to provide scrolling testimonials
		
		Written by:		Ken Kolodziej
		Created on:		June 17, 2009
		
*/

var Testimonials = new Class({
	options: {
		smoothScroll: true,
		delay: 80,
		increment: 1,
		testimonials: [],
		containerID: null,
		showContainerParent: true
	},
	initialize: function(options) {
		this.setOptions(options);
		
		if(!this.options.containerID)
			return;
		
		if(this.options.testimonials.length == 0)
			return;
		
		this.callback = null;
		this.idPrefix = parseInt(new Date().getTime().toString().substring(0, 10));
		
		this.parentDiv = $(this.options.containerID);
		
		// Create testimonial wrapper div
			this.wrapperDiv = new Element('div', {
				id: '__' + this.idPrefix + '_wrapper',
				'styles': {
					'position': 'relative',
					'width': '100%',
					'height': '100%',
					'overflow': 'hidden'
				},
				'events': {
					'mouseenter': this.pause.bind(this),
					'mouseleave': this.unpause.bind(this)
				}
			}).inject(this.parentDiv);
		
		// Create testimonial div
			this.testimonialDiv = new Element('div', {
				id: '__' + this.idPrefix + '_testimonials',
				'styles': {
					'position': 'absolute',
					'width': '100%',
					'height': 'auto'
				}
			}).inject(this.wrapperDiv);
		
		// Inject testimonials
			for(var i = 0; i < this.options.testimonials.length; i++) {
				var test = this.options.testimonials[i];
				var provider = '';
				if(test.provider.length > 0)
					provider = ' (' + test.provider + ')';
				var theEl = new Element('div', {
					id: '__' + this.idPrefix + '_' + i,
					html: '<span class="testimonial">' + test.body + '</span><br/><span class="testimonial">-&nbsp;<strong>' + test.name + '</strong>,&nbsp;' + test.city + ',&nbsp;' + test.state + ' - ' + test.location + provider + '</span>',
					'styles': {
						'padding-bottom': '40px'
					}
				}).inject(this.testimonialDiv);
			}
		
		// Set container height
		this.containerHeight = this.testimonialDiv.getScrollSize().y;
		this.parentDivHeight = this.parentDiv.getSize().y;
		
		// Set container padding
		this.testimonialDiv.setStyles({
			'top': this.parentDivHeight + 'px',
			'left': '0px'
		});
		this.testimonialDivPos = this.parentDivHeight;
		
		// Show container parent, if necessary
		if(this.options.showContainerParent == true) {
			var contParent = this.parentDiv.getParent();
			if(contParent) {
				contParent.setStyles({
					'display': 'block',
					'visibility': 'visible'
				});
			}
		}
		
		// Start scrolling
		this.start();
	},
	start: function() {
		// Set up callback
		this.callback = this.doScroll.periodical(this.options.delay, this);
	},
	pause: function() {
		$clear(this.callback);
	},
	unpause: function() {
		this.start();
	},
	doScroll: function() {
		this.testimonialDiv.setStyle('top', --this.testimonialDivPos + 'px');
		
		if(this.testimonialDivPos <= -this.containerHeight)
			this.testimonialDivPos = this.parentDivHeight;
	}
});
Testimonials.implement(new Options, new Events);