var baseFontSize="";

function doPreloadAccessibilityOpts() {
	
	 jQuery.log("doPreloadAccessibilityOpts");
	 
	 jQuery.log("Base font size = "+$('html').css('font-size'));
	 
	 baseFontSize=$('html').css('font-size');
	 
	 // IE only
	 if($.browser.msie){
	 	$("link[title='Normal text']").disabled = true;
		$("link[title='Current Stylesheet']").disabled = true;
	  //$("link[title='High contrast']").disabled = false;
	 }
	 
	 
	 //doResizeCheck();
	 
	 //axs.keyreg("control+=",function(){setTimeout(doResizeCheck,500)},{des:"resize check", bub:true})
	 //axs.keyreg("control+-",function(){setTimeout(doResizeCheck,500)},{des:"resize check", bub:true})

	 
	  $.getScript("/js/jquery.scrollTo-min.js");
	  
	  $("#dash-ctn").focus(function(){
			if( $("#dash-ctn").val()=="Mobile/Username" ){
				$("#dash-ctn").val("");
			}
	   });
	  
	  $("#dash-pass").focus(function(){
			if( $("#dash-pass").val()=="??????" ){
				$("#dash-pass").val("");
			}
	   });
	  
 $("#keywords").focus(function(){
			if( $("#keywords").val()=="Search o2.ie" ){
				$("#keywords").val("");
			}
	   });
	  
	  
	  
	  var cAccess = readCookie('accessPanel');
      if (cAccess && (cAccess=="true") ) {
		  $("#accessPanel").addClass("show-accessPanel");
		  $.ariaShow("#accessPanel");
		  $("#headerAccessLink").html("Hide Accessibility Options");
	  }else{
	 	 $.ariaHide("#accessPanel");
		 $("#headerAccessLink").html("Show Accessibility Options");
	  }
	  
	  var cTextInfo = readCookie('accessPanelSizeInfo');
      if (cTextInfo && (cTextInfo=="true") ) {
		  $.ariaShow("#textSizeChangeInfo");
	  }else{
	 	 $.ariaHide("#textSizeChangeInfo");
	  }
	   
	  $("a#headerAccessLink").click(function()
		  {
			  if($("#accessPanel").hasClass("show-accessPanel")){
				  $("#accessPanel").removeClass("show-accessPanel");
				  $.ariaHide("#accessPanel");
				  createCookie('accessPanel', "false", 365);
				  $("#headerAccessLink").html("Show Accessibility Options");
			  }
			  else{
				  	$("#accessPanel").addClass("show-accessPanel");
					 $.ariaShow("#accessPanel");
					 createCookie('accessPanel', "true", 365);
					 $("#headerAccessLink").html("Hide Accessibility Options");
					// $.scrollTo("#accessPanel", 400);
					$("#accessPanel a:first").focus();
			  }
			  return false;
		  });
		  
	$("a#headerAccessLink").keydown(function(e){
			 $.log($(this) + " keydown");
			if (e.keyCode == 13) {
				 $.log($(this) + " keydown [enter]");
				 $(this).click();
				 return false;
			}
			
			return true;
		});



      $('.text-size-selector a').each(function(){
		  $(this).click(function()
		  {
			 var selStyle = $(this).parent().attr('class');
			 //console.log("Sel Style"+selStyle);
			 doSwitchGrid("grid-"+selStyle);
			 return false;
		  });
	  });
	  
	  $('a.text-contrast-selector').each(function(){
		  $(this).click(function()
		  {
			 var selStyle = $(this).parent().attr('class');
			 //console.log("Sel Style"+selStyle);
			 //doSwitchTextContrast("text-style-"+selStyle);
				 if(  $("body").hasClass("high-contrast") && $(this).attr("id")=="regularContrastLink"){
				 doSwitchTextContrast("Normal text");
				 $("body").removeClass("high-contrast");
				 //$(this).html("Switch to High Contrast");
			 }
			 else{
				 doSwitchTextContrast("High contrast");
				 $("body").addClass("high-contrast");
				 
				 $("body").removeClass("high-contrast-yellow-blue");
				$("body").removeClass(" high-contrast-yellow-black");
				 
				 switch($(this).attr("id")) {
					 case "highYellowBlackLink" :  $("body").addClass("high-contrast-yellow-black"); break;
					 case "highYellowBlueLink" :  $("body").addClass("high-contrast-yellow-blue"); break;
				 }
				 // $(this).html("Switch to Regular Contrast");
			 }
			 return false;
		  });
		  
		  $(this).keydown(function(e){
			if (e.keyCode == 13) {
				 $(this).click();
				 return false;
			}
			return true;
		});
	  });
	  
	   var browseAloud = "This link will bring you to the Browse Aloud website where you can download free software which will read text on this website aloud. This software may assist users with a visual impairment or dyslexia, but it will not be of use to completely blind users. Do you wish to continue to the site?";
	  $("#listenToSiteLink").click(function(){
			return confirm(browseAloud);									
		});
	  
	   $("#listenToSiteLink").keydown(function(e){
			if (e.keyCode == 13) {
			return confirm(browseAloud);		
			}
			
			return true;
		});
	   
	   $("#skipToSearchLink").keydown(function(e){
			$.log("#skipToSearchLink keyDown");
			if (e.keyCode == 13) {
				 $.scrollTo("#keywords", 400);
				 $("#keywords").focus();
			return false;
			}
			
			return true;
		});
	   
	      $("#skipToSearchLink").click(function(){
				 $.scrollTo("#keywords", 400);
				 $("#keywords").focus();
			return false;
	
		});
		  
		    $("#toggleAnimLink").keydown(function(e){
			$.log("#toggleAnimLink keyDown");
			if (e.keyCode == 13) {
					$("object,embed").remove();
					$("body").addClass("no-animation");
			return false;
			}
			
			return true;
		});
	   
	      $("#toggleAnimLink").click(function(){
				$("object,embed").remove();
				$("body").addClass("no-animation");
			return false;
	
		});
		  
		  
	  
	  	  $('a.textSizeInfo').each(function(){
		  $(this).click(function()
		  {
			  	 
			 if(  $(this).hasClass("open") ){
				 $.log("textSizeInfo click hasClass(\"open\")");
				 $.ariaHide("#textSizeChangeInfo");
				 $(this).removeClass("open");
				 createCookie('accessPanelSizeInfo', "false", 365);
			 }
			 else{
				 $.log("textSizeInfo click does not hasClass(\"open\")");
				 $(this).addClass("open");
				 createCookie('accessPanelSizeInfo', "true", 365);
				 $.ariaShow("#textSizeChangeInfo");
				  //$.scrollTo("#textSizeChangeInfo", 700);
				  $("#textSizeChangeInfo").focus();
				  
			 }
			 return false;
		  });
		  
		  $(this).keydown(function(e){
			if (e.keyCode == 13) {
				 $(this).click();
				 return false;
			}
			return true;
		});
	  });
	  
      var cText = readCookie('accessi-style');
      if (cText) doSwitchGrid(cText);
	  
	  var cContrast = readCookie('contrast-style');
      if (cContrast) {
		  	 if( cContrast == "Normal text" ){
				 doSwitchTextContrast("Normal text");
				 $("body").removeClass("high-contrast");
				 $('a.text-contrast-selector').each(function(){
				 //$(this).html("Switch to High Contrast");
															 });
			 }
			 else{
				 doSwitchTextContrast("High contrast");
				 $("body").addClass("high-contrast");
				 $('a.text-contrast-selector').each(function(){
				// $(this).html("Switch to Regular Contrast");
				 });
			 }
	  }
   }
   
 
 function doSwitchGrid(styleName)
 {
	 var selHref = $("link[title='"+styleName+"']").attr('href');
	 $("link[title='grid-active']").attr('href', selHref);
	 createCookie('accessi-style', styleName, 365);
	 //console.log(styleName);
	 /*
	 switch(styleName){
		case "grid-normal": $("body").css('font-size','12px');break;
		case "grid-large": $("body").css('font-size','16px');break;
		case "grid-very-large": $("body").css('font-size','20px');break;
	 }
	 */
 }
 
   function doSwitchTextContrast(styleName)
 {

	 var selHref = $("link[title='"+styleName+"']").attr('href');
	 
	 if($.browser.msie){
			//$("link[title='Current Stylesheet']").remove();
			if(styleName=="High contrast"){
				/*$("link[title='High contrast']").disabled = false;
				$("link[title='Current Stylesheet']").disabled = true;
				$("link[title='Normal text']").disabled = true;*/
					toggleStyle('High contrast', false);
				toggleStyle('Current Stylesheet', true);
				toggleStyle('Normal text', true);
				/*
				$("link[title='High contrast']").attr('rel', 'stylesheet');
				$("link[title='Normal text']").attr('rel', 'alternate stylesheet');
				$("link[title='Current Stylesheet']").attr('rel', 'alternate stylesheet');
				*/
			}
			else{
				/*	$("link[title='High contrast']").disabled = true;
				$("link[title='Current Stylesheet']").disabled = false;
				$("link[title='Normal text']").disabled = false;*/
				toggleStyle('High contrast', true);
				toggleStyle('Current Stylesheet', false);
				toggleStyle('Normal text', false);
				/*
				$("link[title='High contrast']").attr('rel', 'alternate stylesheet');
				$("link[title='Normal text']").attr('rel', 'alternate stylesheet');
				$("link[title='Current Stylesheet']").attr('rel', 'stylesheet');
				*/
			}
	 }
	 else{
	 	$("link[title='Current Stylesheet']").attr('href', selHref);
	 }
	 createCookie('contrast-style', styleName, 365);
	 
	 
 }
 
 
// cookie functions http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name)
{
	createCookie(name,"",-1);
}
// /cookie functions

function doResizeCheck(){
	 jQuery.log("doResizeCheck : Base font size = "+$('html').css('font-size'));
	if( (!$("body").hasClass("high-contrast")) && (!$('html').css('font-size')!=baseFontSize)){
		 jQuery.log("doResizeCheck : remove main menu bg");
		$("body").addClass("hide-nav1-bg");
		 jQuery.log("Base font size = "+$('html').css('font-size'));
		 baseFontSize=$('html').css('font-size');
	}
	else if( $("body").hasClass("high-contrast") ){
		$("body").removeClass("hide-nav1-bg");
	}
}


$(document).ready(doAddConfirmToOffSiteLinks);

function doAddConfirmToOffSiteLinks(){
	$("a.off-site-link-confirm").each(
		function(){
			$(this).click(onOffSiteLinkClick);
		}
	);
}

function onOffSiteLinkClick(){
	return confirm("Do you wish to leave O2.ie and continue to '"+$(this).text()+"'?");	
}

function toggleStyle(styleName, flag){
$('link[rel*=style][title]').each(function(i) 
		{
			if (this.getAttribute('title') == styleName) this.disabled = flag;
		});
}
