/**************************************************************************
* Menu / Rollover Handler
*
* Sets up links, drop down menus, and rollover image links.
**************************************************************************/
var dsd_menu = {
	_auto_close_time: 100
	, _close_timeout: false
	, _current_menu: 0
	, init: function() {	
		// rollovers
		$('a.rollover').each(function(){
			var a = new Image();
			var b = $(this).children().attr('src');
			a.src = ( b.substring(0,b.lastIndexOf('.')) + '-over' + b.substring(b.lastIndexOf('.'),b.length) );
			this.oldImg = a;
			this.onmouseover = dsd_menu.swap;
			this.onmouseout = dsd_menu.swap;
		});
		
		// drop downs
		$('a.drop-down').each(function() {
			this.onmouseover = dsd_menu.open;
			this.onmouseout = dsd_menu.start_close;
			
			$(this).next('.sub-menu').onmouseover = dsd_menu.cancel_close;
			$(this).next('.sub-menu').onmouseout = dsd_menu.start_close;
		});		
	}, swap: function() {
		// if a drop down is open, close it
		if (this.oldImg) {
			a = $(this).children().attr('src');
			$(this).children().attr('src', this.oldImg.src);
			this.oldImg.src = a;
		}
	}, open: function() {
		dsd_menu.cancel_close();	
		dsd_menu.close();
		if (!$(this).next('.sub-menu').hasClass('out')) {
			if ($(this).hasClass('rollover')) {
				dsd_menu.swap.call($(this));
			}
		}
		dsd_menu._current_menu = $(this).next('.sub-menu');
		$(this).next('.sub-menu').show(); // could use effect here
		$(this).next('.sub-menu').addClass('out');
	}, close: function() {
		if (dsd_menu._current_menu) {
			if (dsd_menu._current_menu.prev('a.drop-down').hasClass('rollover')) {
				if (dsd_menu._current_menu.hasClass('out')) {
					dsd_menu.swap.call(dsd_menu._current_menu.prev('a.drop-down'));
				}
			}
			dsd_menu.close_menu(); // could use effect here
		}
	}, close_menu: function() {
		if (dsd_menu._current_menu) {
			dsd_menu._current_menu.removeClassName('out');
			dsd_menu._current_menu.hide(); // could use effect here
			dsd_menu._current_menu = false;
		}
	}, start_close: function() {
		dsd_menu._close_timeout = window.setTimeout(dsd_menu.close, dsd_menu._auto_close_time);
	}, cancel_close: function() {
		if(dsd_menu._close_timeout) {
			window.clearTimeout(dsd_menu._close_timeout);
			dsd_menu._close_timeout = false;
		}
	}, debug_time: function() {
		var d = new Date()

		return d.getMinutes()+':'+d.getSeconds()+'.'+d.getMilliseconds();
	}
};


$(document).ready( function () {
	$('.dropdown').mouseenter( function () { $(this).addClass('shown'); } ).mouseleave( function () { var shown = this; setTimeout( function () { $(shown).removeClass('shown'); }, 500) } );
	$('#new_toc .roll-over').mouseenter( function () { $('.dropdown.shown').removeClass('shown'); });
	
	setInterval("flipToNext()", 7000);
});

function clearShownDropdowns() {

}
function flipToNext() {

	var transitionTime = 500;
	var current = $('.newheaderimg.shown');
	var next = current.next('.newheaderimg');
	
	if (! next.length ) next = $('.newheaderimg').first();
	
	current.fadeOut(transitionTime).removeClass('shown');
	next.fadeIn(transitionTime).addClass('shown');
}
