/**
 * @author spollard
 */

jQuery.fn.trackrecord = function(file, settings) {
	
	settings = jQuery.extend({
			interval: 5,
			start: 1
		}, settings);
	
	var container = jQuery(this.empty().get(0));
	jQuery.ajax({
			dataType: "xml",
			error: function(XHR, error) {alert('error:' + error);},
			success: function(data) {
				var tabs = jQuery("<ul class='tabs'></ul>");
				var tabcount = 0;
				jQuery("slide", data).each(function() {
					var imagesrc = jQuery("image", this).attr("path");
					var quotetext = jQuery("quote", this).text();
					var quotename = jQuery("person", this).text();
					
					var record = jQuery("<div class='record'></div>");
					var quote = jQuery("<div class='quote'></div>");
					var person = jQuery("<div class='person'></div>");

					record.append(quote);
					quote.text(quotetext);
					
					if (imagesrc != undefined) {
						var image = jQuery("<img />");
						image.attr("src", imagesrc);
						quote.prepend(image);
					}
					
					record.append(person);
					person.text(quotename);
					
					record.hide();
					container.append(record);
					container.attr("animate", "1");
					
					var tab = jQuery("<li class='tab'/>");
					tabcount += 1;
					tab.text(tabcount);
					
					tab.click(function() {
						container.attr("animate", "0");
						jQuery('.record', container).hide();
						record.show();
					});
					tabs.append(tab);
					
				});

				container.prepend(tabs);
				jQuery(".record", container).eq(0).show();
				setInterval(function() {
					if (container.attr("animate") == 1) {
					
						current = jQuery(".record:visible");
						if (current.next().length > 0) {
							current.hide().next().show();
						}
						else {
							current.hide().parent().children().filter(".record").eq(0).show();
						}
					}
				}, settings.interval * 1000);
			},
			url: file
		});

};

