$(function() {

    var rotateDelay = 4000; //milliseconds
    var rotateTabs=true;
    var $tabItems = $('#nav ul li a').click(function(){ //clicking a tab should cancel the rotate
        rotateTabs=false;
		var url = this.href;
		location.href = url;
    });
	
    var tabs = $("#nav ul").tabs('#panes > div', {api:true, effect:'fade', fadeInSpeed:1000, fadeOutSpeed:1000}); //create the tabs and return the api
	
    function doRotateTabs(){ //recursive function 
        if (rotateTabs) {
            setTimeout(function(){
                if (!rotateTabs) return;
                if(tabs.getIndex() == $tabItems.length-1){ //last tab. restart on first tab
                    tabs.click(0);
                }
                else {
                    tabs.next();
                }
                doRotateTabs();
            }, rotateDelay);
        }
    }
    doRotateTabs();

});
