/*
 * CrossSlide jQuery plugin v0.6.3
 *
 * Copyright 2007-2010 by Tobia Conforto <tobia.conforto@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
(function(){function f(a){var b=1;var c=a.replace(/^\s*|\s*$/g,"").split(/\s+/);if(c.length>3)throw new Error;if(c[0]=="center")if(c.length==1)c=["center","center"];else if(c.length==2&&c[1].match(/^[\d.]+x$/i))c=["center","center",c[1]];if(c.length==3)b=parseFloat(c[2].match(/^([\d.]+)x$/i)[1]);var d=c[0]+" "+c[1];if(d=="left top"||d=="top left")return{xrel:0,yrel:0,zoom:b};if(d=="left center"||d=="center left")return{xrel:0,yrel:.5,zoom:b};if(d=="left bottom"||d=="bottom left")return{xrel:0,yrel:1,zoom:b};if(d=="center top"||d=="top center")return{xrel:.5,yrel:0,zoom:b};if(d=="center center")return{xrel:.5,yrel:.5,zoom:b};if(d=="center bottom"||d=="bottom center")return{xrel:.5,yrel:1,zoom:b};if(d=="right top"||d=="top right")return{xrel:1,yrel:0,zoom:b};if(d=="right center"||d=="center right")return{xrel:1,yrel:.5,zoom:b};if(d=="right bottom"||d=="bottom right")return{xrel:1,yrel:1,zoom:b};return{xrel:parseInt(c[0].match(/^(\d+)%$/)[1])/100,yrel:parseInt(c[1].match(/^(\d+)%$/)[1])/100,zoom:b}}function e(){arguments[0]="CrossSlide: "+arguments[0];throw new Error(d.apply(null,arguments))}function d(a){for(var b=1;b<arguments.length;b++)a=a.replace(new RegExp("\\{"+(b-1)+"}","g"),arguments[b]);return a}var a=jQuery,b=a.fn.startAnimation?"startAnimation":"animate",c="pause plugin missing.";a.fn.crossSlide=function(c,g,h){var i=this,j=this.width(),k=this.height();if(c.width)j=c.width;if(c.height)k=c.height;if(i.length!=1)e("crossSlide() must be called on exactly 1 element");i.get(0).crossSlideArgs=[c,g,h];g=a.map(g,function(b){return a.extend({},b)});if(!c.easing)c.easing=c.variant?"swing":"linear";if(!h)h=function(){};(function(a){function c(d,e){e.onload=function(c){b++;g[d].width=e.width;g[d].height=e.height;if(b==g.length)a()};e.src=g[d].src;if(d+1<g.length)c(d+1,new Image)}var b=0;c(0,new Image)})(function(){function y(d,e){if(d%2==0){if(m){var f=d/2,i=(f-1+g.length)%g.length,j=w.eq(f),k=w.eq(i);var n=function(){h(f,j.get(0));k.css("visibility","hidden");setTimeout(e,m)}}else{var o=d/2,i=(o-1+g.length)%g.length,p=w.eq(o),k=w.eq(i),q=g[o].time_ms,r=v(g[o],c.variant?3:2);var n=function(){h(o,p.get(0));k.css("visibility","hidden");p[b](r,q,c.easing,e)}}}else{var s=Math.floor(d/2),t=Math.ceil(d/2)%g.length,u=w.eq(s),z=w.eq(t),A={},B={visibility:"visible"},C={};if(t>s){B.opacity=0;C.opacity=1;if(c.doubleFade)A.opacity=0}else{A.opacity=0;if(c.doubleFade){B.opacity=0;C.opacity=1}}if(!m){a.extend(B,v(g[t],0));if(!c.variant){a.extend(A,v(g[s],3));a.extend(C,v(g[t],1))}}if(a.isEmptyObject(C)){var n=function(){h(t,z.get(0),s,u.get(0));z.css(B);u[b](A,l,"linear",e)}}else if(a.isEmptyObject(A)){var n=function(){h(t,z.get(0),s,u.get(0));z.css(B);z[b](C,l,"linear",e)}}else{var n=function(){h(t,z.get(0),s,u.get(0));z.css(B);z[b](C,l,"linear");u[b](A,l,"linear",e)}}}if(c.loop&&d==g.length*2-2){var D=n;n=function(){if(--x)D()}}if(d>0)return y(d-1,n);else return n}function v(a,b){var c=[0,l/(a.time_ms+2*l),1-l/(a.time_ms+2*l),1][b];return{left:Math.round(a.from.left+c*(a.to.left-a.from.left)),top:Math.round(a.from.top+c*(a.to.top-a.from.top)),width:Math.round(a.from.width+c*(a.to.width-a.from.width)),height:Math.round(a.from.height+c*(a.to.height-a.from.height))}}if(!c.fade)e("missing fade parameter.");if(c.speed&&c.sleep)e("you cannot set both speed and sleep at the same time.");var l=Math.round(c.fade*1e3);if(c.sleep)var m=Math.round(c.sleep*1e3);if(c.speed)var n=c.speed/1e3,o=Math.round(l*n);i.empty().css({overflow:"hidden",padding:0});if(!/^(absolute|relative|fixed)$/.test(i.css("position")))i.css({position:"relative"});if(!i.width()||!i.height())e("container element does not have its own width and height");if(c.shuffle)g.sort(function(){return Math.random()-.5});for(var p=0;p<g.length;++p){var q=g[p];if(!q.src)e("missing src parameter in picture {0}.",p+1);if(n){switch(q.dir){case"up":q.from={xrel:.5,yrel:0,zoom:1};q.to={xrel:.5,yrel:1,zoom:1};var r=q.height-k-2*o;break;case"down":q.from={xrel:.5,yrel:1,zoom:1};q.to={xrel:.5,yrel:0,zoom:1};var r=q.height-k-2*o;break;case"left":q.from={xrel:0,yrel:.5,zoom:1};q.to={xrel:1,yrel:.5,zoom:1};var r=q.width-j-2*o;break;case"right":q.from={xrel:1,yrel:.5,zoom:1};q.to={xrel:0,yrel:.5,zoom:1};var r=q.width-j-2*o;break;default:e("missing or malformed dir parameter in picture {0}.",p+1)}if(r<=0)e("impossible animation: either picture {0} is too small or "+"div is too large or fade duration too long.",p+1);q.time_ms=Math.round(r/n)}else if(!m){if(!q.from||!q.to||!q.time)e("missing either speed/sleep option, or from/to/time params "+"in picture {0}.",p+1);try{q.from=f(q.from)}catch(s){e('malformed "from" parameter in picture {0}.',p+1)}try{q.to=f(q.to)}catch(s){e('malformed "to" parameter in picture {0}.',p+1)}if(!q.time)e('missing "time" parameter in picture {0}.',p+1);q.time_ms=Math.round(q.time*1e3)}if(q.from)a.each([q.from,q.to],function(a,b){b.width=Math.round(q.width*b.zoom);b.height=Math.round(q.height*b.zoom);b.left=Math.round((j-b.width)*b.xrel);b.top=Math.round((k-b.height)*b.yrel)});var t,u;u=t=a(d('<img src="{0}" width="'+j+'" height="'+k+'" />',q.src));if(q.href)u=a(d('<a href="{0}"></a>',q.href)).append(t);if(q.onclick)u.click(q.onclick);if(q.alt)t.attr("alt",q.alt);if(q.rel)u.attr("rel",q.rel);if(q.href&&q.target)u.attr("target",q.target);u.appendTo(i)}delete n;var w=i.find("img").css({position:"absolute",visibility:"hidden",top:0,left:0,border:0});w.eq(0).css({visibility:"visible"});if(!m)w.eq(0).css(v(g[0],c.variant?0:1));var x=c.loop;var z=y(g.length*2-1,function(){return z()});z()});return i};a.fn.crossSlideFreeze=function(){this.find("img").stop()};a.fn.crossSlideStop=function(){this.find("img").stop().remove()};a.fn.crossSlideRestart=function(){this.find("img").stop().remove();a.fn.crossSlide.apply(this,this.get(0).crossSlideArgs)};a.fn.crossSlidePause=function(){if(!a.fn.pause)e(c);this.find("img").pause()};a.fn.crossSlideResume=function(){if(!a.fn.pause)e(c);this.find("img").resume()}})();
