/*
 * CrossFader - jQuery plugin for creating CrossFading-Images
 *
 * Author: Simon-Albert Karlen
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 * Version: 1.0
 * Date: 01.06.2010
 *
 *
 */

(function(jQuery) {
	jQuery.fn.crossFader = function(options) {
		return jQuery(this).each(function(index) {
			new jQuery.crossFader(this, options);
		});
	}
	
	jQuery.crossFader = function(el, options) {
		this.settings = {
			delay:				4000,
			height:				'',
			width:				'100%',
			height:				'auto',
			mainItemSelector:	'ul',
			subItemSelector:	'li img',
			elementSelector:	'auto',
			stopOnMouseOver:	false,
			start:				'first',
			overlay:			'',
			href:				''
		}
		jQuery.extend(this.settings, options || {});
		this.mainItem	= jQuery(el);
		var subItems	= this.mainItem.find(this.settings.mainItemSelector+' '+this.settings.subItemSelector);
		this.cnt		= 0;
		
		if (this.settings.height == 'auto') {
			this.detectHeight();
		}
		
		if (this.settings.elementSelector == 'auto') {
			this.detectElementSelector();
		}
		
		this.el = jQuery('<div></div>').css({
			position: 'relative',
			width: this.settings.width,
			height: this.settings.height
		});
		
		if (this.settings.overlay != '') {
			this.el.prepend(
				jQuery('<div></div>').css({
					position: 'absolute',
					width: this.settings.width,
					height: this.settings.height,
					backgroundImage: 'url('+this.settings.overlay+')',
					backgroundPosition: 'center center',
					backgroundRepeat: 'no-repeat',
					zIndex: 1
				})
			);
			if (this.settings.href != '') {
				this.el.children('div').first().append(
					jQuery('<a></a>').attr('href', this.settings.href).html('&nbsp;').css({
						display: 'block',
						height: '100%'
					})
				);
			}
			subItems.css({
				zIndex: 0
			});
		}
		
		this.el.append(subItems);
		
		this.add(subItems);
		
		this.mainItem.children(this.settings.mainItemSelector).remove();
		this.mainItem.append(this.el);
		if (this.settings.stopOnMouseOver) {
			var cls = this;
			this.mainItem.bind({
				mouseenter: function() {
					cls.stop();
				},
				mouseleave: function() {
					cls.start();
				}
			});
		}
		
		if (subItems.length > 1) this.start();
	}
	
	jQuery.crossFader.fn = jQuery.crossFader.prototype;
	jQuery.crossFader.fn.extend = jQuery.crossFader.extend = jQuery.extend;
	
	jQuery.crossFader.fn.extend({
		add: function(items) {
			var cls = this;
			var index = (cls.settings.start == 'random') ? Math.floor(Math.random()*items.length) : 0;
			jQuery(items).each(function(i) {
				var display = (i == index && cls.cnt == 0) ? 'block' : 'none';
				var appCls = (i == index && cls.cnt == 0) ? 'xf-active' : '';
				var top = (jQuery(this).height() == 0) ? 0 : (cls.settings.height-jQuery(this).height())/2;
				cls.el.append(
					jQuery(this).css({
						position: 'absolute',
						top: top,
						left: 0,
						width: cls.settings.width,
						display: display
					}).addClass(appCls)
				);
			});
			this.el.find('img').css({
				height: this.settings.height,
				width: this.settings.width,
				border: 'none'
			});
			this.cnt += jQuery(items).length;
		},
		remove: function(items) {
			if (jQuery.isArray(items)) {
				var cls = this;
				jQuery.each(items, function(i, v) {
					cls.el.children('img:nth-child('+v+')').remove();
				});
			} else {
				this.el.children('img:nth-child('+v+')').remove();
			}
		},
		detectHeight: function() {
			var cls = this, newHeight = 0, maxHeight;
			var items = jQuery(this.settings.mainItemSelector+' img');
			jQuery(items).each(function(i) {
				if (jQuery(this).height() > newHeight) {
					newHeight = jQuery(this).height();
					maxHeight = jQuery(this).height()*parseInt(cls.mainItem.css('width'))/jQuery(this).width();
				}
			});
			if (newHeight > maxHeight) newHeight = maxHeight;
			this.settings.height = newHeight;
		},
		detectElementSelector: function() {
			var l = ' ';
			if (typeof(jQuery.fn.strPos) != 'function') {
				jQuery.strPos = function(haystack, needle, offset) {
					var i = (haystack+'').indexOf(needle, (offset || 0));
					return i === -1 ? false : i;					
				}
			}
			if (jQuery.strPos(this.settings.subItemSelector, l) === false) {
				l = '>';
			}
			var s = this.settings.subItemSelector.split(l);
			this.settings.elementSelector = s[s.length-1];
		},
		start: function() {
			var cls = this;
			this.el.everyTime(this.settings.delay, 'fade', function() {
				cls.next();
			});
		},
		next: function(ind) {
			var img1 = this.el.children(this.settings.elementSelector+'.xf-active');
			var img2 = (!isNaN(ind)) ? this.el.children(this.settings.elementSelector+':nth-child('+ind+')') : img1.next();
			var delay = this.settings.delay/5;
			if (delay > 500) delay = 500;
			
			if (!img2.length) {
				img2 = this.el.children(this.settings.elementSelector).first();
			}
			
			img1.fadeOut(delay).removeClass('xf-active');
			img2.fadeIn(delay).addClass('xf-active');
			this.mainItem.trigger('change');
		},
		stop: function() {
			this.el.stopTime('fade');
		}
	});
})(jQuery);
