var rotatorTimeout;
var rotatorFirstTime = true;
function rotatorGoTo(newItemIndex) {
    if (!$('#rotatorWrapper').hasClass('hovered') || newItemIndex != -1) {
        var previousItem = $('#rotatorBanners li.active');
        var previousItemIndex = $('#rotatorBanners li').index(previousItem);
        var lastItem = $('#rotatorBanners li:last');
        var lastItemIndex = $('#rotatorBanners li').index(lastItem);
        
        if (newItemIndex == -1) {
            newItem = $('#rotatorBanners li.active').next();
            var newItemIndex = $('#rotatorBanners li').index(newItem);
        }
        
        if (newItemIndex > lastItemIndex || newItemIndex == -1) newItemIndex = 0;
        
        //alert(newItemIndex);
        var slide = $('#rotatorBanners li:eq('+previousItemIndex+')').hasClass('slide') || $('#rotatorBanners li:eq('+newItemIndex+')').hasClass('slide');
        if (slide) {
            $('#rotatorBanners li:eq('+previousItemIndex+')').hide();
            $('#rotatorBanners li:eq('+newItemIndex+')').show();
            $('#rotatorBanners li:eq('+previousItemIndex+')').removeClass('active');
    	    $('#rotatorBanners li:eq('+newItemIndex+')').addClass('active');
        } else {
            $('#rotatorBanners li:eq('+previousItemIndex+')').fadeOut(1000);
            $('#rotatorBanners li:eq('+newItemIndex+')').fadeIn(1000, function(){
                $('#rotatorBanners li:eq('+previousItemIndex+')').removeClass('active');
                $('#rotatorBanners li:eq('+newItemIndex+')').addClass('active');
            });
        }
        $('#rotatorNavigation li:eq('+previousItemIndex+')').removeClass('active');
        $('#rotatorNavigation li:eq('+newItemIndex+')').addClass('active');
        if ($('#rotatorBanners li:eq('+newItemIndex+')').hasClass('slide')) {
            rotatorProcessQueue(13500);
        } else {
            rotatorProcessQueue();
        }
    }
}

function rotatorProcessQueue(time) {
    if (rotatorFirstTime) {
        if (!time) time = 13500;
    } else {
        if (!time) time = 13500;
    }
    rotatorFirstTime = false;
    rotatorTimeout = setTimeout(function(){rotatorGoTo(-1)}, 13500); //client wants all states to wait 8 seconds
}

function initializeRotator() {
    var rotatorNavigation = $('#rotatorNavigation');
    var lastItem = $('#rotatorBanners li:last');
    var lastItemIndex = $('#rotatorBanners li').index(lastItem);
    for (i=0; i<=lastItemIndex; i++) {
        $(rotatorNavigation).append('<li>');
    };
    $(rotatorNavigation).find('li').bind('mouseover', function(){
        $(this).addClass('hovered');
    }).bind('mouseout', function(){
        $(this).removeClass('hovered');
    }).bind('click', function() {
        var clickedItemIndex = $('#rotatorNavigation li').index(this);
        clearTimeout(rotatorTimeout);
        rotatorGoTo(clickedItemIndex);
    });
    $('#rotatorNavigation li:first').addClass('active');
    $('#rotatorBanners li:first').addClass('active');
    $('#rotatorWrapper').mouseover(function(){
        $(this).addClass('hovered');
        clearTimeout(rotatorTimeout);
    }).mouseout(function(){
        $(this).removeClass('hovered');
	clearTimeout(rotatorTimeout);
        rotatorProcessQueue();
    });
}

$(document).ready(function() {
    initializeRotator();
});


$(window).load(function() {
    rotatorProcessQueue();
});


