(function(b){b.widget("iOffer.carousel",{options:{btnNext:".next",btnPrev:".previous",element:".element",disabled:"disabled",orientation:"vertical",rows:2,perPage:5,total:null,speed:750,easing:null,url:null,data:{},tracking:null,method:"get",dataType:"html"},_create:function(){this.elements=this.element.find(this.options.element);this.btnNext=b(this.options.btnNext);this.btnPrev=b(this.options.btnPrev);this.perPage=this.options.perPage;this.total=this.options.total||this.elements.length;this.page=
1;this.populated=[];this.locked=!1;this.speed=this.options.speed;this.easing=this.options.easing;this.method=this.options.method;this.url=this.options.url;this.data=this.options.data;this.disabled=this.options.disabled;this.dataType=this.options.dataType;this.tracking=this.options.tracking;this._updateCss();this._attachEvents();this._updateRows();this._appendRow(this.elements);this._prePopulate();this._updateButtons()},_next:function(){this.page!=this.pages&&this._obtainLock()&&(this._visited(this.page+=
1)?this._update():this._populate())},_previous:function(){this.page>1&&this._obtainLock()&&(this._visited(this.page-=1)?this._update():this._populate())},_visited:function(a){return b.inArray(a,this.populated)>=0},_populate:function(){this._trigger("beforePopulate");var a=b.extend({},{limit:this.perPage,page:this.page},this.data);b.ajax({url:this.url,type:this.method,data:a,dataType:this.dataType,success:function(a){this._appendRow(a);this.elements=this.element.find(this.options.element);this.populated.push(this.page);
this._trigger("afterPopulate");this._update()}.bind(this)})},_update:function(){this._updateButtons();var a={};if(this.options.orientation=="horizontal")this.wrapper.width(this.dimensions.width*this.populated.length),a.scrollLeft=this.dimensions.width*(this.page-1);if(this.options.orientation=="vertical")a.scrollTop=this.dimensions.height*(this.page-1);this.element.animate(a,this.speed,this.easing,function(){this._releaseLock()}.bind(this))},_updateButtons:function(){this.pages==1?(this.btnNext.hide(),
this.btnPrev.hide()):(this.page==1&&this.btnPrev.addClass(this.disabled),this.page==this.pages&&this.btnNext.addClass(this.disabled),this.page<this.pages&&this.pages>this.page&&this.btnNext.removeClass(this.disabled),this.page>1&&this.page<this.pages&&this.btnPrev.removeClass(this.disabled))},_obtainLock:function(){return this.locked==!0?!1:this.locked=!0},_releaseLock:function(){this.locked=!1},_prePopulate:function(){var a=Math.floor(this.elements.length/this.perPage);a<=0&&(a=1);b(Array(a)).each(function(a){this.populated.push(a+
1)}.bind(this))},_attachEvents:function(){this.btnNext.click(function(a){a.preventDefault();this._next()}.bind(this));this.btnPrev.click(function(a){a.preventDefault();this._previous()}.bind(this));this.element.click(function(a){var c=b(a.target).closest("a");c.length>0&&this._trigger("selected",a,{element:c})}.bind(this))},_appendRow:function(a){b('<div class="carousel-row"/>').css({position:"relative"}).html(a).appendTo(this.wrapper).css(this.dimensions)},_updateCss:function(){this.element.css({overflow:"hidden",
position:"relative"});if(this.options.orientation=="vertical")this.wrapper=this.element;if(this.options.orientation=="horizontal")this.element.wrapInner('<div class="carousel-wrapper"/>'),this.wrapper=this.element.children();this.element.addClass("carousel-container")},_setDimensions:function(a){this.dimensions=b.extend({width:Math.floor(this.element.width()-1),height:Math.floor(this.element.height())},a);this.element.add(this.element.find(".carousel-row")).css(this.dimensions);this._trigger("resize",
jQuery.Event("resize"),this.dimensions)},_updateRows:function(){var a={};if(this.options.orientation=="horizontal"){this.element.add(this.element.find(".carousel-row")).css({width:""});var b=this.elements.outerWidth(),d=Math.floor(this.element.width()/b);a.width=b*d;this.perPage=this.options.rows*d;this.elements.filter(":gt("+(this.perPage-1)+")").remove();this.elements=this.element.find(this.options.element);this.populated.length=0}this.pages=Math.ceil(this.total/this.perPage);this._setDimensions(a)}})})(jQuery);

