
(function($){$.fn.flow=function(options){var defaults={flowIdSuffix:'',onstart:false,onselect:false,oninit:false,onposition:false,flowWidth:780,flowHeight:250,itemWidth:200,itemHeight:200,itemPadding:3,itemBorder:0,itemBottom:0,itemBottomMove:50,fontSizes:{h3:12,p:11},bottomMargins:{img:12,h3:6,p:6},itemPositions:[0,32,40,46,50],minFontSize:9,selectedIndex:0,itemZindex:5000,runner:false,preloading:false}
return this.each(function(){$(this).data('flow',$.extend(defaults,options));$.flow.init(this);});};jQuery.flow={init:function(el){el.opt=$(el).data('flow');el.opt.container=$('<div></div>').attr('id','flow-container'+el.opt.flowIdSuffix).css({width:el.opt.flowWidth+'px',height:el.opt.flowHeight+'px',position:'relative'}).addClass('flow-container');$(el).before(el.opt.container).appendTo(el.opt.container);var domRefParent=$(el).parent().get(0);if(domRefParent.addEventListener){domRefParent.addEventListener('DOMMouseScroll',function(e){$.flow.handle_wheel(e,el)},false);domRefParent.addEventListener('mousewheel',function(e){$.flow.handle_wheel(e,el)},false);}
else if(domRefParent.attachEvent){domRefParent.attachEvent('onmousewheel',function(){$.flow.handle_wheel(false,el)});}
else{domRefParent.onmousewheel=function(e){$.flow.handle_wheel(e,el);}}
this.list_items(el,el.opt.selectedIndex);},list_items:function(el,selectedIndex){var images;var obj=this;var cnt=(typeof arguments[2]!=="undefined")?arguments[2]+1:0;el.opt=$(el).data('flow');el.opt.items=[];if(el.opt.preloading){clearTimeout(el.opt.preloading);el.opt.preloading=false;}
$(el).find('li:visible').each(function(i){images=[];$(this).find("img").each(function(j){$(this).css({'width':'','height':''});images[j]={img:this,wd:$(this).width(),ht:$(this).height()}});try{if(images[0]["wd"]<1){if(cnt<5){el.opt.preloading=setTimeout(function(){obj.list_items(el,selectedIndex,cnt);},2000);return false;}else{}}}catch(err){}
el.opt.items[i]={images:images,li:this,curr:50};this.index=i;this.el=el;$(this).unbind('click').click(function(){$.flow.handle_click(this);});});if(el.opt.preloading){return;}
$(el).find('li:visible').css({'position':'absolute','overflow':'hidden','margin':'0','visibility':'visible','padding':el.opt.itemPadding+'px'});el.opt.selectedIndex=(selectedIndex<el.opt.items.length)?selectedIndex:0;if(el.opt.oninit){el.opt.oninit(el.opt.items);}
if(el.opt.items.length){this.init_anim(el,false);}},handle_wheel:function(event,el){event=event?event:window.event;var delta=0;if(event.wheelDelta){delta=event.wheelDelta/120;}else if(event.detail){delta=-event.detail/3;}
if(delta>0){this.init_anim(el,'next');}else{this.init_anim(el,'back');}
this.suppress_browser_default(event);},init_anim:function(el,dir){el.opt=$(el).data('flow');var past_index=el.opt.selectedIndex;clearTimeout(el.opt.runner);switch(dir){case"next":if(el.opt.selectedIndex+1<el.opt.items.length){el.opt.selectedIndex++;}
break;case"back":if(el.opt.selectedIndex>0){el.opt.selectedIndex--;}
break;case false:if(el.opt.selectedIndex+1>el.opt.items.length){el.opt.selectedIndex=0;}
break;default:if(dir<el.opt.items.length){el.opt.selectedIndex=dir;}else{el.opt.selectedIndex=0;}}
if(el.opt.onstart){el.opt.onstart(el.opt.items[el.opt.selectedIndex].li,el.opt.items[past_index].li);}
this.do_anim(el);},count_move:function(el,curr){el.opt=$(el).data('flow');var i,index=el.opt.selectedIndex;var neighbourhood,neighbourpos,neighbourhood_abs,neighbourhood_lr,nothing_to_do;for(i=0;i<el.opt.items.length;i++){nothing_to_do=false;neighbourhood=index-i;neighbourhood_abs=Math.abs(neighbourhood);neighbourhood_lr=(neighbourhood>0)?-1:1;if(neighbourhood_abs<el.opt.itemPositions.length){neighbourpos=el.opt.itemPositions[neighbourhood_abs]*neighbourhood_lr;}else{neighbourpos=el.opt.itemPositions[el.opt.itemPositions.length-1]*neighbourhood_lr;}
_curr=curr+neighbourpos;if((curr>50&&_curr>el.opt.items[i].curr)||(curr<50&&_curr<el.opt.items[i].curr)){_curr=el.opt.items[i].curr;nothing_to_do=true;}
$(el.opt.items[i].li).css('zIndex',Math.floor(el.opt.itemZindex-Math.abs(50-_curr)-neighbourhood_abs));if(nothing_to_do){continue;}
el.opt.items[i].curr=_curr;this.create_item_css(el,i,_curr);}},do_anim:function(el,index){el.opt=$(el).data('flow');var index=el.opt.selectedIndex;var curr=el.opt.items[index].curr;var to=50;var dst=Math.abs(to-curr);if(dst>0.2){curr=curr+((to-curr)/3);this.count_move(el,curr);el.opt.runner=setTimeout(function(){$.flow.do_anim(el,index);},20);}else{this.count_move(el,50);if(el.opt.onposition){el.opt.onposition(el.opt.items[index].li);}}},create_item_css:function(el,i,_curr){el.opt=$(el).data('flow');var x=(Math.round(50-_curr)*10)*0.01;var f=1-1/60*(x*x);var current_width=Math.floor(el.opt.itemWidth*f);var current_height=Math.floor(el.opt.itemHeight*f);var current_padding=el.opt.itemPadding;var current_outer=current_width+current_padding+(2*el.opt.itemBorder);var current_left=(el.opt.flowWidth-current_outer)*0.01*_curr;var current_botmove=(el.opt.itemBottomMove+el.opt.itemBottom)-(el.opt.itemBottomMove*f);$(el.opt.items[i].li).css({width:current_width+'px',height:current_height+'px',left:current_left+'px',bottom:current_botmove+'px'});var current_img_width,current_img_height;for(var j=0;j<el.opt.items[i].images.length;j++){current_img_width=Math.floor(el.opt.items[i].images[j].wd*f);current_img_height=Math.floor(el.opt.items[i].images[j].ht*f);$(el.opt.items[i].images[j].img).css({width:current_img_width+'px',height:current_img_height+'px'});}
var current_fontsize;for(var k in el.opt.fontSizes){$(el.opt.items[i].li).find(k).each(function(){current_fontsize=el.opt.fontSizes[k]*f;if(current_fontsize<el.opt.minFontSize){$(this).css('display','none');}else{$(this).css('fontSize',current_fontsize+'px').css('display','');}});}
var current_bottommargin;for(var l in el.opt.bottomMargins){$(el.opt.items[i].li).find(l).each(function(){current_bottommargin=Math.floor(el.opt.bottomMargins[l]*f);$(this).css('marginBottom',current_bottommargin+'px');});}},handle_click:function(item){var el,item;el=item.el;el.opt=$(el).data('flow');if(item.index===el.opt.selectedIndex){if(!el.opt.onselect)return;el.opt.onselect(item);}else{this.init_anim(el,item.index);}},suppress_browser_default:function(e){if(e.preventDefault){e.preventDefault();}else{e.returnValue=false;}
return false;}}})(jQuery);
