/**
 * Class: ImageSlider
 * Author: RossCo-Designs
 * Website: http://www.rossco-designs.com
 * Version: 1.0.1
 * Date: 28/09/2010 21:10
 * Built For:  Mootools 1.3 and MODx Rev Gallery Integration
 * Copyright 2010-2011 by Ross Sivills <rossco@rossco-designs.com>
 * Under the GPL License, you are required to keep this credit intact.
 * 
 * ImageSlider is free software; you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * ImageSlider is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * ImageSlider; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
 * Suite 330, Boston, MA 02111-1307 USA
 *
 */   

var SlideShowScroller = new Class({
    Implements: [Options, Events],
    options: {
        slideSelector: 'li',
        counterIdPrefix: 'carousel-counter-',
        triggerPrefix: 'trig',		
        slideDelay: 4000,
        slideDuration: 800
    },
    initialize: function (b, c, d) {
        this.setOptions(d);
        this.slideEl = $(b);
        this.counterEl = $(c);
        this.currentSlide = 0;
        this.totalSlides = 0;
        this.slides = {};
        this.timer = false;
        var f = this.slideEl.getElements(this.options.slideSelector);
        if (f) {
            this.totalSlides = f.length;
            f.each(function (s, i) {
                var a = {};
                a['index'] = i;
                a['slide'] = s;
                a['width'] = s.getSize().x.toInt();
                a['relPosition'] = s.getPosition(this.slideEl).x.toInt();
                a['counterEl'] = this.counterEl.getElement('#' + this.options.counterIdPrefix + i) || null;
                this.slides[i] = a;
                if (a['counterEl']) {
                    a['counterEl'].addEvent('mouseenter', function (e) {
                        e.stop();
                        this.slideTo(i)
                    }.bind(this))
                }
            }, this);
            this.fx = new Fx.Tween(this.slideEl, {
                duration: this.options.slideDuration,
                link: 'cancel',
                transition: Fx.Transitions.Linear            
			});
            $$('.'+this.options.triggerPrefix).addEvents({
                'mouseenter': function (e) {
                    this.stop()
                }.bind(this),
                'mouseleave': function (e) {
                    this.start()
                }.bind(this)
            });
			$$('#caro li').addEvents({
                'mouseenter': function (e) {
                    this.stop()
                }.bind(this),
                'mouseleave': function (e) {
                    this.start()
                }.bind(this)
            });
            this.start()
        }
    },
    autoSlide: function () {
        var a = this.currentSlide + 1;
		var which = this.currentSlide;
        if (a >= this.totalSlides) {
            a = 0
        }
        this.slideTo(a);
		
		slideInd = $$('#caro li');
		slideInd.each(function(e,index){
		
			document.id('holder-'+which).morph({'opacity':0});	
		});
		
        var b = $$('.'+this.options.triggerPrefix).morph({
            'opacity': '0.5'
        });
        var c = document.id(this.options.counterIdPrefix + a).morph({
            'opacity': '1'
        })
    },
    slideTo: function (i) {
        this.currentSlide = i;
        this.fx.start('left', -1 * this.slides[i]['relPosition']);
		slideInd = $$('#caro li');

		var last = (this.totalSlides - 1);
		slideInd.each(function(){ 
		
			if(i != 0){
				document.id('holder-'+(i-1)).morph({'opacity':0});
			}
			if(i < last){
				document.id('holder-'+(i+1)).morph({'opacity':0});
			}
			document.id('holder-'+i).morph({'opacity':1});	
			
		});
		
			
		
		/*
		if(i == 0){
			document.id('holder-0').morph({'opacity':1});
		}
		if(i != 0){
			document.id('holder-'+(i-1)).morph({
				'opacity': '0'
			});
			
			document.id('holder-'+(i+1)).morph({
				'opacity': '0'
			});
			
			document.id('holder-'+i).morph({
				'opacity': '1'
			});
		}*/
        var a = $$('.'+this.options.triggerPrefix).morph({
            'opacity': '0.5'
        });
        var b = document.id(this.options.counterIdPrefix + i).morph({
            'opacity': '1'
        })
    },
    start: function () {
        this.timer = this.autoSlide.periodical(this.options.slideDelay, this)
    },
    stop: function () {
        if (this.timer) {
            $clear(this.timer)
        }
    }
});
