﻿var carouselInterval = 6000;
var carouselIntervals = new Array();

function popIntervals() {
    if (carouselIntervals.length == 0) return;
    while (carouselIntervals.length) {
        clearInterval(carouselIntervals.pop());
    }
}

function theRotator() {
    //Set the opacity of all images to 0
    $('div#rotator ul#rotator-images li').css({ opacity: 0.0 });

    //Get the first image and display it (gets set to full opacity)
    $('div#rotator ul#rotator-images li:first').css({ opacity: 1.0 });

    addNavRoll();
    positionRotItems();

    carouselIntervals.push(setInterval('rotate()', carouselInterval));
    $('#rotator').hover(function() {
        popIntervals();
    }, function() {
        carouselIntervals.push(setInterval('rotate()', carouselInterval));
    });
}

function addNavRoll() {
    $('ul#rotator-nav').children().each(function(index) {
        $(this).bind({
            mouseenter: function() {
                $(this).siblings().each(
			    function() {
			        $(this).removeClass('active');
			        $('#' + $(this).attr('id') + '-item').stop().animate({ opacity: 0.0 }, 1000)
				    .removeClass('active');
			    }
			  );
                $(this).addClass('active');
                $('#' + $(this).attr('id') + '-item').stop().animate({ opacity: 1.0 }, 1000)
				    .addClass('active');
            }
        });
    })
}

function positionRotItems() {
    var $rotWidth = $('ul#rotator-images').width();
    var $rotHeight = $('ul#rotator-images').height();
    var $capWidth = $('ul#rotator-images li div').width();

    $('ul#rotator-images').children().each(function(index) {
        $itemWidth = $(this).width();
        $itemHeight = $(this).height();
        $('ul#rotator-images li').eq(index).css({
            top: (($rotHeight - $itemHeight) / 2) + 'px',
            left: (($rotWidth - $itemWidth) / 2) + 'px'
        });
        $('ul#rotator-images li div').eq(index).css({
            left: (($itemWidth - $capWidth) / 2) + 'px',
            bottom: (($itemHeight - $rotHeight) / 2) + 'px'
        });
    })
}

function rotate() {

    var $active = $('ul#rotator-nav li.active');
    if ($active.length == 0) $active = $('ul#rotator-nav li:first');
    var $next = $active.next().length ? $active.next() : $('ul#rotator-nav li:first');

    $next.addClass('active')
    $active.removeClass('active');
    $('#' + $active.attr('id') + '-item').animate({ opacity: 0.0 }, 1000);
    $('#' + $next.attr('id') + '-item').css({ opacity: 0.0 })
		           .addClass('active')
				   .animate({ opacity: 1.0 }, 1000, function() {
				       $('#' + $active.attr('id') + '-item').removeClass('active')
				   });
};

$(document).ready(function() {
    //Load the slideshow
    theRotator();
    $('div#rotator').fadeIn(1000);
    $('div#rotator ul#rotator-images li').fadeIn(1000); // tweek for IE

    $(window).unload(function() {
        popIntervals();
    });
});
