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

 var RokIntroScroller = new Class({
     version: '1.0',
     options: {
         'scroll': {
             'duration': 500,
             'itemsPerClick': 2,
             'transition': Fx.Transitions.Quad.easeOut
         },
         'arrows': {
             'effect': false,
             'opacity': 0.85,
             'align': 'center'
         }
     },
     initialize: function (b, c) {
         this.element = $(b);
         if (!this.element) return this;
         this.setOptions(c);
         this.container = new Element('div', {
             'class': 'rokintroscroller-container'
         }).inject(this.element, 'before');
         this.wrapper = new Element('div', {
             'class': 'rokintroscroller-wrapper'
         }).inject(this.container).adopt(this.element);
         if (window.ie6) this.wrapper.setStyle('width', this.element.getCoordinates().width);
         var d = this.element.getCoordinates().width;
         this.elements = this.element.getChildren().filter(function (a) {
             return a.getTag() == 'div'
         });
         this.elements[0].addClass('first');
         this.elements[this.elements.length - 1].addClass('last');
         this.size = this.getSize(this.elements);
         this.size.x += d;
         this.current = 0;
         this.element.setStyles({
             'width': this.size.x,
             'height': this.size.y
         });
         var e = this.wrapper.getStyle('width').toInt() + this.wrapper.getStyle('padding-left').toInt() + this.wrapper.getStyle('padding-right').toInt() + this.wrapper.getStyle('margin-left').toInt() + this.wrapper.getStyle('margin-right').toInt() + this.wrapper.getStyle('border-left').toInt() + this.wrapper.getStyle('border-right').toInt();
         if (this.size.x > e) {
             this.scroller = new Fx.Scroll(this.wrapper, {
                 'duration': this.options.scroll.duration,
                 'transition': this.options.scroll.transition,
                 'wait': false
             }).set([0, 0]);
             this.makeArrows()
         }
         return this
     },
     makeArrows: function () {
         this.leftArrow = new Element('div', {
             'class': 'rokintroscroller-leftarrow'
         }).inject(this.container, 'top');
         this.rightArrow = new Element('div', {
             'class': 'rokintroscroller-rightarrow'
         }).inject(this.container);
         if (window.ie6) {
             this.leftArrow.inject(this.container.getParent(), 'top');
             this.rightArrow.inject(this.container.getParent())
         };
         var c = $$(this.leftArrow, this.rightArrow);
         c.setStyles({
             'position': 'absolute',
             'top': 0,
             'cursor': 'pointer'
         });
         if (this.options.arrows.align == 'center') {
             var d = this.wrapper.getCoordinates().height;
             var e = this.leftArrow.getCoordinates().height;
             var f = this.rightArrow.getCoordinates().height;
             var g = {
                 'left': (d - e) / 2,
                 'right': (d - f) / 2
             };
             this.leftArrow.setStyle('top', g.left);
             this.rightArrow.setStyle('top', g.right)
         } else if (this.options.arrows.align == 'bottom') {
             c.setStyles({
                 'top': '',
                 'bottom': 0
             })
         };
         this.leftArrow.setStyle('left', 0);
         this.rightArrow.setStyle('right', 0);
         if (this.options.arrows.effect) {
             var h = this.options.arrows.opacity,
                 scroller = this.scroller;
             this.leftArrow.addEvents({
                 'mouseenter': function () {
                     this.addClass('rokintroscroller-leftarrow-hover')
                 },
                 'mouseleave': function () {
                     this.removeClass('rokintroscroller-leftarrow-hover')
                 }
             });
             this.rightArrow.addEvents({
                 'mouseenter': function () {
                     this.addClass('rokintroscroller-rightarrow-hover')
                 },
                 'mouseleave': function () {
                     this.removeClass('rokintroscroller-rightarrow-hover')
                 }
             });
             c.each(function (a) {
                 var b = new Fx.Style(a, 'opacity', {
                     duration: 200,
                     wait: false
                 }).set(1);
                 a.addEvents({
                     'mouseenter': function () {
                         b.start(h)
                     },
                     'mouseleave': function () {
                         b.start(1)
                     },
                     'click': function () {
                         var x = scroller.now[0];
                         if (a.hasClass('rokintroscroller-rightarrow')) {
                             this.current = this.setCurrent('right');
                             scroller.toElement(this.elements[this.current])
                         } else {
                             this.current = this.setCurrent('left');
                             scroller.toElement(this.elements[this.current])
                         }
                     }.bind(this)
                 })
             }, this)
         }
     },
     setCurrent: function (a) {
         var b = this.current,
             amount = this.options.scroll.itemsPerClick;
         if (a == 'left') {
             if (b - amount >= 0) this.current -= amount;
             else this.current = 0
         } else {
             if (b + amount < this.elements.length) this.current += amount
         }
         return this.current
     },
     getSize: function (f) {
         var g = {
             'x': 0,
             'y': 0
         };
         f.each(function (a) {
             var b = a.getSize().size;
             var c = {
                 'x': a.getStyle('padding-left').toInt() + a.getStyle('padding-right').toInt(),
                 'y': a.getStyle('padding-top').toInt() + a.getStyle('padding-bottom').toInt()
             };
             var d = {
                 'x': a.getStyle('margin-left').toInt() + a.getStyle('margin-right').toInt(),
                 'y': a.getStyle('margin-top').toInt() + a.getStyle('margin-bottom').toInt()
             };
             var e = {
                 'x': a.getStyle('border-left-width').toInt() + a.getStyle('border-right-width').toInt(),
                 'y': a.getStyle('border-top-width').toInt() + a.getStyle('border-bottom-width').toInt()
             };
             g.x += b.x + d.x + c.x + e.x;
             if (b.y > g.y) g.y = b.y + c.y + d.y + e.y
         });
         return g
     }
 });
 RokIntroScroller.implement(new Options);
