$(document).ready(function() {
	$(function() {
		carousel.init("#carousel");
	});
});

var carousel = {
	el : undefined,
	list_el : undefined,
	current : undefined,
	currentType_el : undefined,
	items : undefined,
	thumbs : undefined,
	scrollIndicator : undefined,
	scrollIndicatorBar : undefined,
	advanceInterval: undefined,
	paused : false,
	settings : {
		autoAdvance: true,
		autoAdvanceDelay: 6000,
		pauseOnHover: true,
		supportsTransitions: false
	},

	init : function(selector, options) {
		if (options) {
			$.extend(this.settings, options);
		}
		this.el = $(selector);
		this.currentType_el = $("h2 span", this.el);
		this.list_el = $("#carousel-thumbs ul", this.el);
		this.items = $("#carousel-feature-display li a", this.el);
		this.thumbs = $("#carousel-thumbs li a", this.el);
		this.scrollIndicator = $("#carousel-scroll-indicator", this.el);
		this.scrollIndicatorBar = $("#carousel-scroll-indicator div", this.el);
		this.testForCSSTransitions();
		if (this.settings.supportsTransitions) {
			this.el.addClass("css-transitions");
		}

		var self = this;

		// next/prev button events
		$("#carousel-prevpage").click(function(e) {
			self.prev();
			self.autoAdvanceStop();
			self.settings.autoAdvance = false; // permanently cancel autoadvance after user interaction
		});
		$("#carousel-nextpage").click(function(e) {
			self.next();
			self.autoAdvanceStop();
			self.settings.autoAdvance = false; // permanently cancel autoadvance after user interaction
		});

		// thumbs
		this.thumbs.click(function(e) {
			self.setCurrent($(e.currentTarget).data('itemNum'));
			return false;
		});
		this.thumbs.each(function(i,el) {
			$(el).data('itemNum',i);
		});
		this.setCurrent(0);

		if (this.thumbs.length > 4) {
			// set initial width of scroll indicator
			var w = 550 * (4/this.thumbs.length);
			this.scrollIndicatorBar.width(w);
			this.scrollIndicator.show();
		}

		// auto advance
		if (this.settings.autoAdvance) {
			this.autoAdvanceStart();
		}
		if (this.settings.pauseOnHover) {
			this.el.mouseenter(function(e) {
				self.paused = true;
				self.autoAdvanceStop();
			});
			this.el.mouseleave(function(e) {
				self.paused = false;
				self.autoAdvanceStart();
			});
		}
	},
	next : function() {
		if (this.current+1 < this.thumbs.length) {
			this.setCurrent(this.current+1);
		} else {
			this.setCurrent(0);
		}
	},
	prev : function() {
		if (this.current > 0) {
			this.setCurrent(this.current-1);
		} else {
			this.setCurrent(this.thumbs.length-1);
		}
	},
	setCurrent : function(n) {
		if (n < 0 || n > this.items.length || n == this.current) {
			// n is not valid or current is unchanged
			return;
		}

		var previousItem = this.current;
		this.current = n;

		// main feature
		if (this.settings.supportsTransitions) {
			this.items.removeClass('selected').hide();
			$(this.items[this.current]).addClass('selected').show();
		} else {
			this.items.removeClass('selected').animate({opacity: 0}, {duration:500,queue:false}).hide();
			$(this.items[this.current]).addClass('selected').animate({opacity:1}, {duration:500,queue:false}).show();
		}
		// set the "type" of feature in the H2
		//var c = $(this.thumbs[this.current]).attr('class');
		//var type = c.match(/carousel-thumb-([^\s]+)/);
		//this.currentType_el.text(type[1]);

		// thumbs and scrolling
		var scrollPosition = (n<(this.thumbs.length-4)) ? n : this.thumbs.length-4;
		if (this.settings.supportsTransitions) {
			this.list_el.css('left', (0-scrollPosition*140)+'px');
			this.scrollIndicatorBar.css('left', (scrollPosition*(550/this.items.length))+'px');
		} else {
			this.list_el.animate({left: (0-scrollPosition*140)}, {duration:500,queue:false});
			this.scrollIndicatorBar.animate({left: (scrollPosition*(550/this.items.length))}, {duration:500,queue:false});
		}

		// set the new selected, page-start and page-end
		this.thumbs.removeClass("page-start page-end selected");

		$(this.thumbs[this.current]).addClass("selected");
		if (this.settings.supportsTransitions) {
			$(this.thumbs[scrollPosition]).addClass("page-start");
			$(this.thumbs[scrollPosition+3]).addClass("page-end");
		} else {
			if (previousItem != undefined) {
				this.thumbs.animate({marginLeft:0,marginRight:0},{duration:500,queue:false});
			}
			$(this.thumbs[scrollPosition]).addClass("page-start").animate({marginLeft:30},{duration:500,queue:false});
			$(this.thumbs[scrollPosition+3]).addClass("page-end").animate({marginRight:30},{duration:500,queue:false});
		}

		if (this.settings.autoAdvance && !this.paused) {
			this.autoAdvanceStart();
		}

	},

	autoAdvanceStart : function() {
		var self = this;
		if (this.advanceInterval) {
			this.autoAdvanceStop();
		}
		this.advanceInterval = setInterval(
			function() {
				self.next();
			},
			this.settings.autoAdvanceDelay
		);
	},

	autoAdvanceStop : function() {
		clearInterval(this.advanceInterval);
	},

	testForCSSTransitions : function() {
		var div = document.createElement('div');
	    div.setAttribute('style', 'transition:top 1s ease;-webkit-transition:top 1s ease;-moz-transition:top 1s ease;');
	  	this.settings.supportsTransitions = !!(div.style.transition || div.style.webkitTransition || div.style.MozTransition || div.style.OTransition);
		//this.settings.supportsTransitions = false; // use this for testing
	    delete div;
	}

}
