/********
	Use to script your SMS class.
********/
function ImageRotator(els) {
	if (!els) { els = ".imagerotator"; }
	this.els = $(els);
	if (!this.els.hasClass("js")) {
		this.init();
	}
	this.getData();
	this.bindEvents();
}
ImageRotator.prototype = {
// Initialize the object
	"init": function(){
		this.els.addClass("js");
		
		this.data = {};
		
		this.itemid = this.els.find(".item").attr("id");
		
		var classpath = $("#"+this.itemid).attr("src");
			this.classpath = classpath.split("_files")[0];
		var curitem = this.itemid;
			this.curitem = curitem.split("_")[0] + "id=" + curitem.split("_")[1];
		var pageregionid = this.els.attr("id").split("_");
			this.pageregionid = pageregionid[pageregionid.length - 1];
	},
	
// Get and set the data, invoking a "displayAjax" method to get all pertinent info up front.
	"getData": function(){
		var ajaxurl = this.classpath + "Public.cfc?method=displayAjax&returnformat=json&pageregionid=" + this.pageregionid;
		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 buttons
	"bindEvents": function(){
		this.bindNavigation();
	},
	"bindNavigation": function(){
		var that = this;
		var items = $(".navigation a", this.els);
		
		items.click(function(ev){
			ev.preventDefault();
			that.stop();
			var thisitem = $(this).attr("href").match(/imagerotatorid=\d+/)[0];
			that.goToItem(thisitem);
		});
	},

// 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);
	},
	"goNext": function(){
		var itemid = parseInt(this.curitem.split("=")[1], 10);
		var i = 0;
		var data = this.data;
		var start = 0;
		var item = data[0];
		
		for (i=start+1; i<=data.length; i++){
			if (parseInt(data[i - 1].IMAGEROTATORID, 10) === itemid) {
				if (i < data.length) {
					item = data[i];
				} else {
					item = data[start];
				}
			}
		}
		this.updateImage(item);
	},
	"goToItem": function(itemid){
		if (!itemid) { itemid = this.itemid; }
		itemid = parseInt(itemid.split("=")[1], 10);

		var i = 0;
		var data = this.data;
		var item = data[0];
		
		for (i=0; i<data.length; i++){
			if (parseInt(data[i].IMAGEROTATORID, 10) === itemid) {
				item = data[i];
			}
		}
		this.updateImage(item);
	},
	
// The one function that does the visual update of the details
	"updateImage": function(item){
		if (!item.POSITION) { item.POSITION = 0; }
		item.POSITION = parseInt(item.POSITION);
		
		var newitemid = "imagerotator_" + item.IMAGEROTATORID;
		$("#"+this.itemid).attr("id", newitemid);
		this.itemid = newitemid;

		var curitem = this.itemid;
		this.curitem = curitem.split("_")[0] + "id=" + curitem.split("_")[1];

		$("#"+newitemid)
			.fadeOut("normal", function(){
				var item_url = "#" + $(this).parent().parent().attr("id");
				var item_target = "_self";
				
				$(this).attr("title", item.TITLE);
				$(this).attr("alt", item.DESCRIPTION);
				$(this).attr("src", item.IMAGE);
				
				if (item.URL.length) {
					item_url = item.URL;
					item_target = item.TARGET;
				}
				
				$(this).parent().attr("href", item_url);
				$(this).parent().attr("target", item_target);
				
				$(".navigation", this.els)
					.find("li")
						.removeClass("active")
						.end()
					.find("li:eq(" + (item.POSITION - 1) + ")")
						.addClass("active");
				
				$(".description", this.els)
					.each(function(){
						$(this).empty();
						
						if (item.DESCRIPTION.length) {
							$("<div></div>").addClass("description_text").html(item.DESCRIPTION).appendTo(this);
						}
						
						if (item.URL.length && item.URL.substring(0,1) !== "#") {
							$("<div></div>").addClass("description_link").html("<a href=\"" + item.URL + "\">Learn more</a>").appendTo(this);
						}
						
					});
						
				$(this).fadeIn("normal");
			});
	}
};
