;(function($) {
	
	$.fn.extend({
		carouselSlider: function(options) {
			return this.each(function() {
				new $.CarouselSlider(this, options);
			});
		}
	});
	
	$.CarouselSlider = function(element, options) {
		
		this.element		= $(element);
		this.tray			= $('.carousel-tray', this.element);
		this.slides			= $('.slide', this.element);
		this.slideWidth		= $(this.slides[0]).innerWidth();
		this.slideHeight	= $(this.slides[0]).innerHeight();
		
		var defaults = {
			panelIdPrefix: 'slide-',
			carouselControls: '.carousel-controls',
			jumpToSlideLinks: $('.jumpToSlide'),
			showSlideCount: false,
			showHash: false,
			callbacks: {
				preSlideChange: function(){},
				postSlideChange: function(){}
			}
		}
		
		this.options = $.extend({}, defaults, options || {});
	
		this.navContainer	= $(this.options.carouselControls);
		this.prevBtn		= $('.prevBtn',this.navContainer);
		this.nextBtn		= $('.nextBtn',this.navContainer);
		
		this.setup();
		
	};
	
	$.extend($.CarouselSlider.prototype, {
		
		setup: function() {
			
			var self = this;
			
			this.element.css({'overflow':'hidden','padding':0});
			
			$(this.tray).css('width', this.slides.length * this.slideWidth);
			
			$(this.prevBtn).click(function(){
				if (!$(this).hasClass('disabled'))
					self.prevSlide();
				return false;
			});
			
			$(this.nextBtn).click(function(){
				if (!$(this).hasClass('disabled'))
					self.nextSlide();
				return false;
			});
			
			$(this.options.jumpToSlideLinks).each(function(i){
				$(this).click(function(){
					if (!$(this).hasClass('selected'))
						self.jumpToSlide(i);
					return false;
				});
			});
			
			if (this.options.showSlideCount) {
				this.slideCount = $('<div class="slide-count"></div>');
				this.element.append(this.slideCount);
			}
			
			// set defaults
			this.index		= 0;
			this.trayMargin = 0;
			this.updateCurrentState();
			
			if (this.options.showHash && window.location.hash) {
				var hash	= window.location.hash.substr(1);
				var start	= hash.indexOf(this.options.panelIdPrefix);
				hash		= hash.substring(start, start + this.options.panelIdPrefix.length + 1); // hash + 1-digit index
				var index	= hash.replace(this.options.panelIdPrefix,'');
				if (index > 0 && index <= this.slides.length) {
					this.index = index - 1;
					this.jumpToSlide(this.index);
				} else {
					window.location.hash = '';
				}
			}
			
		},

		prevSlide: function() {
			var self = this;
			this.preSlideChange();
			if (this.index > 0) {
				this.index--;
				this.trayMargin -= this.slideWidth;
				$(this.tray).animate({
					marginLeft: '-' + this.trayMargin + 'px'
					}, 400, '',
					function() {
						self.postSlideChange();
					}
				);
			} else {
				//this.reset();
			}
		},
		
		nextSlide: function(){
			var self = this;
			
			this.preSlideChange();
			
			if (this.index < this.slides.length - 1) {
				this.index++;
				this.trayMargin += this.slideWidth;
				$(this.tray).animate({
					marginLeft: '-' + this.trayMargin + 'px'
					}, 400, '',
					function() {
						self.postSlideChange();
					}
				);
			} else {
				//this.reset();
			}
		},
		
		jumpToSlide: function(slideNum) {
			var self		= this;
			this.index		= slideNum;
			this.trayMargin	= this.index * this.slideWidth;
			
			this.preSlideChange();
			
			$(this.tray).animate({
				marginLeft: '-' + this.trayMargin + 'px'
				}, 400, '',
				function() {
					self.postSlideChange();
				}
			);
		},
		
		reset: function() {
			var self 		= this;
			this.index		= 0;
			this.trayMargin = 0;
			
			this.preSlideChange();
			
			$(this.tray).animate({
				marginLeft:0
				}, 400, '',
				function() {
					self.postSlideChange();
				}
			);
		},
		
		updateCurrentState: function() {
		
			if (this.index == 0) {
				$(this.prevBtn).addClass('disabled');
			} else {
				$(this.prevBtn).removeClass('disabled');
			}
			if (this.index == this.slides.length - 1) {
				$(this.nextBtn).addClass('disabled');
			} else {
				$(this.nextBtn).removeClass('disabled');
			}
			
			$(this.options.jumpToSlideLinks).each(function() {
					$(this).removeClass('selected');
			});
			$(this.options.jumpToSlideLinks[this.index]).addClass('selected');

			if (this.options.showSlideCount) {
				this.slideCount.html((this.index + 1) + ' of ' + this.slides.length);
			}
			
		},
		
		preSlideChange: function() {
			if (this.options.callbacks.preSlideChange)
				this.options.callbacks.preSlideChange();
		},
		
		postSlideChange: function() {
			this.updateCurrentState();
			if (this.options.callbacks.postSlideChange)
				this.options.callbacks.postSlideChange();
			if (this.options.showHash) {
				window.location.hash = this.options.panelIdPrefix + (this.index + 1);
			}
		}
		
	});
	
})(jQuery);
