/*
 * CrossSlide jQuery plugin v0.6.2
 *
 * 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 g(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 f(a){var b=true;for(var c in a){b=false;break}return 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,h,i){var j=this,k=this.width(),l=this.height();if(j.length!=1)e("crossSlide() must be called on exactly 1 element");j.get(0).crossSlideArgs=[c,h,i];h=a.map(h,function(b){return a.extend({},b)});if(!c.easing)c.easing=c.variant?"swing":"linear";if(!i)i=function(){};(function(a){function c(d,e){e.onload=function(c){b++;h[d].width=e.width;h[d].height=e.height;if(b==h.length)a()};e.src=h[d].src;if(d+1<h.length)c(d+1,new Image)}var b=0;c(0,new Image)})(function(){function z(d,e){if(d%2==0){if(n){var g=d/2,j=(g-1+h.length)%h.length,k=x.eq(g),l=x.eq(j);var o=function(){i(g,k.get(0));l.css("visibility","hidden");setTimeout(e,n)}}else{var p=d/2,j=(p-1+h.length)%h.length,q=x.eq(p),l=x.eq(j),r=h[p].time_ms,s=w(h[p],c.variant?3:2);var o=function(){i(p,q.get(0));l.css("visibility","hidden");q[b](s,r,c.easing,e)}}}else{var t=Math.floor(d/2),u=Math.ceil(d/2)%h.length,v=x.eq(t),A=x.eq(u),B={},C={visibility:"visible"},D={};if(u>t){C.opacity=0;D.opacity=1;if(c.doubleFade)B.opacity=0}else{B.opacity=0;if(c.doubleFade){C.opacity=0;D.opacity=1}}if(!n){a.extend(C,w(h[u],0));if(!c.variant){a.extend(B,w(h[t],3));a.extend(D,w(h[u],1))}}if(f(D)){var o=function(){i(u,A.get(0),t,v.get(0));A.css(C);v[b](B,m,"linear",e)}}else if(f(B)){var o=function(){i(u,A.get(0),t,v.get(0));A.css(C);A[b](D,m,"linear",e)}}else{var o=function(){i(u,A.get(0),t,v.get(0));A.css(C);A[b](D,m,"linear");v[b](B,m,"linear",e)}}}if(c.loop&&d==h.length*2-2){var E=o;o=function(){if(--y)E()}}if(d>0)return z(d-1,o);else return o}function w(a,b){var c=[0,m/(a.time_ms+2*m),1-m/(a.time_ms+2*m),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 m=Math.round(c.fade*1e3);if(c.sleep)var n=Math.round(c.sleep*1e3);if(c.speed)var o=c.speed/1e3,p=Math.round(m*o);j.empty().css({overflow:"hidden",padding:0});if(!/^(absolute|relative|fixed)$/.test(j.css("position")))j.css({position:"relative"});if(!j.width()||!j.height())e("container element does not have its own width and height");if(c.shuffle)h.sort(function(){return Math.random()-.5});for(var q=0;q<h.length;++q){var r=h[q];if(!r.src)e("missing src parameter in picture {0}.",q+1);if(o){switch(r.dir){case"up":r.from={xrel:.5,yrel:0,zoom:1};r.to={xrel:.5,yrel:1,zoom:1};var s=r.height-l-2*p;break;case"down":r.from={xrel:.5,yrel:1,zoom:1};r.to={xrel:.5,yrel:0,zoom:1};var s=r.height-l-2*p;break;case"left":r.from={xrel:0,yrel:.5,zoom:1};r.to={xrel:1,yrel:.5,zoom:1};var s=r.width-k-2*p;break;case"right":r.from={xrel:1,yrel:.5,zoom:1};r.to={xrel:0,yrel:.5,zoom:1};var s=r.width-k-2*p;break;default:e("missing or malformed dir parameter in picture {0}.",q+1)}if(s<=0)e("impossible animation: either picture {0} is too small or "+"div is too large or fade duration too long.",q+1);r.time_ms=Math.round(s/o)}else if(!n){if(!r.from||!r.to||!r.time)e("missing either speed/sleep option, or from/to/time params "+"in picture {0}.",q+1);try{r.from=g(r.from)}catch(t){e('malformed "from" parameter in picture {0}.',q+1)}try{r.to=g(r.to)}catch(t){e('malformed "to" parameter in picture {0}.',q+1)}if(!r.time)e('missing "time" parameter in picture {0}.',q+1);r.time_ms=Math.round(r.time*1e3)}if(r.from)a.each([r.from,r.to],function(a,b){b.width=Math.round(r.width*b.zoom);b.height=Math.round(r.height*b.zoom);b.left=Math.round((k-b.width)*b.xrel);b.top=Math.round((l-b.height)*b.yrel)});var u,v;v=u=a(d('<img src="{0}"/>',r.src));if(r.href)v=a(d('<a href="{0}"></a>',r.href)).append(u);if(r.onclick)v.click(r.onclick);if(r.alt)u.attr("alt",r.alt);if(r.rel)v.attr("rel",r.rel);if(r.href&&r.target)v.attr("target",r.target);v.appendTo(j)}delete o;var x=j.find("img").css({position:"absolute",visibility:"hidden",top:0,left:0,border:0});x.eq(0).css({visibility:"visible"});if(!n)x.eq(0).css(w(h[0],c.variant?0:1));var y=c.loop;var A=z(h.length*2-1,function(){return A()});A()});return j};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()}})()
