﻿/*
* Modified and adapted from Accessible Carousel - original code: http://github.com/ginader/Accessible-Carousel
*/

(function ($) {
    var debugMode = false;
    $.fn.extend({
        accessibleCarousel: function (config) {
            var o = this;
            //debug('accessibleNavigation');
            o.items = []; // contains all item LIs when found
            o.positions = []; // contains the left position of the item LIs
            o.remoteItems = [];
            o.timeout = null;
            o.currentItemIndex = 0;
            o.paused = false;
            o.options = $.extend({
                remote: null, // (optional) valid jQuery Selector that defines a list that can remote control the carousel display
                autoRotate: false, // (optional) boolean to define that the carousel should change its display by itself (slideshow)
                rotateWaitTime: 3000, // (optional) number of miliseconds to wait on one display during autoRotate
                startIndex: 0, //(optional) number starting with 0 to define which of the availabe items to show at start
                updateElementLink: null, // (optional) valid jQuery Selector of an element which href attibut should be updated with the one of the active element. If not <a> element it will be wrapped in one
                updateElementText: null, // (optional) valid jQuery Selector of an element which content should be updated with the Text of the active carusel item. If active item is an image the alt text will be used.
                animation: 'slide', // Defines the way the widget is animated. Either "blend" or "slide"(default)
                navigationElement: null // (optional) valid JQuery selector of an element which contains two links for previous and next navigation
            }, config);


            o.setup = function (list) {
                o.items = list.find('li');
                var px = 0;
                o.items.each(function (i) {
                    o.positions[i] = px;
                    px += o.items.width();
                });
                o.showItem(o.options.startIndex, list, true)
                if (o.options.remote) {
                    o.setupRemote(list);
                }
                if (o.options.navigationElement) {
                    o.setupNavigation(list);
                }
                if (o.options.autoRotate) {
                    o.autoRotateWait(list);
                }
                o.updateRemote(o.currentItemIndex);
            };

            o.setupRemote = function (list) {
                //debug('setupRemote');
                o.remoteItems = $(o.options.remote).find('li');
                o.remoteItems.find('a').each(function (index, el) {
                    debug(index);
                    debug(el);
                    $(el).bind('mouseenter focus', function () {
                        o.stopAutoRotate();
                        o.showItem(index, list);
                    });
                    var href = $(o.items[index]).find('a').attr('href');
                    if (href) {
                        $(el).attr('href', href);
                    }
                });
            };

            o.setupNavigation = function (list) {
                //debug('setupNavigation');

                if (o.options.navigationElement) {
                    $(' > a:first', o.options.navigationElement).bind('click', function () {
                        if (o.options.autoRotate) {
                            o.stopAutoRotate();
                        }
                        o.showPreviousItem(list);
                        if (!o.paused && o.options.autoRotate) {
                            o.autoRotateWait(list);
                        }
                    });
                    $(' > a:last', o.options.navigationElement).bind('click', function () {
                        if (o.options.autoRotate) {
                            o.stopAutoRotate();
                        }
                        o.showNextItem(list);
                        if (!o.paused && o.options.autoRotate) {
                            o.autoRotateWait(list);
                        }
                    });
                    $(' > a.pause', o.options.navigationElement).bind('click', function () {
                        if (o.options.autoRotate) {
                            if (o.paused) {
                                $(o.options.navigationElement).removeClass('pause');
                                o.autoRotateWait(list);
                            }
                            else {
                                $(o.options.navigationElement).addClass('pause');
                                o.stopAutoRotate();
                            }
                            o.paused = !o.paused;
                        }
                    });
                    //                    if (o.options.autoRotate) {
                    //                        list.mouseenter(function () {
                    //                            if (!o.paused)
                    //                                o.stopAutoRotate();
                    //                        });
                    //                        list.mouseleave(function () {
                    //                            if (!o.paused)
                    //                                o.autoRotateWait(list);
                    //                        });
                    //                    }
                }
            };

            o.updateRemote = function (index) {
                //debug('updateRemote');
                debug(index);
                $(o.remoteItems).removeClass('active');
                $(o.remoteItems[index]).addClass('active');
            }

            o.showItem = function (index, list, noAnimation) {
                //debug('showItem');
                //debug(index);
                var lastIndex = o.currentItemIndex;
                o.currentItemIndex = index;
                var position = '-' + o.positions[index] + 'px';
                //debug(position);
                if (o.options.remote) {
                    o.updateRemote(index);
                }
                if (o.options.updateElementLink) {
                    $(o.options.updateElementLink).removeClass('link').unbind('click');
                    if ($(o.items[index]).find('a').attr('href')) {
                        $(o.options.updateElementLink).click(function (event) {
                            document.location.href = $(o.items[index]).find('a').attr('href');
                        }).addClass('link');
                    };
                }
                if (o.options.updateElementText) {
                    var text = '';
                    //debug('test');
                    //debug($(o.items[index]).find('img[title]').length);
                    if ($(o.items[index]).find('img[title]').length) {
                        text = $(o.items[index]).find('img').attr('title');
                        //debug(text);
                    } else {
                        text = o.items[index].text();
                        //debug(text);
                    }

                    $(o.options.updateElementText).html(text);
                    //debug($(o.options.updateElementText));
                }

                $(' > a', o.items[lastIndex]).attr('tabindex', '-1');
                $(' > a', o.items[index]).removeAttr('tabindex');
                switch (o.options.animation) {
                    case 'blend':
                        if (noAnimation) {
                            $(o.items).hide();
                            $(o.items[index]).show();

                            //$(' > div', o.items).hide();
                            //$(' > div', o.items[index]).show();
                            return;
                        }
                        $(o.items[lastIndex]).fadeOut(600);
                        //$(' > div', o.items[lastIndex]).fadeOut('slow');
                        $(o.items[index]).fadeIn(800);
                        //$(' > div', o.items[index]).fadeIn('slow');
                        break;
                    case 'slide':
                    default:
                        $(' > div', o.items[lastIndex]).css("left", "auto");
                        $(' > div', o.items[index]).css("left", "0");
                        if (noAnimation) {
                            //$(' > div', o.items).hide();
                            $(list).css('margin-left', position);
                            //$(' > div', o.items[index]).show();
                            return;
                        }
                        $(list).stop().animate({
                            marginLeft: position
                        }, 800, 'linear');
                        $(' > div', o.items[lastIndex]).fadeOut(600);
                        $(' > div', o.items[index]).fadeIn(800);
                }

            };

            o.showNextItem = function (list) {
                //debug('showNextItem');
                var next = o.currentItemIndex + 1;
                if (next >= o.items.length) {
                    next = 0;
                };
                o.showItem(next, list);
            };

            o.showPreviousItem = function (list) {
                var previous = o.currentItemIndex - 1;
                if (previous < 0) {
                    previous = o.items.length - 1;
                }
                o.showItem(previous, list);
            };

            o.autoRotateWait = function (list) {
                //debug('autoRotateWait');
                o.timeout = window.setTimeout(function () {
                    o.showNextItem(list);
                    o.autoRotateWait(list);
                }, o.options.rotateWaitTime);
            }

            o.stopAutoRotate = function () {
                //debug('stopAutoRotate');
                if (o.timeout) {
                    window.clearTimeout(o.timeout);
                    o.timeout = null;
                }
            };

            return o.each(function () {
                var list = $(this);
                o.setup(list);
            });
        }
    });
    // private Methods
    function debug(msg) {
        if (debugMode && window.console && window.console.log) {
            window.console.log(msg);
        }
    }
})(jQuery);


//Setup carousel on page
function SetupCarousel(element, animate, interval) {
    if (!cssOff) {
        element.find('div.nav span').hide();
        element.find('div.nav').show();
        var list = element.children('ul');
        list.find('li>div').css("opacity", 0.8);
        list.find('li>a').attr('tabindex', '-1');
        list.accessibleCarousel({ autoRotate: animate, rotateWaitTime: interval, navigationElement: element.find('div.nav') });
    }
}

