function NewsRotator(els) {
	if (!els) { els = ".newsrotator"; }
	this.els = $(els);
	if (!this.els.hasClass("js")) {
		this.init();
		this.getData();
		this.bindEvents();
	}
}
NewsRotator.prototype = {
// Initialize the NewsRotator object
	"init": function(){
		this.els.addClass("js");
		
		this.data = {};
		this.pageregionid = this.els.attr("id").split("_")[1];
		this.list = this.els.find(".newsrotator_list .item");
		this.numbertodisplay = this.list.length + 1;
		this.feature = this.els.find(".newsrotator_feature");
		this.curitem = this.feature.attr("id").split("_")[2];
		
		$('<div class="pause"><a href="#pause"><span>Pause</span></a></div>')
			.insertBefore(this.els.find(".controls .next"));
	},
	
// Get and set the data, invoking a "displayAjax" method to get all pertinent info up front.
	"getData": function(){
		var ajaxurl = "/classlibrary/page/news/public.cfc?method=displayAjax&returnformat=json&pageregionid=" + this.pageregionid + "&numbertodisplay=" + this.numbertodisplay;
		var that = this;
		
		$.getJSON(ajaxurl, function(d, s){ if(s === "success"){ that.setData(d); } });
	},
	"setData": function(d){
		this.data = d;
		this.afterData();
	},
	"afterData": function(){
		this.start();
	},
	
// Event management for slideshow buttons and thumbnails
	"bindEvents": function(){
		this.bindPrevious();
		this.bindPause();
		this.bindNext();
	},
	"bindPrevious": function(){
		var that = this;
		this.els.find(".controls .previous").click(function(ev){
			ev.preventDefault();
			that.stop();
			that.goPrevious();
		});
	},
	"bindPause": function(){
		var that = this;
		this.els.find(".controls .pause").click(function(ev){
			ev.preventDefault();
			that.goPause();
		});
	},
	"bindNext": function(){
		var that = this;
		this.els.find(".controls .next").click(function(ev){
			ev.preventDefault();
			that.stop();
			that.goNext();
		});
	},

// Slideshow functions for navigation, automatic and manual
	"start": function(){
		var that = this;
		this.timer = setTimeout(function(){
			that.started = true;
			that.goNext();
			that.start();
		}, 6000);
	},
	"stop": function(){
		this.started = false;
		clearTimeout(this.timer);
	},
	"goPrevious": function(){
		var itemid = this.curitem;
		var i = 0;
		var data = this.data;
		var end = data.length - 1;
		var item = data[end];
		
		for (i=end-1; i>=-1; i--){
			if (data[i + 1].NEWSSTORYID === itemid) {
				if (i > -1) {
					item = data[i];
				} else {
					item = data[end];
				}
			}
		}
		this.direction = -1;
		this.updateItem(item);
	},
	"goPause": function(){
		if (this.started === false){
			this.start();
		} else {
			this.stop();
		}
		this.direction = 0;
	},
	"goNext": function(){
		var itemid = this.curitem;
		var i = 0;
		var data = this.data;
		var start = 0;
		var item = data[start];
		
		for (i=start+1; i<=data.length; i++){
			if (data[i - 1].NEWSSTORYID === itemid) {
			console.log(itemid);
				if (i < data.length) {
					item = data[i];
				} else {
					item = data[start];
				}
			}
		}
		this.direction = 1;
		this.updateItem(item);
	},

// The one function that does the visual update of the tour item details
	"updateItem": function(item){
		var newitem = "<li class=\"item\"><span class=\"date\">" + this.feature.find(".date").text() + "</span><span class=\"title\">" + this.feature.find(".title").html() + "</span></li>";
								
		if (this.direction === -1) {
			this.list.eq(0).remove();
			//this.list.parent().append(newitem);
		} else if (this.direction === 1) {
			//this.list.parent().prepend(newitem);
			this.list.eq(this.list.length - 1).remove();
		}
		
		var newurl = this.feature.find(".title a").attr("href").split("id=0,")[0] + "id=0," + item.NEWSSTORYID;

		this.curitem = item.NEWSSTORYID;
		this.feature.attr("id", "newsrotator_feature_" + this.curitem);
		this.feature.find(".date").text(item.DATE);
		this.feature.find(".title a").text(item.TITLE);
		this.feature.find(".title a").attr("href", newurl);
		this.feature.find(".teaser").html(item.TEASER);
	}
}

$(document).ready(function(){
	$(".newsrotator").each(function(){
		var newid = this.id;
		//var newsrotator = new NewsRotator("#"+newid);
	});
});