/** (C)2007 Profitmaster Systems Ltd. 
	Using jQuery library to create a rotating (slideshow) image panel
	Must be supplied a css selector to target the container (list) object; items in the list will be faded in one after another.
	Note they can be images or links containing images (or pretty much any block-level element but the images
	will work best...)
	Example:
		$("#imageRotatePane1").imageRotate(10,"fast"); // Apply rotation to pane 1 with 10sec delay between slides 
*/

imageRotate = {
	start : function(targetList,delay,speed) {
	 	// Converted s into ms
		delay = delay * 1000;
		// Get all child items of the list
		var targetItems = $(targetList).children();
		// Hide all except first item (and store the first item as targetItem)
		var targetItem = targetItems.slice(0,1).show();
		// Set a JS timeout for next rotation
		setTimeout(function(){
			imageRotate.next(targetList,targetItem,delay,speed)
		},delay);
	},
	
	next : function(targetList,targetItem,delay,speed) {
		var nextTarget = targetItem.next();
		if (!nextTarget.is("li")) {
			// Get first child of the list by slicing first element of collection
			nextTarget = $(targetList).children().slice(0,1);
			// Move it to the end of the list
			$(targetList).append(nextTarget);
		}			
		nextTarget.fadeIn(speed,function(){
			// previous item will be hidden on fade completion
			targetItem.hide();
		});
		setTimeout(function(){imageRotate.next(targetList,nextTarget,delay,speed)},delay);	
	}
}

/*
jQuery.fn.imageRotate = function(delay,speed) {
	 	// Converted s into ms
		delay = delay * 1000;
		// Get all child items of the list
		var targetItems = $(this).children();
		// Hide all except first item (and store the first item as targetItem)
		var targetItem = targetItems.hide().slice(0,1).show();
		// Set a JS timeout for next rotation
		setTimeout(function(){
			$(this).imageRotateNext(targetItem,delay,speed);
		},delay);
	};
	
jQuery.fn.imageRotateNext = function(targetItem,delay,speed) {
		nextTarget = targetItem.next();
		if (!nextTarget.is("li")) {
			// Get first child of the list by slicing first element of collection
			nextTarget = $(this).children(0);
			// Move it to the end of the list
			nextTarget.remove().appendTo($(this));
		}
		nextTarget.fadeIn(speed,function(){
			// Hide the last item
			targetItem.hide();
		});
		setTimeout(function(){$(this).imageRotateNext(nextTarget,delay,speed)},delay);	
};
*/