/**
 * Flyergalerie
 * 2009-04-04
 **/

(function($) {

        $(function () {
                $('#arrow-altes-right').click(nextFlyer);
                $('#arrow-altes-left').click(prevFlyer);
        });

        function nextFlyer() {
                $('#arrow-altes-right').unbind('click', nextFlyer).click(function () {
                        return false;
                });;

                var $aktiv = $('li.flyer-aktiv');
                var $next = $aktiv.next();

                if (typeof $aktiv.prev().get(0) == 'undefined') {
                        $('#arrow-altes-left').fadeIn('normal', function () {
                                $(this).removeClass('hidden');
                        });
                }

                if (typeof $next.next().get(0) == 'undefined') {
                        $(this).fadeOut();
                }

                $aktiv.removeClass('flyer-aktiv');

                $aktiv.find('p.aktiv').fadeOut('normal', function () {
                        $(this).removeClass('aktiv');
                });
                $next.find('p').fadeIn('normal', function () {
                        $(this).addClass('aktiv');
                });

                $aktiv.parent().animate({
                        'left': '-=571px'
                }, 1000, '', function () {
                        $('#arrow-altes-right').click(nextFlyer);
                });

                $next.addClass('flyer-aktiv');

                var index = $aktiv.parent().find('li').index($next);
                $('#flyer-headline span').text(flyerDates[index]);

                return false;
        }

        function prevFlyer() {
                $('#arrow-altes-left').unbind('click', prevFlyer).click(function () {
                        return false;
                });

                var $aktiv = $('li.flyer-aktiv');
                var $prev = $aktiv.prev();

                if (typeof $aktiv.next().get(0) == 'undefined') {
                        $('#arrow-altes-right').fadeIn();
                }

                if (typeof $prev.prev().get(0) == 'undefined') {
                        $(this).fadeOut();
                }

                $aktiv.find('p.aktiv').fadeOut('normal', function () {
                        $(this).removeClass('aktiv');
                });
                $prev.find('p').fadeIn('normal', function () {
                        $(this).addClass('aktiv');
                });

                $aktiv.parent().animate({
                        'left': '+=571px'
                }, 1000, '', function () {
                        $('#arrow-altes-left').click(prevFlyer);
                });

                $aktiv.removeClass('flyer-aktiv');
                $prev.addClass('flyer-aktiv');

                var index = $aktiv.parent().find('li').index($prev);
                $('#flyer-headline span').text(flyerDates[index]);

                return false;
        }

})(jQuery);