﻿//ZennSlider v1.6 "over stop"
var Slider = new (function () {

    function Slider(rootElement, Options) {
        this.rootElement = rootElement;
        var that = this;
        var Items = new Array();
        var Effect;
        var TimerSlider;
        var totalItems = 0;

        //default options
        var Default = {
            Speed: 450,
            ItemPerGroup: 1,
            Orientation: 'horizontal',
            Navigation: 'arrow',
            Pagination: false,
            Effect: 'slide',
            Auto: false,
            OverStop: false,
            Time: 3000,
            ItemLoad: true,
            onLoad: null,
            onSlide: null
        };

        var Options = $.extend(Default, Options);

        //orientation
        if (Options.Orientation == 'horizontal') {
            this.Prev = function () { that.Effect('right', 'left', null); };
            this.Next = function () { that.Effect('left', 'right', null); };
        }
        if (Options.Orientation == 'vertical') {
            this.Prev = function () { that.Effect('down', 'up', null); };
            this.Next = function () { that.Effect('up', 'down', null); };
        };

        //if bullet
        if (Options.Navigation == 'bullet' || Options.Navigation == 'both') {
            this.LoadBullet = function () {
                if (!$(this).hasClass('On')) {
                    $(that.rootElement).find('.Navigation .Bullet').removeClass('On');
                    $(this).addClass('On');
                    that.Effect('left', 'right', $(this).attr('alt'));
                }
            };
        };

        //effect
        this.Effect = function (inDir, outDir, ImageWrapperPosition) {
            that.DisableButtons();
            //current & next

            var Current = $(that.rootElement).find('.ItemWrapper.On');

            //if !LoadBullet
            if (ImageWrapperPosition == null) {

                //default to slider and fade
                if (outDir == "down" || outDir == "right") {
                    var Next = $(Current).next('.ItemWrapper');
                    var Position = 'first';
                }
                else {
                    var Next = $(Current).prev('.ItemWrapper');
                    var Position = 'last';
                }
                //auto bullet
                if (Options.Navigation == 'bullet' || Options.Navigation == 'both') {
                    //next bullet
                    if (outDir == 'right')
                        var NextBullet = $(that.rootElement).find('.Navigation .Bullet.On').next('.Bullet');
                    else
                        var NextBullet = $(that.rootElement).find('.Navigation .Bullet.On').prev('.Bullet');

                    $(that.rootElement).find('.Navigation .Bullet').removeClass('On');
                    $(NextBullet).length ? $(NextBullet).addClass('On') : $(that.rootElement).find('.Navigation .Bullet:' + Position + '').addClass('On');
                }

            } else {
                //if LoadBullet
                var Next = $(that.rootElement).find('.ItemWrapper:nth-child(' + ImageWrapperPosition + ')');
            }

            //slide
            if (Options.Effect == 'slide') {
                $(Current).hide('slide', { direction: outDir }, Options.Speed, function () { $(this).removeClass("On").hide(); });
                if ($(Next).length != 0) {
                    $(Next).show('slide', { direction: inDir }, Options.Speed, function () {
                        $(this).addClass('On');
                        that.EnableButtons();

                        //onSlide
                        var a = $(that.rootElement).find('.ItemWrapper');
                        var b = that.getIndex(a, $(this)[0]);

                        if (typeof Options.onSlide == "function") Options.onSlide(b + 1, $(that.rootElement).find('.ItemWrapper').length);
                    });
                } else {
                    $(that.rootElement).find('.ItemWrapper:' + Position + '').show('slide', { direction: inDir }, Options.Speed, function () {
                        $(this).addClass('On');
                        that.EnableButtons();

                        //onSlide
                        var a = $(that.rootElement).find('.ItemWrapper');
                        var b = that.getIndex(a, $(this)[0]);

                        if (typeof Options.onSlide == "function") Options.onSlide(b + 1, $(that.rootElement).find('.ItemWrapper').length);
                    });
                }
            }

            //fade
            if (Options.Effect == 'fade') {
                $(Current).fadeOut(Options.Speed, function () { $(this).removeClass("On").hide(); });
                if ($(Next).length != 0) {
                    $(Next).fadeIn(Options.Speed, function () {
                        $(this).addClass('On');
                        that.EnableButtons();
                    });
                } else {
                    $(that.rootElement).find('.ItemWrapper:' + Position + '').fadeIn(Options.Speed, function () {
                        $(this).addClass('On');
                        that.EnableButtons();
                    });
                }
            }

        };

        this.getIndex = function (array, data) {
            for (i = 0; i < array.length; ++i) {
                if (array[i] == data) {
                    return i;
                }
            }
            return -1;
        };

        //disable before effects
        this.DisableButtons = function () {
            that.AutoSlider(false);
            if (Options.Navigation == 'arrow' || Options.Navigation == 'both') {
                $(that.rootElement).find('.Navigation .NavNext').unbind('click', that.Next);
                $(that.rootElement).find('.Navigation .NavPrev').unbind('click', that.Prev);
            }
            if (Options.Navigation == 'bullet' || Options.Navigation == 'both')
                $(that.rootElement).find('.Navigation .Bullet').unbind('click', that.LoadBullet);
        };

        //enable after effects
        this.EnableButtons = function () {
            if (Options.Auto) that.AutoSlider(true);
            if (Options.Navigation == 'arrow' || Options.Navigation == 'both') {
                $(that.rootElement).find('.Navigation .NavNext').bind('click', that.Next);
                $(that.rootElement).find('.Navigation .NavPrev').bind('click', that.Prev);
            }
            if (Options.Navigation == 'bullet' || Options.Navigation == 'both')
                $(that.rootElement).find('.Navigation .Bullet').bind('click', that.LoadBullet);
        };

        //addBullet
        this.AddBullet = function () {
            if (totalItems > 1) {
                $(that.rootElement).find('.ItemWrapper').each(function (i) {
                    $(that.rootElement).find('.Navigation').append('<span class="Bullet" alt="' + (i + 1) + '">&nbsp</span>');
                });
                $(that.rootElement).find('.Navigation .Bullet:first').addClass('On');
            }
        };

        //addArrow
        this.AddArrow = function (NumNav) {
            var HTML = '<span class="NavPrev">&nbsp</span>'

            if (NumNav)
                HTML += '<span class="Pages"> <span class="Actual"></span> de <span class="Total"></span></span>'

            HTML += '<span class="NavNext">&nbsp</span>';

            $(that.rootElement).find('.Navigation').append(HTML);
        };

        //loadItem
        this.LoadItem = function () {
            if (!$(this).hasClass('On')) {

                $(that.rootElement).find('.ItemWrapper .Item').unbind('click', that.LoadItem);

                var img = new Image();
                var URL = $(this).find('input').val();

                if (URL.indexOf('?') != -1) URL += '&timestamp=' + new Date().getTime(); //IE caching problem
                else URL += '?timestamp=' + new Date().getTime();

                $(rootElement).find('.ImageBig').find('.Status').html('');
                $(rootElement).find('.Item').removeClass('On');
                $(img).load(function () {
                    $(this).css('display', 'none');
                    $(rootElement).find('.ImageBig').append(this);
                    $(rootElement).find('.Zoom').attr('href', URL);
                    $(this).fadeIn('fast', function () {
                        $(that.rootElement).find('.ItemWrapper .Item').bind('click', that.LoadItem);
                    });
                }).error(function () {
                    $(rootElement).find('.ImageBig').find('.Status').html("<b>Impossível apresentar a imagem seleccionada!</b>");
                }).attr('src', '' + URL + '');
                $(rootElement).find('.ImageBig img').remove();
                $(this).addClass('On');
            }
        };

        //autoSlider
        this.AutoSlider = function (Action) {
            if (Action) {
                clearTimeout(TimerSlider);
                TimerSlider = setTimeout(function () { that.Effect('left', 'right', null); }, Options.Time);
            } else {
                clearTimeout(TimerSlider);
            }
        };

        //setup slider
        this.Setup = function () {
            if ($(that.rootElement).find('.Item').length == 0) return;

            var Html;
            var nItemPerGroup = Options.ItemPerGroup;
            var closed = 0;

            //prepare
            totalItems = $(that.rootElement).find('.Item').length;
            $(that.rootElement).find('.Item').each(function (i) {

                if (i == 0)
                    Html = '<div class="ItemWrapper">';

                if (i != nItemPerGroup - 1)
                    Html += '<span class="Item">' + $(this).html() + '</span>';

                if (i + 1 == nItemPerGroup) {
                    Html += '<span class="Item">' + $(this).html() + '</span>';
                    Html += '</div>';
                    closed = 1;
                    nItemPerGroup += Options.ItemPerGroup;
                }

                if (i == totalItems - 1) {
                    Html += '</div>';
                    return 0;
                }

                if (closed != 0) {
                    Html += '<div class="ItemWrapper">'
                    closed = 0;
                }
            });

            //slider
            $(that.rootElement).find('.Thumbs').html(Html).delay(300);
            $(that.rootElement).find('.ItemWrapper').hide();
            $(that.rootElement).find('.ItemWrapper:first').addClass('On').show();
            $(that.rootElement).find('.Thumbs').css('visibility', 'visible');

            //some public vars
            this.TotalItems = $(that.rootElement).find('.ItemWrapper').length;

            //css
            $(that.rootElement).css('position', 'relative');
            $(that.rootElement).find('.ItemWrapper').css({ 'width': '100%', 'position': 'absolute', 'left': '0', 'top': '0' });

            //add navigation
            if (Options.Navigation == 'bullet' || Options.Navigation == 'both')
                that.AddBullet(Options.Pagination);
            if (Options.Navigation == 'arrow' || Options.Navigation == 'both')
                that.AddArrow(Options.Pagination);

            //!=1
            if (totalItems > 0) {
                if (Options.ItemLoad) {
                    $(that.rootElement).find('.ItemWrapper .Item').show().click(that.LoadItem);
                    $(that.rootElement).find('.ItemWrapper.On .Item:first').click();
                }
            }

            if (totalItems > Options.ItemPerGroup) {

                if (Options.Navigation == 'arrow' || Options.Navigation == 'both') {
                    $(that.rootElement).find('.Navigation .NavNext').show().click(that.Next);
                    $(that.rootElement).find('.Navigation .NavPrev').show().click(that.Prev);
                }

                if (Options.Navigation == 'bullet' || Options.Navigation == 'both')
                    $(that.rootElement).find('.Navigation .Bullet').show().click(that.LoadBullet);

            }

            //autostart
            if (Options.Auto && totalItems > 1)
                that.AutoSlider(Options.Auto);

            //autoSlide mouseover
            if (Options.OverStop && totalItems > 1) {
                $(that.rootElement).find('.Thumbs').mouseenter(function () {
                    Options.Auto = false;
                    that.AutoSlider(Options.Auto);
                }).mouseleave(function () {
                    Options.Auto = true;
                    that.AutoSlider(Options.Auto);
                });
            }

            //onLoad
            if (typeof Options.onLoad == "function") Options.onLoad($(that.rootElement).find('.ItemWrapper').length);
        };

        //init
        this.Setup();
    }

    //start
    this.Start = function (rootElement, Options) {
        return new Slider(rootElement, Options);
    };
})();



