﻿/* Javascript by Daniel Cohen Gindi (c) danielgindi@gmail.com 054-5655765 */
/* Version: 2011-01-13 */

function ContentCarouselle(opts) {
    this._init(opts);
};

ContentCarouselle.prototype = {
    opts: null,
    DefaultOptions:
    {
        animate: false,
        buttonStopsAnimation: false,
        autoStart: false,
        tagContent: 'LI',
        classContent: null,
        tagButton: 'LI',
        classButton: null,
        btnPrev: null,
        btnNext: null,
        timerDelay: 10000,
        animationDuration: 500,
        animationFps: 40,
        currentSelected: 0,
        buttonSelectedClass: 'sel',
        buttonClickTransition: false,
        buttonOverTransition: false,
        autoWidth: false,
        direction: 'auto'
    },

    _init: function(opts) {
        var self = this;

        if (opts['buttonTriggersAnimation'] !== undefined && opts['buttonTriggersAnimation'] !== null) // legacy
        {
            opts.buttonClickTransition = opts.buttonOverTransition = opts['buttonTriggersAnimation'] ? ContentCarouselle.TransitionType.SLIDE : ContentCarouselle.TransitionType.STATIC;
        }
        var o = self.opts = dgTools.extend({}, 'animate buttonStopsAnimation buttonClickTransition buttonOverTransition btnPrev btnNext animationDuration animationFps timerDelay autoStart currentSelected tagContent classContent tagButton classButton container buttons buttonSelectedClass autoWidth direction', opts, self.DefaultOptions);
        o.inited = false;

        o.btnPrev = $(o.btnPrev);
        o.btnNext = $(o.btnNext);

        o.container = $(o.container);
        if (!o.container) return;
        o.containerLi = [];
        for (var j = 0; j < o.container.childNodes.length; j++) {
            if (o.container.childNodes[j].tagName == o.tagContent.toUpperCase()
                && (o.classContent == null || o.classContent == o.container.childNodes[j].className)) {
                o.containerLi.push(o.container.childNodes[j]);
            }
        }
        if (o.direction != 'rtl' && o.direction != 'ltr') {
            function isElmRtl(elm) {
                if (!elm) return false;
                var dir = dgTools.Elm.getRenderedStyle(elm, 'direction');
                if (dir == 'rtl') return true;
                if (dir == 'auto' || dir == 'inherit') return isElmRtl(elm.parentNode);
                return false;
            }
            o.direction = isElmRtl(o.container) ? 'rtl' : 'ltr';
        }
        if (o.autoWidth) {
            var w = 0;
            o.containerLi.iterate(function(item, idx) {
                w += dgTools.Elm.clientWidth(item);
            }, self);
            o.container.style.width = w + 'px';
        }

        o.buttons = $(o.buttons);
        if (!o.buttons) return;
        o.buttonsLi = [];
        for (var j = 0; j < o.buttons.childNodes.length; j++) {
            if (o.buttons.childNodes[j].tagName == o.tagButton.toUpperCase()
                && (o.classButton == null || o.classButton == o.buttons.childNodes[j].className)) {
                o.buttonsLi.push(o.buttons.childNodes[j]);
            }
        }
        while (o.buttonsLi.length > o.containerLi.length) o.buttonsLi.pop();

        var inMouseOver = null;
        function registerButton(btn, idx) {
            dgTools.observe(btn, 'mouseover', function(e) {
                if (o.buttonOverTransition == ContentCarouselle.TransitionType.NONE) return;
                var actualIdx = (typeof (idx) == 'function') ? idx() : idx;
                var evt = new dgTools.Event(e || event);
                if ((!o.buttonStopsAnimation && o.animationId) || o.currentSelected == actualIdx) return;
                if (evt.relatedTarget == btn || dgTools.Elm.hasChild(btn, evt.relatedTarget)) return;
                evt.stop();
                inMouseOver = btn;
                self.stopTimer(true);
                dgTools.Elm.removeClassName(o.buttonsLi[o.currentSelected], o.buttonSelectedClass);
                o.currentSelected = actualIdx;
                dgTools.Elm.addClassName(o.buttonsLi[o.currentSelected], o.buttonSelectedClass);
                if (o.buttonOverTransition == ContentCarouselle.TransitionType.SLIDE && o.animate) self._doAnimate();
                else {
                    if (o.animationId) {
                        o.animationId.stop();
                        o.animationId = null;
                    }
                    o.container.style.left = self._getContentLeft(o.currentSelected) + 'px';
                }
            });
            dgTools.observe(btn, 'click', function(e) {
                if (o.buttonClickTransition == ContentCarouselle.TransitionType.NONE) return;
                var actualIdx = (typeof (idx) == 'function') ? idx() : idx;
                var evt = new dgTools.Event(e || event);
                if ((!o.buttonStopsAnimation && o.animationId) || o.currentSelected == actualIdx) return;
                if (evt.relatedTarget == btn || dgTools.Elm.hasChild(btn, evt.relatedTarget)) return;
                evt.stop();
                inMouseOver = btn;
                self.stopTimer(true);
                dgTools.Elm.removeClassName(o.buttonsLi[o.currentSelected], o.buttonSelectedClass);
                o.currentSelected = actualIdx;
                dgTools.Elm.addClassName(o.buttonsLi[o.currentSelected], o.buttonSelectedClass);
                if (o.buttonClickTransition == ContentCarouselle.TransitionType.SLIDE && o.animate) self._doAnimate();
                else {
                    if (o.animationId) {
                        o.animationId.stop();
                        o.animationId = null;
                    }
                    o.container.style.left = self._getContentLeft(o.currentSelected) + 'px';
                }
            });
            dgTools.observe(btn, 'mouseout', function(e) {
                var evt = new dgTools.Event(e || event);
                if (evt.relatedTarget == btn || dgTools.Elm.hasChild(btn, evt.relatedTarget)) return;
                if (o.timerEnabled && !o.animationId) {
                    evt.stop();
                    self.stopTimer(true); self.startTimer();
                }
                inMouseOver = null;
            });
        }
        o.buttonsLi.iterate(function(item, idx) {
            registerButton(item, idx);
        }, self);
        if (o.btnPrev) registerButton(o.btnPrev, function() { if (o.currentSelected <= 0) return 0; else return o.currentSelected - 1; });
        if (o.btnNext) registerButton(o.btnNext, function() { if (o.currentSelected >= o.containerLi.length - 1) return o.currentSelected = o.containerLi.length - 1; else return o.currentSelected + 1; });

        if (o.currentSelected < 0) o.currentSelected = 0;
        else if (o.currentSelected >= o.containerLi.length - 1) o.currentSelected = o.containerLi.length - 1;

        o.inited = true;

        o.container.style.left = self._getContentLeft(o.currentSelected) + 'px';
        dgTools.Elm.addClassName(o.buttonsLi[o.currentSelected], o.buttonSelectedClass);

        if (o.autoStart) {
            self.startTimer();
        }
    },
    _doAnimate: function(renewTimer) {
        var self = this;
        var o = self.opts;
        if (o.animationId) {
            o.animationId.stop();
            o.animationId = null;
        }
        var destL = self._getContentLeft(o.currentSelected);
        var afterEnd = function() {
            o.animationId = null;
            if (renewTimer) self.startTimer(true);
        };
        o.animationId = dgTools.easyAnimate(o.container, null, { 'left': destL }, o.animationDuration, o.animationFps, afterEnd);
    },
    startTimer: function(doNotSetTimerEnabled) {
        var self = this;
        var o = self.opts;
        if (!o.inited) return;
        if (!doNotSetTimerEnabled) o.timerEnabled = true;
        if (o.timerTimeout) return;
        o.timerTimeout = setTimeout(function() {
            dgTools.Elm.removeClassName(o.buttonsLi[o.currentSelected], o.buttonSelectedClass);
            o.currentSelected = o.currentSelected + 1;
            if (o.currentSelected >= o.containerLi.length) o.currentSelected = 0;
            dgTools.Elm.addClassName(o.buttonsLi[o.currentSelected], o.buttonSelectedClass);
            if (o.animate) self._doAnimate(true);
            else {
                var destL = self._getContentLeft(o.currentSelected);
                o.container.style.left = destL;
                o.timerTimeout = null;
                self.startTimer(true);
            }
            o.timerTimeout = null;
        }, o.timerDelay);
    },
    stopTimer: function(doNotSetTimerEnabled) {
        var self = this;
        var o = self.opts;
        if (!o.inited) return;
        if (!doNotSetTimerEnabled) o.timerEnabled = false;
        if (o.timerTimeout) {
            clearTimeout(o.timerTimeout);
            o.timerTimeout = null;
        }
    },
    _getContentLeft: function(idx) {
        var self = this;
        var o = self.opts;
        if (!o.inited) return;
        var p = -dgTools.Elm.offset(o.containerLi[idx], o.container)[0];
        return p;
    }
};
ContentCarouselle.TransitionType = {
    NONE: 0,
    STATIC: 1,
    SLIDE: 2
};
