// Maintain Scope with Ajax calls

Abstract.XMLHttpCatch = Class.create();  
Abstract.XMLHttpCatch.prototype =  
{  
	initialize: function(opts){},  
	onGetTestimonials: function(response, scope)  
	{  
		scope.onGetTestimonials(response);  
	}  
};  

Abstract.Home = Class.create();  
Abstract.Home.prototype =  
{  
	initialize: function(opts)  
	{  
		for (var prop in opts)  
		{  
			this[prop] = opts[prop];  
		}  
	this.XMLHttpCatch = new Abstract.XMLHttpCatch();  
	},  
	
	getTestimonials: function()  
	{  
		// get a local name for our catch object  
		XMLHttpCatch = this.XMLHttpCatch;  
		
		// get a local name for "this" (what we want to run the response under)  
		scope = this;  
		
		// prototype does the rest  
		ajax = new Ajax.Request(  
			'/home/testimonials',  // URL  
			{           // options  
				method:'get',  
				onComplete: function(resp) 
				{  
					XMLHttpCatch.onGetTestimonials(resp, scope);  
				}
			});  
	},
	
	onGetTestimonials: function(response)  
	{  
		this.testimonials = eval(response.responseText);  
		this.switchTestimonial(0);
	},
	
	hideTestimonial: function(i)
	{
		this.switchTestimonial(1);
	},
	
	switchTestimonial: function(i)
	{	
		var self = this;
		setTimeout(function(){
			var length = self.testimonials.length;

			if (i == 0)
			{
				var n = 0;
			}
			else
			{
				var n = this.currentTestimonial + i

				if (n < 0)	
				{
					n = length - 1;
				}

				if (n >= length) 
				{
					n = 0;
				}
			}
			var t = self.testimonials[n].attributes;
			// text
			$('quote').innerHTML = "&#8220;";
			$('quote').innerHTML += t.pull_quote;
			$('quote').innerHTML += "&#8221;";
			$('client').innerHTML = "<b>" + t.name + "</b><br/>" 
			$('client').innerHTML += t.job_title + "<br/>";
			$('client').innerHTML += t.retailer;
			// set current
			this.currentTestimonial = n;
		}, 200);	
	}
};