﻿(function($) {
    var rotator = function(lst, options) {
        this.options = $.extend({}, rotator.defaultOptions, options);

        this.parts = [];
        this.currentPosition = -1;
        this.timer = null;
        this.playing = false;
        this.listContainer = $(lst);
        this.container = $("<div class='item-rotator' />");
        this.container.addClass(this.listContainer.attr('class'));
        this.container.addClass(this.options.extraClasses);
        this.imgContainer = $("<div class='rotator-images' />");
        this.itemDescription = $("<div  class='rotator-description'/>");
        title = this.listContainer.attr('title');
        this.listDescriptionText = $('<span />').addClass("rotator-list-description rotator-even").text(title);
        this.listDescription = $("<div class='rotator-description'>");
        this.nextButton = $('<a class="rotator-next rotator-even">next</a>');
        this.prevButton = $('<a class="rotator-previous  rotator-even">previous</a>');
        this.itemDescription.hide();
        if (this.options.showListActions) {
            if (this.options.showNextListActions)
                this.listDescription.append(this.nextButton);
            if (this.options.showLastListActions)
                this.listDescription.append(this.prevButton);
        }
        if (this.options.showListLabel)
            this.listDescription.append(this.listDescriptionText);

        this.container.append(this.itemDescription);
        if (this.options.showListActions || this.options.showListLabel)
            this.container.append(this.listDescription);

        this.container.append(this.imgContainer);
        this.listContainer.before(this.container);
        this.listContainer.hide();
        this.populateParts();
        var self = this;
        var slideUp = function() {
            self.itemDescription.stop(false, true);
            self.listDescription.stop(false, true);
            self.itemDescription.slideDown();
            self.listDescription.slideUp();
        }
        var slideDown = function() {
            self.itemDescription.stop(false, true);
            self.listDescription.stop(false, true);
            self.listDescription.slideDown();
            self.itemDescription.slideUp();
        }
        this.itemDescription.mouseover(function() {
            clearTimeout(realMouseOutTimer);
            slideUp();
        });
        this.imgContainer.mouseover(function() {
            clearTimeout(realMouseOutTimer);
            slideUp();
        });
        var realMouseOutTimer;
        this.container.mouseout(function() {
            clearTimeout(realMouseOutTimer);
            realMouseOutTimer = setTimeout(slideDown, 100);
        });

        this.nextButton.click(function() { self.next() });
        this.prevButton.click(function() { self.previous() });

        if (this.options.random)
            this.parts.sort(function() { return (Math.round(Math.random()) - 0.5); });

        var start = function() {
            self.play();
        }

        if (this.options.delay >= 0)
            setTimeout(start, this.options.delay);

        this.itemDescription.children().hide();
        this.imgContainer.children().hide();
        this.showItem(this.options.defaultSelected);
    }
    rotator.defaultOptions = {
        delay: 0,
        showListLabel: true,
        showNextListActions: true,
        showLastListActions: true,
        showListActions: true,
        speed: 5000,
        resetTimerOnAction: false,
        random: false,
        defaultSelected: 0,
        extraClasses: '',
        changed: function() { }
    };

    rotator.prototype.play = function() {
        if (!this.playing) {

            var self = this;
            var moveNext = function() {
                self.next();
            }

            this.timer = setInterval(moveNext, this.options.speed);

            this.playing = true;
        }
    };
    rotator.prototype.stop = function() {
        if (this.timer != null) {
            clearInterval(this.timer);
        }
        this.playing = false;
    };

    rotator.prototype.next = function() {
        this.showItem(this.currentPosition + 1);
        if (this.options.resetTimerOnAction && this.playing) {
            this.stop();
            this.play();
        }
    };
    rotator.prototype.previous = function() {
        this.showItem(this.currentPosition - 1);
        if (this.options.resetTimerOnAction && this.playing) {
            this.stop();
            this.play();
        }
    };

    rotator.prototype.showItem = function(i) {
        var pos = 0;
        if (i >= this.parts.length) {
            pos = i - this.parts.length;
        } else if (i < 0) {
            pos = this.parts.length + i;
        } else { pos = i };
        if (pos != this.currentPosition) {
            this.hidePart(this.parts[this.currentPosition]);
            this.showPart(this.parts[pos]);
            this.currentPosition = pos;
            this.options.changed(this.currentPosition);
        }
    }

    rotator.prototype.showPart = function(p) {
        if (p && p != undefined) {
            if (p.img && p.img != null)
                p.img.fadeIn();
            if (p.content && p.content != null) {
                p.content.fadeIn();
            }
        }
    }

    rotator.prototype.hidePart = function(p) {
        if (p && p != undefined) {
            if (p.img && p.img != null)
                p.img.fadeOut(function() { $(this).hide(); });
            if (p.content && p.content != null) {
                p.content.css({ 'position': 'absolute' });
                p.content.fadeOut(function() { $(this).hide(); $(this).css({ 'position': 'relative' }); });
            }
        }
    }

    rotator.prototype.populateParts = function() {
        var self = this;
        var selected = -1;
        var beenSelected = false;
        if (this.listContainer.is('dl')) {

            first = this.listContainer.children(':first');

            if (!first.is('dt')) {
                html = "";
                var otherFirstDD = first.nextAll('dd').not(first.nextAll('dt').nextAll());
                if (first.is('dd'))
                    html += first.html()

                html += allhtml(otherFirstDD);

                this.listDescriptionText.html(html);

            }

            $('dt', this.listContainer).each(function() {
                itm = $(this);
                if (!beenSelected) selected++;
                if (itm.is('.selected')) {
                    beenSelected = true;
                }
                var img = $(this);

                var elements = itm.nextAll('dd').not(itm.nextAll('dt').nextAll());
                var item = $('<div class="rotator-item-description" />');
                item.append(allhtml(elements));
                self.parts.push({
                    img: img,
                    content: item
                });
            });
        }
        else {
            $('li', this.listContainer).each(function() {
                if (!beenSelected) selected++;
                if (itm.hasClass('selected')) {
                    beenSelected = true;
                }
                var img = $('img', this);
                var elements = $(img.attr('longdesc'));
                var item = $('<div class="rotator-item-description"  />');
                item.append(allhtml(elements));

                self.parts.push({
                    img: img,
                    content: item
                });
            });
        }

        for (var i = 0; i < this.parts.length; i++) {
            var p = this.parts[i];


            if (p.img && p.img != null) {
                this.imgContainer.append(p.img);
            }
            if (p.content && p.content != null) {
                this.itemDescription.append(p.content);
            }
        }
        if (beenSelected)
            this.options.defaultSelected = selected;
    }

    jQuery.fn.slideshow = function() {
        var self = $(this);

        var fadeSpeed = 1000;
        var animateSpeed = 5000;
        //Load the slideshow
        var act = self.find('a:first');

        self.find('a').hide();
        act.show();
        var switchSlide = function() {
            var crnt = self.find('a.active');
            if (crnt.length == 0) crnt = self.find('a:first');

            var nxt = crnt.next();
            if (nxt.length == 0) nxt = self.find('a:first');
            crnt.removeClass('active');
            nxt.addClass('active');

            nxt.fadeIn(fadeSpeed);
            crnt.fadeOut(fadeSpeed);
        };
        var timer = null;

        self.find('a.active').live("mouseover", function() {
            clearInterval(timer);
        });

        self.find('a.active').live("mouseout", function() {
            startTimer();
        });

        var startTimer = function() {
            timer = setInterval(switchSlide, animateSpeed);
        }
        startTimer();
    }

    //jQuery(document).ready(function($) {
    //    var fadeSpeed = 1000;
    //    var animateSpeed = 5000;
    //    //Load the slideshow
    //    var act = jQuery('.slideshow a:first');

    //    $('.slideshow a').hide();
    //    act.show();
    //    var switchSlide = function() {
    //        var crnt = $('.slideshow a.active');
    //        if (crnt.length == 0) crnt = $('.slideshow a:first');

    //        var nxt = crnt.next();
    //        if (nxt.length == 0) nxt = $('.slideshow a:first');
    //        crnt.removeClass('active');
    //        nxt.addClass('active');

    //        nxt.fadeIn(fadeSpeed);
    //        crnt.fadeOut(fadeSpeed);
    //    };
    //    var timer = null;

    //    $('.slideshow a.active').live("mouseover", function() {
    //        clearInterval(timer);
    //    });

    //    $('.slideshow a.active').live("mouseout", function() {
    //        startTimer();
    //    });

    //    var startTimer = function() {
    //        timer = setInterval(switchSlide, animateSpeed);
    //    }
    //    startTimer();
    //})

})(jQuery);
