// JScript File

//Global variables
/*var min=8;
var max=18;
var elementArray = new Array(2);
var s;
var contrastCookie = null;
var fontSizeCookie = null;
var FONT_COOKIE_NAME = 'dementiaFontSizeCookie';
var DEFAULT_FONT_SIZE = 12;
//FONT SIZING//
//Get all the elements that might contain text. 
//Note: we aren't changing the H2 or H3 tags
var p   = document.getElementsByTagName('p');
var li  = document.getElementsByTagName('li');

elementArray[0] = p;
elementArray[1] = li;

//increase font
function increaseFontSize() {
    s = getFontSizeFromCookie();
    for(var index = 0; index < elementArray.length; index++) {
        var curElements = elementArray[index];
       
        for(var i=0;i<curElements.length;i++) {
          if(curElements[i].style.fontSize) {
             s = parseInt(curElements[i].style.fontSize.replace("px",""));
          } else {
             
             s = 12;
          }
          if(s!=max) {
             s += 1;
          }
          curElements[i].style.fontSize = s+"px";
           
        }          
    } 
    updateFontCookie(s);
}

//Decrease font 
function decreaseFontSize() {
    s = getFontSizeFromCookie();
   for(var index = 0; index < elementArray.length; index++) {
        var curElements = elementArray[index];
       
        for(var i=0;i<curElements.length;i++) {
          if(curElements[i].style.fontSize) {
             s = parseInt(curElements[i].style.fontSize.replace("px",""));
          } else {
             s = 12;
          }
          if(s!=min) {
             s -= 1;
          }
          curElements[i].style.fontSize = s+"px";
        }          
    }   
        updateFontCookie(s);
}

//When going to a new page, check for cookie.
function newPageFontCookieCheck()
{
    if(isFontCookie()) {
        var currentSize;
        currentSize = getFontSizeFromCookie();
        updateFontCookie(currentSize);
        setFontSize(currentSize);
    } 
}

//Check for cookie
function isFontCookie() 
{
    var doesExist = null;    
    return doesExist = readCookie(FONT_COOKIE_NAME);

}

//delete then re-create cookie
function updateFontCookie(newFontSize)
{
    eraseCookie(FONT_COOKIE_NAME);
    createCookie(FONT_COOKIE_NAME,newFontSize);
}


//Get the font size from the cookie, 
//apply the new size to the p and li elements
function setFontSize(newFontSize)
{
    for(var index = 0; index < elementArray.length; index++) {
        var curElements = elementArray[index];
        for(var i = 0; i < curElements.length; i++) {
            curElements[i].style.fontSize = newFontSize+'px';          
        }            
    }
}

//Get the font size from the cookie
function getFontSizeFromCookie()
{
    var fontCookie = readCookie(FONT_COOKIE_NAME);
    
    if(fontCookie) {        
       return fontCookie;    
    }
}

*/

var currentSize = null;
var maxSize = 20;
var minSize = 12;

var isJourney = null;

//*************************************************************************************************
// Font sizer

// Make font larger
function makeLarger() {
	currentSize = currentSize.substring(0,2);
	var newSize = (currentSize * 1) + 2;
	var el = document.getElementById("container");
	
	if (newSize <= maxSize) {
		el.style.fontSize = newSize + "px";
	
		var lists = el.getElementsByTagName("li");
		for (var i = 0; i < lists.length; i++) {
			lists[i].style.fontSize = newSize + "px";
		}
	}
	
	currentSize = newSize + "px";
	
	// Set cookie for new size
	createCookie("font", newSize);
	
	journeyNav();
}

function makeSmaller() {
	currentSize = currentSize.substring(0,2);
	if (currentSize > 20) {
		currentSize = 20;
	}
	var newSize = (currentSize * 1) - 2;
	var el = document.getElementById("container");
	
	if (newSize >= minSize) {
		el.style.fontSize = newSize + "px";
	
		var lists = el.getElementsByTagName("li");
		for (var i = 0; i < lists.length; i++) {
			lists[i].style.fontSize = newSize + "px";
		}
		
		if (newSize <= 12) {
			var nav = document.getElementById("navigation").getElementsByTagName("ul")[0].getElementsByTagName("li");
			for (var i = 0; i <= nav.length; i++) {
				nav[i].style.fontSize = "14px";
			}
		}
	}
	
	currentSize = newSize + "px";
	
	// Set cookie for new size
	createCookie("font", newSize);
	
	if ((isJourney) && (newSize < 16)) {
		var header = document.getElementById("header");
		header.style.height = "287px";
		header.style.backgroundImage = "url(/App_Themes/DementiaJourney/Images/journey_top_bg.jpg)";
	}
}



// Set up font sizer event handlers
function fontEvent() {
	
	var resizeLinks = document.getElementById("siteFunctions").getElementsByTagName("a");
	var smaller = resizeLinks[1];
	var larger = resizeLinks[2];
	
	if (navigator.appName == "Microsoft Internet Explorer") {
      smaller.attachEvent("onclick", makeSmaller);
	  larger.attachEvent("onclick", makeLarger);
	  currentSize = getStyle("container","fontSize");
      }
      
   else {
      smaller.addEventListener("click", makeSmaller, false);
	  larger.addEventListener("click", makeLarger, false);
	  currentSize = getStyle("container","font-size");
      }
}

// Check for font size cookie - if one is set, load the saved font size
function checkFont() {
	if (readCookie("font")) {
		newSize = readCookie("font");
		var el = document.getElementById("container");
		el.style.fontSize = newSize + "px";
		var lists = el.getElementsByTagName("li");
		for (var i = 0; i < lists.length; i++) {
			lists[i].style.fontSize = newSize + "px";
		}
	}
}

//*************************************************************************************************
// Journey nav bar
// Check whether the journey nav bar is present, and if it is then set a different page header

function journeyNav() {
	var isPresent = null;
	var h = "287px";
	var bg = "url(/App_Themes/DementiaJourney/Images/journey_top_bg.jpg)";
	isPresent = document.getElementById("journey_nav");
	if (isPresent) {
		// If font size is larger, sub larger header 
		var fontSize = document.getElementById("navigation").getElementsByTagName("li")[0].style.fontSize;
		if (fontSize) {
			fontSize = fontSize.split("px");
			fontSize = fontSize[0];
			if (fontSize >= 16) {
				h = "330px";
				bg = "url(/App_Themes/DementiaJourney/Images/journey_top_bg_ext.jpg)";
				document.getElementById("navigation").style.background = "none";
			}
		}
		
		var header = document.getElementById("header");
		header.style.height = h;
		header.style.backgroundImage = bg;
		isJourney = true;
	}
}


//*************************************************************************************************
/* CONTRAST */
// Check whether contrast cookie exists, if it does then apply the high contrast styles on page load
function checkContrast() {
   var isSet = readCookie('contrast');
   
   if (isSet) {
      var bodyClass = document.body.className;
      bodyClass = bodyClass + " contrast";
      
      if (navigator.appName == "Microsoft Internet Explorer") {
         document.body.setAttribute('className', bodyClass);
      }
      else {
         document.body.setAttribute('class', bodyClass);
         }
   }
}

// Change the contrast from default to high contrast and back again
function doContrast() {
   var docBody = document.body;
   var bodyClass = document.body.className;
   
   var isContrast = bodyClass.split('cont');
   if (isContrast[1] == "rast") {
      var newClass = bodyClass.split(' cont');
      newClass = newClass[0];
      contrastCookie = false;
      eraseCookie("contrast");
      }
   else {
     var newClass = bodyClass + " contrast";
     contrastCookie = true; // Set contrast cookie
     createCookie("contrast", contrastCookie);
     }
   
    if (navigator.appName == "Microsoft Internet Explorer") {
       docBody.setAttribute('className', newClass);
       }
    else {
       docBody.setAttribute('class', newClass);
       }
}

// Add the event handler to the contrast text
function contrastEvent() {
   var contrastButton = document.getElementById("highcontrast");
  
   if (navigator.appName == "Microsoft Internet Explorer") {
      contrastButton.attachEvent("onclick", doContrast);
      }
      
   else {
      contrastButton.addEventListener("click", doContrast, false);
      }
}


/* COOKIE */
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);
}

function getStyle(el,styleProp)
{
	var x = document.getElementById(el);
	if (x.currentStyle)
		var y = x.currentStyle[styleProp];
	else if (window.getComputedStyle)
		var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
	return y;
}



function init() {
/*    //Check font cookie
    newPageFontCookieCheck();
	*/
	
	// Check for and load saved font size
	checkFont();
	
	// Add font sizer event handlers
	fontEvent();
    
   // Check whether high contrast cookie is set
   checkContrast();
   
   // Add contrast event handlers
   contrastEvent();
   
   // Check whether the journey nav bar is present, and if it is then set different header properties
   journeyNav();
   
  /* // Run menu function for Internet Explorer 6
  This function should only be enabled when drop-down menus are active - they're currently disabled
   menu(); */
   
}

window.onload = init;


    
