var curFontSize			= 0.95;
var curLineHeight		= 1.2;
var fontModifier		= 0.14;
var lineHeightModifier	= 0.05;
var maxFontSize			= 1.6;
var minFontSize			= 0.6;
var cookie 				= readCookie("style");
var contraste			= cookie ? cookie : getPreferredStyleSheet();

function textSizing(act) {        
        var arrIds	= new Array;
        arrIds[0]	= 'ESTRUTURA_CONTEUDO';
        arrIds[1]	= 'BOTOES_NAVEGACAO';
        arrIds[2]	= 'MENU_TOPO';
        arrIds[3]	= 'MENU_RODAPE_LEGENDA';
        arrIds[4]	= 'ESTRUTURA_VOLTAR_TOPO';
        arrIds[5]	= 'MENU_TOPO_CURSO';
        arrIds[6]	= 'contraste';
        
        curLineHeight = curLineHeight * 1;
        curFontSize   = curFontSize * 1;
        if (act == 1) {
            curFontSize += fontModifier;
            if(curFontSize > maxFontSize){
            	curFontSize -= fontModifier;
            	return false;
            }
            
            curLineHeight += lineHeightModifier;
        }
        else if (act == 0) {
            curFontSize -= fontModifier;            
        	if(curFontSize < minFontSize){
        		curFontSize += fontModifier;
        		return false;
        	}
            curLineHeight -= lineHeightModifier;
        }
        
        for(x=0;x<arrIds.length;x++){
        	tmpStoryBody = document.getElementById(arrIds[x]);
        	if(tmpStoryBody){
	        	tmpStoryBody.style.fontSize = curFontSize + "em";
	        	tmpStoryBody.style.lineHeight = curLineHeight+"em";
        	}
        }
        createCookie("fontSize", curFontSize, 365);
        createCookie("lineHeight", curLineHeight, 365);
        
}

function saveExercicio(){
	if(document.getElementById('formExercicio')){
		document.getElementById('formExercicio').submit();
	}
}

function load(){
	el = document.getElementById('aMais');
	if(el){
		el.onclick = function(){
			textSizing(1);
		}
	}
	
	el = document.getElementById('aMenos');
	if(el){
		el.onclick = function(){
			textSizing(0);
		}
	}
	el = document.getElementById('voltarPaginaAnterior');
	if(el){
		el.onclick = function(){
			history.go(-1);
		}
	}
	
	el = document.getElementById('contraste');
	if(el){
		el.onclick = function(){			
			if(contraste == 'normal'){
				setActiveStyleSheet('preto_branco');
				contraste = "preto_branco";
			}else{
				setActiveStyleSheet('normal');
				contraste = "normal";
			}
		}
	}
	var cookie = readCookie("style");
	var title = cookie ? cookie : getPreferredStyleSheet();
	setActiveStyleSheet(title);
	var fontSize = readCookie("fontSize");
	var lineHeight = readCookie("lineHeight");
	if(fontSize > 0){
		curFontSize = fontSize;
		curLineHeight = lineHeight;
		textSizing(2);
	}

}

function limpaLoginSenha(){
	el = document.getElementById('login');
	if(el){
		el.value = '';
	}
	el = document.getElementById('senha');
	if(el){
		el.value = '';
	}
}

function limpaFormInscricao(){
	el = document.getElementById('nome');
	if(el){
		el.value = '';
	}
	el = document.getElementById('email');
	if(el){
		el.value = '';
	}
	el = document.getElementById('login');
	if(el){
		el.value = '';
	}
	el = document.getElementById('senha');
	if(el){
		el.value = '';
	}
	el = document.getElementById('anoNascimento');
	if(el){
		el.value = '';
	}
	el = document.getElementById('instituicao');
	if(el){
		el.value = '';
	}
}

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;
    }
  }
}

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;
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}
