/*
 * Author:   Metronet LTD (http://www.metronet.ie)
 * Requires: Mootools 1.3
 */
Mnet.ItemScroller=new Class({Implements:[Options,Events],options:{items:null,layout:"horizontal",direction:"normal",wrap:false,size:null,transition:"pow:out",duration:1000,index:0,autoInit:true},itemCount:0,initialize:function(a){this.setOptions(a);this._valid=false;this._index=null;this._prevIndex=null;if(this.options.autoInit){this.init()}},init:function(){var c=this;if(this.options.layout=="vertical"){this.isHorizontal=false;this.styleSize="height";this.stylePos="top"}else{this.isHorizontal=true;this.styleSize="width";this.stylePos="left"}this.elScrollerItems=$$(this.options.items);this.itemCount=this.elScrollerItems.length;if(this.itemCount>1){var b=this.elScrollerItems[0].parentNode;if(this.options.size>0){this.itemSize=this.options.size}else{this.itemSize=this.elScrollerItems[0].getStyle(this.styleSize).toInt()}var a={position:"absolute"};a[this.stylePos]=0;this.elFrame=new Element("div",{styles:a});this.elFrame.inject(b);this.elFrame.adopt(this.elScrollerItems);if(this.options.wrap){this._wrapBounds={prev:-(this.itemCount-2)*this.itemSize,next:-this.itemSize}}var a={position:"absolute"};this.elScrollerItems.each(function(e,d){a[c.stylePos]=c.itemSize*d+"px";e.setStyles(a)});this._fx=new Fx.Tween(this.elFrame,{duration:Mnet.getDuration(this.options.duration),transition:this.options.transition,link:"cancel",property:this.stylePos});this._valid=true;this.moveTo(this.options.index,{set:true})}this.fireEvent("ready")},moveTo:function(c,b){if(!this._valid){return}var f=c;var a=null;if(c>=this.itemCount){if(this.options.wrap&&b&&b.action=="next"){a=this.itemCount}c=0}else{if(c<0){if(this.options.wrap&&b&&b.action=="prev"){a=-1}c=this.itemCount-1}}if(c==this._index||this._wrapIndex!=null){this._moveToEnd(true);return}var e=c;if(a!=null){var d=this.elFrame.getStyle(this.stylePos).toInt();if(a==-1){if(d<this._wrapBounds.prev){this._moveToEnd(true);return}this.elScrollerItems[this.itemCount-1].setStyle(this.stylePos,-this.itemSize)}else{if(d>this._wrapBounds.next){this._moveToEnd(true);return}this.elScrollerItems[0].setStyle(this.stylePos,this.itemCount*this.itemSize)}this._wrapIndex=a;e=a}var g=-(this.itemSize*e);this._fx.cancel();if(b&&b.set){this._fx.set(g);this._updateIndex(c);this._moveToEnd()}else{this._fx.chain(this._moveToEnd.bind(this));this._fx.start(g);this._updateIndex(c)}this.fireEvent("moveTo",[f,b])},_moveToEnd:function(b){if(!b){if(this._wrapIndex!=null){if(this._wrapIndex==-1){this.elScrollerItems[this.itemCount-1].setStyle(this.stylePos,(this.itemCount-1)*this.itemSize);this._fx.set(-(this.itemCount-1)*this.itemSize)}else{this.elScrollerItems[0].setStyle(this.stylePos,0);this._fx.set(0)}this._wrapIndex=null}}var a=this._prevIndex;this._prevIndex=this._index;this.fireEvent("changed",[this._index,a])},_updateIndex:function(a){this._index=a;this.fireEvent("change",[a,this._prevIndex])},getIndex:function(){return this._index},move:function(a){if(this.options.direction=="reverse"){a=-a}this.moveTo(this._index+a,{action:(a>0)?"next":"prev"})},prev:function(){this.move(-1)},next:function(){this.move(1)},unload:function(){if(this._fx){this._fx.cancel();this._fx=null}this.removeEvents();this.elScrollerItems=null;this.elFrame=null}});
