﻿function Fonte( alvo )
{

	this.TAMANHO_DEFAULT = 'Fonte padrão';
	this._tamanho = this.TAMANHO_DEFAULT;
	this.ALTO_CONTRASTE = false;

}

// atribui tamanho da fonte
Fonte.prototype.setTamanho = function( tamanho )
{
	this._tamanho = tamanho;
}

// retorna tamanho de fonte atual
Fonte.prototype.getTamanho = function()
{
	return this._tamanho;
}

// carrega tamanho de fonte salvo
Fonte.prototype.carregarTamanho = function()
{
	this.controlarTamanho( this.lerCookie() );
}

// aumenta tamanho da fonte e salva cookie
Fonte.prototype.controlarTamanhoFonte = function( tamanho )
{

	if (tamanho != undefined) {
		this.setTamanho(tamanho);
	}else{
		this.setTamanho(this.lerCookie());
	}

	if ( this.getTamanho() != this.lerCookie() )
	{
		this.criarCookie();
	}

	var i, a;
    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") == this.getTamanho()) {
				a.disabled = false;
				if (a.getAttribute("title").indexOf('Contraste') != -1) {
					this.ALTO_CONTRASTE = !this.ALTO_CONTRASTE;
				}
			}

		}

	}

}

// cria cookies
Fonte.prototype.criarCookie = function( nome, valor, dias )
{
	if ( nome == undefined )
	{
		nome = 'tamanhoFonte';
	}

	if ( valor == undefined )
	{
		valor = this.getTamanho();
	}

	if ( dias == undefined )
	{
		dias = 360;
	}

	if ( dias )
	{
		var data = new Date();
		data.setTime( data.getTime() + ( dias * 24 * 60 * 60 * 1000 ) );
		var expires = "; expires=" + data.toGMTString();
	}
	else
	{
		var expires = "";
	}

	document.cookie = nome + "=" + valor + expires + "; path=/";
}

// le cookies
Fonte.prototype.lerCookie = function( nome )
{
	if ( nome == undefined )
	{
		nome = 'tamanhoFonte';
	}

	var strNome = nome + "=";
	var cookies = document.cookie.split(';');

	for ( var i=0; i < cookies.length; i++ ) {
		var entrada = cookies[ i ];

		while ( entrada.charAt(0) == ' ' )
		{
			entrada = entrada.substring( 1, entrada.length );
		}

		if ( entrada.indexOf( strNome ) == 0 )
		{
			return entrada.substring( strNome.length, entrada.length );
		}
	}

	return null;
}
