$(document).ready(function () {
	
	Prolific.app('wineCarousel', function (wine) {
	
		var $base = $('#wine_carousel'),
			$frame = $('#wine_collection'),
			$items = $frame.find('li'),
			$firstItem = $items.eq(0),
			$slides,
			bWidth = parseInt($firstItem.css('width')) + parseInt($firstItem.css('margin-left')) + parseInt($firstItem.css('margin-right')),
			bottlesPerSlide = Math.floor(parseInt($base.width()) / bWidth),
			pixelJump = bWidth * bottlesPerSlide,
			nmrSlides = Math.ceil($items.length / bottlesPerSlide),
			_items = [];
			
		wine.feature('controls', function (controls) {
		
			var $left = $base.parent().find('a.go_left'),
				$right = $base.parent().find('a.go_right'),
				curSlide = 0;
			
			function disableLeft () {
				$left.addClass('disabled');
				return false;
			}
			
			function enableLeft () {
				$left.removeClass('disabled');
				return true;
			}
			
			function checkLeft () {
				return curSlide > 0? enableLeft(): disableLeft();
			}
			
			function disableRight () {
				$right.addClass('disabled');
				return false;
			}
			
			function enableRight () {
				$right.removeClass('disabled');
				return true;
			}
			
			function checkRight () {
				return curSlide < nmrSlides - 1? enableRight(): disableRight();
			}
			
			function check () {
				return checkLeft() === true && checkRight() === true;
			}
			
			function update (inc) {
				curSlide += inc;
				check();
				$frame.animate({
					left: -curSlide * pixelJump + 'px'
				}, 500);
				wine.dots.select(curSlide);
			}
			
			function left () {
				if (!$left.hasClass('disabled')) {
					update(-1);
				}
			}
			
			function right () {
				if (!$right.hasClass('disabled')) {
					update(1);
				}
			}
			
			//Bind things
			$left.click(left);
			$right.click(right);
			check();
			
			return {
				left: left,
				right: right
			};
			
		});
		
		wine.feature('dots', function (dots) {
			var $dotContainer = $('#dots'),
				$dots,
				i;
			for (i = 0; i < nmrSlides; i++) {
				$dotContainer.append('<li class="dot">.</li>');
			}
			$dots = $dotContainer.children('li');
			
			dots.method('select', function (which) {
				$dots.filter('.selected').removeClass('selected');
				$dots.eq(which).addClass('selected');
			});
			
			dots.select(0);
		});
		
		wine.feature('popup', function (popup) {
			var $popup = $('<div id="wine_popup"></div>').appendTo('body');
			
			popup.method('show', function (css) {
				$popup.stop().css(Prolific.augment(css, {
					opacity: 0
				})).animate({
					opacity: 1
				}, 500);
				return this;
			});
			
			popup.method('hide', function () {
				$popup.stop().animate({
					opacity: 0
				}, 200, function () {
					$(this).css({
						left: '-9999px'
					})
				});
				return this;
			});
			
			popup.method('content', function (c) {
				$popup.empty().append(c);
				return this;
			});
			
			
			//Bindings
			$items.mouseenter(function () {
				var $this = $(this),
					$clone = $this.clone(true).find('*').not('img');
				popup.content($clone).show({
					left: $this.offset().left + 75 + 'px',
					top: $this.offset().top - 15 + 'px'
				});
				$this.siblings().stop().animate({
					opacity: .5
				}, 500);
			}).mouseleave(function () {
				popup.hide();
				$(this).siblings().stop().animate({
					opacity: 1
				}, 500);
			});
			
		});
		
		wine.feature('autoscroll', function (autoscroll) {
		});
	});
	
});
