/*
 * jFlip plugin for jQuery v0.4 (28/2/2009)
 * 
 * A plugin to make a page flipping gallery    
 *
 * Copyright (c) 2008-2009 Renato Formato (rformato@gmail.com)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *    
 */
(function(b){var a=function(L,e,W,R,A,n){A=b.extend({background:"green",cornersTop:true,scale:"noresize",circular:true},A);var g=this,T=L.prev(),E=0,O=true,i=false,S=false,l=A.background,f=A.cornersTop,B=A.circular,d=A.gradientColors||["#4F2727","#FF8F8F","#F00"],q=A.curlSize||0.1,G=A.scale,m=[],p=L.clone(),z=b.browser.msie?null:p[0].getContext("2d"),L=b.browser.msie?b(G_vmlCanvasManager.initElement(L[0])):L,t=L[0].getContext("2d"),r=0;var R=R.each(function(ab){if(m[ab]){return}var c=this;c.onload=function(){var ac=1;if(G!="noresize"){var ae=e/this.width,ad=W/this.height;if(G=="fit"){ac=(ae<1||ad<1)?Math.min(ae,ad):1}if(G=="fill"){ac=Math.min(ae,ad)}}b(c).data("flip.scale",ac);m[ab]=t.createPattern(c,"no-repeat");r++;if(r==R.length&&!S){S=true;b(n).css({position:"absolute",left:"-9000px",top:"-9000px"});b(L).css({position:"",left:0,top:0});if(g.positionDiv){g.positionDiv()}Z()}};if(c.complete){window.setTimeout(function(){c.onload()},10)}}).get();var e=e,W=W,y=e,v=W,V=y*(1-q),Y=v*q,k=false,M=b.browser.msie?L.offset():null,h=false,Q=400,F=false,J,K,C=700,D=false,H,I,u,x,N=false,o=false,P=false;b(window).scroll(function(){});var aa=b.browser.msie?(function(){var ab=b("<div>").width(e).height(W).css({position:"absolute",cursor:"default",zIndex:1}).appendTo("body");if(parseInt(b.browser.version)==7){ab.css({opacity:0.000001,background:"#FFF"})}var c=function(){M=L.offset();return ab.css({left:M.left+"px",top:M.top+"px"})};g.positionDiv=c;b(window).resize(c);return c()})():L;aa.mousemove(function(c){if(!M){M=L.offset()}if(o&&h){if(!P){P=true;window.clearInterval(J)}y=!k?c.pageX-M.left:e-(c.pageX-M.left);v=f?c.pageY-M.top:W-(c.pageY-M.top);window.setTimeout(Z,0);return false}u=c.pageX||u,x=c.pageY||x;if(!D){if(!B&&O){k=false}else{if(!B&&i){k=true}else{k=(u-M.left)<e/2}}}if(!D&&((u-M.left)>V||(u-M.left)<(e-V))&&((f&&(x-M.top)<Y)||(!f&&(x-M.top)>(W-Y)))){if(!h){h=true;if(!B&&((k&&O)||(!k&&i))){return false}aa.css("cursor","pointer")}}else{if(h){h=false;aa.css("cursor","default")}}return false}).bind("mouseenter",function(c){N=true;if(D){return}if(!B&&((k&&O)||(!k&&i))){return false}window.clearInterval(J);K=new Date().getTime();J=window.setInterval(s,10);return false}).bind("mouseleave",function(c){N=false;P=false;o=false;if(D){return}window.clearInterval(J);K=new Date().getTime();J=window.setInterval(U,10);return false}).click(function(){if(h&&!D){if(!B&&((k&&O)||(!k&&i))){return false}D=true;aa.triggerHandler("mousemove");window.clearInterval(J);K=new Date().getTime();H=y;I=v;J=window.setInterval(X,10);E+=k?-1:1;if(E<0){E=R.length-1}if(E==R.length){E=0}O=(E==0);i=((E+1)==R.length);T.trigger("flip.jflip",[E,R.length])}return false}).mousedown(function(){P=false;o=true;return false}).mouseup(function(){o=false;return false});var X=function(){var c=new Date(),ab=c.getTime()-K;if(ab>=C){window.clearInterval(J);if(k){R.unshift(R.pop());m.unshift(m.pop())}else{R.push(R.shift());m.push(m.shift())}y=e;v=W;Z();D=false;if(N){if(!B&&((k&&O)||(!k&&i))){return}K=new Date().getTime();J=window.setInterval(s,10);aa.triggerHandler("mousemove")}return}y=H-2*(e)*ab/C;v=I+2*(W)*ab/C;Z()},j=function(){var c=new Date(),ab=c.getTime()-K;y=V+Math.sin(Math.PI*2*ab/1000);v=Y+Math.cos(Math.PI*2*ab/1000);drawing=true;window.setTimeout(Z,0)},s=function(){var c=new Date(),ab=c.getTime()-K;if(ab>=Q){window.clearInterval(J);K=new Date().getTime();J=window.setInterval(j,10)}y=e-(e-V)*ab/Q;v=Y*ab/Q;Z()},U=function(){var c=new Date(),ab=c.getTime()-K;if(ab>=Q){window.clearInterval(J)}y=V+(e-V)*ab/Q;v=Y-Y*ab/Q;Z()},w=function(c,ad){var ab=c*e+ad,ac=-ad/c;if(b.browser.msie){ab=Math.round(ab);ac=Math.round(ac)}t.beginPath();t.moveTo(e,Math.min(ab,W));t.lineTo(e,0);t.lineTo(Math.max(ac,0),0);if(ac<0){t.lineTo(0,Math.min(ad,W));if(ad<W){t.lineTo((W-ad)/c,W)}t.lineTo(e,W)}else{if(ab<W){t.lineTo(e,ab)}else{t.lineTo((W-ad)/c,W);t.lineTo(e,W)}}},Z=function(){if(!S){return}if(b.browser.msie){t.clearRect(0,0,e,W)}t.fillStyle=l;t.fillRect(0,0,e,W);var ag=R[0],ab=b(ag).data("flip.scale");if(b.browser.msie){t.fillStyle=m[0];t.fillStyle.width2=t.fillStyle.width*ab;t.fillStyle.height2=t.fillStyle.height*ab;t.fillRect(0,0,e,W)}else{t.drawImage(ag,(e-ag.width*ab)/2,(W-ag.height*ab)/2,ag.width*ab,ag.height*ab)}if(v&&y!=e){var ad=2,ac=(v-ad*(y+e))/2;m2=v/(e-y),q2=y*m2;if(ad==m2){return}var an=1,ak=1,af=0,ae=0;t.save();if(k){af=e;an=-1}if(!f){ae=W;ak=-1}t.translate(af,ae);t.scale(an,ak);var aq=(q2-ac)/(ad-m2);var ao=ad*aq+ac;var am=(2*ao+aq+2*ad*y-2*v)/(2*ad+1);var aj=-am/ad+ao+aq/ad;var ah=Math.sqrt(Math.pow(aq-am,2)+Math.pow(ao-aj,2));var ap=Math.min(ah*0.5,30);var ai;if(!(b.browser.mozilla&&parseFloat(b.browser.version)<1.9)){ai=t}else{ai=z;ai.clearRect(0,0,e,W);ai.save();ai.translate(1,0)}var al=ai.createLinearGradient(aq,ao,am,aj);al.addColorStop(0,d[0]);al.addColorStop(ap/ah,d[1]);al.addColorStop(1,d[2]);ai.fillStyle=al;ai.beginPath();ai.moveTo(-ac/ad,0);ai.quadraticCurveTo((-ac/ad+y)/2+0.02*y,v/2,y,v);ai.quadraticCurveTo((e+y)/2,(ad*e+ac+v)/2-0.02*(W-v),e,ad*e+ac);if(!(b.browser.mozilla&&parseFloat(b.browser.version)<1.9)){ai.fill()}else{ai.save();ai.clip();ai.fillRect(0,0,e,W);ai.restore();t.drawImage(p[0],0,0);ai.restore()}al=null;t.fillStyle=l;w(ad,ac);t.fill();w(ad,ac);if(!b.browser.safari&&!b.browser.opera){t.restore()}var ag=k?R[R.length-1]:R[1];ab=b(ag).data("flip.scale");if(b.browser.msie){t.fillStyle=k?m[m.length-1]:m[1];t.fillStyle.width2=t.fillStyle.width*ab;t.fillStyle.height2=t.fillStyle.height*ab;t.fill()}else{t.save();t.clip();if(b.browser.safari||b.browser.opera){t.scale(1/an,1/ak);t.translate(-af,-ae)}t.drawImage(ag,(e-ag.width*ab)/2,(W-ag.height*ab)/2,ag.width*ab,ag.height*ab);t.restore();if(b.browser.safari||b.browser.opera){t.restore()}}}}};b.fn.jFlip=function(c,f,d,e){return this.each(function(){b(this).wrap("<div class='flip_gallery'>");if(!e){e=b(this).find("img")}var g=b(document.createElement("canvas")).attr({width:c,height:f}).css({margin:0,width:c+"px",height:f+"px"});b(this).after(g);b(g).css({position:"absolute",left:"-9000px",top:"-9000px"});new a(b(this).next(),c||300,f||300,e,d,this)})}})(jQuery);