//////////////////////////////////////////////////////////////////////////////////
// CloudCarousel V1.0.3
// (c) 2010 by R Cecco. <http://www.professorcloud.com>
// MIT License
//
// Reflection code based on plugin by Christophe Beyls <http://www.digitalia.be>
//
// Please retain this copyright header in all versions of the software
//////////////////////////////////////////////////////////////////////////////////
(function(c){function b(g,m,j){var f,h,l=g.width,i=g.width,n,o;o=c(g.parentNode);if(c.browser.msie){this.element=f=o.append("<img class='reflection' style='position:absolute'/>").find(":last")[0];f.src=g.src;f.style.filter="flipv progid:DXImageTransform.Microsoft.Alpha(opacity="+(j*100)+", style=1, finishOpacity=0, startx=0, starty=0, finishx=0, finishy="+(m/i*100)+")"}else{this.element=f=o.append("<canvas class='reflection' style='position:absolute'/>").find(":last")[0];if(!f.getContext){return}h=f.getContext("2d");try{c(f).attr({width:l,height:m});h.save();h.translate(0,i-1);h.scale(1,-1);h.drawImage(g,0,0,l,i);h.restore();h.globalCompositeOperation="destination-out";n=h.createLinearGradient(0,0,0,m);n.addColorStop(0,"rgba(255, 255, 255, "+(1-j)+")");n.addColorStop(1,"rgba(255, 255, 255, 1.0)");h.fillStyle=n;h.fillRect(0,0,l,m)}catch(k){return}}c(f).attr({alt:c(g).attr("alt"),title:c(g).attr("title")})}var a=function(f,e){this.orgWidth=f.width;this.orgHeight=f.height;this.image=f;this.reflection=null;this.alt=f.alt;this.title=f.title;this.imageOK=false;this.options=e;this.imageOK=true;if(this.options.reflHeight>0){this.reflection=new b(this.image,this.options.reflHeight,this.options.reflOpacity)}c(this.image).css("position","absolute")};var d=function(f,k,p){var j=[],h=Math.sin,l=Math.cos,o=this;this.controlTimer=0;this.stopped=false;this.container=f;this.xRadius=p.xRadius;this.yRadius=p.yRadius;this.showFrontTextTimer=0;this.autoRotateTimer=0;if(p.xRadius===0){this.xRadius=(c(f).width()/2.3)}if(p.yRadius===0){this.yRadius=(c(f).height()/6)}this.xCentre=p.xPos;this.yCentre=p.yPos;this.frontIndex=0;this.stageWidth=c(f).width();this.stageHeight=c(f).height();var m=false;var n=[];this.rotation=this.destRotation=Math.PI/2;this.timeDelay=1000/p.FPS;if(p.altBox!==null){c(p.altBox).css("display","block");c(p.titleBox).css("display","block")}c(f).css({position:"relative",overflow:"hidden"});c(p.buttonLeft).css("display","inline");c(p.buttonRight).css("display","inline");c(p.buttonLeft).bind("mousedown",this,function(q){q.data.rotate(-1);return false});c(p.buttonRight).bind("mousedown",this,function(q){q.data.rotate(1);return false});if(p.mouseGesture){c(f).gesture(function(q){if(q.moves==8){c(p.buttonRight).trigger("mousedown");m=true;return false}else{if(q.moves==4){c(p.buttonLeft).trigger("mousedown");m=true;return false}}})}if(p.mouseWheel){c(f).bind("mousewheel",this,function(q,r){q.data.rotate(r);return false})}c(f).bind("mouseover",this,function(q){clearInterval(q.data.autoRotateTimer)});c(f).bind("mouseout",this,function(r){var q=r.data;clearTimeout(q.showFrontTextTimer);q.showFrontTextTimer=setTimeout(function(){q.showFrontText()},1000);q.autoRotate()});var i=false;var e=false;if(p.mouseGesture==false){c(f).bind("mousemove",this,function(t){if(i==false){return}e=true;var u=n[0];var q={left:(t.pageX-u.left),top:(t.pageY-u.top)};var v={left:t.pageX,top:t.pageY};if(n.length==10){n.shift()}n.push(v);var r=q.left;if(r<this.old_rotation){this.new_direction="right"}else{this.new_direction="left";this.direction_right_pos=r}if(this.old_direction!=undefined&&this.old_direction!=this.new_direction){var s={left:t.pageX,top:t.pageY};global.mouseOffset={0:s}}this.old_direction=this.new_direction;this.old_rotation=r;t.data.mouserotate(q.left)})}if(m!=true){var g=this.frontIndex;c(f).bind("mouseup",this,function(r){n=[];i=false;if(e==true||m==true){e=false;m=false;r.data.rotatetoclosest();return}if(r.target.src!=undefined){var q=0;c.each(j,function(v,x){if(x.image.src==r.target.src&&g!=q){var w=Math.ceil(j.length/2);var s=q-g;var u=s;var y=false;var t=0;if(s<=0){y=true;u=-s}if(u>=w){if(y==true){t=-(j.length-u)}else{t=j.length-u}}else{if(y!=true){t=-u}else{t=u}}r.data.rotate(t)}else{if(x.image.src==r.target.src&&g==q){c.address.value(c(j[g].image).parent().attr("href"));return false}}q++})}return false})}f.onselectstart=function(){return false};this.innerWrapper=c(f).wrapInner('<div style="position:absolute;width:100%;height:100%;"/>').children()[0];this.showFrontText=function(){if(j[this.frontIndex]===undefined){return}c(p.titleBox).html('<img src="'+c(j[this.frontIndex].image).attr("ref")+'" alt="" />');c(p.altBox).html(c(j[this.frontIndex].image).attr("alt"))};this.go=function(){if(this.controlTimer!==0){return}var q=this;this.controlTimer=setTimeout(function(){q.updateAll()},this.timeDelay)};this.stop=function(){clearTimeout(this.controlTimer);this.controlTimer=0};this.mouserotate=function(q){q=q/500;q=-q;this.destRotation=this.rotation+((Math.PI/j.length)*(q));this.showFrontText();this.go()};this.rotatetoclosest=function(){g=this.frontIndex;var q=(2*Math.PI)/j.length;var t=global.destRotation;var r=this.destRotation;if(global.destRotation>this.destRotation){var u=(t-r)/q;if(u<=1){u=1}else{u=Math.round(u)}direction=-u;this.destRotation=global.destRotation-(q*u);global.destRotation=this.destRotation;this.frontIndex=this.frontIndex+u;if(this.frontIndex<0){this.frontIndex=j.length+g-direction}if(this.frontIndex>=j.length){var s=(-direction);this.frontIndex=g-j.length+s}g=this.frontIndex;this.showFrontText();this.go()}else{var u=(r-t)/q;if(u<=1){u=1}else{u=Math.round(u)}direction=u;this.destRotation=global.destRotation+(q*u);global.destRotation=this.destRotation;this.frontIndex=this.frontIndex-u;if(this.frontIndex<0){this.frontIndex=j.length+g-direction}if(this.frontIndex>=j.length){var s=(-direction);this.frontIndex=g-j.length+s}g=this.frontIndex;this.showFrontText();this.go()}};this.rotate=function(r){this.frontIndex-=r;this.direction=r;if(this.frontIndex<0){this.frontIndex=j.length+g-r}if(this.frontIndex>=j.length){var q=(-r);this.frontIndex=g-j.length+q}g=this.frontIndex;this.destRotation+=(Math.PI/j.length)*(2*r);global.destRotation=this.destRotation;this.showFrontText();this.go()};this.autoRotate=function(){if(p.autoRotate!=="no"){var q=(p.autoRotate==="right")?1:-1;this.autoRotateTimer=setInterval(function(){o.rotate(q)},p.autoRotateDelay)}};this.updateAll=function(){var M=p.minScale;var B=(1-M)*0.5;var r=p.minAlpha;var s=(1-r)*0.5;var z,K,v,u,O,L,F;var A=(this.destRotation-this.rotation);var E=Math.abs(A);if(this.direction<0){this.direction=-this.direction}this.rotation+=A*p.speed/(this.direction*0.5);if(E<0.001){this.rotation=this.destRotation}var I=j.length;var t=(Math.PI/I)*2;var N=this.rotation;var D=c.browser.msie;this.innerWrapper.style.display="none";var J;var C="px",G;var q=this;for(var H=0;H<I;H++){L=j[H];L.radians=N;F=h(N);O=((F+1)*B)+M;alpha=((F+1)*s)+r;v=this.xCentre+(((l(N)*this.xRadius)-(L.orgWidth*0.5))*O);u=this.yCentre+(((F*this.yRadius))*O);c("#carousel-info").append(L);if(L.imageOK){var P=L.image;z=P.width=L.orgWidth*O;K=P.height=L.orgHeight*O;P.style.left=v+C;P.style.top=u+C;if((v+z)<0){P.style.display="none"}else{P.style.display="block"}if(v>this.stageWidth){P.style.display="none"}else{P.style.display="block"}P.style.opacity=alpha;P.style.zIndex=(O*100)>>0;if(L.reflection!==null){G=p.reflHeight*O;J=L.reflection.element.style;J.left=v+C;J.top=u+K+p.reflGap*O+C;J.width=z+C;if(D){J.filter.finishy=(G/K*100)}else{J.height=G+C}}}N+=t}this.innerWrapper.style.display="block";if(E>=0.001){this.controlTimer=setTimeout(function(){q.updateAll()},this.timeDelay)}else{this.stop()}};this.checkImagesLoaded=function(){var q;for(q=0;q<k.length;q++){if((k[q].width===undefined)||((k[q].complete!==undefined)&&(!k[q].complete))){return}}for(q=0;q<k.length;q++){j.push(new a(k[q],p))}if(p.reverse){j.reverse();this.rotate(1)}clearInterval(this.tt);this.showFrontText();this.autoRotate();this.updateAll()};this.tt=setInterval(function(){o.checkImagesLoaded()},50)};c.fn.CloudCarousel=function(e){this.each(function(){global={},e=c.extend({},{reflHeight:0,reflOpacity:0.5,reflGap:0,minScale:0.5,minAlpha:1,xPos:0,yPos:0,xRadius:0,yRadius:0,altBox:null,titleBox:null,FPS:50,autoRotate:"no",autoRotateDelay:1500,speed:0.2,reverse:false,mouseWheel:false,mouseGesture:false},e);c(this).data("cloudcarousel",new d(this,c(".cloudcarousel",c(this)),e))});return this}})(jQuery);
