var Carousel = function() {
	var target, timeout, interval = 10,
		realInterval = interval * 1000,
		index = -1,
		links = [];
	return {
		init: function(config) {
			links = $$(config.source || 'no-links-supplied');
			target = $(config.target || 'no-target-supplied');
			Jelo.on(links, 'click', function(e) {
				Carousel.show(e.target);
			})
			Carousel.next();
		},
		prepare: function() {
			if (timeout) {
				clearTimeout(timeout);
			}
			timeout = setTimeout(Carousel.next, realInterval);
		},
		next: function(link) {	
			if (!link) {
				if (++index >= links.length) {
					index = 0;
				}
				link = links[index];
			}
			Carousel.show(link);
		},
		show: function(link) {
			if (link) {
				var type = link.getAttribute('alt');
				index = links.indexOf(link);
				Jelo.Ajax.request({
					url: '/_geeklets/' + type + '.txt',
					success: function(ajax) {
						Jelo.css(target, 'position', 'relative'); // should probably just move this to the stylesheet
						Jelo.Anim.ate({
							me: target,
							css: 'top',
							easing: 'out',
							from: '0px',
							to: '-20px',
							after: function() {
								Jelo.Anim.ate({
									me: target,
									css: 'top',
									easing: 'out',
									from: '20px',
									to: '0px'
								});
							}
						});
						Jelo.Anim.ate({
							me: target,
							css: 'opacity',
							easing: 'out',
							from: '1',
							to: '0',
							after: function() {
								target.innerHTML = ajax.responseText;
								Jelo.Anim.ate({
									me: target,
									css: 'opacity',
									easing: 'out',
									to: '1'
								});
							}
						})
					}
				});
			}
			Carousel.prepare();
		}
	};
}();

Jelo.onReady(function() {
	
	Jelo.Event.normalize();
		
	Carousel.init({
		interval: 10,                  // seconds
		source: '.services_thumbnail', // one or more links
		target: '#services_content'    // one target element
	});
	
});
