(function($) {
	var defaults = {},
	ZoomSlideshow = function(el,opts) {
		var o = this;
		o.o = opts;
		o.el = $(el);
		o.imgs = o.el.find(o.o.els).hide();
		o.length = o.imgs.length;
		o.current = 0;
		o.timer = false;
		o.delay = 3000;
		o.duration = 2000;
		o.div = 1.5;
		o.height = 0;
		o.width = 0;
		o.inc = typeof o.o.inc!='undefined'?o.o.inc:200;
		o.directions = typeof o.o.direction!='undefined'?[o.o.direction]:['tl','tr','bl','br'];
		o.dir = 0;
		o.loader = typeof o.o.loader!='undefined'?$(o.o.loader):false;
		if(o.loader) {
			o.load();
		} else {
			o.init();
		}
	};
	ZoomSlideshow.prototype.load = function() {
		var o = this,
		cpt = 0,
		check = function() {
			if(cpt===o.length) {
				o.loader.hide();
				o.el.show();
				o.init();
			}
		};
		o.el.hide();
		o.loader.show();
		o.imgs.each(function() {
			if(this.complete) {
				cpt++;
				check();
			} else {
				$(this).load(function() {
					cpt++;
					check();
				});
			}
		});
	};
	ZoomSlideshow.prototype.init = function() {
		var o = this;
		o.el.css({position:'relative'});
		o.w = o.imgs.eq(0).width();
		o.h = o.imgs.eq(0).height();
		o.imgs.css({position:'absolute',top:0,left:0,zIndex:0}).eq(0).css({zIndex:10}).show();
		o.timer = setTimeout(function() { o.next(); },o.delay);
	};
	ZoomSlideshow.prototype.next = function() {
		var o = this,
		next = o.current+1,
		w = o.inc,
		h = o.inc*o.h/o.w,
		dir = o.directions[o.dir],
		l,t,
		w3, h3, l3, t3;
		o.dir++;
		if(o.dir>=o.directions.length) { o.dir = 0; }
		switch(dir) {
			case 'tl' : l=0; t=0; o.imgs.eq(o.current).css({left:0,top:0,right:'auto',bottom:'auto'}); break;
			case 'tr' : l=-o.inc; t=0; o.imgs.eq(o.current).css({left:'auto',top:0,right:0,bottom:'auto'}); break;
			case 'bl' : l=0; t=-(o.inc*o.h/o.w); o.imgs.eq(o.current).css({left:0,top:'auto',right:'auto',bottom:0}); break;
			case 'br' : l=-o.inc; t=-(o.inc*o.h/o.w); o.imgs.eq(o.current).css({left:'auto',top:'auto',right:0,bottom:0}); break;
		}
		w3 = (w<0?'-':'+')+'='+Math.abs(w)/o.div+'px';
		h3 = (h<0?'-':'+')+'='+Math.abs(h)/o.div+'px';
		l3 = (l<0?'-':'+')+'='+Math.abs(l)/o.div+'px';
		t3 = (t<0?'-':'+')+'='+Math.abs(t)/o.div+'px';
		w = (w<0?'-':'+')+'='+(o.div-1)*Math.abs(w)/o.div+'px';
		h = (h<0?'-':'+')+'='+(o.div-1)*Math.abs(h)/o.div+'px';
		l = (l<0?'-':'+')+'='+(o.div-1)*Math.abs(l)/o.div+'px';
		t = (t<0?'-':'+')+'='+(o.div-1)*Math.abs(t)/o.div+'px';
		if(next>=o.length) { next = 0; }
		o.imgs.eq(next).css({zIndex:5,opacity:1,width:o.w,height:o.h,top:0,left:0}).show();
		o.imgs.eq(o.current).animate({width:w3,height:h3,left:l3,top:t3},o.duration/o.div,'linear',function() {
			o.imgs.eq(o.current).animate({width:w,height:h,left:l,top:t,opacity:0},(o.div-1)*o.duration/o.div,'linear',function() {
				o.current = next;
				o.imgs.eq(o.current).css({zIndex:10});
				clearTimeout(o.timer);
				o.timer = setTimeout(function() { o.next(); },o.delay);
			});
		});
	};
	$.fn.zoomSlideshow = function(opts) {
		return this.each(function() {
			new ZoomSlideshow(this,opts);
		});
	};
}(jQuery));
