window.addEvent('domready', function() {
	var selectedSubMenu;

	$$('.dropmenudiv a').each(function(obj, index) {
		obj.addEvents({					
			mouseover: function(e) {
				$$('.dropmenudiv a').removeClass('hover');
				e.target.addClass('hover');
			},
			mouseout: function(e) {
				e.target.removeClass('hover');
			}
		});
	});
	
	$$('.topNav > li > a').each(function(obj, index) {
		if( obj.getAttribute("rel") ) {
			obj.addEvent('mouseover', function(e) {
				e.target.focus();
			})
			
			obj.addEvent('focus', function(e) {
				//when this event is triggered, the down key needs to set the hover/over status of each of the subitems in the menu
				selectedSubMenu = $(obj.getAttribute("rel"));
			});
		}
	});
	
	window.addEvent('keypress', function(e) {
		//exit if nothing is there
		if (e.key.match(/^(up)|(down)$/)) {
			if (typeof(selectedSubMenu) == 'undefined') 
				return e;
			
			//check if the menu item is currently visible
			v = !(selectedSubMenu.getStyle('visibility') == 'hidden');
			if (!v) 
				return e;
			
			//stop it from scrolling the page
			e.stop();
			
			var selectedElement = selectedSubMenu.getElement('.hover');
			var nextElement;
		}
			
		if( e.key.match(/^up$/) ) {
			if (selectedElement) {
				nextElement = selectedElement.getParent().getPrevious('li').getFirst('a');
			} else {
				nextElement = selectedSubMenu.getLast('ul').getLast('li').getLast('a');
			}
		}
		
		if( e.key.match(/^down$/) ) {
			if (selectedElement) {
				nextElement = selectedElement.getParent().getNext('li').getFirst('a');
			} else {
				nextElement = selectedSubMenu.getFirst('ul').getFirst('li').getFirst('a');
			}
		}

		if (selectedElement) {
			selectedElement.removeClass('hover');
		}
		if( nextElement ) {
			nextElement.addClass('hover');
		}
	});
	
	//adjust text size if the client has it
	if( Cookie.read('curTextSizeMod') ) {
		fontSize( (Cookie.read('curTextSizeMod')*1) );
	} else {
		Cookie.write('curTextSizeMod',curTextSizeMod, {duration:1, domain:location.hostname, path:'/'});
	}
});

var minTextSizeMod = 0;
var maxTextSizeMod = 4;
var curTextSizeMod = 0;

function increaseFontSize() {
	adjustFontSize( +1 );
}

function decreaseFontSize() {
	adjustFontSize( -1 );
}

function adjustFontSize( mod ) {
	fontSize( curTextSizeMod+mod );
}

function checkFontSizeLinkDisplay() {
	$('decreaseFontSizeLink').removeClass('maxed');
	$('increaseFontSizeLink').removeClass('maxed');

	if( curTextSizeMod == minTextSizeMod) {
		$('decreaseFontSizeLink').addClass('maxed');
	}
	if( curTextSizeMod == maxTextSizeMod) {
		$('increaseFontSizeLink').addClass('maxed');
	}
}

function fontSize( mod ) {
	var elems = $$('p,li > a,div > a,div > a > b,#footerWrap > div > div,.breadcrumbs,input,select,h1,h3,h4,h5,li,td,.urbanLogo');
	
	if( mod < minTextSizeMod ) mod = minTextSizeMod;
	if( mod > maxTextSizeMod ) mod = maxTextSizeMod;
	
	msg = '';
	
	elems.each(function(o, i) {
		ns = (o.getStyle('font-size').replace(/px/,'')*1) + mod-curTextSizeMod;
		try {
			o.setStyle('font-size', ns + 'px');
		} catch ( e ) {
			//msg +='Could not adjust font-size (' + o.getStyle('font-size') + ')\n';
		}
		
		nh = (o.getStyle('line-height').replace(/px/,'')*1) + mod-curTextSizeMod;
		try {
			o.setStyle( 'line-height' , nh + 'px' );
		} catch ( e ) {
			//msg +='Could not adjust line-height (' + o.getStyle('line-height') + ')\n';
		}
	});
	
	if( msg.length > 0) {
		alert( msg );
	}
	
	curTextSizeMod = mod;
	
	Cookie.write('curTextSizeMod',curTextSizeMod, {duration:1, domain:location.hostname, path:'/'});

	checkFontSizeLinkDisplay();
}

