/**
 * --------------------------------------------------------------------
 * jQuery-Plugin "Image Cross-Fader"
 * Version: 1.1.1, 2009-06-04
 * by Christophe DELIENS (G2F - GET2FUTURE)
 * 
 * http://twitter.com/cdeliens
 * chris[at]g2f[dot]be
 *
 * Copyright (c) 2009 Christophe DELIENS
 * Licensed under the MIT Licence (http://www.opensource.org/licenses/mit-license.php)
 */
jQuery.noConflict();
jQuery(document).ready(function($) {
        $('#images').imageCrossFader({
                fadeIn: 400,
                fadeOut: 200,
                delay: 2500,
                index: true,
                pauseOnHover: true
        });
});
(function($) {
	// TODO: defining "global" variables prevents this plugin from being usable with multiple instances
	var timerId = null;
	var imageItems = Array();
	var nextIndex = 0;
	var mouseOnContainer = false;
	var settings = null;
	var $me = null;

	$.fn.imageCrossFader = function(options) {
		settings = $.extend($.fn.imageCrossFader.defaults, options);
		
		return this.each(function() {
			$me = $(this);
			
			// finding items
			var items = $me.find('ul li');
			var itemsCount = items.size();
			imageItems = Array(itemsCount);

			// we have our items, emptying the container
			$me.empty().show();
			
			// binding mouse events
			if (settings.pauseOnHover) {
				$me.mouseenter(function() {
					mouseOnContainer = true;
					deleteTimer();
				}).mouseleave(function() {
					mouseOnContainer = false;
					createTimer();
				});
			}
			
			if (settings.index) {
				// building items index container
				var itemsIndex = $('<div></div>').attr('class', 'index').appendTo($me);
			}
				
			for (i = 0; i < itemsCount; ++i) {
				// building imageCrossFader array
				var itemHtml = $(items[i]).html();
				imageItems[i] = itemHtml;
				
				if (settings.index) {
					// adding new index for current item
					$('<p></p>').text(i +1).attr('rel', i).click(function() {
						showItem(parseInt($(this).attr('rel')));
						return false;
					}).appendTo($(itemsIndex));
				}
			}
			
			// initiating imageCrossFader with first item
			showItem(0);
		});
	};

	$.fn.imageCrossFader.defaults = {
		fadeIn: 600,
		fadeOut: 200,
		delay: 3000,
		pauseOnHover: true,
		index: true
	};
	
	function showItem(index) {
		// clearing current running timer
		deleteTimer();
		
		if (settings.index) {
			// "selecting" current item index
			$me.find('div.index p').removeClass('selected');
			$me.find('div.index p[rel=' + index + ']').addClass('selected');
		}
		
		// crossfading in new item
		$me.find('div.item').fadeOut(settings.fadeOut);
		$('<div></div>').attr('class', 'item').append(imageItems[index]).appendTo($me).fadeIn(settings.fadeIn);
		
		// next index math
		nextIndex = index +1;
		if (nextIndex >= imageItems.length) {
			nextIndex = 0;
		}
		
		if (!mouseOnContainer) {
			createTimer();
		}
	}
	
	function createTimer() {
		// setting timer for next item display
		timerId = window.setTimeout(function() {
			showItem(nextIndex);
		}, settings.delay);
	}
	
	function deleteTimer() {
		if (timerId != null) {
			window.clearTimeout(timerId);
		}
	}	
})(jQuery);