(function($) {
	var defaults = {
		auto: 2500,
		duration: 500
	},
	O = function(el,opts) {
		var o = this,
		checkLoaded = function(im) {
			if(!im.complete) {
				setTimeout(function() { checkLoaded(im); },50);
			} else {
				o.loaded++; o.check();
			}
		};
		opts = opts||{};
		o.loaded = 0;
		o.timer = false;
		o.opts = {}; $.extend(o.opts,defaults,opts);
		o.el = $(el);
		o.images = o.el.find(o.opts.imageList);
		o.texts = o.el.find(o.opts.text);		
		o.list = o.el.find(o.opts.list);
		o.loader = o.el.find(o.opts.loader);
		o.current = 0;
		o.list.removeClass('current');
		o.images.hide();
		o.texts.hide();
		o.list.hide();
		o.loader.show();
		o.images.each(function() {
			var im = new Image();
			im.src = this.src;
			checkLoaded(im);
			/*if(this.complete) { o.loaded++; o.check(); }
			else { $(this).load(function() { alert('loaded'); o.loaded++; alert('loaded++'); o.check(); }); }*/
		});

	};
	O.prototype.check = function() {
		var o = this;
		if(o.loaded==o.images.length) {
			o.list.show();
			o.loader.hide();
			o.list.eq(0).toggleClass('current');
			o.images.eq(0).stop(true,true).show();
			o.texts.eq(0).stop(true,true).show();
			o.list.each(function(i) {
				$(this).hover(function() {
					o.stopTimer();
					o.list.eq(o.current).removeClass('current');
					o.images.eq(o.current).stop(true,true).hide();
					o.texts.eq(o.current).stop(true,true).hide();
					o.list.eq(i).addClass('current');
					o.images.eq(i).stop(true,true).css({left:0,zIndex:0}).show();
					o.texts.eq(i).stop(true,true).css({left:0,zIndex:0}).show();
					o.current = i;
				},function() {
					o.resetTimer();
				});
			});
			o.resetTimer();
		}
	};
	O.prototype.stopTimer = function() {
		clearTimeout(this.timer);
	};
	O.prototype.resetTimer = function() {
		var o = this;
		clearTimeout(o.timer);
		o.opts.auto&&(o.timer = setTimeout(function() { o.auto(); },o.opts.auto));
	};
	O.prototype.auto = function() {
		var o = this;
		o.list.eq(o.current).removeClass('current');
		/*
		o.images.eq(o.current).stop().fadeOut(o.opts.duration);
		o.texts.eq(o.current).stop().fadeOut(o.opts.duration);
		*/
		o.images.eq(o.current).stop().css({zIndex:10}).animate({left:-720},o.opts.duration,function() {
			
		});
		o.texts.eq(o.current).stop().css({zIndex:10}).animate({left:-720},o.opts.duration);
		++o.current>=o.list.length&&(o.current=0);
		o.list.eq(o.current).addClass('current');
		o.images.eq(o.current).stop().css({left:0,zIndex:0}).show()//fadeIn(o.opts.duration);
		o.texts.eq(o.current).stop().css({left:0,zIndex:0}).show()//fadeIn(o.opts.duration);
		o.resetTimer();
	};
	$.fn.idSlideshow = function(opts) {
		return this.each(function() {
			new O(this,opts);
		});
	};

}(jQuery));
