/* accessability.js */



FOCUS_OUTLINE = '1px dotted blue';
FOCUS_OFFSET  = '-1px';
FOCUS_NONE    = 'none';
CANTAB        = 0;
CANNOTTAB     = -1;

INACTIVE_TXT = " [not selected]";
ACTIVE_TXT   = " [currently selected]";

// Set tab panels to be aria-compatible
$("#Address_Book").attr('role','tabpanel').attr('aria-labelledby','contacts-tab');
$("#Settings").attr('role','tabpanel').attr('aria-labelledby','settings-tab');

function showOutline(e){
  $("body *").css('outline',FOCUS_NONE);
  $(this).css('outline',FOCUS_OUTLINE)
         .css('outline-offset',FOCUS_OFFSET);  
}

function hideOutline(e){
  $(this).css('outline',FOCUS_NONE)
         .css('outline-offset',FOCUS_NONE);
}

function setFocusableElements()
{
/*
  $("[tabindex]").each(
    function(i,elem){

    if(parseInt($(elem).attr('tabindex')) >= CANTAB  ){
      if( $(elem).attr('disabled') != "true" ){
        $(elem).bind('focus', showOutline );
        $(elem).bind('blur',  hideOutline );
      } else {
        $(elem).attr('tabindex','-1');
      }
    } else if( parseInt( $(elem).attr('tabindex') ) == CANNOTTAB ){
      if($(elem).attr('disabled') != "true" ){
        $(elem).attr('tabindex','0');
        $(elem).bind('focus', showOutline );
        $(elem).bind('blur',  hideOutline );
      }
    }
	});
	*/
  // emulates focus for the parent objects of hidden elements
  $("input[type='checkbox'][name^='fields']").focus(
	function(){
	  $(this).parent().css('outline',FOCUS_OUTLINE);
	  /* $(this).parent().css('outline-offset',FOCUS_OFFSET); */
	}
  );
	
  $("input[type='checkbox'][name^='fields']").blur(
	function(){
	  $(this).parent().css('outline',FOCUS_NONE);
	}
  );

}

function setEmailContextTabOrder()
{
	var sections = ["#header","#actions","#secondary", "#primary #primary_content","#primary_content_inner","#mail_info"];
	var lastIdx  = setContextTabOrder(sections);
	$(".messageCheckbox").focus(function(e){
		$(this).parent().parent().css('outline',FOCUS_OUTLINE);
		$(this).parent().parent().css('outline-offset',FOCUS_OFFSET);
	});
	return lastIdx;
}

function setComposeMessageContextTabOrder()
{
	var lastIdx = setEmailContextTabOrder();
 
}

function setContactsContextTabOrder()
{
	var sections = ["#header","#actions","#secondary", "#address_contacts"];
	setContextTabOrder(sections);
}

function setSettingsContextTabOrder()
{
	var sections = ["#header","#Settings #secondary"];
	setContextTabOrder(sections);
	$("#Settings #primary_content [tabindex]").each(function(i,elem){
    $(elem).attr('tabindex','0');
	  $(elem).bind('focus',showOutline);
	});

}

function setContextTabOrder(sections)
{
	var ePos = 2;
	var lastIdx = 0;
	$("[tabindex]").filter(function(i){ return $(this).attr('tabindex') > -1 }).attr('tabindex',0);
	for(var i = 0; i < sections.length; i++)
	{
		$(sections[i] + " [tabindex]").each(function(e,elem){
			if(parseInt(elem.tabIndex) > -1 && $(elem).attr('disabled') != 'true')
			{
				elem.tabIndex = lastIdx = ePos++;
				$(elem).bind('focus',showOutline);
				// console.log(sections[i]+": <"+elem.tagName+ " tabindex='"+elem.tabIndex +"' type='"+elem.type+"' id='"+elem.id+"' name='"+elem.name+"' value='"+elem.innerHTML+"'  />");
			}
		});
	}
	return lastIdx;
}

function skipToContent(e)
{
	if(e.which == ENTER_KEY)
	{
		e.stopPropagation();
		e.preventDefault();
		context = $('ul#nav li a[aria-selected="true"]');
		switch($(context).attr('href'))
		{
			case '#Email':
				$('.messageCheckbox:first').focus();
				break;
			case '#Address_Book':
				$('.address_row:first').focus();
				break;
			case '#Settings':
			  $('table.settings td[tabindex]:first').focus();
				break;
			default:
		}

 		return false;
	}
}
