/**
 * Carroucel, rotates sheets in a set
 * 
 * @package carroucel
 * @author Bert de Weerd <bert.de.weerd@e-dynamics.nl>
 */

(function($) {
    $.widget("ui.carroucel", {
		options: {
			delay:4*1000
			,animationSpeed:'slow'
			,activeItem:0 //first
		},
		_create: function() {
			var self = this;
			var o = self.options;
			var el = self.element;
			
			// set object data
			o.items=null;
			o.navItems=null;
			o.intervalID=null;
			
			/* check if the element is visible and has layout
			 * - if the element has no with when (display:none), skip this element, call the function again when showing the element.
			 */
			if(el.width()<1) return;
			// if the element is already enhanced, only update it.
			if(!el.hasClass('enhancedCarroucel')){ 
				
				// get all items
				o.items = $('.carroucelitem', el);
				$.each(o.items, function(index, item){
					var el = $(item);
					el.css({
						'visibility':'visible'
					});
					el.hide();
				});
				$(o.items[o.activeItem]).fadeIn(o.animationSpeed);
				
				// navigation
				$('.carroucelheader a').click(function(event){
					event.preventDefault();
					self._rotate(event.target.className);
				});
				o.navItems = $('.nav a', el);
				$.each(o.navItems, function(index, item){
					var el = $(item);
					el.removeClass('active');
					el.click(function(event){
						event.preventDefault();
						self._navigate($(event.target).prevAll().length);
					});
				});
				$(o.navItems[o.activeItem]).addClass('active');
				
				// timed auto rotator
				el.hover(function(event){
					// mouse-over
					window.clearInterval(o.intervalID);
				}, function(event){
					// mouse-out
					o.intervalID = window.setInterval(function(){
						self._rotate("next");
					}, o.delay);
				});
				
				el.addClass('enhancedCarroucel');
			}
			this.update();	
			
		},
		_rotate: function(direction) {
			var self = this;
			var o = self.options;
			var el = self.element;
			
			switch (direction) {
				case "next":
					// next
					if(parseInt(o.activeItem)+1<o.items.length) {
						self._navigate(parseInt(o.activeItem)+1);
					} else {
						self._navigate(0);
					}
					break;
				case "previous":
					// previous
					if(parseInt(o.activeItem)>0) {
						self._navigate(parseInt(o.activeItem)-1);
					} else {
						self._navigate(o.items.length-1);
					}
					break;
			}
		},
		_navigate: function(position) {
			var self = this;
			var o = self.options;
			var el = self.element;
			
			// reset/hide all
			$(o.items[o.activeItem]).fadeOut(o.animationSpeed);
			$(o.navItems[o.activeItem]).removeClass('active');
			// set activeitem
			o.activeItem = position;
			// shoe item
			$(o.items[o.activeItem]).fadeIn(o.animationSpeed);
			$(o.navItems[o.activeItem]).addClass('active');
		},
		update: function() {
			// update
		},
		destroy: function() {			
			this.element.removeClass('enhancedCarroucel');
			$.Widget.prototype.destroy.apply(this, arguments); // default destroy
		},
		_setOption: function(option, value) {
			$.Widget.prototype._setOption.apply( this, arguments );
			
			var el = this.element;
			switch (option) {
				case "some option":
					// do something
					break;
			}
		}
	});
})(jQuery);
