//loads image arrays for fontSwitcher control
if (document.images) {
    var imgActive = new Array();
    imgActive['Normal Font'] = new Image();
    imgActive['Large Font'] = new Image();
    imgActive['Normal Font'].src = 'http://www.keylaboratory.com/images/normalfont_active.gif';
    imgActive['Large Font'].src = 'http://www.keylaboratory.com/images/largefont_active.gif';
    
    var imgInactive = new Array();
    imgInactive['Normal Font'] = new Image();
    imgInactive['Large Font'] = new Image();
    imgInactive['Normal Font'].src = 'http://www.keylaboratory.com/images/normalfont.gif';
    imgInactive['Large Font'].src = 'http://www.keylaboratory.com/images/largefont.gif';        
}

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
  setImages(title); //custom code
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

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 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;
}

//this shows the fontSwitcher control if JavaScript is working in the browser
function showFontSwitcher() {
    document.getElementById('fontSwitcher').style.display = 'block';
}

//this sets the images in the fontSwitcher control based on current user preference
function setImages(title) {
    //reset images first
    document.images['NormalFont'].src = imgInactive['Normal Font'].src;    
    document.images['LargeFont'].src = imgInactive['Large Font'].src;
    var titleId = trimSpaces(title); //we need to do this because our element ids can't have spaces
    document.images[titleId].src = imgActive[title].src;
}

//define the variable for the go-away timer
var timer;

//show the accessibility popup when the fontSwitcher is hovered over
function showAccessPopup() {
    //first stop the hide timer if it is running
    clearTimeout(timer);
    placeAccessPopup();    
    var accessPopup = document.getElementById('accessPopup');
    //if (accessPopup.filters)
    //    accessPopup.filters[0].apply();            
    accessPopup.style.display = 'block';
    //if (accessPopup.filters)
    //   accessPopup.filters[0].play(0.3);
}//end function

//place the fontSwitcher on the page where we want it
function placeAccessPopup() {
    var accessPopup = document.getElementById('accessPopup');
    accessPopup.style.top = (findPosY(document.getElementById('NormalFont')) + 14) + 'px';
    accessPopup.style.left = (findPosX(document.getElementById('NormalFont')) - findPosX(document.getElementById('aspnetForm'))) + 'px';
}//end function

//hides (or fades out if IE) the accessibility popup
function fadeAccessPopup() {
    var accessPopup = document.getElementById('accessPopup');
    //if (accessPopup.filters)
    //    accessPopup.filters[0].apply();            
    accessPopup.style.display = 'none';
    //if (accessPopup.filters)
    //    accessPopup.filters[0].play(0.5);
}//end function

//call the function after a set amount of time
function hideAccessPopup() {
    timer = setTimeout("fadeAccessPopup()", 1000);
}//end function

//load the user's font size preference, or the default if none is chosen
window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();   
  showFontSwitcher();
  setActiveStyleSheet(title);
}

//sets the user's font size preference if it was changed
window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}