function Testimonials(baseNode, titleNode) {
	
	this.init = function() {
		
		showLoading(me.baseNode);
		me.getTestimonials();
		
	};
	
	this.getTestimonials = function() {
		
		var th = new TransferHelper();
		var d = th.createDocument();
		
		th.sendDocument("/lib/xml/testimonials/testimonials.xml?q=m", d, me.getTestimonialsComplete, "get");
	};
	
	this.getTestimonialsComplete = function(doc) {
		if(!testDoc(doc)) {
			// do nothing
			clearNode(me.baseNode);
			clearNode(me.titleNode);
		} else {
			var nodes = doc.getElementsByTagName("testimonial");
			
			var results = new Array();
			for(var i=0; i<nodes.length; i++) {
				results[i] = me.testimonialFromNode(nodes[i]);
			}
			me.displayTestimonials(results);
		}
	};

	this.displayTestimonials = function(results) {
		clearNode(me.baseNode);
		clearNode(me.titleNode);
		
		if(!results.length) return;
		
		me.setTitle();
		
		for(var i=0; i<2; i++) {
			var result = results[Math.floor(Math.random()*(results.length))];
			if(result.length >= 2) {
				if(i==0) {
					me.firstId = result.id;
				} else {
					if(result.id == me.firstId) {
						var seg_results = new Array();
						var z = 0;
						for(var r=0; r<results.length; r++) {
							if(results[r].id != me.firstId) {
								seg_results[z++] = results[r];
							}
						}
						
						var result = seg_results[Math.floor(Math.random()*(seg_results.length))];
					}
				}
			} else if(i == 1) {
				continue;
			}
			
			var p = document.createElement("p");
			p.appendChild(document.createTextNode(result.testimonial));
			
			me.baseNode.appendChild(p);
			
			var table = document.createElement("table");
			me.baseNode.appendChild(table);
			
			var tbody = document.createElement("tbody");
			table.appendChild(tbody);
			
			var tr = document.createElement("tr");
			tbody.appendChild(tr);
			
			var td = document.createElement("td");
			td.style.width = "27px";
			var img = document.createElement("img");
			img.src = "/lib/images/bubble.jpg";
			img.style.width = "17px";
			img.style.height = "18px";
			img.style.margin = "5px";
			td.appendChild(img);
			tr.appendChild(td);
			
			var td = document.createElement("td");
			td.style.width = "195px";
			var span = document.createElement("span");
			span.className = "testimonial_author";
			span.appendChild(document.createTextNode(result.author_name));
			td.appendChild(span);
			td.appendChild(document.createElement("br"));
			td.appendChild(document.createTextNode(result.author_url));
			tr.appendChild(td);
			
			var td = document.createElement("td");
			td.style.width = "63px";
			var img = document.createElement("img");
			img.src = "/lib/images/quotes.jpg";
			img.style.width = "49px";
			img.style.height = "37px";
			td.appendChild(img);
			tr.appendChild(td);
		}
		if(results.length > 2) {
			if(me.redrawTimer) window.clearTimeout(me.redrawTimer);
			me.redrawTimer = window.setTimeout(me.getTestimonials, 5*60*1000); // 5 min
		}
		
	};
	
	this.testimonialFromNode = function(node) {
		var testimonial = new Testimonial();
		for(var i=0; i<node.childNodes.length; i++) {
			switch(node.childNodes[i].nodeName) {
				case "id": testimonial.id = nodeVal(node.childNodes[i]); break;
				case "name": testimonial.name = nodeVal(node.childNodes[i]); break;
				case "created": testimonial.created = nodeVal(node.childNodes[i]); break;
				case "modified": testimonial.modified = nodeVal(node.childNodes[i]); break;
				case "created_by": testimonial.created_by = nodeVal(node.childNodes[i]); break;
				case "modified_by": testimonial.modified_by = nodeVal(node.childNodes[i]); break;
				case "author_name": testimonial.author_name = nodeVal(node.childNodes[i]); break;
				case "author_url": testimonial.author_url = nodeVal(node.childNodes[i]); break;
				case "testimonial_text": testimonial.testimonial = nodeVal(node.childNodes[i]); break;
			}
		}
		return testimonial;
	};
	
	this.setTitle = function() {
		if(!me.titleNode) return;
		
		var title = document.createElement("img");
		title.src = "/lib/images/testimonials.jpg";
		title.style.width = "126px";
		title.style.height = "18px";
		
		me.titleNode.appendChild(title);
	};
	
	var me = this;
	
	this.baseNode = baseNode;
	this.titleNode = titleNode;
	this.firstId = 0;
	this.redrawTimer = null;
	
	this.init();
}

function Testimonial() {
	this.id;
	this.name;
	this.created;
	this.modified;
	this.created_by;
	this.modified_by;
	this.author_name;
	this.author_url;
	this.testimonial;
}
