//console.log
var debug = true;
var Log = function (message) {
    if (debug && console != undefined)
        console.log(message);
};

//v1.3.1 (bullet direction)
var Slider = new (function () {

    function Slider(rootElement, Options) {
        this.rootElement = rootElement;
        var that = this;
        var Items = new Array();
        var Effect;
        var TimerSlider;
        var OldImageWrapperPosition = 0;

        //default options
        var Default = {
            Speed: 450,
            ItemPerGroup: 1,
            Orientation: 'horizontal',
            Navigation: 'tab',
            Effect: 'slide',
            Auto: false,
            Time: 3000,
            ItemLoad: true,
            Groups: false
        };
        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'));
                }
            };
        };

        //if tab
        if (Options.Navigation == 'tab' || Options.Navigation == 'both') {
            this.LoadTab = function () {
                if (!$(this).hasClass('On')) {
                    $(that.rootElement).find('.Navigation .Tab').removeClass('On');
                    $(this).addClass('On');
                    that.Effect('left', 'right', $(this).attr('alt'));
                }
            };
        };

        //effect
        this.Effect = function (inDir, outDir, ImageWrapperPosition) {
            that.DisableButtons();

            if (ImageWrapperPosition != null) {
                if (ImageWrapperPosition < OldImageWrapperPosition) {
                    inDir = 'right';
                    outDir = 'left';
                };
            }

            OldImageWrapperPosition = ImageWrapperPosition;

            //current & next
            var Current = $(that.rootElement).find('.Items.On');

            //if !LoadBullet
            if (ImageWrapperPosition == null) {

                //default to slider and fade
                if (outDir == "down" || outDir == "right") {
                    var Next = $(Current).next('.Items');
                    var Position = 'first';
                }
                else {
                    var Next = $(Current).prev('.Items');
                    var Position = 'last';
                }
                //auto bullet																										//Melhorar
                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');
                }

                //auto tab
                if (Options.Navigation == 'tab' || Options.Navigation == 'both') {
                    //next tab
                    if (outDir == 'right')
                        var NextBullet = $(that.rootElement).find('.Navigation .Tab.On').next('.Tab');
                    else
                        var NextBullet = $(that.rootElement).find('.Navigation .Tab.On').prev('.Tab');

                    $(that.rootElement).find('.Navigation .Tab').removeClass('On');
                    $(NextBullet).length ? $(NextBullet).addClass('On') : $(that.rootElement).find('.Navigation .Tab:' + Position + '').addClass('On');
                }

            } else {
                //if LoadBullet
                var Next = $(that.rootElement).find('.Items: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();
                    });
                } else {
                    $(that.rootElement).find('.Items:' + Position + '').show('slide', { direction: inDir }, Options.Speed, function () {
                        $(this).addClass('On');
                        that.EnableButtons();
                    });
                }
            }

            //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('.Items:' + Position + '').fadeIn(Options.Speed, function () {
                        $(this).addClass('On');
                        that.EnableButtons();
                    });
                }
            }
        };

        //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);

            if (Options.Navigation == 'tab' || Options.Navigation == 'both')
                $(that.rootElement).find('.Navigation .Tab').unbind('click', that.LoadTab);
        };

        //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);

            if (Options.Navigation == 'tab' || Options.Navigation == 'both')
                $(that.rootElement).find('.Navigation .Tab').bind('click', that.LoadTab);
        };

        //addBullet
        this.AddBullet = function () {
            $(that.rootElement).find('.Items').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 () {
            $(that.rootElement).find('.Navigation').append('<span class="NavPrev">&nbsp</span><span class="NavNext">&nbsp</span>');
        };

        //addTab
        this.AddTab = function (ItemTitle) {
            $(ItemTitle).each(function (i) {
                $(that.rootElement).find('.Navigation').append('<span class="Tab" alt="' + (i + 1) + '">' + $(this).text() + '</span>');
            });
            $(that.rootElement).find('.Navigation .Tab:first').addClass('On');
        };

        //loadItem
        this.LoadItem = function () {
            if (!$(this).hasClass('On')) {
                var that = this;

                $(rootElement).find('.Loading').show();

                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('.Item').removeClass('On');
                $(img).load(function () {
                    $(this).css('display', 'none');
                    $(rootElement).find('.ItemFull').append(this);
                    $(this).fadeIn('fast', function () {
                        $(rootElement).find('.Loading').hide();
                        $(rootElement).find('.ItemFull .Description').text($(that).find('.Description').val());
                    });
                }).error(function () {
                    $(rootElement).find('.Loading').hide();
                    $(rootElement).find('.ItemFull .Status').html("<b>Impossível apresentar a imagem seleccionada!</b>");
                }).attr('src', '' + URL + '');

                $(rootElement).find('.ItemFull 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 totalItems = 0;
            var nItemPerGroup = Options.ItemPerGroup;
            var closed = 0;

            $(that.rootElement).find('.Item').each(function (i) { totalItems++; });

            //prepare
            $(that.rootElement).find('.Item').each(function (i) {
                if (i == 0)
                    Html = '<span class="Items">';

                if (i != nItemPerGroup - 1)
                    Html += '<span class="Item">' + $(this).html() + '</span>';

                if (i + 1 == nItemPerGroup) {
                    Html += '<span class="Item last">' + $(this).html() + '</span>';
                    Html += '</span>';
                    closed = 1;
                    nItemPerGroup += Options.ItemPerGroup;
                }

                if (i == totalItems - 1) {
                    Html += '</span>';
                    return 0;
                }

                if (closed != 0) {
                    Html += '<span class="Items">'
                    closed = 0;
                }
            });

            //slider
            $(that.rootElement).find('.Thumbs').html(Html).delay(300).fadeIn('fast');
            $(that.rootElement).find('.Items').hide();
            $(that.rootElement).find('.Items:first').show();
            $(that.rootElement).find('.Items:first-child').addClass('On');

            //add navigation
            if (Options.Navigation == 'bullet' || Options.Navigation == 'both')
                that.AddBullet();

            if (Options.Navigation == 'arrow' || Options.Navigation == 'both')
                that.AddArrow();

            if (Options.Navigation == 'tab' || Options.Navigation == 'both')
                that.AddTab($(that.rootElement).find('.Items .Title h1')); //from title h1

            //!=1
            if (totalItems > 0) {
                if (Options.ItemLoad) {
                    $(that.rootElement).find('.Items .Item').show().click(that.LoadItem);
                    $(that.rootElement).find('.Items.On .Item:first').click();
                }

                //fullscreen
                if ($(that.rootElement).find('.Item img').attr('alt') != 'Sem Foto') {
                    $(that.rootElement).find('.FullscreenIcon').show().click(function () {
                        that.Fullscreen($(that.rootElement).find('.Items.On'));
                    });
                }
            }

            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);

                if (Options.Navigation == 'tab' || Options.Navigation == 'both')
                    $(that.rootElement).find('.Navigation .Tab').show().click(that.LoadTab);

            }

            //autostart
            if (Options.Auto)
                that.AutoSlider(Options.Auto);
        };

        //init
        this.Setup();
    }

    //start
    this.Start = function (rootElement, Options) {
        new Slider(rootElement, Options);
    };
})();
