$(document).ready(function () {
	
	Prolific.app('travelWidget', function (widget) {
	
		var $base = $('#travel_widget'),
			$frame = $('ul.slides', $base),
			$slides = $('li', $frame),
			nmrSlides = $slides.length,
			curSlide = 0,			
			pauseTime = 8000,
			autoTime = 8000,
			autopage = true;
			
		function getCurSlide () {
			return $slides.eq(curSlide);
		}
			
		widget.feature('controls', function (controls) {
		
			var $left = $base.find('a.go_left'),
				$right = $base.find('a.go_right');
			
			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 hideCurSlide () {
				getCurSlide().animate({
					opacity: 0
				}, 300);
				return this;
			}
			
			function showCurSlide () {
				getCurSlide().css({
					opacity: 0,
					display: 'block'
				}).animate({
					opacity: 1
				}, 300);
				return this;
			}
			
			function update (inc) {
				hideCurSlide();
				curSlide += inc;
				check();
				widget.dots.select(curSlide);
				showCurSlide();
			}
			
			function left () {
				if (!$left.hasClass('disabled')) {
					update(-1);
					return true;
				} else {
					return false;
				}
			}
			
			function right () {
				if (!$right.hasClass('disabled')) {
					update(1);
					return true;
				} else {
					return false;
				}
			}
			
			//Bind things
			$left.click(function () {
				controls.marioLeft();
				widget.autoscroll.pause();
			});
			$right.click(function () {
				controls.marioRight();
				widget.autoscroll.pause();
			});
			check();
			
			
			controls.augment({
				left: left,
				right: right,
				marioRight: function () {
					if (!right()) {
						update(1 - nmrSlides);
						enableRight();
						disableLeft();
					}
				},
				marioLeft: function () {
					if (!left()) {
						update(nmrSlides - 1);
						disableRight();
						enableLeft();
					}
				}
			});
			
		});
		
		widget.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);
		});
		
				
		widget.feature('autoscroll', function (autoscroll) {
			var pauseTimeout = null,
				autoInterval = window.setInterval(auto, autoTime);
							
			function auto () {
				if (autopage) {
					widget.controls.marioRight();
				}
			}
					
			function pause (indefinite) {
				autopage = false;
				window.clearTimeout(pauseTimeout);
				if (!indefinite) {
					pauseTimeout = window.setTimeout(function () {
						resume();
					}, pauseTime);
				}
				return this;
			}
			
			function resume () {
				autopage = true;
				return this;
			}

			autoscroll.augment({
				pause: pause,
				resume: resume
			});
			
		});
	});
	
});
