(function($){$.movingBoxes=function(el,options){var o,base=this;base.$el=$(el).addClass('mb-slider');base.el=el;base.$el.data('movingBoxes',base);base.init=function(){base.options=o=$.extend({},$.movingBoxes.defaultOptions,options);base.$el.wrap('<div class="movingBoxes mb-wrapper"><div class="mb-scroll" /></div>');base.$window=base.$el.parent();base.$wrap=base.$window.parent().css({width:o.width}).prepend('<a class="mb-scrollButtons mb-left"></a>').append('<a class="mb-scrollButtons mb-right"></a><div class="mb-left-shadow"></div><div class="mb-right-shadow"></div>');base.$panels=base.$el.children().addClass('mb-panel');base.runTime=$('.mb-slider').index(base.$el)+1;base.regex=new RegExp('slider'+base.runTime+'=(\\d+)','i');base.initialized=false;base.currentlyMoving=false;base.curPanel=1;base.$left=base.$wrap.find('.mb-left').click(function(){base.goBack();return false;});base.$right=base.$wrap.find('.mb-right').click(function(){base.goForward();return false;});setInterval(function(){$('.mb-right').click();},4000);base.update();$(window).load(function(){base.update(false);});base.$el.delegate('.mb-panel','click',function(){base.change(base.$panels.index($(this))+base.adj);});base.$wrap.click(function(){base.active();});base.$panels.delegate('a','focus',function(){var loc=base.$panels.index($(this).closest('.mb-panel'))+1;if(loc!==base.curPanel){base.change(base.$panels.index($(this).closest('.mb-panel'))+1,{},false);}});$(document).keyup(function(e){if(e.target.tagName.match('TEXTAREA|INPUT|SELECT')){return;}
switch(e.which){case 39:case 32:if(base.$wrap.is('.mb-active-slider')){base.goForward();}
break;case 37:if(base.$wrap.is('.mb-active-slider')){base.goBack();}
break;}});var startPanel=(o.hashTags)?base.getHash()||o.startPanel:o.startPanel;$.each('initialized initChange beforeAnimation completed'.split(' '),function(i,evt){if($.isFunction(o[evt])){base.$el.bind(evt+'.movingBoxes',o[evt]);}});setTimeout(function(){base.change(startPanel,function(){base.initialized=true;base.$el.trigger('initialized.movingBoxes',[base,startPanel]);});},o.speed*2);};base.update=function(flag){var t;base.$el.children('.cloned').remove();base.$panels=base.$el.children();base.adj=(o.wrap&&base.$panels.length>1)?0:1;if(o.wrap&&base.$panels.length>1){base.$el.prepend(base.$panels.filter(':last').clone().removeAttr('id').addClass('cloned'));base.$el.append(base.$panels.filter(':first').clone().removeAttr('id').addClass('cloned'));base.$el.find('.cloned').each(function(){$(this).find('a,input,textarea,select,button,area').attr('disabled','disabled');$(this).find('[id]').removeAttr('id');});}
base.$panels=base.$el.children().addClass('mb-panel').css({width:o.width*o.panelWidth,margin:0}).each(function(){if($(this).find('.mb-inside').length===0){$(this).wrapInner('<div class="mb-inside" />');}});base.totalPanels=base.$panels.length;base.totalPanels-=(o.wrap&&base.totalPanels>1)?2:0;t=base.$panels.eq(base.curPanel-base.adj);base.curWidth=base.curWidth||t.outerWidth();base.regWidth=base.curWidth*o.reducedSize;base.$panels.css({width:base.curWidth,fontSize:'1em'});base.$panels.eq(base.curPanel-base.adj).addClass(o.currentPanel);base.heights=base.$panels.map(function(i,e){return $(e).outerHeight(true);}).get();base.returnToNormal(base.curPanel,0);base.growBigger(base.curPanel,0,flag);base.updateArrows(base.curPanel);base.$el.css({position:'absolute',width:(base.curWidth+100)*base.$panels.length+(o.width-base.curWidth)/2,height:Math.max.apply(this,base.heights)+10});base.$window.css({height:(o.fixedHeight)?Math.max.apply(this,base.heights):base.heights[base.curPanel-base.adj]});base.$panels.eq(0).css({'margin-left':(o.width-base.curWidth)/2});base.buildNav();base.change(base.curPanel,null,true);};base.buildNav=function(){if(base.$nav){base.$nav.remove();}
if(o.buildNav&&base.totalPanels>1){base.$nav=$('<div class="mb-controls"><a class="mb-testing"></a></div>').appendTo(base.$wrap);var j,a='',navFormat=$.isFunction(o.navFormatter),hiddenText=parseInt(base.$nav.find('.mb-testing').css('text-indent'),10)<0;base.$panels.filter(':not(.cloned)').each(function(i){j=i+1;a+='<a href="#" class="mb-panel'+j;if(navFormat){var tmp=o.navFormatter(j,$(this));a+=(hiddenText)?' '+o.tooltipClass+'" title="'+tmp:'';a+='">'+tmp+'</a> ';}else{a+='">'+j+'</a> ';}});base.$nav.html(a).find('a').bind('click',function(){base.change($(this).index()+1);return false;});}};base.returnToNormal=function(num,time){var panels=base.$panels.not(':eq('+(num-base.adj)+')').removeClass(o.currentPanel);if(o.reducedSize===1){panels.css({width:base.regWidth});}else{panels.animate({width:base.regWidth,fontSize:o.reducedSize+'em'},(time===0)?0:o.speed);}};base.growBigger=function(num,time,flag){var panels=base.$panels.eq(num-base.adj);if(o.reducedSize===1){panels.css({width:base.curWidth});if(base.initialized){base.completed(num,flag);}}else{panels.animate({width:base.curWidth,fontSize:'1em'},(time===0)?0:o.speed,function(){if(base.initialized){base.completed(num,flag);}});}};base.completed=function(num,flag){base.$panels.eq(num-base.adj).addClass(o.currentPanel);if(flag!==false){base.$el.trigger('completed.movingBoxes',[base,num]);}};base.goForward=function(){if(base.initialized){base.change(base.curPanel+1);}};base.goBack=function(){if(base.initialized){base.change(base.curPanel-1);}};base.change=function(curPanel,callback,flag){if(base.totalPanels<1){if(typeof(callback)==='function'){callback(base);}
return;}
var ani,leftValue,wrapped=false;curPanel=parseInt(curPanel,10);if(base.initialized){base.active();base.$el.trigger('initChange.movingBoxes',[base,curPanel]);}
if(o.wrap){if(curPanel>base.totalPanels){wrapped=true;curPanel=1;base.returnToNormal(0,0);base.growBigger(0,0,false);leftValue=base.$panels.eq(0).position().left-(o.width-base.curWidth)/2;base.$window.scrollLeft(leftValue);}else if(curPanel===0){wrapped=false;curPanel=base.totalPanels;base.growBigger(curPanel+1,0,false);leftValue=base.$panels.eq(curPanel+1).position().left-(o.width-base.curWidth)/2;base.$window.scrollLeft(leftValue);}}
if(curPanel<base.adj){curPanel=(o.wrap)?base.totalPanels:1;}
if(curPanel>base.totalPanels-base.adj){curPanel=(o.wrap)?1:base.totalPanels;}
if(base.initialized&&base.curPanel===curPanel&&!flag){return false;}
if(!base.currentlyMoving||!base.initialized){base.currentlyMoving=true;leftValue=base.$panels.eq(curPanel-base.adj).position().left-(o.width-base.curWidth)/2;if(curPanel>base.curPanel||wrapped){leftValue-=(base.curWidth-base.regWidth);}
ani=(o.fixedHeight)?{scrollLeft:leftValue}:{scrollLeft:leftValue,height:base.heights[curPanel-base.adj]};if(base.initialized){base.$el.trigger('beforeAnimation.movingBoxes',[base,curPanel]);}
base.$window.animate(ani,{queue:false,duration:o.speed,easing:o.easing,complete:function(){base.curPanel=curPanel;if(base.initialized){base.$window.scrollTop(0);}
base.currentlyMoving=false;if(typeof(callback)==='function'){callback(base);}}});base.returnToNormal(curPanel);base.growBigger(curPanel);if(!o.wrap){base.updateArrows(curPanel);}
if(o.hashTags&&base.initialized){base.setHash(curPanel);}}
if(o.buildNav&&base.$nav){base.$nav.find('a').removeClass(o.currentPanel).eq(curPanel-1).addClass(o.currentPanel);}};base.updateArrows=function(cur){base.$left.toggleClass(o.disabled,cur===base.adj);base.$right.toggleClass(o.disabled,(cur===base.totalPanels||base.totalPanels===0));};base.getHash=function(){var n=window.location.hash.match(base.regex);return(n===null)?'':parseInt(n[1],10);};base.setHash=function(n){var s='slider'+base.runTime+"=",h=window.location.hash;if(typeof h!=='undefined'){window.location.hash=(h.indexOf(s)>0)?h.replace(base.regex,s+n):h+"&"+s+n;}};base.active=function(el){$('.mb-active-slider').removeClass('mb-active-slider');base.$wrap.addClass('mb-active-slider');};base.currentPanel=function(panel,callback){if(typeof(panel)!=='undefined'){base.change(panel,callback);}
return base.curPanel;};base.init();};$.movingBoxes.defaultOptions={startPanel:1,width:800,panelWidth:0.5,reducedSize:0.8,fixedHeight:false,speed:500,hashTags:true,wrap:false,buildNav:false,navFormatter:null,easing:'swing',currentPanel:'current',tooltipClass:'tooltip',disabled:'disabled',initialized:null,initChange:null,beforeAnimation:null,completed:null};$.fn.movingBoxes=function(options,callback){var num,mb;return this.each(function(){mb=$(this).data('movingBoxes');if((typeof(options)).match('object|undefined')){if(mb){mb.update();}else{(new $.movingBoxes(this,options));}}else if(/\d/.test(options)&&!isNaN(options)&&mb){num=(typeof(options)==="number")?options:parseInt($.trim(options),10);if(num>=1&&num<=mb.totalPanels){mb.change(num,callback);}}});};$.fn.getMovingBoxes=function(){return this.data('movingBoxes');};})(jQuery);
