/**
 * Configure the main variables
 */
var font_size = 17;
var font_original = font_size;
var bigger_font_factor = 7;
var min_font = 11;
var max_font = 27;
var cookie_name = "font_size_cookie";

function handleOnLoad(){
    window.setTimeout("setFontSize()", 100);
}


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 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=/; domain=.powerandlife.com";
}


/**
 * Gets the font_size from the cookie and assigns 
 * it to the global variable font_size
 */
function loadFontSize(){
    var cookie = readCookie(cookie_name);
    if ( cookie != null ){
	return parseInt(cookie);
    } else{
	return null;
    } 
}

/**
 * Sets the font size to a selected HTML element
 */
function setFontSize(){
    var fSize =  loadFontSize();
    if ( fSize == null ){
	return;
    }else{
	font_size = fSize;
    }

    setFont(font_size);
}

function setFont(fSize){
    var elements = document.getElementsByTagName('p');
    var was_set;
    for (i = 0; i < elements.length; i++) {
	if (elements[i].className == 'header-title') {
	    font_to_set = fSize + bigger_font_factor;
	}else{
	    font_to_set = fSize;
	}
	elements[i].style.fontSize = font_to_set + 'px';
    }

    elements = document.getElementsByTagName('a');
    for (i = 0; i < elements.length; i++) {
	elements[i].style.fontSize = fSize + 'px';
    }
}

function getFontSizeFromElement(){
    var obj = document.getElementsByTagName('a')[0];
    if ( obj.style.fontSize ){
	font_size = obj.style.fontSize;
    }
   
    var fSize = parseInt(font_size);
    return  fSize;
}

/**
 * Increases the font size of a HTML element with 1px
 * if the size is greater than the maximum allowed 
 * the size is not increased further more
 */
function increaseFontSize() {
    font_size = getFontSizeFromElement();

    if (font_size < max_font)  font_size = font_size + 1;
 
    setFont(font_size);

    createCookie(cookie_name, font_size, 365);
}

/**
 * Decreases the font size of a HTML element with 1px
 * if the size is smaller than the minimum allowed 
 * the size is not decreased further more
 */
function decreaseFontSize() {
    font_size = getFontSizeFromElement();

    if (font_size > min_font)  font_size = font_size - 1;

    setFont(font_size);

    createCookie(cookie_name, font_size, 365);
}

