var PANES = ['undergrad','postgrad','current','employers','staff'];
var SWITCH_TIMEOUT_TIME = '20';

var currentPane = 'about';
var switchTimout;

var setActiveButton = function(newPane) {
	// Set link box hover and remove hover from others
	PANES.each(function(pane){ $(pane + '-button').removeClassName('hover'); });
	$(newPane + '-button').addClassName('hover');
};

var catchNoPanelsVisible = function() {
	if ((!Effect.Queues.get('fades').length && !Effect.Queues.get('appears').length) || (
		Effect.Queues.get('fades').length == 0 && Effect.Queues.get('appears').length == 0)) {
		if (!$(currentPane+'-pane').visible()) {
			$(currentPane+'-pane').style.display = 'block';
		}
	}
};

var clearPaneChange = function() {
	if (switchTimout) clearTimeout(switchTimout);
	setActiveButton(currentPane);
};

var setActivePane = function(newPane) {
	
	setActiveButton(newPane);

	// Set pane to visible
	if (switchTimout) clearTimeout(switchTimout);
	if (newPane == currentPane) return true;
	
	switchTimout = setTimeout(function(){
		Effect.Fade($(currentPane+'-pane'), { duration: 0.6, queue: { position:'end', scope:'fades' }, afterFinish: catchNoPanelsVisible });
		currentPane = newPane;
		var cookie = new WCookie('enghomepage-load-pane',newPane,0.5);
		Effect.Appear($(newPane+'-pane'), { duration: 0.6, queue: { position:'end', scope:'appears' }, afterFinish: catchNoPanelsVisible });
	},SWITCH_TIMEOUT_TIME);
};

Event.onDOMReady(function(){
	PANES.each(function(pane){
		$(pane + '-button').observe('mouseover',function(){setActivePane(pane);});
		$(pane + '-button').observe('mouseout',function(){clearPaneChange();});
		$(pane+'-pane').style.display = 'none';
	});
	
	$$('.newsSchedule .newsItem')[0].addClassName('top');
});