﻿
// Rotator Version 1.1 - Added Dynamic Navigation

if ($('.masthead-inner li').length <= 1) {

} else {
    var t;

    function rotator() {
        $('.masthead-inner li').hide().css({ 'opacity': '0' });
        $('.masthead-inner li:first').addClass('show').css({ 'opacity': '1' }).show();
        t = setInterval('rotate()', 10000);
    }
    function rotate() {
        if (!$('.masthead-inner li.show').is(':last-child')) {
            $('.masthead-inner li.show').animate({ 'opacity': '0' }, 0).hide().removeClass('show').next().animate({ 'opacity': '1' }, 1000).show().addClass('show');
        } else {
            $('.masthead-inner li.show').animate({ 'opacity': '0' }, 0).hide().removeClass('show');
            $('.masthead-inner li:first-child').animate({ 'opacity': '1' }, 1000).show().addClass('show');
        }
    };

    function createNav() {
        $('.masthead-inner').append('<div class="rotator-nav"><a class="prev">Previous</a><a class="next">Next</a></div>');
        $('.rotator-nav').css({ 'opacity': '0' });
        $('.masthead-inner').hover(
                function () {
                    $('.rotator-nav').stop().animate({ 'marginTop': '20px', 'opacity': '1' }, { duration: 'fast', easing: 'easeOut' });
                    clearInterval(t);
                },
                function () {
                    $('.rotator-nav').stop().animate({ 'marginTop': '0', 'opacity': '0' }, { duration: 'fast', easing: 'easeIn' });
                    t = setInterval('rotate()', 10000);
                }
            );

        $('.next').live('click', function () {
            clearInterval(t);
            rotate();
            t = setInterval('rotate()', 10000);
        });

        $('.prev').live('click', function () {
            clearInterval(t);
            if ($('.masthead-inner li.show').is(':first-child')) {
                $('.masthead-inner li.show').animate({ 'opacity': '0' }, 0).hide().removeClass('show')
                $('.masthead-inner li:last-child').animate({ 'opacity': '1' }, 1000).show().addClass('show');
            } else {
                $('.masthead-inner li.show').animate({ 'opacity': '0' }, 500).hide().removeClass('show').prev('li').animate({ 'opacity': '1' }, 500).show().addClass('show');
            }
            t = setInterval('rotate()', 10000);
        });
    };
}
$(function () {
    createNav();
    rotator();
});
