jQuery(document).ready(function () {

    var currentFeature = jQuery('#featured .current');
    var currentHandle = jQuery('#rotatorNav ul .current');
    var count = 0;
    var rotateTime = 6000;
    var fadeTime = 300;
    var numFeatures = 0;
    var features = jQuery('#featured div.feature');
    var allowChange = true;

    jQuery.each(features, function () {
        numFeatures++;
    });

    var setCurrent = function (num) {
        currentFeature.fadeOut(fadeTime, function () {
            currentFeature.removeClass('current');
            currentHandle.removeClass('current');
            currentFeature = jQuery('#featured div.feature:eq(' + num + ')');
            currentHandle = jQuery('#rotatorNav ul li:eq(' + num + ')');
            currentHandle.addClass('current');
            currentFeature.addClass('current');
            currentFeature.hide();
            currentFeature.fadeIn(fadeTime);
            allowChange = true;
        });

        count = num;

    }

    var setRotate = function () {
        count++;
        if (count >= numFeatures) {
            count = 0;
        }
        setCurrent(count);
    }

    jQuery('#featuredRotator').everyTime(rotateTime, "rotate", function () {
        setRotate()
    });

    jQuery('#rotatorNav li').click(function () {

        if (!jQuery(this).hasClass('current')) {
            setCurrent(jQuery(this).index());
        }

    });

    jQuery('#rotatorNav .left').click(function () {
        if (allowChange) {
            allowChange = false;
            if (count <= 0) {
                count = 3;
            }
            setCurrent(count - 1);
        }
    });

    jQuery('#rotatorNav .right').click(function () {
        if (allowChange) {
            allowChange = false;
            if (count >= numFeatures - 1) {
                count = -1;
            }
            setCurrent(count + 1);
        }
    });

    jQuery('#featuredRotator').hover(
	  	function () {
	  	    jQuery('#featuredRotator').stopTime("rotate");
	  	},
	  	function () {
	  	    jQuery('#featuredRotator').everyTime(rotateTime, "rotate", function () {
	  	        setRotate()
	  	    });
	  	}
	);


});
