﻿jQuery.extend({roundabout_shape:{def:"lazySusan",lazySusan:function(b,a,c){return {x:Math.sin(b+a),y:Math.sin(b+3*Math.PI/2+a)/8*c,z:(Math.cos(b+a)+1)/2,scale:Math.sin(b+Math.PI/2+a)/2+.5}}}});jQuery.fn.roundabout=function(){var a=typeof arguments[0]!="object"?{}:arguments[0];a={bearing:typeof a.bearing=="undefined"?0:jQuery.roundabout_toFloat(a.bearing%360),tilt:typeof a.tilt=="undefined"?0:jQuery.roundabout_toFloat(a.tilt),minZ:typeof a.minZ=="undefined"?100:parseInt(a.minZ,10),maxZ:typeof a.maxZ=="undefined"?400:parseInt(a.maxZ,10),minOpacity:typeof a.minOpacity=="undefined"?.4:jQuery.roundabout_toFloat(a.minOpacity),maxOpacity:typeof a.maxOpacity=="undefined"?1:jQuery.roundabout_toFloat(a.maxOpacity),minScale:typeof a.minScale=="undefined"?.4:jQuery.roundabout_toFloat(a.minScale),maxScale:typeof a.maxScale=="undefined"?1:jQuery.roundabout_toFloat(a.maxScale),duration:typeof a.duration=="undefined"?600:parseInt(a.duration,10),btnNext:a.btnNext||null,btnPrev:a.btnPrev||null,easing:a.easing||"swing",clickToFocus:a.clickToFocus!==false,focusBearing:typeof a.focusBearing=="undefined"?0:jQuery.roundabout_toFloat(a.focusBearing%360),shape:a.shape||"lazySusan",debug:a.debug||false,childSelector:a.childSelector||"li",startingChild:typeof a.startingChild=="undefined"?null:parseInt(a.startingChild,10),reflect:typeof a.reflect=="undefined"||a.reflect===false?false:true};this.each(function(){var b=jQuery(this),c=jQuery.roundabout_toFloat(360/b.children(a.childSelector).length),d=a.startingChild===null?a.bearing:a.startingChild*c;b.addClass("roundabout-holder").css("padding",0).css("position","relative").css("z-index",a.minZ);b.data("roundabout",{bearing:d,tilt:a.tilt,minZ:a.minZ,maxZ:a.maxZ,minOpacity:a.minOpacity,maxOpacity:a.maxOpacity,minScale:a.minScale,maxScale:a.maxScale,duration:a.duration,easing:a.easing,clickToFocus:a.clickToFocus,focusBearing:a.focusBearing,animating:0,childInFocus:-1,shape:a.shape,period:c,debug:a.debug,childSelector:a.childSelector,reflect:a.reflect});if(a.clickToFocus===true)b.children(a.childSelector).each(function(d){jQuery(this).click(function(f){var e=a.reflect===true?360-c*d:c*d;e=jQuery.roundabout_toFloat(e);if(!jQuery.roundabout_isInFocus(b,e)){f.preventDefault();if(b.data("roundabout").animating===0)b.roundabout_animateAngleToFocus(e);return false}})});if(a.btnNext)jQuery(a.btnNext).bind("click.roundabout",function(a){a.preventDefault();if(b.data("roundabout").animating===0)b.roundabout_animateToNextChild();return false});if(a.btnPrev)jQuery(a.btnPrev).bind("click.roundabout",function(a){a.preventDefault();if(b.data("roundabout").animating===0)b.roundabout_animateToPreviousChild();return false})});this.roundabout_startChildren();if(typeof arguments[1]==="function"){var b=arguments[1],c=this;setTimeout(function(){b(c)},0)}return this};jQuery.fn.roundabout_startChildren=function(){this.each(function(){var b=jQuery(this),a=b.data("roundabout"),c=b.children(a.childSelector);c.each(function(b){var c=a.reflect===true?360-a.period*b:a.period*b;jQuery(this).addClass("roundabout-moveable-item").css("position","absolute");jQuery(this).data("roundabout",{startWidth:jQuery(this).width(),startHeight:jQuery(this).height(),startFontSize:parseInt(jQuery(this).css("font-size"),10),degrees:c})});b.roundabout_updateChildPositions()});return this};jQuery.fn.roundabout_setTilt=function(b){this.each(function(){jQuery(this).data("roundabout").tilt=b;jQuery(this).roundabout_updateChildPositions()});if(typeof arguments[1]==="function"){var a=arguments[1],c=this;setTimeout(function(){a(c)},0)}return this};jQuery.fn.roundabout_setBearing=function(a){this.each(function(){jQuery(this).data("roundabout").bearing=jQuery.roundabout_toFloat(a%360,2);jQuery(this).roundabout_updateChildPositions()});if(typeof arguments[1]==="function"){var b=arguments[1],c=this;setTimeout(function(){b(c)},0)}return this};jQuery.fn.roundabout_adjustBearing=function(a){a=jQuery.roundabout_toFloat(a);if(a!==0)this.each(function(){jQuery(this).data("roundabout").bearing=jQuery.roundabout_getBearing(jQuery(this))+a;jQuery(this).roundabout_updateChildPositions()});if(typeof arguments[1]==="function"){var b=arguments[1],c=this;setTimeout(function(){b(c)},0)}return this};jQuery.fn.roundabout_adjustTilt=function(a){a=jQuery.roundabout_toFloat(a);if(a!==0)this.each(function(){jQuery(this).data("roundabout").tilt=jQuery.roundabout_toFloat(jQuery(this).roundabout_get("tilt")+a);jQuery(this).roundabout_updateChildPositions()});if(typeof arguments[1]==="function"){var b=arguments[1],c=this;setTimeout(function(){b(c)},0)}return this};jQuery.fn.roundabout_animateToBearing=function(b){b=jQuery.roundabout_toFloat(b);var c=new Date,e=typeof arguments[1]=="undefined"?null:arguments[1],d=typeof arguments[2]=="undefined"?null:arguments[2],a=typeof arguments[3]!=="object"?null:arguments[3];this.each(function(){var g=jQuery(this),h=g.data("roundabout"),f,l,k,j=e===null?h.duration:e,i=d!==null?d:h.easing||"swing";if(a===null)a={timerStart:c,start:jQuery.roundabout_getBearing(g),totalTime:j};f=c-a.timerStart;if(f<j){h.animating=1;if(typeof jQuery.easing.def=="string"){l=jQuery.easing[i]||jQuery.easing[jQuery.easing.def];k=l(null,f,a.start,b-a.start,a.totalTime)}else k=jQuery.easing[i](f/a.totalTime,f,a.start,b-a.start,a.totalTime);g.roundabout_setBearing(k,function(){g.roundabout_animateToBearing(b,j,i,a)})}else{b=b<0?b+360:b%360;h.animating=0;g.roundabout_setBearing(b)}});return this};jQuery.fn.roundabout_animateToDelta=function(a){var b=arguments[1],c=arguments[2];this.each(function(){a=jQuery.roundabout_getBearing(jQuery(this))+jQuery.roundabout_toFloat(a);jQuery(this).roundabout_animateToBearing(a,b,c)});return this};jQuery.fn.roundabout_animateToChild=function(a){var b=arguments[1],c=arguments[2];this.each(function(){var e=jQuery(this),d=e.data("roundabout");if(d.childInFocus!==a&&d.animating===0){var f=jQuery(e.children(d.childSelector)[a]);e.roundabout_animateAngleToFocus(f.data("roundabout").degrees,b,c)}});return this};jQuery.fn.roundabout_animateToNearbyChild=function(a,d){var b=a[0],c=a[1];this.each(function(){var h=jQuery(this).data("roundabout"),a=jQuery.roundabout_toFloat(360-jQuery.roundabout_getBearing(jQuery(this))),g=h.period,f=0,e,i=h.reflect,j=jQuery(this).children(h.childSelector).length;a=i===true?a%360:a;if(h.animating===0)if(i===false&&d==="next"||i===true&&d!=="next"){a=a===0?360:a;while(true&&f<j){e={lower:jQuery.roundabout_toFloat(g*f),upper:jQuery.roundabout_toFloat(g*(f+1))};e.upper=f==j-1?360:e.upper;if(a<=e.upper&&a>e.lower){jQuery(this).roundabout_animateToDelta(a-e.lower,b,c);break}f++}}else while(true){e={lower:jQuery.roundabout_toFloat(g*f),upper:jQuery.roundabout_toFloat(g*(f+1))};e.upper=f==j-1?360:e.upper;if(a>=e.lower&&a<e.upper){jQuery(this).roundabout_animateToDelta(a-e.upper,b,c);break}f++}});return this};jQuery.fn.roundabout_animateToNextChild=function(){return this.roundabout_animateToNearbyChild(arguments,"next")};jQuery.fn.roundabout_animateToPreviousChild=function(){return this.roundabout_animateToNearbyChild(arguments,"previous")};jQuery.fn.roundabout_animateAngleToFocus=function(c){var a=arguments[1],b=arguments[2];this.each(function(){var d=jQuery.roundabout_getBearing(jQuery(this))-c;d=Math.abs(360-d)<Math.abs(0-d)?360-d:0-d;d=d>180?360-d:d;if(d!==0)jQuery(this).roundabout_animateToDelta(d,a,b)});return this};jQuery.fn.roundabout_updateChildPositions=function(){this.each(function(){var c=jQuery(this),a=c.data("roundabout"),d=-1,b={bearing:jQuery.roundabout_getBearing(c),tilt:a.tilt,stage:{width:Math.floor(c.width()*.9),height:Math.floor(c.height()*.9)},animating:a.animating,inFocus:a.childInFocus,focusBearingRad:jQuery.roundabout_degToRad(a.focusBearing),shape:jQuery.roundabout_shape[a.shape]||jQuery.roundabout_shape[jQuery.roundabout_shape.def]};b.midStage={width:b.stage.width/2,height:b.stage.height/2};b.nudge={width:b.midStage.width+b.stage.width*.05,height:b.midStage.height+b.stage.height*.05};b.zValues={min:a.minZ,max:a.maxZ,diff:a.maxZ-a.minZ};b.opacity={min:a.minOpacity,max:a.maxOpacity,diff:a.maxOpacity-a.minOpacity};b.scale={min:a.minScale,max:a.maxScale,diff:a.maxScale-a.minScale};c.children(a.childSelector).each(function(a){if(jQuery.roundabout_updateChildPosition(jQuery(this),c,b,a)&&b.animating===0){d=a;jQuery(this).addClass("roundabout-in-focus")}else jQuery(this).removeClass("roundabout-in-focus")});if(d!==b.inFocus){jQuery.roundabout_triggerEvent(c,b.inFocus,"blur");if(d!==-1)jQuery.roundabout_triggerEvent(c,d,"focus");a.childInFocus=d}});return this};jQuery.roundabout_getBearing=function(a){return jQuery.roundabout_toFloat(a.data("roundabout").bearing)%360};jQuery.roundabout_degToRad=function(a){return a%360*Math.PI/180};jQuery.roundabout_isInFocus=function(b,a){return jQuery.roundabout_getBearing(b)%360===a%360};jQuery.roundabout_triggerEvent=function(b,a,c){return a<0?this:jQuery(b.children(b.data("roundabout").childSelector)[a]).trigger(c)};jQuery.roundabout_toFloat=function(a){a=Math.round(parseFloat(a)*1e3)/1e3;return parseFloat(a.toFixed(2))};jQuery.roundabout_updateChildPosition=function(i,g,b,h){var c=jQuery(i),f=c.data("roundabout"),d=[],e=jQuery.roundabout_degToRad(360-c.data("roundabout").degrees+b.bearing);while(e<0)e=e+Math.PI*2;while(e>Math.PI*2)e=e-Math.PI*2;var a=b.shape(e,b.focusBearingRad,b.tilt);a.scale=a.scale>1?1:a.scale;a.adjustedScale=(b.scale.min+b.scale.diff*a.scale).toFixed(4);a.width=(a.adjustedScale*f.startWidth).toFixed(4);a.height=(a.adjustedScale*f.startHeight).toFixed(4);c.css("left",(a.x*b.midStage.width+b.nudge.width-a.width/2).toFixed(1)+"px").css("top",(a.y*b.midStage.height+b.nudge.height-a.height/2).toFixed(1)+"px").css("width",a.width+"px").css("height",a.height+"px").css("opacity",(b.opacity.min+b.opacity.diff*a.scale).toFixed(2)).css("z-index",Math.round(b.zValues.min+b.zValues.diff*a.z)).css("font-size",(a.adjustedScale*f.startFontSize).toFixed(2)+"px").attr("current-scale",a.adjustedScale);if(g.data("roundabout").debug===true){d.push('<div style="font-weight: normal; font-size: 10px; padding: 2px; width: '+c.css("width")+'; background-color: #ffc;">');d.push('<strong style="font-size: 12px; white-space: nowrap;">Child '+h+"</strong><br />");d.push("<strong>left:</strong> "+c.css("left")+"<br /><strong>top:</strong> "+c.css("top")+"<br />");d.push("<strong>width:</strong> "+c.css("width")+"<br /><strong>opacity:</strong> "+c.css("opacity")+"<br />");d.push("<strong>z-index:</strong> "+c.css("z-index")+"<br /><strong>font-size:</strong> "+c.css("font-size")+"<br />");d.push("<strong>scale:</strong> "+c.attr("current-scale"));d.push("</div>");c.html(d.join(""))}return jQuery.roundabout_isInFocus(g,c.data("roundabout").degrees)}
