var TestimonialTicker = new Class({
	Implements: [Options, Chain, Event],

	options: {
		delay: 5000
	},

	testimonials: null,
	containerNode: null,
	textContainer: null,
	imageContainer: null,
	counter: null,

	initialize: function(testimonials, containerId, textId, imageId, options)
	{
		this.setOptions(options);
		this.counter = 0;
		this.testimonials = testimonials;
		this.containerNode = $(containerId);
		this.showTestimonial.delay(this.options.delay, this);
		TestimonialTicker.instance = this;
	},

	showTestimonial: function()
	{
		// dont go past the end of the testimonials list
		this.chain(
			function()
			{
				// fade out the container
				var tween = new Fx.Tween(this.containerNode, {property: "opacity"});
				tween.addEvent("complete", function(){
					TestimonialTicker.instance.callChain();
				});
				tween.start(1, 0);
			},
			function()
			{
				if (this.counter >= this.testimonials.length)
				{
					this.counter = 0;
				}
				// set the new text and image
				var testimonial = this.testimonials[this.counter];
				this.containerNode.empty();
				//this.containerNode.innerHTML = "<p>" + testimonial.testimonial + "<br/><span style=\"font-weight: bold;\">" + testimonial.name + "</span></p>";
				
				var paragraph = new Element("p");
				paragraph.addClass("first");
				paragraph.appendText(testimonial.testimonial);
				paragraph.inject(this.containerNode);
				var p = new Element("p");
				p.inject(this.containerNode, "bottom");
				p.setStyle("text-align", "right");
				var span = new Element("span");
				span.setStyle("font-weight", "bold");
				span.set("text", testimonial.name);
				span.inject(p);
				var br = new Element("br");
				br.inject(p, "bottom");
				
				// add the video link
				if (testimonial.video)
				{
					var action = "view";
					if (testimonial["media_type"] == "text")
					{
						action = "read";
					}
					else if (testimonial["media_type"] == "sound")
					{
						action = "hear";
					}
					p.appendText("Click ");
					var link = new Element("a");
					link.setProperty("href", "index.php?id=" + testimonial.video);
					link.set("text", "here");
					link.inject(p, "bottom");
					p.appendText(" to " + action + " " + testimonial.name);
					//var text = "Click <a href=\"index.php?" + testimonial.video + "\">here</a> to " + action + " " + testimonial.name;
					//p.appendText(text);
				}
				//this.containerNode.appendText(testimonial.name);
				this.containerNode.setStyle("background", "url(/uploads/tx_phtestimonial/" + testimonial.image + ") top right no-repeat");
				this.containerNode.setProperty("title", testimonial.name);
				this.callChain();
			},
			function()
			{
				// fade in the container
				var tween = new Fx.Tween(this.containerNode, {property: "opacity"});
				tween.addEvent("complete", function(){
					TestimonialTicker.instance.showTestimonial.delay(TestimonialTicker.instance.options.delay, TestimonialTicker.instance);
				});
				tween.start(0, 1);
				this.counter++;
			}
		);
		this.callChain();
	}

});