var tabRotator = function() {
	
	var taskManager = new Ext.util.TaskRunner();
	
	var schedule = [0,0,0,1,2,3];
	
	var selectTab = function(currentIndex) {
		var targetTab =  schedule[currentIndex+1];
		oTabRotator.stop();
		
		var target = {
			selector : oTabRotator.selectors[ targetTab ],
			tab: oTabRotator.tabs[ targetTab ]
		};
		
		target.selector.radioClass('ui-tabs-selected');

		Ext.each( oTabRotator.tabs, function(thisItem) {
			if( thisItem.id != target.tab.id ) {
				thisItem.addClass('ui-tabs-hide');
				thisItem.dom.style.display='none';
			}	
		});
		
		target.tab.removeClass('ui-tabs-hide');
		target.tab.dom.style.display='block';		
	};
	
	var rotatingTask = {
		run : function() {
			var currentIndex = oTabRotator.currentIndex > 5 ? 0 : oTabRotator.currentIndex;
			
			var targetTab =  schedule[currentIndex];

			var target = {
				selector : oTabRotator.selectors[ targetTab ],
				tab: oTabRotator.tabs[ targetTab ]
			};
			
			target.selector.radioClass('ui-tabs-selected');

			Ext.each( oTabRotator.tabs, function(thisItem) {
				if( thisItem.id != target.tab.id ) {
					thisItem.addClass('ui-tabs-hide');
					thisItem.dom.style.display='none';
				}	
			});
			
			target.tab.removeClass('ui-tabs-hide');
			target.tab.dom.style.display='block';
						
			oTabRotator.currentIndex = currentIndex + 1;
			
		},
		interval: 10000
	};
	
	return {
		taskManager : taskManager,
		task: rotatingTask,
		schedule: schedule,
		currentIndex: 0,
		active: 0,
		select: selectTab,
		stop: function() {
			this.taskManager.stopAll();
		},
		resume: function() {
			this.taskManager.start(this.task);
		},
		init : function() {
			this.tabs = [];
			this.selectors = [];
				
			for(var i=1;i<=4;i++) {
				var selector = Ext.get('tab'+i+'_selector');
				var tab = Ext.get('tab'+i);
				
				this.selectors.push(selector);
				this.tabs.push(tab);
								
			}	
			
			this.taskManager.start( this.task );
		}
	}
};

var oTabRotator = tabRotator();

Ext.onReady(function() {
	oTabRotator.init();
});
