// --------------------------------------------------------------------
// Javascript Magnifier v 0.97
// Written by Dino Termini - termini@email.it - May 9, 2003
// This script is freeware (GPL) but if you use it, please let me know!
//
// Portions of code by zoomIN, zoomOUT
// Author: Nguyen Duong Minh (Obie) - obie4web@yahoo.com
// WWW: http://ObieWebsite.SourceForge.net
// License: GNU (GPL)
//
// --------------------------------------------------------------------
//
// Please refer to DEMO.htm file for details and usage
//
// --------------------------------------------------------------------

// Configuration parameters
// ------------------------
// Measure unit in pixel (px) or points (pt)
// measureUnit = "pt"
measureUnit = "px";

// Minimum size allowed for SIZE attribute (like in <FONT SIZE="1"> )
minSize = 1;

// Minimum size allowed for STYLE attribute (like in <FONT STYLE="font-size: 10px"> )
minStyleSize = 10;

// Maximum size allowed for SIZE attribute
maxSize = 6;

// Maximum size allowed for STYLE attribute
maxStyleSize = 30;

// Start size for tags with no SIZE attribute defined
startSize = 5;

// Start size for tags with no font-size STYLE or CLASS attribute defined
startStyleSize = 10;

// Increasing and decreasing step
stepSize = 1;

// Increasing step for STYLE definition (measure previously declared will be used)
stepStyleSize = 2;

// To set your own hotkeys, use key generator tool page included
// Keys to zooming in (with and without CAPS lock). Default: "+"
var keyin = 61;
var keyinCAPS = 43;

// Keys to zooming out (with and without CAPS lock). Default: "-"
var keyout = 45;
var keyoutCAPS = 95;

// Keys for "hard" zooming in (with and without CAPS lock). Default: ">"
var keyinIe = 46;
var keyinIeCAPS = 62;

// Keys for "hard" zooming out (with and without CAPS lock). Default: "<"
var keyoutIe = 44;
var keyoutIeCAPS = 60;

// "Hard" zoom factor
var zoomFactor = 1.1;

// Max zoom allowed
var maxZoom = 4.096;

// Min zoom allowed
var minZoom = 0.625;

// Initial decrease zoom
var startDecZoom = 0.7;

// Initial increase zoom
var startIncZoom = 1.3;

// Enable or disable alert messages
alertEnabled = false;

// Allow input fields resize (text, buttons, and so on)
allowInputResize = false;

var myFC;
var myBC;

// End of configuration parameters. Please do not edit below this line
// --------------------------------------------------------------------------------

function searchTags(childTree, level) {
  var retArray = new Array();
  var tmpArray = new Array();
  var j = 0;
  var childName = "";
  for (var i=0; i<childTree.length; i++) {
    childName = childTree[i].nodeName;
    if (childTree[i].hasChildNodes()) {
      if ((childTree[i].childNodes.length == 1) && (childTree[i].childNodes[0].nodeName == "#text"))
        retArray[j++] = childTree[i];
      else {
        tmpArray = searchTags(childTree[i].childNodes, level+1);
        for (var k=0;k<tmpArray.length; k++)
          retArray[j++] = tmpArray[k];
        retArray[j++] = childTree[i];
      }
    }
    else
      retArray[j++] = childTree[i];
  }
  return(retArray);
}

function changeFontSize(stepSize, stepStyleSize) {
  if (document.body) {
    var myObj = searchTags(document.body.childNodes, 0);
    var myStepSize = stepSize;
    var myStepStyleSize = stepStyleSize;

    myObjNumChilds = myObj.length;
    for (i=0; i<myObjNumChilds; i++) {
      myObjName = myObj[i].nodeName;

      // Only some tags will be parsed
      if (myObjName != "#text" && myObjName != "HTML" &&
          myObjName != "HEAD" && myObjName != "TITLE" &&
          myObjName != "STYLE" && myObjName != "SCRIPT" &&
          myObjName != "BR" && myObjName != "TBODY" &&
          myObjName != "#comment" && myObjName != "FORM") {

        // Skip INPUT fields, if required
        if (!allowInputResize && myObjName == "INPUT") continue;

        size = parseInt(myObj[i].getAttribute("size"));
        // Internet Explorer uses a different DOM implementation
        if (myObj[i].currentStyle)
          styleSize = parseInt(myObj[i].currentStyle.fontSize);
        else 
          if (window.getComputedStyle)
		styleSize = parseInt(window.getComputedStyle(myObj[i], null).fontSize);
	  else
		styleSize = parseInt(document.defaultView.getComputedStyle(myObj[i],null).fontSize);

        // For debug purpose only. Note: can be very annoying
        // if (!confirm("TAG ["+myObjName+"] SIZE ["+size+"] STYLESIZE ["+styleSize+"]")) return(0);

        if (isNaN(size) || (size < minSize) || (size > maxSize))
          size = startSize;

        if (isNaN(styleSize) || (styleSize < minStyleSize) || (styleSize > maxStyleSize))
          styleSize = startStyleSize;
        if ( ((size > minSize) && (size < maxSize)) || 
             ((size == minSize) && (stepSize > 0)) || 
             ((size == maxSize) && (stepSize < 0)) ) {
      	myObj[i].setAttribute("size", size+myStepSize);
		myObj[i].setAttribute("color", myFC);
      }


        if ( ((styleSize > minStyleSize) && (styleSize < maxStyleSize)) || 
             ((styleSize == minStyleSize) && (stepStyleSize > 0)) ||
             ((styleSize == maxStyleSize) && (stepStyleSize < 0)) ) {
          newStyleSize = styleSize+myStepStyleSize;
          myObj[i].style.fontSize = newStyleSize+measureUnit;
    	    myObj[i].style.color = myFC;

        }
      } // End if condition ("only some tags")
    } // End main for cycle
  } // End if condition ("document.body exists")
} // End function declaration

function highContrast(foreC, backC) {
	myFC = foreC;
	myBC = backC;

	changeFontSize (0,0);
	if (document.body) {
		document.body.style.background = "";
		document.body.style.backgroundColor = myBC;
		document.body.setAttribute("background-image", "");
		document.body.setAttribute("background", myBC);

   		var myObj = searchTags(document.body.childNodes, 0);

		myObjNumChilds = myObj.length;
    		for (i=0; i<myObjNumChilds; i++) {

      		myObjName = myObj[i].nodeName;

    			if (myObjName == "TABLE" || myObjName == "BODY" || myObjName == "TD" || myObjName == "TR") {


				myObj[i].setAttribute("background-image", "");
				myObj[i].setAttribute("background-color", myBC);
				myObj[i].setAttribute("color", myFC);

				myObj[i].style.backgroumyFC;
				myObj[i].style.backgroundColor = myBC;
			}
		}
	}
}

function increaseFontSize() {
  if (document.body) {

    changeFontSize(stepSize, stepStyleSize);
  }
  else {
    if (alertEnabled) {
      alert("Your browser does not support this function.");
    }
  }
}

function decreaseFontSize() {
  if (document.body) {
    myStepSize = -stepSize;
    myStepStyleSize = -stepStyleSize;
    changeFontSize(myStepSize, myStepStyleSize);
  }
  else {
    if (alertEnabled) {
      alert("Spiacente, il tuo browser non supporta questa funzione");
    }
  }
}

function zoomin() {
  if (window.parent.document.body.style.zoom < maxZoom) {
    if (window.parent.document.body.style.zoom > 0) {
      window.parent.document.body.style.zoom *= zoomFactor; 
    }
    else { 
      window.parent.document.body.style.zoom = startIncZoom;
    }
  }
  else {
    if (alertEnabled) {
      alert("Warning: Max size reached");
    }
  }
}

function zoomout() {
  if ( (window.parent.document.body.style.zoom > minZoom) ||
       (window.parent.document.body.style.zoom == 0) ) {
    if (window.parent.document.body.style.zoom > 0) {
      window.parent.document.body.style.zoom /= zoomFactor; 
    }
    else {
      window.parent.document.body.style.zoom = startDecZoom;
    }
  }
  else {
    if (alertEnabled) {
      alert("Warning: Min size reached");
    }
  }
}

function checkzoom(e) {

  if (document.all) {
    myEvent = event.keyCode;
  }
  else {
    myEvent = e.which;
  }

  switch(myEvent) {
    case keyinIe:
    case keyinIeCAPS:
      zoomin();
      break;

    case keyoutIe:
    case keyoutIeCAPS:
      zoomout();
      break;

    case keyin:
    case keyinCAPS:
      increaseFontSize();
      break;

    case keyout:
    case keyoutCAPS:
      decreaseFontSize();
      break;

    default:
      break;
  }
}

if (document.layers) {
  document.captureEvents(Event.KEYPRESS);
}

document.onkeypress = checkzoom;


function writeJS(){
var str='';
str+='<table border=0><tr><td><font face="Arial">';
str+='    <BUTTON  ';
str+='      onClick="javascript:increaseFontSize();"';
str+='      STYLE="background-color:#EEEEEE; font-family: Verdana, Helvetica, sans-serif;width:60px;">font+<\/BUTTON>';
str+='    <BUTTON  ';
str+='      onClick="javascript:decreaseFontSize();"';
str+='      STYLE="background-color:#EEEEEE; font-family: Verdana, Helvetica, sans-serif;width:60px;">font-<\/BUTTON><br>';
str+='    <BUTTON  ';
str+='      onClick="javascript:highContrast(\'#ffffff\',\'#000000\');"';
str+='      STYLE="color: #ffffff ;background-color: #000000; font-weight:bold;font-family: Verdana, Helvetica, sans-serif;width:60px;">  HC  <\/BUTTON>';
str+='    <BUTTON  ';
str+='      onClick="javascript:highContrast(\'#0000ff\',\'#ffff00\');"';
str+='      STYLE="color: #0000ff;background-color: #ffff00; font-weight:bold;font-family: Verdana, Helvetica, sans-serif;width:60px;">  HC  <\/BUTTON><\/p>';
str+='<\/td>';
str+='<td valign=top><BUTTON id="TTS" STYLE="font-family: Verdana, Helvetica, sans-serif;" onClick="javascript:alert(99);">Enable/Disable<\/BUTTON>';
str+='<br><a href="http://kids-learn.org/sharkbytes/instructions.htm">Click here to learn about the UDL features of this site</a><\/td><\/tr><\/font><\/table>';

document.write(str);
}
writeJS();

//////////////////////////////////////////////
//  COOKIES TO ENABLE/DISABLE TEXT-TO-SPEECH
//////////////////////////////////////////////
function enableTTS(){
	createCookie ('TTS', 'yes', 365);
	document.all.item('TTS').innerHTML = '<font color=red>DISABLE</font> Text-to-Speech';
	document.all.item('TTS').onclick = disableTTS;
	startMyCharacter();
}

function disableTTS(){
	createCookie ('TTS', 'no', 365);
	document.all.item('TTS').innerHTML = '<font color=green>ENABLE</font> Text-to-Speech';
	document.all.item('TTS').onclick = enableTTS;
	stopMyCharacter();
}

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);
}

if (readCookie('TTS') == 'yes')
	{
	document.all.item('TTS').innerHTML = '<font color=red>DISABLE</font> Text-to-Speech';
	document.all.item('TTS').onclick = disableTTS;
	}

if (readCookie('TTS') == 'no')
	{
	document.all.item('TTS').innerHTML = '<font color=green>ENABLE</font> Text-to-Speech';
	document.all.item('TTS').onclick = enableTTS;
	}

if (readCookie('TTS') == null)
	{
	document.all.item('TTS').innerHTML = '<font color=green>ENABLE</font> Text-to-Speech';
	document.all.item('TTS').onclick = enableTTS;
	}

