/*
 * CrossSlide jQuery plugin v0.4
 *
 * 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.
 */
jQuery.fn.crossSlide=function(c,f){var a=this,e=this.width(),b=this.height();function d(j){for(var h=1;h<arguments.length;h++){j=j.replace(new RegExp("\\{"+(h-1)+"}","g"),arguments[h])}return j}function g(){arguments[0]="CrossSlide: "+arguments[0];throw new Error(d.apply(null,arguments))}(function(j){var h=0;function i(l,k){k.onload=function(m){h++;f[l].width=k.width;f[l].height=k.height;if(h==f.length){j()}};k.src=f[l].src;if(l+1<f.length){i(l+1,new Image())}}i(0,new Image())})(function(){function u(y){var i=1;var p=y.replace(/^\s*|\s*$/g,"").split(/\s+/);if(p.length>3){throw new Error()}if(p[0]=="center"){if(p.length==1){p=["center","center"]}else{if(p.length==2&&p[1].match(/^[\d.]+x$/i)){p=["center","center",p[1]]}}}if(p.length==3){i=parseFloat(p[2].match(/^([\d.]+)x$/i)[1])}var z=p[0]+" "+p[1];if(z=="left top"||z=="top left"){return{xrel:0,yrel:0,zoom:i}}if(z=="left center"||z=="center left"){return{xrel:0,yrel:0.5,zoom:i}}if(z=="left bottom"||z=="bottom left"){return{xrel:0,yrel:1,zoom:i}}if(z=="center top"||z=="top center"){return{xrel:0.5,yrel:0,zoom:i}}if(z=="center center"){return{xrel:0.5,yrel:0.5,zoom:i}}if(z=="center bottom"||z=="bottom center"){return{xrel:0.5,yrel:1,zoom:i}}if(z=="right top"||z=="top right"){return{xrel:1,yrel:0,zoom:i}}if(z=="right center"||z=="center right"){return{xrel:1,yrel:0.5,zoom:i}}if(z=="right bottom"||z=="bottom right"){return{xrel:1,yrel:1,zoom:i}}return{xrel:parseInt(p[0].match(/^(\d+)%$/)[1])/100,yrel:parseInt(p[1].match(/^(\d+)%$/)[1])/100,zoom:i}}function j(y,i){switch(i){case 1:var z=0;break;case 2:var z=w/(y.time_ms+2*w);break;case 3:var z=1-w/(y.time_ms+2*w);break;case 4:var z=1;break}return{left:Math.round(y.from.left+z*(y.to.left-y.from.left)),top:Math.round(y.from.top+z*(y.to.top-y.from.top)),width:Math.round(y.from.width+z*(y.to.width-y.from.width)),height:Math.round(y.from.height+z*(y.to.height-y.from.height))}}if(!c.fade){g("missing fade parameter.")}if(c.speed&&c.sleep){g("you cannot set both speed and sleep at the same time.")}var w=Math.round(c.fade*1000);if(c.sleep){var v=Math.round(c.sleep*1000)}if(c.speed){var k=c.speed/1000,q=Math.round(w*k)}a.empty().css({overflow:"hidden",padding:0});if(!/^(absolute|relative|fixed)$/.test(a.css("position"))){a.css({position:"relative"})}if(!a.width()||!a.height()){g("container element does not have its own width and height")}if(c.shuffle){f.sort(function(){return Math.random()-0.5})}for(var n=0;n<f.length;++n){var h=f[n];if(!h.src){g("missing src parameter in picture {0}.",n+1)}if(k){switch(h.dir){case"up":h.from={xrel:0.5,yrel:0,zoom:1};h.to={xrel:0.5,yrel:1,zoom:1};var s=h.height-b-2*q;break;case"down":h.from={xrel:0.5,yrel:1,zoom:1};h.to={xrel:0.5,yrel:0,zoom:1};var s=h.height-b-2*q;break;case"left":h.from={xrel:0,yrel:0.5,zoom:1};h.to={xrel:1,yrel:0.5,zoom:1};var s=h.width-e-2*q;break;case"right":h.from={xrel:1,yrel:0.5,zoom:1};h.to={xrel:0,yrel:0.5,zoom:1};var s=h.width-e-2*q;break;default:g("missing or malformed dir parameter in picture {0}.",n+1)}if(s<=0){g("picture {0} is too small for the desired fade duration.",n+1)}h.time_ms=Math.round(s/k)}else{if(!v){if(!h.from||!h.to||!h.time){g("missing either speed/sleep option, or from/to/time params in picture {0}.",n+1)}try{h.from=u(h.from)}catch(r){g('malformed "from" parameter in picture {0}.',n+1)}try{h.to=u(h.to)}catch(r){g('malformed "to" parameter in picture {0}.',n+1)}if(!h.time){g('missing "time" parameter in picture {0}.',n+1)}h.time_ms=Math.round(h.time*1000)}}if(h.from){jQuery.each([h.from,h.to],function(y,p){p.width=Math.round(h.width*p.zoom);p.height=Math.round(h.height*p.zoom);p.left=Math.round((e-p.width)*p.xrel);p.top=Math.round((b-p.height)*p.yrel)})}var t;if(h.href){t=jQuery(d('<a href="{0}" target="_blank"><img src="{1}"/></a>',h.href,h.src))}else{t=jQuery(d('<img src="{0}"/>',h.src))}if(h.onclick){t.click(h.onclick)}if(h.alt){t.find("img").attr("alt",h.alt)}t.appendTo(a)}k=undefined;var o=a.find("img").css({position:"absolute",visibility:"hidden",top:0,left:0,border:0});o.eq(0).css({visibility:"visible"});if(!v){o.eq(0).css(j(f[0],2))}var x=c.loop;function l(K,p){if(K%2==0){if(v){var A=K/2,O=(A-1+f.length)%f.length,L=o.eq(A),I=o.eq(O);var H=function(){I.css("visibility","hidden");setTimeout(p,v)}}else{var D=K/2,O=(D-1+f.length)%f.length,N=o.eq(D),I=o.eq(O),B=f[D].time_ms,J=j(f[D],3);var H=function(){I.css("visibility","hidden");N.animate(J,B,"linear",p)}}}else{if(v){var z=Math.floor(K/2),C=Math.ceil(K/2)%f.length,M=o.eq(z),y=o.eq(C),P={},G={visibility:"visible"},F={};if(C>z){G.opacity=0;F.opacity=1;if(c.doubleFade){P.opacity=0}}else{P.opacity=0;if(c.doubleFade){G.opacity=0;F.opacity=1}}var H=function(){y.css(G);if(P.opacity!=undefined){if(c.doubleFade){y.animate(F,w,"linear")}M.animate(P,w,"linear",p)}else{if(c.doubleFade){M.animate(P,w,"linear")}y.animate(F,w,"linear",p)}}}else{var z=Math.floor(K/2),C=Math.ceil(K/2)%f.length,M=o.eq(z),y=o.eq(C),P=j(f[z],4),G=j(f[C],1),F=j(f[C],2);if(C>z){G.opacity=0;F.opacity=1}else{P.opacity=0}G.visibility="visible";var H=function(){M.animate(P,w,"linear");y.css(G);y.animate(F,w,"linear",p)}}}if(c.loop&&K==f.length*2-2){var E=H;H=function(){if(--x){E()}}}if(K>0){return l(K-1,H)}else{return H}}var m=l(f.length*2-1,function(){return m()});m()});return a};
