/**
 * RokVirtuemartScroller - A VirtueMart Class that horizontally scrolls the content using left and right arrows, with ability to set the amount of pixel to skip by every click.
 * 
 * @version		1.0
 * 
 * @author		Djamil Legato <djamil@rockettheme.com>
 * @copyright	Andy Miller @ Rockettheme, LLC
 */

var RokVirtuemartScroller=new Class({version:'1.0',options:{'direction':'horizontal','height':300,'scroll':{'duration':500,'amount':200,'transition':Fx.Transitions.Quad.easeOut},'arrows':{'effect':false,'color':'auto','align':'center'},'autoplay':{'enabled':false,'delay':2}},initialize:function(el,options){this.element=$(el);if(!this.element)return this;this.setOptions(options);this.container=new Element('div',{'class':this.options.direction+'-rokvmscroller-container'}).inject(this.element,'before').setStyle('position','relative');this.wrapper=new Element('div',{'class':this.options.direction+'-rokvmscroller-wrapper'}).inject(this.container).adopt(this.element);if(window.ie6)this.wrapper.setStyle('width',this.element.getCoordinates().width);var wrapperWidth=this.element.getCoordinates().width;this.elements=this.element.getChildren().filter(function(div){return div.getTag()=='div'});this.elements[0].addClass('first');this.elements[this.elements.length-1].addClass('last');this.size=this.getSize(this.elements);this.current=0;this.timer=null;this.linkColor=(this.options.arrows.color!='auto')?this.options.arrows.color:$$('a')[0].getStyle('color');if(this.options.direction=='vertical'){var wrapperSize=this.wrapper.getStyle('height').toInt();this.element.getParent().setStyles({'height':this.options.height})}else{var wrapperSize=this.wrapper.getStyle('width').toInt();this.element.setStyles({'width':this.size.x})}if(this.size.x>wrapperSize||this.size.y>wrapperSize){this.scroller=new Fx.Scroll(this.wrapper,{'wheelStops':false,'duration':this.options.scroll.duration,'transition':this.options.scroll.transition,'wait':false}).set([0,0]);this.makeArrows()}else{this.container.getParent('.'+this.options.direction+'-scroller-bottom').addClass('noarrows')}if(this.options.autoplay.enabled){this.start(this.options.autoplay.delay,'next');var parent=this.element.getParent().getParent().getParent();parent.addEvents({'mouseenter':function(){this.stop()}.bind(this),'mouseleave':function(){this.start(this.options.autoplay.delay,'next')}.bind(this)},this)}return this},makeArrows:function(){this.leftArrow=new Element('div',{'class':this.options.direction+'-rokvmscroller-leftarrow png'}).inject(this.container,'before');this.rightArrow=new Element('div',{'class':this.options.direction+'-rokvmscroller-rightarrow png'}).inject(this.container,'after');if(window.ie6){this.leftArrow.inject(this.container.getParent(),'top');this.rightArrow.inject(this.container.getParent())};var arrows=$$(this.leftArrow,this.rightArrow);if(this.options.direction!='vertical'){arrows.setStyles({'position':'absolute','top':0,'cursor':'pointer'})}if(this.options.arrows.align=='center'){var wrapperHeight=this.wrapper.getCoordinates().height;var leftHeight=this.leftArrow.getCoordinates().height;var rightHeight=this.rightArrow.getCoordinates().height;var substract={'left':(wrapperHeight-leftHeight)/2,'right':(wrapperHeight-rightHeight)/2};this.leftArrow.setStyle('top',substract.left);this.rightArrow.setStyle('top',substract.right)}else if(this.options.arrows.align=='bottom'){arrows.setStyles({'top':'','bottom':0})};this.leftArrow.setStyle('left',0);this.rightArrow.setStyle('right',0);var opacity=this.options.arrows.opacity,scroller=this.scroller;if(this.options.arrows.effect){arrows.each(function(arrow){var current=arrow.getStyle('background-color');if(current=='transparent')current='#ffffff';var fx=new Fx.Style(arrow,'background-color',{duration:350,transition:Fx.Transitions.Quad.easeIn,wait:false}).set(current);arrow.addEvents({'mouseenter':function(){fx.start(this.linkColor)}.bind(this),'mouseleave':function(){fx.start(current)}.bind(this)})},this)}if(this.options.direction=='vertical'){arrows.each(function(arrow){arrow.addEvent('click',function(){var y=scroller.now[1];if(arrow.hasClass(this.options.direction+'-rokvmscroller-rightarrow')){var size=this.wrapper.getSize();var amount=this.wrapper.getSize().scroll.y+this.options.scroll.amount;if(amount>(size.scrollSize.y-size.size.y)||(amount+this.options.scroll.amount)>=(size.scrollSize.y-size.size.y))amount=size.scrollSize.y-size.size.y;scroller.scrollTo(false,amount)}else{var size=this.wrapper.getSize();var amount=this.wrapper.getSize().scroll.y-this.options.scroll.amount;if(amount<0||(amount-this.options.scroll.amount)<0)amount=0;scroller.scrollTo(false,amount)}}.bind(this))},this)}else{arrows.each(function(arrow){arrow.addEvent('click',function(){var x=scroller.now[0];if(arrow.hasClass(this.options.direction+'-rokvmscroller-rightarrow')){var size=this.wrapper.getSize();var amount=this.wrapper.getSize().scroll.x+this.options.scroll.amount;if(amount>(size.scrollSize.x-size.size.x)||(amount+this.options.scroll.amount)>=(size.scrollSize.x-size.size.x))amount=size.scrollSize.x-size.size.x;scroller.scrollTo(amount,false)}else{var size=this.wrapper.getSize();var amount=this.wrapper.getSize().scroll.x-this.options.scroll.amount;if(amount<0||(amount-this.options.scroll.amount)<0)amount=0;scroller.scrollTo(amount,false)}}.bind(this))},this)}},start:function(delay,direction){$clear(this.timer);this.timer=this[direction].periodical(delay*1000,this,delay)},stop:function(){$clear(this.timer);return this},next:function(delay){var size=this.wrapper.getSize();if(this.options.direction=='vertical'){if((size.scroll.y+this.options.scroll.amount)>=(size.scrollSize.y-size.size.y)){this.stop().start(delay,'previous')}else this.rightArrow.fireEvent('click')}else{if((size.scroll.x+this.options.scroll.amount)>=(size.scrollSize.x-size.size.x)){this.stop().start(delay,'previous')}else this.rightArrow.fireEvent('click')}},previous:function(delay){var size=this.wrapper.getSize();if(this.options.direction=='vertical'){if((size.scroll.y-this.options.scroll.amount)<=0){this.stop().start(delay,'next')}else this.leftArrow.fireEvent('click')}else{if((size.scroll.x-this.options.scroll.amount)<=0){this.stop().start(delay,'next')}else this.leftArrow.fireEvent('click')}},getSize:function(els){var size={'x':0,'y':0};els.each(function(el,i){var tmp=el.getSize().size;var padding={'x':el.getStyle('padding-left').toInt()+el.getStyle('padding-right').toInt(),'y':el.getStyle('padding-top').toInt()+el.getStyle('padding-bottom').toInt()};var margin={'x':el.getStyle('margin-left').toInt()+el.getStyle('margin-right').toInt(),'y':el.getStyle('margin-top').toInt()+el.getStyle('margin-bottom').toInt()};var border={'x':el.getStyle('border-left-width').toInt()+el.getStyle('border-right-width').toInt(),'y':el.getStyle('border-top-width').toInt()+el.getStyle('border-bottom-width').toInt()};size.x+=tmp.x;size.y+=tmp.y;if(i==els.length-1){size.x+=padding.x+margin.x+border.x;size.y+=padding.y+margin.y+border.y}});return size}});RokVirtuemartScroller.implement(new Options);

