/* Javascript for home page testimonial tab */

function CycleTestimonials(testimonialId, path) {
	this.testimonialId = testimonialId;
	this.path = path;
	var self = this;
	
	this.construct = function() {
		self.makeRequest();
	}
	
	this.makeRequest = function() {
		$.ajax({
			async: true, // default
			beforeSend: function() { self.preloader(); },
			cache: true, // default
			//complete: function(data, textStatus) { self.complete(data, textStatus); },
			contentType: 'application/x-www-form-urlencoded', //default
			data: "id=" + self.testimonialId, // default, and difficult to test without a server-side script
			dataFilter: null, //default
			//dataType: 'json',
			error: function(data, textStatus, errorThrown) { self.error(); },
			global: true, //default
			ifModified: false, //default
			success: function(data, textStatus) { self.success(data, textStatus); },
			timeout: 10000, // milliseconds
			url: self.path,
			type: 'get'
			
		});
	}
	
	this.preloader = function() {
		if(self.path == "getTestimonial.php") {
			$("#loader").html('<img src="_images/loader.gif" class="preLoader" />');
		} else {
			$("#loader").html('<img src="../_images/loader.gif" class="preLoader" />');
		}
	}
	
	this.error = function() {
		$("#loader").html('');
		$("#contentArea").fadeOut("slow", function() { $(this).html('<address>no data</address><h4>no data</h4><p>There was a problem loading the testimonial. Please try again.</p>');
		});
		
		$("#contentArea").fadeIn("slow");	
			
		cycleTestimonials.testimonialId++
		self.nextTestimonial(total);
	}
	
	this.success = function(data, testStatus) {
		this.dataArray = data.split("###SPACE###");
		var total = this.dataArray[0];
		var name = this.dataArray[1];
		var jobTitle = this.dataArray[2];
		var city = this.dataArray[3];
		var state = this.dataArray[4];
		var quote = this.dataArray[5];
		
		$("#loader").html('');
		$("#contentArea").fadeOut("slow", function() { $(this).html('<address>' + name + ', ' + jobTitle + ', ' + city + ', ' + state + '</address>' + '<h4>' + name + ' writes:' + '</h4>' + '<p>' + quote + '<a href="testimonials.php">Read more</a></p>');
		});
		
		$("#contentArea").fadeIn("slow");	
			
		cycleTestimonials.testimonialId++
		self.nextTestimonial(total);
	}
	
	this.nextTestimonial = function(total) {
		
		if (cycleTestimonials.testimonialId == total) {
			cycleTestimonials.testimonialId = 0;
		}
		
		$("#nextTestimonial").unbind().bind('click', function() {
			self.construct();
			return false;
		});
	}
}

/* OLD CODE */
/*
$(function() {
	var current = 1;
	var total;
	var idArr = new Array;

	$.ajax({
		cache: true, // default is true - turn off if you expect fresh info
		//complete: function(data, textStatus) { $('#contentArea').append('&mdash;<em>Ajax Completed!</em>') },// will typically always run, when ajax function complete, probably best practice
		data: {'firstTime':'true'}, // default, and difficult to test without a server-side script, can send array in key/value pairs
		dataType: 'json',// the data you expect back from the server
		error: function(data, textStatus, errorThrown) { alert("Ajax error: " + textStatus) },
		success: function(data, textStatus) {
					total = data.total;
					for( var i=0; i<data.total; i++ ){
						idArr.push(data.idArr[i]);
					}
					var d = data.quote;
					$('#contentArea').html(
						'<address>'+d.name+', '+d.job_title+', '+d.city+', '+d.state+'</address>'+
						'<h4>'+d.name+' writes:'+'</h4>'+
						'<p>'+d.quote+'<a href="testimonials.php"> Read more</a></p>'
					);
				},// required
		timeout: 3000, // milliseconds, default of null
		url: 'getTestimonial.php', // required
		type: 'get'
	});

	$('#nextTestimonial').click( function(){
		$.ajax({
			cache: true, // default is true - turn off if you expect fresh info
			//complete: function(data, textStatus) { $('#contentArea').append('&mdash;<em>Ajax Completed!</em>') },// will typically always run, when ajax function complete, probably best practice
			data: {'id':idArr[current], 'firstTime':'false'}, // default, and difficult to test without a server-side script, can send array in key/value pairs
			dataType: 'json',// the data you expect back from the server
			error: function(data, textStatus, errorThrown) { alert("Ajax error: " + textStatus) },
			success: function(data, textStatus) {
						//alert(idArr[current]);
						var d = data.quote;
						$('#contentArea').fadeOut('normal', function(){
							$(this).html(
								'<address>'+d.name+', '+d.job_title+', '+d.city+', '+d.state+'</address>'+
								'<h4>'+d.name+' writes:'+'</h4>'+
								'<p>'+d.quote+'<a href="testimonials.php"> Read more</a></p>'
							).fadeIn('normal');
						});
						current == data.total-1 ? current = 0 : current++;
					},// required
			timeout: 3000, // milliseconds, default of null
			url: 'getTestimonial.php', // required
			type: 'get'
		});
	});
});
*/