/**************************************************************************
* Menu / Rollover Handler
*
* Sets up links, drop down menus, and rollover image links.
* Scott Wespi (scott@darkstardesign.com)
*
* Requires JQuery
**************************************************************************/
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;
		});		
		
		// offsite and file
		$('a[rel=offsite]').each(dsd_menu.new_window);
		$('a[rel=off-site]').each(dsd_menu.new_window);
		$('a[rel=file]').each(dsd_menu.new_window);	
	}, 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;
		}
	}, new_window: function() {
		this.target = '_blank';
	}
};
