/***
	* FontSwitcher v0.1
	* Copyright (c) 2007 Pavol Biely
	* http://pabi3.com/
	*
	* This program is free software; you can redistribute it and/or
	* modify it under the terms of the GNU General Public License
	* as published by the Free Software Foundation; either version 2
	* of the License, or (at your option) any later version.
	*
	* This program is distributed in the hope that it will be useful,
	* but WITHOUT ANY WARRANTY; without even the implied warranty of
	* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	* GNU General Public License for more details.
	*
	* You should have received a copy of the GNU General Public License
	* along with this program; if not, write to the Free Software
	* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**/

// jednotka, v ktorej sa má meniť veľkosť písma
var unit    = 'px';

// aktuálna veľkosť písma, od ktorej sa začne odčítavať alebo pričítavať
var size    = 12;

// implicitná veľkosť písma
var defsize = 12;

// aktuálne použité písmo
var font    = 'sans-serif';

// implicitne použité písmo
var deffont = 'sans-serif';

// elementy, v ktorých sa má meniť typ písma a jeho veľkosť
var element = document.getElementsByTagName('p');

// prefix pre cookies; prvá cookie sa volá -family, druhá -size
var cookiename = 'font';

// doba platnosti cookies v dňoch
var cookiedays = 30;

// udalosť onload; po načítaní stránky sa vykoná JS kód
// copyright (c) Simon Willison
// http://simonwillison.net/2004/May/26/addLoadEvent/
addLoadEvent(setFontFromCookies);

function addLoadEvent(func)
{
	var oldonload = window.onload;
	if(typeof window.onload != 'function')
	{
	  window.onload = func;
	} else {
	  window.onload = function()
		{
	    oldonload();
	    func();
	  }
	}
}

// odošle cookie
// http://www.quirksmode.org/js/cookies.html
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=/';
}

// prečíta cookie
// http://www.quirksmode.org/js/cookies.html
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;
}

// nastaví písmo a jeho veľkosť + odošle cookies
function setFont()
{
	if(!(document.getElementById || document.all || document.layers)) return;

	createCookie(cookiename + '-family',font,cookiedays);
	createCookie(cookiename + '-size',size,cookiedays);

	for(var i = 0; i < element.length; i++)
	{
		element[i].style.fontFamily = font;
		element[i].style.fontSize   = size + unit;
	}
}

// obnoví pôvodné písmo a jeho veľkosť + zruší cookies
function setFontDefault()
{
	createCookie(cookiename + '-family','',-1);
	createCookie(cookiename + '-size','',-1);

	font = deffont;
	size = defsize;

	setFont();
}

// nastaví typ písma
function setFontFamily(param)
{
	font = param;
	setFont();
}

// nastaví veľkosť písma
function setFontSize(param)
{
	if(param == 0) size = defsize;
	else size = parseInt(size)+parseInt(param);
	setFont();
}

// nastaví typ písma a jeho veľkosť za pomoci cookies
function setFontFromCookies()
{
	var tmp_font = readCookie(cookiename + '-family');
	var tmp_size = readCookie(cookiename + '-size');

	if(tmp_font != null) font = tmp_font;
	if(tmp_size != null) size = tmp_size;

	setFont();
}
