function Tour(els) {
	if (!els) { els = ".tourdetail"; }
	this.els = $(els);
	if (!this.els.hasClass("js")) {
		this.init();
	}
	this.getData();
	this.bindEvents();
}
Tour.prototype = {
// Initialize the Tour object
	"init": function(){
		this.els.addClass("js");
		
		this.data = {};
		
		this.tourid = this.els.children("div:first").attr("id");
		this.touritemid = this.els.find(".item").attr("id");
		
		var classpath = this.els.find(".left", "#"+this.touritemid).children().attr("src");
			this.classpath = classpath.split("_files")[0];
		var curtour = this.tourid;
			this.curtour = curtour.split("_")[0] + "id=" + curtour.split("_")[1];
		var curitem = this.touritemid;
			this.curitem = curitem.split("_")[0] + "id=" + curitem.split("_")[1];
			
		$("<span></span>").addClass("pause").append($("<a></a>").text("Pause").attr("href", "#"+this.curitem)).insertAfter(".right .navigation .previous", "#"+this.touritemid);
		
		var tourthumb = this.els.children(".tourthumbs");
			tourthumb.css("overflow", "hidden");
		var tourthumbs = tourthumb.children(".thumbnail");
		$("<div></div>").addClass("tourthumbscontent").append(tourthumbs).appendTo(tourthumb);
		$("<div></div>").addClass("previous").html("&larr;").prependTo(tourthumb);
		$("<div></div>").addClass("next").html("&rarr;").appendTo(tourthumb);
		var tourthumbscontentwidth = tourthumb.innerWidth() - $(".tourthumbs .previous").outerWidth(true) - $(".tourthumbs .next").outerWidth(true) - 20;
		$(".tourthumbscontent").width(tourthumbscontentwidth);
	},
	
// Get and set the data, invoking a "displayAjax" method to get all pertinent info up front.
	"getData": function(){
		var now = new Date().getTime() / 1000;
		var now = parseInt(now, 10);
		var ajaxurl = this.classpath + "Public.cfc?method=displayAjax&ts=" + now + "&returnformat=json&tourid=" + this.tourid.split("_")[1];
		var that = this;
		
		$.getJSON(ajaxurl, function(d, s){ if(s === "success"){ that.setData(d); } });
	},
	"setData": function(d){
		this.data = d;
		this.afterData();
	},
	"afterData": function(){
		if (window.location.hash.length && window.location.hash !== this.curitem){
			this.goToItem(window.location.hash);
		} else {
			window.location.hash = this.curitem;
		}
		this.start();
	},
	
// Event management for slideshow buttons and thumbnails
	"bindEvents": function(){
		this.bindPrevious();
		this.bindPause();
		this.bindNext();
		this.bindThumbnails();
	},
	"bindPrevious": function(){
		var that = this;
		this.previous = $(".right .navigation .previous", "#"+this.touritemid);
		this.previous.click(function(ev){
			ev.preventDefault();
			that.stop();
			that.goPrevious();
		});
	},
	"bindPause": function(){
		var that = this;
		this.pause = $(".right .navigation .pause", "#"+this.touritemid);
		this.pause.click(function(ev){
			ev.preventDefault();
			that.goPause();
		});
	},
	"bindNext": function(){
		var that = this;
		this.next = $(".right .navigation .next", "#"+this.touritemid);
		this.next.click(function(ev){
			ev.preventDefault();
			that.stop();
			that.goNext();
		});
	},
	"bindThumbnails": function(){
		var that = this;
		var thumbs = $(".tourthumbs .thumbnail a");
		var thumbsPrevious = $(".tourthumbs .previous");
		var thumbsNext = $(".tourthumbs .next");
		
		thumbs.click(function(ev){
			ev.preventDefault();
			that.stop();
			var thisitem = $(this).attr("href").match(/touritemid=\d+/)[0];
			that.goToItem(thisitem);
		});
		thumbsNext.click(function(ev){
			ev.preventDefault();
			that.goNextThumbs();
		});
		thumbsPrevious.click(function(ev){
			ev.preventDefault();
			that.goPreviousThumbs();
		});
	},

// 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 = parseInt(this.curitem.split("=")[1]);
		var i = 0;
		var data = this.data;
		var end = data.length - 1;
		var item = data[data.length];
		
		for (i=end-1; i>=-1; i--){
			if (data[i + 1].TOURITEMID === itemid) {
				if (i > -1) {
					item = data[i];
				} else {
					item = data[end];
				}
			}
		}
		this.updateTour(item);
	},
	"goPause": function(){
		if (this.started === false){
			this.start();
		} else {
			this.stop();
		}
	},
	"goNext": function(){
		var itemid = parseInt(this.curitem.split("=")[1]);
		var i = 0;
		var data = this.data;
		var start = 0;
		var item = data[0];
		
		for (i=start+1; i<=data.length; i++){
			if (data[i - 1].TOURITEMID === itemid) {
				if (i < data.length) {
					item = data[i];
				} else {
					item = data[start];
				}
			}
		}
		this.updateTour(item);
	},
	"goToItem": function(itemid){
		if (!itemid) { itemid = this.touritemid; }
		itemid = parseInt(itemid.split("=")[1]);

		var i = 0;
		var data = this.data;
		var item = data[0];
		
		for (i=0; i<data.length; i++){
			if (data[i].TOURITEMID === itemid) {
				item = data[i];
			}
		}
		this.updateTour(item);
	},
	"goPreviousThumbs": function(){
		var content = $(".tourthumbs");
		var thumbHeight = content.children(".tourthumbscontent").children(".thumbnail").outerHeight(true);
		var curHeight = content.scrollTop();
		var newHeight = curHeight - thumbHeight;

		if (curHeight > 0) {
			var thumbsPrevious = $(".tourthumbs .previous");
			var thumbsNext = $(".tourthumbs .next");
			content.scrollTop(newHeight);
			thumbsPrevious.css("paddingTop", newHeight+"px");
			thumbsNext.css("paddingTop", newHeight+"px");
		}
	},
	"goNextThumbs": function(){
		var content = $(".tourthumbs");
		var thumbHeight = content.children(".tourthumbscontent").children(".thumbnail").outerHeight(true);
		var thumbsHeight = content.children(".tourthumbscontent").outerHeight(true);
		var curHeight = content.scrollTop();
		var newHeight = curHeight + thumbHeight;

		if ( (thumbsHeight > curHeight + thumbHeight) && (curHeight < content.outerHeight(true)) ) {
			var thumbsPrevious = $(".tourthumbs .previous");
			var thumbsNext = $(".tourthumbs .next");
			content.scrollTop(newHeight);
			thumbsPrevious.css("paddingTop", newHeight+"px");
			thumbsNext.css("paddingTop", newHeight+"px");
		}
	},
	
// The one function that does the visual update of the tour item details
	"updateTour": function(item){
		if (!item.POSITION) { item.POSITION = 0; }
		item.POSITION = parseInt(item.POSITION);
		
		var newitemid = "touritem_" + item.TOURITEMID;
		$("#"+this.touritemid).attr("id", newitemid);
		this.touritemid = newitemid;

		var curitem = this.touritemid;
			this.curitem = curitem.split("_")[0] + "id=" + curitem.split("_")[1];

		window.location.hash = this.curitem;

		$("#"+newitemid)
			.children(".right")
				.children("h4").fadeOut("normal", function(){ $(this).text(item.NAME).fadeIn(); }).end()
				.children(".description")
					.children(".wrapper").fadeOut("normal", function(){ $(this).html(item.DESCRIPTION).fadeIn(); }).end()
				.end()
				.children(".navigation .detail strong:first").text(item.POSITION).end()
			.end()
			.children(".left")
				.each(function(){
					var html = '<img height="290" src="' + item.FILE + '" alt="Image for ' + item.NAME + '" title="' + item.NAME + '" />';
					if (item.FILE.substring(item.FILE.length - 3) === "swf") {
						html = '<embed height="290" width="360" type="application/x-shockwave-flash" src="' + item.FILE + '" />';
					}
					$(this).fadeOut("normal", function(){ $(this).html(html).fadeIn(); });
				});
				
		$(".tourthumbs")
			.find(".thumbnail")
				.removeClass("active")
				.end()
			.find(".thumbnail:eq(" + (item.POSITION - 1) + ")")
				.addClass("active");
		
	}
};
