// Initiates keyboard actions
jQuery(document).bind('keydown', 'Alt+Ctrl+space', function(){
	// prevent events from being added more than once
	if (!this.isPolicyMapKeyboardInit)
		initKeyboardAccessibility();

	this.isPolicyMapKeyboardInit = true;
});

function initKeyboardAccessibility() {
	var majorTools = {};
	majorTools.options = [];

	// text only link should be first so it gets highlighted when keyboard accessibility is initiated
	majorTools.options.push(jQuery('#textOnlyLink'));

	// get the links from each section that will be focused on
	var links = [];
	links.push(jQuery('#searchboxtabs').find('a'));
	links.push(jQuery('#nav').find('a'));

	// when menu is hidden don't make it keyboard accessible
	// widgetcolumn is the table field that holds the widget menus
	if (jQuery('#ind_menu_widget').css("display") != "none" && jQuery("#widgetcolumn").css("display") != "none")
		links.push(jQuery('#ind_menu_widget').find('a')); // for the widget

	links.push(jQuery('#legend').find('a'));

	// when menu is hidden don't make it keyboard accessible
	// widgetcolumn is the table field that holds the widget menus
	if (jQuery('#addsites').css("display") != "none" && jQuery("#widgetcolumn").css("display") != "none")
		links.push(jQuery('#addsites').find('a'));

	// loop through all the links and return the first focusable one from each section
	// a link has to have an href attribute to be focusable
	for (var j=0; j<links.length; j++) {
		for (var i=0; i<links[j].length; i++) {
			if (jQuery(links[j][i]).attr("href")) {
				majorTools.options.push(links[j][i]);
				break;
			}
		}
	}

	// this is the map link in template_top
	if (jQuery('#mapPageLink').length > 0)
		majorTools.options.push(jQuery('#mapPageLink'));

	// this is the map layer checkbox in the widget
	if (jQuery('#mapOptionsLayer').length > 0)
		majorTools.options.push(jQuery('#mapOptionsLayer'));

	// keeps track of what the next feature to focus on
	majorTools.index = 0;

	// set up keyboard actions
	if (map) {
		jQuery(document).bind('keydown', 'Ctrl+up', function(){ map.zoomIn(); });
		jQuery(document).bind('keydown', 'Ctrl+down', function(){ map.zoomOut(); });
	}
	jQuery(document).bind('keydown', 'Ctrl+right', function(){ 
		majorTools.index++;
		if (majorTools.index > majorTools.options.length-1)
			majorTools.index = 0;
		jQuery(majorTools.options[majorTools.index]).focus();
	});
	jQuery(document).bind('keydown', 'Ctrl+left', function(){
		majorTools.index--
		if (majorTools.index < 0)
			majorTools.index = majorTools.options.length-1;
		jQuery(majorTools.options[majorTools.index]).focus();
	});

	// focus on either the map page link or on the first available feature
	if (jQuery('#mapPageLink').length > 0)
		jQuery('#mapPageLink').focus();
	else
		jQuery(majorTools.options[0]).focus();
}


