﻿var allhtml = function(elements) {
    var html = "";
    elements.each(function() {
        html += $(this).html();
    })
    var rot_offset = 150;
    var rot_frequancy = 7000;
    return html;
}

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;
}

var paging = function(node) {

    var settingup = true;
    var currentItems = $(node);
    var lastItems = currentItems.clone();
    var nextItems = currentItems.clone();
    var container = $("<div class='pager' />");
    container.addClass(currentItems.attr('class'));


    currentItems.before(container);
    container.append(currentItems);
    currentItems.before(lastItems);
    currentItems.after(nextItems);
    var currentItemRotator = new rotator(currentItems,
            {
                delay: -1,
                resetTimerOnAction: true,
                showListLabel: true,
                showListActions: true,
                showLastListActions: false,
                showNextListActions: false
            });

    var startSelected = currentItemRotator.currentPosition;

    var nextItemRotator = new rotator(nextItems,
            {
                delay: -1,
                resetTimerOnAction: true,
                showListLabel: false,
                showListActions: true,
                showLastListActions: false,
                extraClasses: ' next',
                changed: function(i) {
                    if (!settingup) {
                        lastItemRotator.showItem(i - 2);
                        currentItemRotator.showItem(i - 1);
                    }
                }
            });
    var lastItemRotator = new rotator(lastItems,
            {
                delay: -1,
                resetTimerOnAction: true,
                showListLabel: false,
                showListActions: true,
                showNextListActions: false,
                extraClasses: ' last',
                changed: function(i) {
                    if (!settingup) {
                        nextItemRotator.showItem(i + 2);
                        currentItemRotator.showItem(i + 1);
                    }
                }

            });

    currentItemRotator.showItem(startSelected);
    lastItemRotator.showItem(startSelected - 1);
    nextItemRotator.showItem(startSelected + 1);
    settingup = false;

    if (container.hasClass('sticky')) {
        var timeoutRef = null;
        container.hover(function() {
            clearTimeout(timeoutRef);
        },
        function() {
            timeoutRef = setTimeout(function() { nextItemRotator.showItem(startSelected + 1); }, 500);
        });
    }

    //even out bar heights
    var height = 0;
    $('.rotator-even', container).each(function() { if ($(this).height() > height) height = $(this).height() });
    $('.rotator-even', container).height(height);
}

$(function() {
    var inital_delay = 0;
    var delay_offset = 200;
    var current_delay_offset = inital_delay;
    $('.rotator').each(function() {
        includeNav = true;
        if ($(this).hasClass('noNav')) {
            includeNav = false;
        }

        current_delay_offset += delay_offset;
        if ($(this).is('.Random_True'))
            new rotator(this, { delay: current_delay_offset, resetTimerOnAction: true,
                showListLabel: includeNav,
                showListActions: includeNav
            });
        else
            new rotator(this, { delay: current_delay_offset, random: true, resetTimerOnAction: true,
                showListLabel: includeNav,
                showListActions: includeNav
            });
    });

    $('.paging').each(function() {
        new paging(this);
    })
})

$(function() {
    startItems = $('.div-start-item');
    $('.description', startItems).hide();
    startItems.mouseover(function() {
        $('.description', this).show();
    });
    startItems.mouseout(function() {
        $('.description', this).hide();
    });
})


$(function() {
    $('.sideNav span').click(function() {
        var element = $(this).next();
        if ((element.is('ul')) && (element.is(':visible'))) {
            $(this).removeClass('active');
            element.slideUp('fast');
            return false;
        }
        if ((element.is('ul')) && (!element.is(':visible'))) {
            $('.sideNav span').removeClass('active');
            $(this).addClass('active');
            $('.sideNav ul:visible').slideUp('fast');
            element.slideDown('fast');
        }
    });

    $(".sideNav li ul").hide();
    $(".sideNav li.selected ul").show();
});

$(function() {
    $('.kwicksSlider').kwicks({
        max: 400,
        spacing: 5,
        duration: 750,
        easing: 'easeInOutQuad'
    });
});