/**
 * YJSlide8 - content slider
 * @version		1.0.0
 * @MooTools version 1.1
 * Copyright Youjoomla LLC
 * @author	Constantin Boiangiu
 */
var YJSports = new Class({
    initialize: function (a) {
        this.options = Object.extend({
            outerContainer: null,
            innerContainer: null,
            elements: null,
            navigation: {
                forward: null,
                back: null,
                container: null,
                elements: null,
                outer: null,
                visibleItems: 0
            },
            slideType: 2,
            orientation: 0,
            slideTime: 3000,
            duration: 600,
            tooltips: 0,
            autoslide: 0,
            navInfo: null,
            navLinks: null,
            startElem: null
        }, a || {});
        this.navElements = $(this.options.navigation.container).getElements(this.options.navigation.elements);
        this.navScroll = new Fx.Scroll(this.options.navigation.outer, {
            wait: false,
            duration: 800,
            transition: Fx.Transitions.Quad.easeInOut
        });
        this.correction = Math.round(this.options.navigation.visibleItems / 2.00001);
        this.start()
    },
    start: function () {
        this.currentElement = this.options.startElem;
        this.direction = 1;
        this.elements = $(this.options.innerContainer).getElements(this.options.elements);
        this.showEffect = {};
        this.hideEffect = {};
        this.firstRun = {};
        if (this.options.slideType !== 0) {
            if (this.options.orientation == 1) {
                this.showEffect.left = [1200, 0];
                this.hideEffect.left = [0, 1200];
                this.firstRun.left = 1200
            } else {
                this.showEffect.top = [400, 0];
                this.hideEffect.top = [0, 400];
                this.firstRun.top = 400
            }
        }
        if (this.options.slideType !== 1) {
            this.showEffect.opacity = [0, 1];
            this.hideEffect.opacity = [1, 0];
            this.firstRun.opacity = 0
        }
        this.elements.each(function (b, a) {
            b.setStyles({
                display: "block",
                position: "absolute",
                top: 0,
                left: 0,
                "z-index": (100 - a)
            });
            if (this.options.slideType !== 1 && a !== this.currentElement) {
                b.setStyle("opacity", 0)
            }
            this.elements[a]["fx"] = new Fx.Styles(b, {
                wait: false,
                duration: this.options.duration
            });
            if (a !== this.currentElement) {
                this.elements[a]["fx"].set(this.firstRun)
            }
            b.addEvent("mouseover", function (c) {
                if ($defined(this.period)) {
                    $clear(this.period)
                }
            }.bind(this));
            b.addEvent("mouseout", function (c) {
                if (this.options.autoslide == 0) {
                    this.period = this.rotateSlides.periodical(this.options.slideTime, this)
                }
            }.bind(this))
        }.bind(this));
        if (!this.options.tooltips) {
            new Tips($$(".YJSports_link"), {
                className: "YJSports_tips"
            })
        }
        if (!this.options.autoslide) {
            this.period = this.rotateSlides.periodical(this.options.slideTime, this)
        }
        this.setNavigation();
        if (this.options.navLinks) {
            this.secondNavigation();
            $(this.options.navigation.container).addEvent("mousewheel", function (b) {
                b = new Event(b);
                b.stop();
 				var dir = b.wheel > 0 ? 1 : -1;
				var a = this.currentElement - dir;
               // var a = this.currentElement - b.wheel;
                if (b.wheel > 0 && a < 0) {
                    a = this.navElements.length - 1
                }
                if (b.wheel < 0 && a > this.navElements.length - 1) {
                    a = 0
                }
                this.resetAutoslide();
                this.period = this.rotateSlides.bind(this).periodical(this.options.autoSlide);
                this.nextSlide(a)
            }.bind(this))
        }
    },
    rotateSlides: function () {
        var a = this.currentElement + this.direction;
        if (a < 0) {
            a = this.elements.length - 1
        }
        if (a > this.elements.length - 1) {
            a = 0
        }
        this.nextSlide(a)
    },
    nextSlide: function (a) {
        if (a == this.currentElement) {
            return
        }
        this.elements[this.currentElement]["fx"].start(this.hideEffect);
        this.elements[a]["fx"].start(this.showEffect);
        this.currentElement = a;
        if ($(this.options.navInfo)) {
            $(this.options.navInfo).setHTML("Link " + (a + 1) + " of " + this.elements.length)
        }
        if ($defined(this.navElements)) {
            this.navElements.removeClass("selected");
            this.navElements[a].addClass("selected");
            var b = a - this.correction < 0 ? 0 : a - this.correction;
            if (b + this.correction >= this.navElements.length - this.correction) {
                b = (this.navElements.length - 1) - this.correction * 2
            }
            this.navScroll.toElement(this.navElements[b])
        }
    },
    setNavigation: function () {
        if (!$(this.options.navigation.forward)) {
            return
        }
        $(this.options.navigation.forward).addEvent("click", function (a) {
            new Event(a).stop();
            this.direction = 1;
            this.resetAutoslide();
            this.rotateSlides()
        }.bind(this));
        $(this.options.navigation.back).addEvent("click", function (a) {
            new Event(a).stop();
            this.direction = -1;
            this.resetAutoslide();
            this.rotateSlides()
        }.bind(this))
    },
    resetAutoslide: function () {
        if ($defined(this.period)) {
            $clear(this.period);
            this.period = this.rotateSlides.periodical(this.options.slideTime, this)
        }
    },
    secondNavigation: function () {
        this.navElements = $$(this.options.navLinks);
        this.navElements.each(function (b, a) {
            if (a == this.currentElement) {
                this.navScroll.toElement(b);
                b.addClass("selected")
            }
            b.addEvent("click", function (c) {
                new Event(c).stop();
                this.resetAutoslide();
                this.nextSlide(a)
            }.bind(this))
        }.bind(this));
        if (!this.options.tooltips) {
            new Tips(this.navElements, {
                className: "YJSports_tips"
            })
        }
    }
});
