/*
 * jQuery rondell plugin
 * @name jquery.rondell.js
 * @author Sebastian Helzle
 * @version 0.6
 * @date Feb 2, 2010
 * @category jQuery plugin
 * @copyright (c) 2010 Sebastian Helzle (www.sebastianhelzle.de)
 * @license CC Attribution 3.0 - http://creativecommons.org/licenses/by/3.0/deed.en
 */
jQuery.rondell={name:"rondell",layer_count:0,current_layer:1,radius:{x:300,y:300},center:{left:400,top:350},controls_margin:{x:20,y:20},size:{width:undefined,height:undefined},visible_items:"auto",scaling:2,opacity_min:0.1,fade_time:300,item_delay:100,item_class:"rondell_item",resizeable_class:"resizeable",small_class:"item_small",hidden_class:"item_hidden",item_size:{width:150,height:150},item_size_focus:{width:0,height:0},item_top_margin:20,items:[],repeating:true,auto_rotation:false,auto_rotation_paused:false,auto_rotation_timer:-1,auto_rotation_direction:0,auto_rotation_once:false,auto_rotation_delay:5000,controls_enabled:true,controls_fade_time:500,strings:{prev:"prev",next:"next"},func_left:function(A,B){return B.center.left-B.item_size.width/2+Math.sin(A)*B.radius.x;},func_top:function(A,B){return B.center.top-B.item_size.height/2+Math.cos(A)*B.radius.y;},func_diff:function(A,B){return Math.pow(Math.abs(A)/B.layer_count,0.5)*Math.PI;},func_ease:function(B,C,A,E,D){if((C/=D/2)<1){return E/2*C*C+A;}return -E/2*((--C)*(C-2)-1)+A;},func_opacity:function(A,B){return B.opacity_min+(1-B.opacity_min)*(1-Math.pow(A/B.visible_items,2));},event:{layer_fadein:function(C,A){var B=A.items[C];B.small=false;B.object.animate({width:B.size_focus.width+"px",height:B.size_focus.height+"px",left:A.center.left-B.size_focus.width/2+"px",top:A.center.top-B.size_focus.height/2+"px",opacity:"1.0"},A.fade_time,A.func_ease,A.event.auto_shift(A)).css("z-index",6000).addClass("rondell_item_focused");if(B.icon&&!B.resizeable){B.icon.animate({marginTop:A.item_top_margin+(A.item_size.height-B.icon.height())/2+"px"},A.fade_time);}},layer_fadeout:function(F,C){var D=C.items[F];var B=Math.abs(F-C.current_layer);if(B>C.visible_items&&C.repeating){if(C.current_layer+C.visible_items>C.layer_count){F+=C.layer_count;}else{if(C.current_layer-C.visible_items<=C.layer_count){F-=C.layer_count;}}B=Math.abs(F-C.current_layer);}var A=C.func_diff(F-C.current_layer,C);if(F<C.current_layer){A*=-1;}if(B<=C.visible_items){D.object.animate({width:D.size_small.width+"px",height:D.size_small.height+"px",left:C.func_left(A,C)+(C.item_size.width-D.size_small.width)/2+"px",top:C.func_top(A,C)+(C.item_size.height-D.size_small.height)/2+"px",opacity:C.opacity_min!=1?C.func_opacity(B,C):1},C.fade_time+C.item_delay*B,C.func_ease);if(D.hidden){D.hidden=false;}if(!D.small){D.small=true;if(!D.resizeable){var E=(C.item_size.height-D.icon.height())/2+"px";D.icon.animate({marginTop:E,marginBottom:E},C.fade_time);}}D.object.css("z-index",A<0?5000+F:5000-F).removeClass("rondell_item_focused");}else{if(!D.hidden){D.hidden=true;D.object.fadeTo(C.fade_time/2+C.item_delay*B,0);}}},shift_to:function(C,A){if((C>0&&C<=A.layer_count)||A.repeating){if(C<1){C=A.layer_count;}else{if(C>A.layer_count){C=1;}}A.current_layer=C;for(var B=1;B<=A.layer_count;B++){if(B!=A.current_layer){A.event.layer_fadeout(B,A);}}A.event.layer_fadein(A.current_layer,A);}},shift_left:function(A){A.event.shift_to(A.current_layer-1,A);},shift_right:function(A){A.event.shift_to(A.current_layer+1,A);},auto_shift:function(A){if(A.auto_rotation&&A.auto_rotation_timer==-1){A.auto_rotation_timer=window.setTimeout(function(){A.auto_rotation_timer=-1;if(!A.auto_rotation_paused){if(A.auto_rotation_direction==0){A.event.shift_left(A);}else{A.event.shift_right(A);}}},A.auto_rotation_delay);}},key_down:function(B,A){switch(B){case 37:A.event.shift_left(A);break;case 39:A.event.shift_right(A);}}}};jQuery.fn.rondell=function(D){D=jQuery.extend(jQuery.rondell,D||{});this.each(function(){obj=jQuery(this);D.layer_count+=1;var L=D.layer_count;icon=obj.find("img:first");var M=icon.hasClass(D.resizeable_class);var I=icon.width();var K=icon.height();var N=I;var J=K;if(M){if(I>=K){K*=D.item_size.width/I;J*=D.item_size_focus.width/N;I=D.item_size.width;N=D.item_size_focus.width;}else{I*=D.item_size.height/K;N*=D.item_size_focus.height/J;K=D.item_size.height;J=D.item_size_focus.height;}}else{I=D.item_size.width;K=D.item_size.height;N=D.item_size_focus.width>0?D.item_size_focus.width:I*D.scaling;J=D.item_size_focus.height>0?D.item_size_focus.height:K*D.scaling;}D.items[L]={object:obj,icon:icon,small:false,hidden:false,resizeable:M,size_small:{width:I,height:K},size_focus:{width:N,height:J}};obj.addClass(D.item_class).click(function(){if(D.current_layer!=L){D.event.shift_to(L,D);return false;}});});this.wrapAll('<div class="rondell_container" style="overflow:hidden; width: '+(D.size.width||(D.center.left+1.5*D.item_size.width+D.radius.x/2))+"px; display:block; position:relative; height: "+(D.size.height||(D.center.top+D.item_size.height+D.radius.y/2))+'px"></div>');var A=this.parent();A.hover(function(){if(!D.auto_rotation_paused){D.auto_rotation_paused=true;}},function(){if(D.auto_rotation_paused&&!D.auto_rotation_once){D.auto_rotation_paused=false;D.event.auto_shift(D);}});if(D.controls_enabled){A.append('<div class="rondell_controls"><a href="/" class="rondell_control rondell_shift_left">'+D.strings.prev+'</a><a href="/" class="rondell_control rondell_shift_right">'+D.strings.next+"</a></div>");var B=A.children(".rondell_controls").hover(function(){B.children().stop().fadeTo(D.controls_fade_time,1);},function(){B.children().stop().fadeTo(D.controls_fade_time,0);});B.children().fadeTo(0,0);var H=B.children(".rondell_shift_left");var G=B.children(".rondell_shift_right");var F=D.item_size_focus.width>0?D.item_size_focus.width:D.item_size.width*D.scaling;var C=D.item_size_focus.height>0?D.item_size_focus.height:D.item_size.height*D.scaling;B.css({left:D.center.left-F/2+D.controls_margin.x+"px",top:D.center.top-C/2+D.controls_margin.y+"px",width:D.item_size.width*D.scaling-D.controls_margin.x*2+"px"});H.click(function(){D.event.shift_left(D);return false;});G.click(function(){D.event.shift_right(D);return false;});}jQuery(document).keydown(function(I){D.event.key_down(I.which,D);});D.current_layer=Math.floor(D.layer_count/2);if(D.visible_items=="auto"){D.visible_items=D.current_layer;}for(var E=1;E<=D.layer_count;E++){D.event.layer_fadeout(E,D);}D.event.layer_fadein(D.current_layer,D);return jQuery(this);};
