/*
 *	jQuery itemswitcher
 *	copyright 2009
 *
 *	@author 	Tam Tam - Boy van Amstel
 *	@version 	1.0
 *
 *	Changes 1.0:
 *	[15/12/2009]	- First release 
 *
 */
 
jQuery.itemswitcher = 
{
	changeObjects : function(object, options) {
		
		var items = jQuery(object).find(options.carousel); 			
		var amountItems = jQuery(object).find(options.carousel).size();
		var currentIndex = options.currentIndex;
				
		// Fade out and remove class
		jQuery(items).eq(currentIndex)
       		.fadeOut(options.fadeTime)
       		.removeClass(options.activeClass);
       		
       	// Remove class
		jQuery(object).find("." + options.controlClass + " a." + options.activeClass)
			.removeClass(options.activeClass);

		if (currentIndex >= amountItems - 1) {
			currentIndex = 0;
		} else {
			currentIndex = currentIndex + 1;
		}
       	
		// Fade in and add class           	
		jQuery(items).eq(currentIndex)
			.fadeIn(options.fadeTime)
			.addClass(options.activeClass);
		
		// Set current items
		jQuery(object).find("." + options.currentClass).html(currentIndex + 1);

		options.currentIndex = currentIndex;
		
		// Add class
		jQuery(object).find("." + options.controlClass + " a").eq(currentIndex)
			.addClass(options.activeClass);
			
			
			if(typeof options.onSwitch == 'function'){
 				options.onSwitch.call(this, object, options);
			}

	
	},
	build : function(options) {

		try {
			// Default settings
			var defaults = {
				carousel: ".switcher-item",
				activeClass: "active",
				controlClass: "switcher-controls",
				currentClass: "switcher-current",
				totalClass: "switcher-total",
				timeout: 5000,
				fadeTime: 800,
				currentIndex: 0,
				onSwitch: null
			}
	
			// Move to options
			var options = jQuery.extend(defaults, options);
			
			var object = this;
			
			jQuery(object).find(options.carousel).hide();
			jQuery(object).find(options.carousel + "." + options.activeClass).show();
			
			// Set amount of items			
			var amountItems = jQuery(object).find(options.carousel).size();
			jQuery(object).find("." + options.totalClass).html(amountItems);
			jQuery(object).find("." + options.currentClass).html("1");
			
	        // Setup the timer
	        jQuery(object).everyTime(options.timeout, 2, function() {
				jQuery.itemswitcher.changeObjects(object, options);	
	        });
	        
	        // When hovering controls
			jQuery(object).find("." + options.controlClass).hover(function() {
				// Stop timer
				jQuery(object).stopTime(2);
			}, function() {
				// Start timer
				jQuery(object).everyTime(options.timeout, 2, function() {
					jQuery.itemswitcher.changeObjects(object, options);
				});
			});
	        
	        // When clicking the controls
			jQuery(object).find("." + options.controlClass + " a").click(function() {
				// Stop timer
				jQuery(object).stopTime(2);
	         
	         	// Hide everything and remove active class
				jQuery(object).find(options.carousel + ":visible").fadeOut(options.fadeTime);
				jQuery(object).find(options.carousel).removeClass(options.activeClass);
				jQuery(object).find("." + options.controlClass + " a").removeClass(options.activeClass);
				
				// Add active class to this link
				jQuery(this).addClass(options.activeClass);
				var selectedIndex = jQuery(object).find("." + options.controlClass + " a").index(jQuery(this));

				jQuery(object).find("." + options.currentClass).html(selectedIndex + 1);
				

				// Fade in and add class
				jQuery(object).find(options.carousel).eq(selectedIndex)
					.fadeIn(options.fadeTime)
					.addClass(options.activeClass);
				
				if(typeof options.onSwitch == 'function'){
 					options.onSwitch.call(this, object, options);
				}
				
				// Start timer
				/*
				jQuery(object).everyTime(options.timeout, 2, function() {
					jQuery.itemswitcher.changeObjects(object, options);
				});
				*/
				
				return false;
				
			});
			
			return this;
		}
		catch(err) {
			return false;
		}
	}
};

jQuery.fn.extend({
		itemswitcher: jQuery.itemswitcher.build
});	