﻿rotationInterval = ""

function theRotator() {
    $('div.rotator ul li').css({ opacity: 0.0 });
    $('div.rotator ul li:first').css({ opacity: 1.0 });
    rotationInterval = setInterval('rotate()', 4000);

}

function rotate() {
    //Get the first image
    var current = ($('div.rotator ul li.show') ? $('div.rotator ul li.show') : $('div.rotator ul li:first'));
    var currentBug = ($('#feature_promo_triggers ul li.active') ? $('#feature_promo_triggers ul li.active') : $('#feature_promo_triggers ul li:first'));

    if (current.length == 0) current = $('div.rotator ul li:first');
    if (currentBug.length == 0) currentBug = $('#feature_promo_triggers ul li:first');


    //Get next image, when it reaches the end, rotate it back to the first image
    var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div.rotator ul li:first') : current.next()) : $('div.rotator ul li:first'));
    var nextBug = ((currentBug.next().length) ? ((currentBug.next().hasClass('show')) ? $('#feature_promo_triggers ul li:first') : currentBug.next()) : $('#feature_promo_triggers ul li:first'));


    next.css({ opacity: 0.0 })
	.addClass('show')
	.animate({ opacity: 1.0 }, 1500);

    //Hide the current image
    current.animate({ opacity: 0.0 }, 1500)
	.removeClass('show');

    //advance bugs
    nextBug.addClass('active');
    currentBug.removeClass('active');
};

function stopSlideshow(showThisOne) {
    clearInterval(rotationInterval);

    $('#feature_promo_triggers ul li').removeClass('active');

    //set first in the lists
    activeOne = $('div.rotator ul li:first');
    activeBug = $('#feature_promo_triggers ul li:first');

    for (var i = 1; i < showThisOne + 1; i++) {
        if (i == showThisOne) {
            $('div.rotator ul li').animate({ opacity: 0.0 }, 250);
            activeOne.animate({ opacity: 1.0 }, 250);
            activeBug.addClass('active').css;
        }
        nextOne = activeOne.next();
        activeOne = nextOne;

        nextBug = activeBug.next();
        activeBug = nextBug;
    }
}

$(document).ready(function() {
    //Load the slideshow
    theRotator();
    $('div.rotator').fadeIn(1500);
    $('div.rotator ul li').fadeIn(1500); // tweek for IE
});
