(function(f)
	{
	var c=navigator.platform==="iPhone";
	var b=navigator.platform==="iPod";
	var g=navigator.platform==="iPad";
	var e=c||b||g;
	f.fn.dmxSlideshow=function(i)
		{
		var h=Array.prototype.slice.call(arguments,1);
		this.each(function()
			{
			var j;
			if(f(this).data("slideshow"))
				{
				j=f(this).data("slideshow")
				}
			else
				{
				j=new d(this,i);
				f(this).data("slideshow",j)
				}
			if(typeof i==="string"&&j[i])
			{
			j[i].apply(j,h)
			}
		}
		);
	return this
	};

function d(i,h)
	{
	this.$target=f(i);
	this.opts=f.extend({},d.options,h||{});
	this.init()
	}

d.options={slide:{duration:5000},transition:{duration:1000},kenburns:{enabled:true,start:{zoom:"1~1",pan:"random"},end:{zoom:"1~1",pan:"random"}}};


d.prototype={init:function()
	{
	this.$slides=this.$target.children(".slide");
	var h=this;
	this.curr=0;
	this.lastIndex=this.$slides.length-1;
	this.images=[];
	this.descriptions=[];
	this.isPause=false;
	this.timeoutId=0;
	this.width=this.$target.width();
	this.height=this.$target.height();
	this.$slides.find("img").each(function()
		{
		h.descriptions.push(this.alt||"")
		}
		);
		if(this.opts.kenburns.enabled)
			{
			this.$slides.dmxKenburns(this.opts.kenburns)
			}
			this.$slides.hide().eq(this.curr).show();
			if(this.opts.kenburns.enabled)
				{
				this.$slides.eq(this.curr).dmxKenburns("animate",this.opts.slide.duration+(2*this.opts.transition.duration))
				}
				this.timeoutId=setTimeout(function(){h.next.call(h)},this.opts.transition.duration+this.opts.slide.duration)},play:function(){this.isPause=false;this.next()},pause:function(){this.isPause=true;
				clearTimeout(this.timeoutId)},first:function(){this.go(0)},prev:function(){var h=this.curr-1;if(h<0){h=this.lastIndex}this.go(h)},next:function(){var h=this.curr+1;
					if(h>this.lastIndex)
						{
						h=0
						}
						this.go(h)},last:function(){this.go(this.lastIndex)},go:function(i){clearTimeout(this.timeoutId);
						var j=this.curr;
						this.curr=Math.min(Math.max(0,i),this.lastIndex);
						this.$slides.eq(j).fadeOut(this.opts.transition.duration);
						this.$slides.eq(this.curr).fadeIn(this.opts.transition.duration);
						if(this.opts.kenburns.enabled)
							{
							if(this.isPause){this.$slides.eq(this.curr).dmxKenburns("animate",0)
							}
							else
							{
							this.$slides.eq(this.curr).dmxKenburns("animate",this.opts.slide.duration+(2*this.opts.transition.duration))
							}
							}
						if(!this.isPause)
							{
							var h=this;this.timeoutId=setTimeout(function(){h.next.call(h)},this.opts.transition.duration+this.opts.slide.duration)
							}
							}
			};
			f.fn.dmxKenburns=function(i)
				{
				var h=Array.prototype.slice.call(arguments,1);
				this.each(function()
					{
					var j;
					if(f(this).data("kenburns"))
						{
						j=f(this).data("kenburns")
						}
					else
						{
						j=new a(this,i);f(this).data("kenburns",j)
						}
					if(typeof i==="string"&&j[i])
						{
						j[i].apply(j,h)
						}
					});
					return this
				};
				
				function a(i,h)
					{
					this.$target=f(i);
					this.opts=f.extend({},d.options,h||{});
					this.init()
					}
					a.options={start:{zoom:"1~1.5",pan:"random"},end:{zoom:"1~1.5",pan:"random"}};
					a.positions=["top-left","top-right","bottom-left","bottom-right"];
					a.prototype={init:function(){this.width=this.$target.width();
					this.height=this.$target.height();
					if(e)
						{
						this.useCanvas=false;
						this.useTransition=true
						}
					else
						{
						this.checkCanvas();
						this.useTransition=false
						}
					if(this.useCanvas)
						{
						this.setupCanvas()
						}
					else
						{
						this.$image=this.$target.find("img")}},checkCanvas:function(){this.useCanvas=!!document.createElement("canvas").getContext},setupCanvas:function(){var i=this.$target.find("img");
						this.image=new Image();
						this.image.loaded=false;
						this.image.onload=function(){this.loaded=true};
						this.image.src=i.attr("src");
						var h=document.createElement("canvas");
						h.width=this.width;
						h.height=this.height;this.ctx=h.getContext("2d");
						i.replaceWith(h);
						this.intervalId=0},animate:function(h,k){var t=f.extend({},this.opts,k||{}),p=this.getZoom(t.start),w=this.getZoom(t.end),v=this.getPan(t.start),B=this.getPan(t.end),m={width:p*this.width,height:p*this.height},l={width:w*this.width,height:w*this.height};
						m.x=v.indexOf("left")>-1?0:v.indexOf("right")>-1?this.width-m.width:(this.width-m.width)/2;m.y=v.indexOf("top")>-1?0:v.indexOf("bottom")>-1?this.height-m.height:(this.height-m.height)/2;l.x=B.indexOf("left")>-1?0:B.indexOf("right")>-1?this.width-l.width:(this.width-l.width)/2;l.y=B.indexOf("top")>-1?0:B.indexOf("bottom")>-1?this.height-l.height:(this.height-l.height)/2;
					if(this.useCanvas)
						{
						var q=this,n=(new Date()).getTime();
						clearInterval(this.intervalId);
						this.intervalId=setInterval(function(){var F=(new Date()).getTime()-n,E=(F>h)?1:F/h;
						if(q.image.loaded)
							{
							var o=m.x+((l.x-m.x)*E),D=m.y+((l.y-m.y)*E),s=m.width+((l.width-m.width)*E),C=m.height+((l.height-m.height)*E);
							q.ctx.clearRect(0,0,q.width,q.height);
							q.ctx.drawImage(q.image,o,D,s,C)
							}
						if(F>h)
							{
							clearInterval(q.intervalId)}},1000/60)
							}
						else
							{
							if(this.useTransition)
								{
								var r=Math.round((this.width-(this.width*p))/2),A=Math.round((this.height-(this.height*p))/2),y=(v.indexOf("left")>-1)?-r:(v.indexOf("right")>-1)?r:0,u=(v.indexOf("top")>-1)?-A:(v.indexOf("bottom")>-1)?A:0;
								var x=Math.round((this.width-(this.width*w))/2),j=Math.round((this.height-(this.height*w))/2),i=(B.indexOf("left")>-1)?-x:(B.indexOf("right")>-1)?x:0,z=(B.indexOf("top")>-1)?-j:(B.indexOf("bottom")>-1)?j:0;
								this.$image.css({width:this.width,height:this.height,"-webkit-transition-property":"-webkit-transform","-webkit-transition-duration":"0s","-webkit-transition-timing-function":"linear"});
								this.$image.css("-webkit-transform","translate3d("+y+"px,"+u+"px,0px) scale("+p+")");
								var q=this;
								setTimeout(function(){q.$image.css("-webkit-transition-duration",h+"ms");
								q.$image.css("-webkit-transform","translate3d("+i+"px,"+z+"px,0px) scale("+w+")")},0)
								}
							else
								{
								this.$image.css(m).animate(l,h,"linear")}}},getZoom:function(l){var k=l.zoom;
								if(typeof k==="string")
									{
									if(k.indexOf("~")>-1)
										{
										var j=k.split("~"),i=parseFloat(j[0]),h=parseFloat(j[1]);
										k=i+(Math.random()*(h-i))
										}
									else
										{
										k=parseFloat(k)
										}
									}
									return k},getPan:function(i){var h=i.pan;
								if(h==="random")
									{
									h=a.positions[Math.floor(Math.random()*a.positions.length)]
									}
									return h
									}
								}
	}
)
(jQuery);
